Receiver

Struct Receiver 

Source
pub struct Receiver<T> { /* private fields */ }
Expand description

Receiver for one-shot value transfer

Implements Future directly, allowing direct .await usage on both owned values and mutable references

一次性值传递的接收器

直接实现了 Future,允许对拥有的值和可变引用都直接使用 .await

§Examples

§Basic usage

use lite_sync::oneshot::generic::Sender;
 
let (sender, receiver) = Sender::<String>::new();
 
tokio::spawn(async move {
    sender.send("Hello, World!".to_string()).unwrap();
});
 
// Direct await via Future impl
let message = receiver.await;
assert_eq!(message, "Hello, World!");

§Awaiting on mutable reference

use lite_sync::oneshot::generic::Sender;
 
let (sender, mut receiver) = Sender::<i32>::new();
 
tokio::spawn(async move {
    sender.send(42).unwrap();
});
 
// Can also await on &mut receiver
let value = (&mut receiver).await;
assert_eq!(value, 42);

Implementations§

Source§

impl<T> Receiver<T>

Source

pub async fn wait(self) -> T

Wait for value asynchronously

This is equivalent to using .await directly on the receiver

异步等待值

这等同于直接在 receiver 上使用 .await

§Returns

Returns the received value

§返回值

返回接收到的值

Source

pub fn try_recv(&mut self) -> Option<T>

Try to receive a value without blocking

Returns None if no value has been sent yet

尝试接收值而不阻塞

如果还没有发送值则返回 None

Trait Implementations§

Source§

impl<T> 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 value already sent (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 状态
Source§

type Output = T

The type of value produced on completion.
Source§

fn poll(self: Pin<&mut Self>, cx: &mut Context<'_>) -> Poll<Self::Output>

Attempts to resolve the future to a final value, registering the current task for wakeup if the value is not yet available. Read more
Source§

impl<T> 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>
where T: Send,

§

impl<T> Sync for Receiver<T>
where T: Send,

§

impl<T> !UnwindSafe for Receiver<T>

Blanket Implementations§

Source§

impl<T> Any for T
where T: 'static + ?Sized,

Source§

fn type_id(&self) -> TypeId

Gets the TypeId of self. Read more
Source§

impl<T> Borrow<T> for T
where T: ?Sized,

Source§

fn borrow(&self) -> &T

Immutably borrows from an owned value. Read more
Source§

impl<T> BorrowMut<T> for T
where T: ?Sized,

Source§

fn borrow_mut(&mut self) -> &mut T

Mutably borrows from an owned value. Read more
Source§

impl<T> From<T> for T

Source§

fn from(t: T) -> T

Returns the argument unchanged.

Source§

impl<T, U> Into<U> for T
where U: From<T>,

Source§

fn into(self) -> U

Calls U::from(self).

That is, this conversion is whatever the implementation of From<T> for U chooses to do.

Source§

impl<F> IntoFuture for F
where F: Future,

Source§

type Output = <F as Future>::Output

The output that the future will produce on completion.
Source§

type IntoFuture = F

Which kind of future are we turning this into?
Source§

fn into_future(self) -> <F as IntoFuture>::IntoFuture

Creates a future from a value. Read more
Source§

impl<T, U> TryFrom<U> for T
where U: Into<T>,

Source§

type Error = Infallible

The type returned in the event of a conversion error.
Source§

fn try_from(value: U) -> Result<T, <T as TryFrom<U>>::Error>

Performs the conversion.
Source§

impl<T, U> TryInto<U> for T
where U: TryFrom<T>,

Source§

type Error = <U as TryFrom<T>>::Error

The type returned in the event of a conversion error.
Source§

fn try_into(self) -> Result<U, <U as TryFrom<T>>::Error>

Performs the conversion.