Skip to content

Commit 39d5264

Browse files
Merge #507
507: From trait for Pin, PartiallyErasedPin, and ErasedPin structs r=burrbull a=romancardenas I implemented the `For` trait for the following pin type conversions: - From `Pin<P, N, MODE>` to `PartiallyErasedPin<P, MODE>` - From `Pin<P, N, MODE>` to `ErasedPin<MODE>` - From `PartiallyErasedPin<P, MODE>` to `ErasedPin<MODE>` Co-authored-by: Román Cárdenas <rcardenas.rod@gmail.com>
2 parents 9595e6a + 10a5e9f commit 39d5264

File tree

3 files changed

+35
-0
lines changed

3 files changed

+35
-0
lines changed

CHANGELOG.md

Lines changed: 8 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -7,6 +7,14 @@ and this project adheres to [Semantic Versioning](http://semver.org/).
77

88
## [Unreleased]
99

10+
### Added
11+
12+
- Implementation of From trait for Pin-to-PartiallyErasedPin [#507]
13+
- Implementation of From trait for Pin-to-ErasedPin [#507]
14+
- Implementation of From trait for PartiallyErasedPin-to-ErasedPin [#507]
15+
16+
[#507]: https://github.com/stm32-rs/stm32f4xx-hal/pull/507
17+
1018
### Changed
1119
- use `stm32_i2s_v12x` version 0.3, reexport it, and implements requirement for it
1220
- i2s module don't reuse marker from spi module and define its own.

src/gpio.rs

Lines changed: 18 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -341,6 +341,24 @@ impl<const P: char, const N: u8, MODE> Pin<P, N, MODE> {
341341
}
342342
}
343343

344+
impl<const P: char, const N: u8, MODE> From<Pin<P, N, MODE>> for PartiallyErasedPin<P, MODE> {
345+
/// Pin-to-partially erased pin conversion using the [`From`] trait.
346+
///
347+
/// Note that [`From`] is the reciprocal of [`Into`].
348+
fn from(p: Pin<P, N, MODE>) -> Self {
349+
p.erase_number()
350+
}
351+
}
352+
353+
impl<const P: char, const N: u8, MODE> From<Pin<P, N, MODE>> for ErasedPin<MODE> {
354+
/// Pin-to-erased pin conversion using the [`From`] trait.
355+
///
356+
/// Note that [`From`] is the reciprocal of [`Into`].
357+
fn from(p: Pin<P, N, MODE>) -> Self {
358+
p.erase()
359+
}
360+
}
361+
344362
impl<const P: char, const N: u8, MODE> Pin<P, N, MODE> {
345363
/// Set the output of the pin regardless of its mode.
346364
/// Primarily used to set the output value of the pin

src/gpio/partially_erased.rs

Lines changed: 9 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -136,3 +136,12 @@ where
136136
unsafe { (*Gpio::<P>::ptr()).idr.read().bits() & (1 << self.i) == 0 }
137137
}
138138
}
139+
140+
impl<const P: char, MODE> From<PartiallyErasedPin<P, MODE>> for ErasedPin<MODE> {
141+
/// Partially erased pin-to-erased pin conversion using the [`From`] trait.
142+
///
143+
/// Note that [`From`] is the reciprocal of [`Into`].
144+
fn from(p: PartiallyErasedPin<P, MODE>) -> Self {
145+
ErasedPin::new(P as u8 - b'A', p.i)
146+
}
147+
}

0 commit comments

Comments
 (0)