Add constructor for dynamic channel

This commit is contained in:
Peter Krull 2024-03-02 13:13:26 +01:00
parent 64890498ca
commit 69d37503c2

View file

@ -507,6 +507,16 @@ where
Receiver { channel: self } Receiver { channel: self }
} }
/// Get a sender for this channel using dynamic dispatch.
pub fn dyn_sender(&self) -> DynamicSender<'_, T> {
DynamicSender { channel: self }
}
/// Get a receiver for this channel using dynamic dispatch.
pub fn dyn_receiver(&self) -> DynamicReceiver<'_, T> {
DynamicReceiver { channel: self }
}
/// Send a value, waiting until there is capacity. /// Send a value, waiting until there is capacity.
/// ///
/// Sending completes when the value has been pushed to the channel's queue. /// Sending completes when the value has been pushed to the channel's queue.
@ -648,7 +658,7 @@ mod tests {
} }
#[test] #[test]
fn dynamic_dispatch() { fn dynamic_dispatch_into() {
let c = Channel::<NoopRawMutex, u32, 3>::new(); let c = Channel::<NoopRawMutex, u32, 3>::new();
let s: DynamicSender<'_, u32> = c.sender().into(); let s: DynamicSender<'_, u32> = c.sender().into();
let r: DynamicReceiver<'_, u32> = c.receiver().into(); let r: DynamicReceiver<'_, u32> = c.receiver().into();
@ -657,6 +667,16 @@ mod tests {
assert_eq!(r.try_receive().unwrap(), 1); assert_eq!(r.try_receive().unwrap(), 1);
} }
#[test]
fn dynamic_dispatch_constructor() {
let c = Channel::<NoopRawMutex, u32, 3>::new();
let s = c.dyn_sender();
let r = c.dyn_receiver();
assert!(s.try_send(1).is_ok());
assert_eq!(r.try_receive().unwrap(), 1);
}
#[futures_test::test] #[futures_test::test]
async fn receiver_receives_given_try_send_async() { async fn receiver_receives_given_try_send_async() {
let executor = ThreadPool::new().unwrap(); let executor = ThreadPool::new().unwrap();