Skip to content

Commit 1d4e00f

Browse files
Merge #310
310: Add blanket impls for references r=therealprof a=GrantM11235 I would prefer to use a proc-macro like [auto-impl](https://crates.io/crates/auto_impl), but that doesn't support no_std yet. (see auto-impl-rs/auto_impl#73) Co-authored-by: Grant Miller <[email protected]>
2 parents bd7f607 + f7fe5bb commit 1d4e00f

14 files changed

+389
-0
lines changed

CHANGELOG.md

Lines changed: 1 addition & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -16,6 +16,7 @@ and this project adheres to [Semantic Versioning](http://semver.org/).
1616
- Added `IoPin` trait for pins that can change between being inputs or outputs
1717
dynamically.
1818
- Added `Debug` to all spi mode types.
19+
- Add impls of all traits for references (`&T` or `&mut T` depending on the trait) when `T` implements the trait.
1920

2021
### Changed
2122
- Swap PWM channel arguments to references

src/adc.rs

Lines changed: 19 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -51,6 +51,14 @@ pub mod nb {
5151
fn channel(&self) -> Self::ID;
5252
}
5353

54+
impl<T: Channel<ADC>, ADC> Channel<ADC> for &T {
55+
type ID = T::ID;
56+
57+
fn channel(&self) -> Self::ID {
58+
T::channel(self)
59+
}
60+
}
61+
5462
/// ADCs that sample on single channels per request, and do so at the time of the request.
5563
///
5664
/// This trait is the interface to an ADC that is configured to read a specific channel at the time
@@ -92,4 +100,15 @@ pub mod nb {
92100
/// whatever channel underlies the pin.
93101
fn read(&mut self, pin: &mut Pin) -> nb::Result<Word, Self::Error>;
94102
}
103+
104+
impl<T, ADC, Word, Pin: Channel<ADC>> OneShot<ADC, Word, Pin> for &mut T
105+
where
106+
T: OneShot<ADC, Word, Pin>,
107+
{
108+
type Error = T::Error;
109+
110+
fn read(&mut self, pin: &mut Pin) -> nb::Result<Word, Self::Error> {
111+
T::read(self, pin)
112+
}
113+
}
95114
}

src/capture.rs

Lines changed: 33 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -94,4 +94,37 @@ pub mod nb {
9494
where
9595
R: Into<Self::Time>;
9696
}
97+
98+
impl<T: Capture> Capture for &mut T {
99+
type Error = T::Error;
100+
101+
type Channel = T::Channel;
102+
103+
type Time = T::Time;
104+
105+
type Capture = T::Capture;
106+
107+
fn capture(&mut self, channel: Self::Channel) -> nb::Result<Self::Capture, Self::Error> {
108+
T::capture(self, channel)
109+
}
110+
111+
fn disable(&mut self, channel: Self::Channel) -> Result<(), Self::Error> {
112+
T::disable(self, channel)
113+
}
114+
115+
fn enable(&mut self, channel: Self::Channel) -> Result<(), Self::Error> {
116+
T::enable(self, channel)
117+
}
118+
119+
fn get_resolution(&self) -> Result<Self::Time, Self::Error> {
120+
T::get_resolution(self)
121+
}
122+
123+
fn set_resolution<R>(&mut self, resolution: R) -> Result<(), Self::Error>
124+
where
125+
R: Into<Self::Time>,
126+
{
127+
T::set_resolution(self, resolution)
128+
}
129+
}
97130
}

src/delay.rs

Lines changed: 16 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -21,6 +21,14 @@ pub mod blocking {
2121
fn delay_ms(&mut self, ms: UXX) -> Result<(), Self::Error>;
2222
}
2323

24+
impl<UXX, T: DelayMs<UXX>> DelayMs<UXX> for &mut T {
25+
type Error = T::Error;
26+
27+
fn delay_ms(&mut self, ms: UXX) -> Result<(), Self::Error> {
28+
T::delay_ms(self, ms)
29+
}
30+
}
31+
2432
/// Microsecond delay
2533
///
2634
/// `UXX` denotes the range type of the delay time. `UXX` can be `u8`, `u16`, etc. A single type can
@@ -32,4 +40,12 @@ pub mod blocking {
3240
/// Pauses execution for `us` microseconds
3341
fn delay_us(&mut self, us: UXX) -> Result<(), Self::Error>;
3442
}
43+
44+
impl<UXX, T: DelayUs<UXX>> DelayUs<UXX> for &mut T {
45+
type Error = T::Error;
46+
47+
fn delay_us(&mut self, us: UXX) -> Result<(), Self::Error> {
48+
T::delay_us(self, us)
49+
}
50+
}
3551
}

src/digital.rs

Lines changed: 46 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -77,6 +77,22 @@ pub mod blocking {
7777
}
7878
}
7979

