aboutsummaryrefslogtreecommitdiff
diff options
context:
space:
mode:
authorhuntc <[email protected]>2021-07-14 11:31:15 +1000
committerhuntc <[email protected]>2021-07-15 12:31:52 +1000
commit7c723d2bfd3e8b8cf6fa289b822a254180601528 (patch)
treee10ed02fdaf5d60599ebc8bd68a7b94bb4323fb4
parentbaab52d40cb8d1ede339a3a422006108a86d8efb (diff)
Removed UB code around the send future
-rw-r--r--embassy/src/util/mpsc.rs32
1 files changed, 15 insertions, 17 deletions
diff --git a/embassy/src/util/mpsc.rs b/embassy/src/util/mpsc.rs
index 8d534dc49..f049b6217 100644
--- a/embassy/src/util/mpsc.rs
+++ b/embassy/src/util/mpsc.rs
@@ -210,7 +210,7 @@ where
210 pub async fn send(&self, message: T) -> Result<(), SendError<T>> { 210 pub async fn send(&self, message: T) -> Result<(), SendError<T>> {
211 SendFuture { 211 SendFuture {
212 sender: self.clone(), 212 sender: self.clone(),
213 message: UnsafeCell::new(message), 213 message: Some(message),
214 } 214 }
215 .await 215 .await
216 } 216 }
@@ -266,7 +266,7 @@ where
266 M: Mutex<Data = ()>, 266 M: Mutex<Data = ()>,
267{ 267{
268 sender: Sender<'ch, M, T, N>, 268 sender: Sender<'ch, M, T, N>,
269 message: UnsafeCell<T>, 269 message: Option<T>,
270} 270}
271 271
272impl<'ch, M, T, const N: usize> Future for SendFuture<'ch, M, T, N> 272impl<'ch, M, T, const N: usize> Future for SendFuture<'ch, M, T, N>
@@ -275,25 +275,23 @@ where
275{ 275{
276 type Output = Result<(), SendError<T>>; 276 type Output = Result<(), SendError<T>>;
277 277
278 fn poll(self: Pin<&mut Self>, cx: &mut Context<'_>) -> Poll<Self::Output> { 278 fn poll(mut self: Pin<&mut Self>, cx: &mut Context<'_>) -> Poll<Self::Output> {
279 match self 279 match self.message.take() {
280 .sender 280 Some(m) => match self.sender.channel.get().try_send_with_context(m, Some(cx)) {
281 .channel 281 Ok(..) => Poll::Ready(Ok(())),
282 .get() 282 Err(TrySendError::Closed(m)) => Poll::Ready(Err(SendError(m))),
283 .try_send_with_context(unsafe { self.message.get().read() }, Some(cx)) 283 Err(TrySendError::Full(m)) => {
284 { 284 self.message.insert(m);
285 Ok(..) => Poll::Ready(Ok(())), 285 Poll::Pending
286 Err(TrySendError::Closed(m)) => Poll::Ready(Err(SendError(m))), 286 }
287 Err(TrySendError::Full(..)) => { 287 },
288 Poll::Pending 288 None => panic!("Message cannot be None"),
289 // Note we leave the existing UnsafeCell contents - they still
290 // contain the original message. We could create another UnsafeCell
291 // with the message of Full, but there's no real need.
292 }
293 } 289 }
294 } 290 }
295} 291}
296 292
293impl<'ch, M, T, const N: usize> Unpin for SendFuture<'ch, M, T, N> where M: Mutex<Data = ()> {}
294
297struct CloseFuture<'ch, M, T, const N: usize> 295struct CloseFuture<'ch, M, T, const N: usize>
298where 296where
299 M: Mutex<Data = ()>, 297 M: Mutex<Data = ()>,