Merge pull request #2648 from peterkrull/dyn-channel-constructor

Add constructor for dynamic channel
This commit is contained in:
Dario Nieuwenhuis 2024-03-03 20:57:49 +00:00 committed by GitHub
commit 20760ff4f7
No known key found for this signature in database
GPG Key ID: B5690EEEBB952194

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();