Skip to content

Commit 0392676

Browse files
committed
Add support for all move combinations
1 parent a6f623b commit 0392676

File tree

1 file changed

+39
-0
lines changed

1 file changed

+39
-0
lines changed

embedded-hal-async/src/spi.rs

Lines changed: 39 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -91,6 +91,45 @@ macro_rules! spi_transaction_helper {
9191
}
9292
})
9393
};
94+
($device:expr, move |$bus:ident| async $closure_body:expr) => {
95+
$crate::spi::SpiDevice::transaction($device, move |$bus| {
96+
// Safety: Implementers of the `SpiDevice` trait guarantee that the pointer is
97+
// valid and dereferencable for the entire duration of the closure.
98+
let $bus = unsafe { &mut *$bus };
99+
async {
100+
let result = $closure_body;
101+
let $bus = $bus; // Ensure that the bus reference was not moved out of the closure
102+
let _ = $bus; // Silence the "unused variable" warning from prevous line
103+
result
104+
}
105+
})
106+
};
107+
($device:expr, |$bus:ident| async move $closure_body:expr) => {
108+
$crate::spi::SpiDevice::transaction($device, |$bus| {
109+
// Safety: Implementers of the `SpiDevice` trait guarantee that the pointer is
110+
// valid and dereferencable for the entire duration of the closure.
111+
let $bus = unsafe { &mut *$bus };
112+
async move {
113+
let result = $closure_body;
114+
let $bus = $bus; // Ensure that the bus reference was not moved out of the closure
115+
let _ = $bus; // Silence the "unused variable" warning from prevous line
116+
result
117+
}
118+
})
119+
};
120+
($device:expr, |$bus:ident| async $closure_body:expr) => {
121+
$crate::spi::SpiDevice::transaction($device, |$bus| {
122+
// Safety: Implementers of the `SpiDevice` trait guarantee that the pointer is
123+
// valid and dereferencable for the entire duration of the closure.
124+
let $bus = unsafe { &mut *$bus };
125+
async {
126+
let result = $closure_body;
127+
let $bus = $bus; // Ensure that the bus reference was not moved out of the closure
128+
let _ = $bus; // Silence the "unused variable" warning from prevous line
129+
result
130+
}
131+
})
132+
};
94133
}
95134

96135
#[doc(inline)]

0 commit comments

Comments
 (0)