80+
impl<T: OutputPin> OutputPin for &mut T {
81+
type Error = T::Error;
82+
83+
fn set_low(&mut self) -> Result<(), Self::Error> {
84+
T::set_low(self)
85+
}
86+
87+
fn set_high(&mut self) -> Result<(), Self::Error> {
88+
T::set_high(self)
89+
}
90+
91+
fn set_state(&mut self, state: PinState) -> Result<(), Self::Error> {
92+
T::set_state(self, state)
93+
}
94+
}
95+
8096
/// Push-pull output pin that can read its output state
8197
pub trait StatefulOutputPin: OutputPin {
8298
/// Is the pin in drive high mode?
@@ -90,6 +106,16 @@ pub mod blocking {
90106
fn is_set_low(&self) -> Result<bool, Self::Error>;
91107
}
92108

109+
impl<T: StatefulOutputPin> StatefulOutputPin for &mut T {
110+
fn is_set_high(&self) -> Result<bool, Self::Error> {
111+
T::is_set_high(self)
112+
}
113+
114+
fn is_set_low(&self) -> Result<bool, Self::Error> {
115+
T::is_set_low(self)
116+
}
117+
}
118+
93119
/// Output pin that can be toggled
94120
///
95121
/// See [toggleable](toggleable) to use a software implementation if
@@ -104,6 +130,14 @@ pub mod blocking {
104130
fn toggle(&mut self) -> Result<(), Self::Error>;
105131
}
106132

133+
impl<T: ToggleableOutputPin> ToggleableOutputPin for &mut T {
134+
type Error = T::Error;
135+
136+
fn toggle(&mut self) -> Result<(), Self::Error> {
137+
T::toggle(self)
138+
}
139+
}
140+
107141
/// Single digital input pin
108142
pub trait InputPin {
109143
/// Error type
@@ -116,6 +150,18 @@ pub mod blocking {
116150
fn is_low(&self) -> Result<bool, Self::Error>;
117151
}
118152

153+
impl<T: InputPin> InputPin for &T {
154+
type Error = T::Error;
155+
156+
fn is_high(&self) -> Result<bool, Self::Error> {
157+
T::is_high(self)
158+
}
159+
160+
fn is_low(&self) -> Result<bool, Self::Error> {
161+
T::is_low(self)
162+
}
163+
}
164+
119165
/// Single pin that can switch from input to output mode, and vice-versa.
120166
///
121167
/// Example use (assumes the `Error` type is the same for the `IoPin`,

src/i2c.rs

Lines changed: 79 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -147,6 +147,14 @@ pub mod blocking {
147147
fn read(&mut self, address: A, buffer: &mut [u8]) -> Result<(), Self::Error>;
148148
}
149149

150+
impl<A: AddressMode, T: Read<A>> Read<A> for &mut T {
151+
type Error = T::Error;
152+
153+
fn read(&mut self, address: A, buffer: &mut [u8]) -> Result<(), Self::Error> {
154+
T::read(self, address, buffer)
155+
}
156+
}
157+
150158
/// Blocking write
151159
pub trait Write<A: AddressMode = SevenBitAddress> {
152160
/// Error type
@@ -171,6 +179,14 @@ pub mod blocking {
171179
fn write(&mut self, address: A, bytes: &[u8]) -> Result<(), Self::Error>;
172180
}
173181

182+
impl<A: AddressMode, T: Write<A>> Write<A> for &mut T {
183+
type Error = T::Error;
184+
185+
fn write(&mut self, address: A, bytes: &[u8]) -> Result<(), Self::Error> {
186+
T::write(self, address, bytes)
187+
}
188+
}
189+
174190
/// Blocking write (iterator version)
175191
pub trait WriteIter<A: AddressMode = SevenBitAddress> {
176192
/// Error type
@@ -186,6 +202,17 @@ pub mod blocking {
186202
B: IntoIterator<Item = u8>;
187203
}
188204

205+
impl<A: AddressMode, T: WriteIter<A>> WriteIter<A> for &mut T {
206+
type Error = T::Error;
207+
208+
fn write_iter<B>(&mut self, address: A, bytes: B) -> Result<(), Self::Error>
209+
where
210+
B: IntoIterator<Item = u8>,
211+
{
212+
T::write_iter(self, address, bytes)
213+
}
214+
}
215+
189216
/// Blocking write + read
190217
pub trait WriteRead<A: AddressMode = SevenBitAddress> {
191218
/// Error type
@@ -221,6 +248,19 @@ pub mod blocking {
221248
) -> Result<(), Self::Error>;
222249
}
223250

251+
impl<A: AddressMode, T: WriteRead<A>> WriteRead<A> for &mut T {
252+
type Error = T::Error;
253+
254+
fn write_read(
255+
&mut self,
256+
address: A,
257+
bytes: &[u8],
258+
buffer: &mut [u8],
259+
) -> Result<(), Self::Error> {
260+
T::write_read(self, address, bytes, buffer)
261+
}
262+
}
263+
224264
/// Blocking write (iterator version) + read
225265
pub trait WriteIterRead<A: AddressMode = SevenBitAddress> {
226266
/// Error type
@@ -242,6 +282,22 @@ pub mod blocking {
242282
B: IntoIterator<Item = u8>;
243283
}
244284

285+
impl<A: AddressMode, T: WriteIterRead<A>> WriteIterRead<A> for &mut T {
286+
type Error = T::Error;
287+
288+
fn write_iter_read<B>(
289+
&mut self,
290+
address: A,
291+
bytes: B,
292+
buffer: &mut [u8],
293+
) -> Result<(), Self::Error>
294+
where
295+
B: IntoIterator<Item = u8>,
296+
{
297+
T::write_iter_read(self, address, bytes, buffer)
298+
}
299+
}
300+
245301
/// Transactional I2C operation.
246302
///
247303
/// Several operations can be combined as part of a transaction.
@@ -280,6 +336,18 @@ pub mod blocking {
280336
) -> Result<(), Self::Error>;
281337
}
282338

339+
impl<A: AddressMode, T: Transactional<A>> Transactional<A> for &mut T {
340+
type Error = T::Error;
341+
342+
fn exec<'a>(
343+
&mut self,
344+
address: A,
345+
operations: &mut [Operation<'a>],
346+
) -> Result<(), Self::Error> {
347+
T::exec(self, address, operations)
348+
}
349+
}
350+
283351
/// Transactional I2C interface (iterator version).
284352
///
285353
/// This allows combining operation within an I2C transaction.
@@ -304,4 +372,15 @@ pub mod blocking {
304372
where
305373
O: IntoIterator<Item = Operation<'a>>;
306374
}
375+
376+
impl<A: AddressMode, T: TransactionalIter<A>> TransactionalIter<A> for &mut T {
377+
type Error = T::Error;
378+
379+
fn exec_iter<'a, O>(&mut self, address: A, operations: O) -> Result<(), Self::Error>
380+
where
381+
O: IntoIterator<Item = Operation<'a>>,
382+
{
383+
T::exec_iter(self, address, operations)
384+
}
385+
}
307386
}

src/pwm.rs

Lines changed: 71 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -102,6 +102,51 @@ pub mod blocking {
102102
P: Into<Self::Time>;
103103
}
104104

105+
impl<T: Pwm> Pwm for &mut T {
106+
type Error = T::Error;
107+
108+
type Channel = T::Channel;
109+
110+
type Time = T::Time;
111+
112+
type Duty = T::Duty;
113+
114+
fn disable(&mut self, channel: &Self::Channel) -> Result<(), Self::Error> {
115+
T::disable(self, channel)
116+
}
117+
118+
fn enable(&mut self, channel: &Self::Channel) -> Result<(), Self::Error> {
119+
T::enable(self, channel)
120+
}
121+
122+
fn get_period(&self) -> Result<Self::Time, Self::Error> {
123+
T::get_period(self)
124+
}
125+
126+
fn get_duty(&self, channel: &Self::Channel) -> Result<Self::Duty, Self::Error> {
127+
T::get_duty(self, channel)
128+
}
129+
130+
fn get_max_duty(&self) -> Result<Self::Duty, Self::Error> {
131+
T::get_max_duty(self)
132+
}
133+
134+
fn set_duty(
135+
&mut self,
136+
channel: &Self::Channel,
137+
duty: Self::Duty,
138+
) -> Result<(), Self::Error> {
139+
T::set_duty(self, channel, duty)
140+
}
141+
142+
fn set_period<P>(&mut self, period: P) -> Result<(), Self::Error>
143+
where
144+
P: Into<Self::Time>,
145+
{
146+
T::set_period(self, period)
147+
}
148+
}
149+
105150
/// A single PWM channel / pin
106151
///
107152
/// See `Pwm` for details
@@ -133,4 +178,30 @@ pub mod blocking {
133178
/// Sets a new duty cycle
134179
fn set_duty(&mut self, duty: Self::Duty) -> Result<(), Self::Error>;
135180
}
181+
182+
impl<T: PwmPin> PwmPin for &mut T {
183+
type Error = T::Error;
184+
185+
type Duty = T::Duty;
186+
187+
fn disable(&mut self) -> Result<(), Self::Error> {
188+
T::disable(self)
189+
}
190+
191+
fn enable(&mut self) -> Result<(), Self::Error> {
192+
T::enable(self)
193+
}
194+
195+
fn get_duty(&self) -> Result<Self::Duty, Self::Error> {
196+
T::get_duty(self)
197+
}
198+
199+
fn get_max_duty(&self) -> Result<Self::Duty, Self::Error> {
200+
T::get_max_duty(self)
201+
}
202+
203+
fn set_duty(&mut self, duty: Self::Duty) -> Result<(), Self::Error> {
204+
T::set_duty(self, duty)
205+
}
206+
}
136207
}

0 commit comments

Comments
 (0)