Struct thingbuf::mpsc::StaticChannel
source · pub struct StaticChannel<T, const CAPACITY: usize, R = DefaultRecycle> { /* private fields */ }
static
only.Expand description
A statically-allocated, asynchronous bounded MPSC channel.
A statically-allocated channel allows using a MPSC channel without
requiring any heap allocations, and can be used in environments that
don’t support liballoc
.
In order to use a statically-allocated channel, a StaticChannel
must
be constructed in a static
initializer. This reserves storage for the
channel’s message queue at compile-time. Then, at runtime, the channel
is split
into a StaticSender
/StaticReceiver
pair in order to
be used.
§Examples
use thingbuf::mpsc::StaticChannel;
// Construct a statically-allocated channel of `usize`s with a capacity
// of 16 messages.
static MY_CHANNEL: StaticChannel<usize, 16> = StaticChannel::new();
fn main() {
// Split the `StaticChannel` into a sender-receiver pair.
let (tx, rx) = MY_CHANNEL.split();
// Now, `tx` and `rx` can be used just like any other async MPSC
// channel...
}
Implementations§
source§impl<T, const CAPACITY: usize> StaticChannel<T, CAPACITY>
impl<T, const CAPACITY: usize> StaticChannel<T, CAPACITY>
sourcepub const fn new() -> Self
pub const fn new() -> Self
Constructs a new statically-allocated, asynchronous bounded MPSC channel.
A statically-allocated channel allows using a MPSC channel without
requiring any heap allocations, and can be used in environments that
don’t support liballoc
.
In order to use a statically-allocated channel, a StaticChannel
must
be constructed in a static
initializer. This reserves storage for the
channel’s message queue at compile-time. Then, at runtime, the channel
is split
into a StaticSender
/StaticReceiver
pair in order to
be used.
§Examples
use thingbuf::mpsc::StaticChannel;
// Construct a statically-allocated channel of `usize`s with a capacity
// of 16 messages.
static MY_CHANNEL: StaticChannel<usize, 16> = StaticChannel::new();
fn main() {
// Split the `StaticChannel` into a sender-receiver pair.
let (tx, rx) = MY_CHANNEL.split();
// Now, `tx` and `rx` can be used just like any other async MPSC
// channel...
}
source§impl<T, R, const CAPACITY: usize> StaticChannel<T, CAPACITY, R>
impl<T, R, const CAPACITY: usize> StaticChannel<T, CAPACITY, R>
sourcepub fn split(&'static self) -> (StaticSender<T, R>, StaticReceiver<T, R>)
pub fn split(&'static self) -> (StaticSender<T, R>, StaticReceiver<T, R>)
Split a StaticChannel
into a StaticSender
/StaticReceiver
pair.
A static channel can only be split a single time. If
StaticChannel::split
or StaticChannel::try_split
have been
called previously, this method will panic. For a non-panicking version
of this method, see StaticChannel::try_split
.
§Panics
If the channel has already been split.
sourcepub fn try_split(
&'static self
) -> Option<(StaticSender<T, R>, StaticReceiver<T, R>)>
pub fn try_split( &'static self ) -> Option<(StaticSender<T, R>, StaticReceiver<T, R>)>
Try to split a StaticChannel
into a StaticSender
/StaticReceiver
pair, returning None
if it has already been split.
A static channel can only be split a single time. If
StaticChannel::split
or StaticChannel::try_split
have been
called previously, this method returns None
.