pub struct Channel<T> { /* private fields */ }
Available on crate feature channel only.
Expand description

A restricted async-signal-safe channel

This is a bit like the usual channel used for inter-thread communication, but with several restrictions:

  • There’s a limited number of slots (currently 5).
  • There’s no way to wait for a place in it or for a value. If value is not available, None is returned. If there’s no space for a value, the value is silently dropped.

In exchange for that, all the operations on that channel are async-signal-safe. That means it is possible to use it to communicate between a signal handler and the rest of the world with it (specifically, it’s designed to send information from the handler to the rest of the application). The throwing out of values when full is in line with collating of the same type in kernel (you should not use the same channel for multiple different signals).

Technically, this is a MPMC queue which preserves order, but it is expected to be used in MPSC mode mostly (in theory, multiple threads can be executing a signal handler for the same signal at the same time). The channel is not responsible for wakeups.

While the channel is async-signal-safe, you still need to make sure creating of the values is too (it should not contain anything that allocates, for example ‒ so no Strings inside, etc).

The code was not tuned for performance (signals are not expected to happen often).

Implementations§

source§

impl<T> Channel<T>

source

pub fn new() -> Self

Creates a new channel with nothing in it.

source

pub fn send(&self, val: T)

Inserts a value into the channel.

If the value doesn’t fit, it is silently dropped. Never blocks.

source

pub fn recv(&self) -> Option<T>

Takes a value from the channel.

Or returns None if the channel is empty. Never blocks.

Trait Implementations§

source§

impl<T> Default for Channel<T>

source§

fn default() -> Self

Returns the “default value” for a type. Read more
source§

impl<T: Send> Send for Channel<T>

source§

impl<T: Send> Sync for Channel<T>

Auto Trait Implementations§

§

impl<T> !RefUnwindSafe for Channel<T>

§

impl<T> Unpin for Channel<T>where T: Unpin,

§

impl<T> UnwindSafe for Channel<T>where T: UnwindSafe,

Blanket Implementations§

source§

impl<T> Any for Twhere T: 'static + ?Sized,

source§

fn type_id(&self) -> TypeId

Gets the TypeId of self. Read more
source§

impl<T> Borrow<T> for Twhere T: ?Sized,

source§

fn borrow(&self) -> &T

Immutably borrows from an owned value. Read more
source§

impl<T> BorrowMut<T> for Twhere T: ?Sized,

source§

fn borrow_mut(&mut self) -> &mut T

Mutably borrows from an owned value. Read more
source§

impl<T> From<T> for T

source§

fn from(t: T) -> T

Returns the argument unchanged.

source§

impl<T, U> Into<U> for Twhere U: From<T>,

source§

fn into(self) -> U

Calls U::from(self).

That is, this conversion is whatever the implementation of From<T> for U chooses to do.

source§

impl<T, U> TryFrom<U> for Twhere U: Into<T>,

§

type Error = Infallible

The type returned in the event of a conversion error.
source§

fn try_from(value: U) -> Result<T, <T as TryFrom<U>>::Error>

Performs the conversion.
source§

impl<T, U> TryInto<U> for Twhere U: TryFrom<T>,

§

type Error = <U as TryFrom<T>>::Error

The type returned in the event of a conversion error.
source§

fn try_into(self) -> Result<U, <U as TryFrom<T>>::Error>

Performs the conversion.