State

Trait State 

Source
pub trait State:
    Sized
    + Send
    + Sync
    + 'static {
    // Required methods
    fn to_u8(&self) -> u8;
    fn from_u8(value: u8) -> Option<Self>;
    fn pending_value() -> u8;
    fn closed_value() -> u8;
    fn receiver_closed_value() -> u8;
}
Expand description

Trait for types that can be used as oneshot state

Types implementing this trait can be converted to/from u8 for atomic storage. This allows for zero-allocation, lock-free state transitions.

可用作 oneshot 状态的类型的 trait

实现此 trait 的类型可以与 u8 互相转换以进行原子存储。 这允许零分配、无锁的状态转换。

§Built-in Implementations

  • (): Simple completion notification without state

§Example: 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
    }
     
    fn closed_value() -> u8 {
        255
    }
     
    fn receiver_closed_value() -> u8 {
        254
    }
}

// Usage:
let (notifier, receiver) = Sender::<CustomState>::new();
tokio::spawn(async move {
    notifier.send(CustomState::Success);
});
let result = receiver.await; // Direct await
assert_eq!(result, Ok(CustomState::Success));

Required Methods§

Source

fn to_u8(&self) -> u8

Convert the state to u8 for atomic storage

将状态转换为 u8 以进行原子存储

Source

fn from_u8(value: u8) -> Option<Self>

Convert u8 back to the state type

Returns None if the value doesn’t represent a valid state

将 u8 转换回状态类型

如果值不代表有效状态则返回 None

Source

fn pending_value() -> u8

The pending state value (before completion)

待处理状态值(完成前)

Source

fn closed_value() -> u8

The closed state value (sender was dropped without sending)

已关闭状态值(发送器被丢弃而未发送)

Source

fn receiver_closed_value() -> u8

The receiver closed state value

接收器关闭状态值

Dyn Compatibility§

This trait is not dyn compatible.

In older versions of Rust, dyn compatibility was called "object safety", so this trait is not object safe.

Implementations on Foreign Types§

Source§

impl State for ()

Implementation for unit type () - simple completion notification without state

为单元类型 () 实现 - 简单的完成通知,无需状态信息

Implementors§