more clean up, refactor channel into module to share code

This commit is contained in:
Scott Mabin 2023-11-18 15:01:12 +00:00
parent 7589b5e13e
commit f482a105b8
3 changed files with 6 additions and 48 deletions

View File

@ -29,6 +29,8 @@ use crate::blocking_mutex::raw::RawMutex;
use crate::blocking_mutex::Mutex; use crate::blocking_mutex::Mutex;
use crate::waitqueue::WakerRegistration; use crate::waitqueue::WakerRegistration;
pub mod priority;
/// Send-only access to a [`Channel`]. /// Send-only access to a [`Channel`].
pub struct Sender<'ch, M, T, const N: usize> pub struct Sender<'ch, M, T, const N: usize>
where where
@ -76,7 +78,7 @@ where
/// Send-only access to a [`Channel`] without knowing channel size. /// Send-only access to a [`Channel`] without knowing channel size.
pub struct DynamicSender<'ch, T> { pub struct DynamicSender<'ch, T> {
pub(crate) channel: &'ch dyn DynamicChannel<T>, channel: &'ch dyn DynamicChannel<T>,
} }
impl<'ch, T> Clone for DynamicSender<'ch, T> { impl<'ch, T> Clone for DynamicSender<'ch, T> {
@ -176,7 +178,7 @@ where
/// Receive-only access to a [`Channel`] without knowing channel size. /// Receive-only access to a [`Channel`] without knowing channel size.
pub struct DynamicReceiver<'ch, T> { pub struct DynamicReceiver<'ch, T> {
pub(crate) channel: &'ch dyn DynamicChannel<T>, channel: &'ch dyn DynamicChannel<T>,
} }
impl<'ch, T> Clone for DynamicReceiver<'ch, T> { impl<'ch, T> Clone for DynamicReceiver<'ch, T> {
@ -321,7 +323,7 @@ impl<'ch, T> Future for DynamicSendFuture<'ch, T> {
impl<'ch, T> Unpin for DynamicSendFuture<'ch, T> {} impl<'ch, T> Unpin for DynamicSendFuture<'ch, T> {}
pub(crate) trait DynamicChannel<T> { trait DynamicChannel<T> {
fn try_send_with_context(&self, message: T, cx: Option<&mut Context<'_>>) -> Result<(), TrySendError<T>>; fn try_send_with_context(&self, message: T, cx: Option<&mut Context<'_>>) -> Result<(), TrySendError<T>>;
fn try_receive_with_context(&self, cx: Option<&mut Context<'_>>) -> Result<T, TryReceiveError>; fn try_receive_with_context(&self, cx: Option<&mut Context<'_>>) -> Result<T, TryReceiveError>;

View File

@ -183,23 +183,6 @@ where
} }
} }
/// Future returned by [`DynamicReceiver::receive`].
#[must_use = "futures do nothing unless you `.await` or poll them"]
pub struct DynamicReceiveFuture<'ch, T> {
channel: &'ch dyn DynamicChannel<T>,
}
impl<'ch, T> Future for DynamicReceiveFuture<'ch, T> {
type Output = T;
fn poll(self: Pin<&mut Self>, cx: &mut Context<'_>) -> Poll<T> {
match self.channel.try_receive_with_context(Some(cx)) {
Ok(v) => Poll::Ready(v),
Err(TryReceiveError::Empty) => Poll::Pending,
}
}
}
/// Future returned by [`PriorityChannel::send`] and [`Sender::send`]. /// Future returned by [`PriorityChannel::send`] and [`Sender::send`].
#[must_use = "futures do nothing unless you `.await` or poll them"] #[must_use = "futures do nothing unless you `.await` or poll them"]
pub struct SendFuture<'ch, M, T, K, const N: usize> pub struct SendFuture<'ch, M, T, K, const N: usize>
@ -242,32 +225,6 @@ where
{ {
} }
/// Future returned by [`DynamicSender::send`].
#[must_use = "futures do nothing unless you `.await` or poll them"]
pub struct DynamicSendFuture<'ch, T> {
channel: &'ch dyn DynamicChannel<T>,
message: Option<T>,
}
impl<'ch, T> Future for DynamicSendFuture<'ch, T> {
type Output = ();
fn poll(mut self: Pin<&mut Self>, cx: &mut Context<'_>) -> Poll<Self::Output> {
match self.message.take() {
Some(m) => match self.channel.try_send_with_context(m, Some(cx)) {
Ok(..) => Poll::Ready(()),
Err(TrySendError::Full(m)) => {
self.message = Some(m);
Poll::Pending
}
},
None => panic!("Message cannot be None"),
}
}
}
impl<'ch, T> Unpin for DynamicSendFuture<'ch, T> {}
struct ChannelState<T, K, const N: usize> { struct ChannelState<T, K, const N: usize> {
queue: BinaryHeap<T, K, N>, queue: BinaryHeap<T, K, N>,
receiver_waker: WakerRegistration, receiver_waker: WakerRegistration,
@ -386,7 +343,7 @@ where
/// ///
/// ``` /// ```
/// # use heapless::binary_heap::Max; /// # use heapless::binary_heap::Max;
/// use embassy_sync::priority_channel::PriorityChannel; /// use embassy_sync::channel::priority::PriorityChannel;
/// use embassy_sync::blocking_mutex::raw::NoopRawMutex; /// use embassy_sync::blocking_mutex::raw::NoopRawMutex;
/// ///
/// // Declare a bounded channel of 3 u32s. /// // Declare a bounded channel of 3 u32s.

View File

@ -15,7 +15,6 @@ pub mod blocking_mutex;
pub mod channel; pub mod channel;
pub mod mutex; pub mod mutex;
pub mod pipe; pub mod pipe;
pub mod priority_channel;
pub mod pubsub; pub mod pubsub;
pub mod signal; pub mod signal;
pub mod waitqueue; pub mod waitqueue;