Struct MessageQueueBroker

Source
pub struct MessageQueueBroker<T: Hash + Eq, M> { /* private fields */ }
Expand description

Lock free in memory message queue broker for sending values between asynchronous tasks by tags.

Implementations§

Source§

impl<T, M> MessageQueueBroker<T, M>
where T: Hash + Eq + Clone,

Source

pub fn unbounded() -> Self

Creates unbounded broker.

Source

pub fn bounded(cap: usize) -> Self

Creates bounded broker.

Source

pub fn subscribe(&self, tag: T) -> Subscriber<T, M>

Subscribes to the tag.

If a queue with provided tag already exists, a new subscriber to it will be created, otherwise a new queue will be created.

Until the queue with provided tag has been created (no one has subscribed), all attempts to send a message will fail.

Source§

impl<T, M> MessageQueueBroker<T, M>
where T: Hash + Eq,

Source

pub fn close(&self)

Closes the broker. Sends and receives will fail.

Source

pub fn is_closed(&self) -> bool

Checks if the broker is closed.

Source

pub fn len(&self) -> usize

Gets the global queue length.

Source

pub fn is_empty(&self) -> bool

Checks if the global queue is empty.

Source

pub fn try_send<Q>(&self, tag: &Q, msg: M) -> Result<(), TrySendError<M>>
where Q: Hash + Equivalent<T> + ?Sized,

Trying to send a message with the tag.

If broker is closed, or there are no any subscriber to provided tag then returns TrySendError::Closed(_). If the tagged queue is full then returns TrySendError::Full(_).

§Ok
use mqb::MessageQueueBroker;

let mqb = MessageQueueBroker::unbounded();
let sub = mqb.subscribe(1);

assert!(mqb.try_send(&1, 1).is_ok());
assert_eq!(sub.try_recv().unwrap(), 1);
§No subscribers
use mqb::{MessageQueueBroker, TrySendError};

let mqb = MessageQueueBroker::<i32, i32>::unbounded();

assert_eq!(mqb.try_send(&1, 1).unwrap_err(), TrySendError::Closed(1));
§Broker closed
use mqb::{MessageQueueBroker, TrySendError};

let mqb = MessageQueueBroker::unbounded();
let sub = mqb.subscribe(1);

mqb.close();
assert_eq!(mqb.try_send(&1, 1).unwrap_err(), TrySendError::Closed(1));
§Queue is full
use mqb::{MessageQueueBroker, TrySendError};

let mqb = MessageQueueBroker::bounded(1);
let sub = mqb.subscribe(1);

assert!(mqb.try_send(&1, 1).is_ok());
assert_eq!(mqb.try_send(&1, 1).unwrap_err(), TrySendError::Full(1));
Source

pub async fn send<Q>(&self, tag: &Q, msg: M) -> Result<(), SendError<M>>
where Q: Hash + Equivalent<T> + ?Sized,

Sends a message with the tag.

If broker is closed, or there are no any subscriber to provided tag then returns TrySendError::Closed(_). If the tagged queue is full, it will wait until a slot becomes available.

§Ok
use mqb::MessageQueueBroker;

let mqb = MessageQueueBroker::unbounded();
let sub = mqb.subscribe(1);

assert!(mqb.send(&1, 1).await.is_ok());
assert_eq!(sub.recv().await.unwrap(), 1);
§No subscribers
use mqb::{MessageQueueBroker, SendError};

let mqb = MessageQueueBroker::<i32, i32>::unbounded();

assert_eq!(mqb.send(&1, 1).await.unwrap_err(), SendError(1));
§Broker closed
use mqb::{MessageQueueBroker, SendError};

let mqb = MessageQueueBroker::unbounded();
let sub = mqb.subscribe(1);

mqb.close();
assert_eq!(mqb.send(&1, 1).await.unwrap_err(), SendError(1));
§Queue is full
use mqb::{MessageQueueBroker, TrySendError};

let mqb = MessageQueueBroker::bounded(1);
let sub = mqb.subscribe(1);

assert!(mqb.send(&1, 1).await.is_ok());
// Wait endlessly...
mqb.send(&1, 2).await;

Trait Implementations§

Source§

impl<T: Debug + Hash + Eq, M: Debug> Debug for MessageQueueBroker<T, M>

Source§

fn fmt(&self, f: &mut Formatter<'_>) -> Result

Formats the value using the given formatter. Read more
Source§

impl<T, M> Drop for MessageQueueBroker<T, M>
where T: Hash + Eq,

Source§

fn drop(&mut self)

Executes the destructor for this type. Read more

Auto Trait Implementations§

§

impl<T, M> Freeze for MessageQueueBroker<T, M>

§

impl<T, M> RefUnwindSafe for MessageQueueBroker<T, M>

§

impl<T, M> Send for MessageQueueBroker<T, M>
where T: Send + Sync, M: Send,

§

impl<T, M> Sync for MessageQueueBroker<T, M>
where T: Send + Sync, M: Send,

§

impl<T, M> Unpin for MessageQueueBroker<T, M>

§

impl<T, M> UnwindSafe for MessageQueueBroker<T, M>

Blanket Implementations§

Source§

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

Source§

fn type_id(&self) -> TypeId

Gets the TypeId of self. Read more
Source§

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

Source§

fn borrow(&self) -> &T

Immutably borrows from an owned value. Read more
Source§

impl<T> BorrowMut<T> for T
where 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 T
where 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> Pointable for T

Source§

const ALIGN: usize

The alignment of pointer.
Source§

type Init = T

The type for initializers.
Source§

unsafe fn init(init: <T as Pointable>::Init) -> usize

Initializes a with the given initializer. Read more
Source§

unsafe fn deref<'a>(ptr: usize) -> &'a T

Dereferences the given pointer. Read more
Source§

unsafe fn deref_mut<'a>(ptr: usize) -> &'a mut T

Mutably dereferences the given pointer. Read more
Source§

unsafe fn drop(ptr: usize)

Drops the object pointed to by the given pointer. Read more
Source§

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

Source§

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 T
where U: TryFrom<T>,

Source§

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.