Struct thingbuf::mpsc::blocking::StaticChannel
source · pub struct StaticChannel<T, const CAPACITY: usize, R = DefaultRecycle> { /* private fields */ }
std
and static
only.Expand description
A statically-allocated, blocking bounded MPSC channel.
A statically-allocated channel allows using a MPSC channel without
requiring any heap allocations. The asynchronous variant may be
used in #![no_std]
environments without requiring liballoc
. This is a
synchronous version which requires the Rust standard library, because it
blocks the current thread in order to wait for send capacity. However, in
some cases, it may offer very slightly better performance than the
non-static blocking channel due to requiring fewer heap pointer
dereferences.
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::blocking::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, blocking bounded MPSC channel.
A statically-allocated channel allows using a MPSC channel without
requiring any heap allocations. The asynchronous variant may be
used in #![no_std]
environments without requiring liballoc
. This is a
synchronous version which requires the Rust standard library, because it
blocks the current thread in order to wait for send capacity. However, in
some cases, it may offer very slightly better performance than the
non-static blocking channel due to requiring fewer heap pointer
dereferences.
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
.