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
sourceimpl<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...
}
sourceimpl<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
.
Auto Trait Implementations
impl<T, const CAPACITY: usize, R = DefaultRecycle> !RefUnwindSafe for StaticChannel<T, CAPACITY, R>
impl<T, const CAPACITY: usize, R> Send for StaticChannel<T, CAPACITY, R> where
R: Send,
T: Send,
impl<T, const CAPACITY: usize, R> Sync for StaticChannel<T, CAPACITY, R> where
R: Sync,
T: Sync,
impl<T, const CAPACITY: usize, R> Unpin for StaticChannel<T, CAPACITY, R> where
R: Unpin,
T: Unpin,
impl<T, const CAPACITY: usize, R = DefaultRecycle> !UnwindSafe for StaticChannel<T, CAPACITY, R>
Blanket Implementations
sourceimpl<T> BorrowMut<T> for T where
T: ?Sized,
impl<T> BorrowMut<T> for T where
T: ?Sized,
const: unstable · sourcefn borrow_mut(&mut self) -> &mut T
fn borrow_mut(&mut self) -> &mut T
Mutably borrows from an owned value. Read more