pub fn channel<T, const N: usize>(
capacity: NonZeroUsize,
) -> (Sender<T, N>, Receiver<T, N>)Expand description
SPSC channel creation function
Creates a bounded SPSC channel with the specified capacity.
§Type Parameters
T: The type of messages to be sent through the channelN: The size of the inline buffer (number of elements stored inline before heap allocation)
§Parameters
capacity: Channel capacity (total number of elements the channel can hold)
§Returns
A tuple of (Sender, Receiver)
§Examples
use lite_sync::spsc::channel;
use std::num::NonZeroUsize;
#[tokio::main]
async fn main() {
#[cfg(not(feature = "loom"))]
{
// Create a channel with capacity 32 and inline buffer size 8
let (tx, rx) = channel::<i32, 8>(NonZeroUsize::new(32).unwrap());
tokio::spawn(async move {
tx.send(42).await.unwrap();
});
let value = rx.recv().await.unwrap();
assert_eq!(value, 42);
}
}SPSC 通道创建函数
创建指定容量的有界 SPSC 通道。
§类型参数
T: 通过通道发送的消息类型N: 内联缓冲区大小(在堆分配之前内联存储的元素数量)
§参数
capacity: 通道容量(通道可以容纳的元素总数)
§返回值
返回 (Sender, Receiver) 元组