Explicitly drop non consumed items

This commit is contained in:
huntc 2021-07-14 12:17:27 +10:00
parent 7c723d2bfd
commit a247fa4f2c

View File

@ -41,6 +41,7 @@ use core::cell::UnsafeCell;
use core::fmt; use core::fmt;
use core::mem::MaybeUninit; use core::mem::MaybeUninit;
use core::pin::Pin; use core::pin::Pin;
use core::ptr;
use core::task::Context; use core::task::Context;
use core::task::Poll; use core::task::Poll;
use core::task::Waker; use core::task::Waker;
@ -416,6 +417,16 @@ impl<T, const N: usize> ChannelState<T, N> {
} }
} }
impl<T, const N: usize> Drop for ChannelState<T, N> {
fn drop(&mut self) {
while self.read_pos != self.write_pos || self.full {
self.full = false;
unsafe { ptr::drop_in_place(self.buf[self.read_pos].as_mut_ptr()) };
self.read_pos = (self.read_pos + 1) % N;
}
}
}
/// A a bounded mpsc channel for communicating between asynchronous tasks /// A a bounded mpsc channel for communicating between asynchronous tasks
/// with backpressure. /// with backpressure.
/// ///