pub struct Receiver<T: State> { /* private fields */ }Expand description
Completion receiver for one-shot tasks
Implements Future directly, allowing direct .await usage on both owned values and mutable references
一次性任务完成通知接收器
直接实现了 Future,允许对拥有的值和可变引用都直接使用 .await
§Examples
§Using unit type for simple completion
use lite_sync::oneshot::lite::Sender;
let (notifier, receiver) = Sender::<()>::new();
tokio::spawn(async move {
// ... do work ...
notifier.notify(()); // Signal completion
});
// Two equivalent ways to await:
let result = receiver.await; // Direct await via Future impl
assert_eq!(result, ());§Awaiting on mutable reference
use lite_sync::oneshot::lite::Sender;
let (notifier, mut receiver) = Sender::<()>::new();
tokio::spawn(async move {
// ... do work ...
notifier.notify(());
});
// Can also await on &mut receiver
let result = (&mut receiver).await;
assert_eq!(result, ());§Using custom state
use lite_sync::oneshot::lite::{State, Sender};
#[derive(Debug, Clone, Copy, PartialEq, Eq)]
enum CustomState {
Success,
Failure,
Timeout,
}
impl State for CustomState {
fn to_u8(&self) -> u8 {
match self {
CustomState::Success => 1,
CustomState::Failure => 2,
CustomState::Timeout => 3,
}
}
fn from_u8(value: u8) -> Option<Self> {
match value {
1 => Some(CustomState::Success),
2 => Some(CustomState::Failure),
3 => Some(CustomState::Timeout),
_ => None,
}
}
fn pending_value() -> u8 {
0
}
}
let (notifier, receiver) = Sender::<CustomState>::new();
tokio::spawn(async move {
notifier.notify(CustomState::Success);
});
match receiver.await {
CustomState::Success => { /* Success! */ },
CustomState::Failure => { /* Failed */ },
CustomState::Timeout => { /* Timed out */ },
}Implementations§
Trait Implementations§
Source§impl<T: State> Future for Receiver<T>
Direct Future implementation for Receiver
impl<T: State> Future for Receiver<T>
Direct Future implementation for Receiver
This allows both receiver.await and (&mut receiver).await to work
Optimized implementation:
- Fast path: Immediate return if already completed (no allocation)
- Slow path: Direct waker registration (no Box allocation, just copy two pointers)
- No intermediate future state needed
为 Receiver 直接实现 Future
这允许 receiver.await 和 (&mut receiver).await 都能工作
优化实现:
- 快速路径:如已完成则立即返回(无分配)
- 慢速路径:直接注册 waker(无 Box 分配,只复制两个指针)
- 无需中间 future 状态
impl<T: State> Unpin for Receiver<T>
Auto Trait Implementations§
impl<T> Freeze for Receiver<T>
impl<T> !RefUnwindSafe for Receiver<T>
impl<T> Send for Receiver<T>
impl<T> Sync for Receiver<T>
impl<T> !UnwindSafe for Receiver<T>
Blanket Implementations§
Source§impl<T> BorrowMut<T> for Twhere
T: ?Sized,
impl<T> BorrowMut<T> for Twhere
T: ?Sized,
Source§fn borrow_mut(&mut self) -> &mut T
fn borrow_mut(&mut self) -> &mut T
Mutably borrows from an owned value. Read more
Source§impl<F> IntoFuture for Fwhere
F: Future,
impl<F> IntoFuture for Fwhere
F: Future,
Source§type IntoFuture = F
type IntoFuture = F
Which kind of future are we turning this into?
Source§fn into_future(self) -> <F as IntoFuture>::IntoFuture
fn into_future(self) -> <F as IntoFuture>::IntoFuture
Creates a future from a value. Read more