Struct Carousel

Source
pub struct Carousel { /* private fields */ }
Expand description

Carousel represents a multi producer multi polling consumer data carousel. It enables message passing from multiple producers to multiple consumers asynchronously.

It accepts a vector of bytes as a message/ event.

A mp2c Carousel can be created for a list of consumers. However, each consumer is expected to implement the Consumer trait.

A multiplier thread is started which receives one end of an async channel. Each message put on the Carousel is sent to this multiplier thread. The job of the Multiplier is to clone each incoming event/ message and send it to each polling consumer.

For each consumer, a poller thread is started which receives one end of an async channel. The transmitting end of the channel is with the Multiplier thread. The poller calls Consumer::consume on it’s registered consumer.

An Carousel can be cloned and the clone creates a clone of the Sender from which it is cloned. When Carousel::put is called to send a message, it’ll be sent to the pollers in the originating Carousel.

§Example

use mp2c::asynch::{Carousel, Consumer};

struct TestConsumer1;

impl Consumer for TestConsumer1 {
  fn consume(&mut self, data: Vec<u8>) {
    let msg = String::from_utf8(data).unwrap();
    // do something with msg
  }
}

struct TestConsumer2;

impl Consumer for TestConsumer2 {
 fn consume(&mut self, data: Vec<u8>) {
   let msg = String::from_utf8(data).unwrap();
   // do something with msg   
 }
}

let mut v: Vec<Box<dyn Consumer + Send + 'static>> = Vec::new();
v.push(Box::new(TestConsumer1));
v.push(Box::new(TestConsumer2));

let c = Carousel::new(v);

for _ in 1..10 {
  let cloned_c = c.clone();
  let t = std::thread::spawn(move || {
    cloned_c.put(String::from("test").into_bytes());
  });
  t.join().unwrap();
}

Implementations§

Source

pub fn new<T>(consumers: Vec<Box<T>>) -> Carousel
where T: Consumer + Send + 'static + ?Sized,

Creates a new Carousel for a vector of consumers.

Source

pub fn put(&self, data: Vec<u8>)

Puts a message on the Carousel which will be asynchronously sent to all it’s consumers.

Trait Implementations§

Source§

fn clone(&self) -> Self

Returns a copy of the value. Read more
1.0.0 · Source§

fn clone_from(&mut self, source: &Self)

Performs copy-assignment from source. Read more
Source§

fn drop(&mut self)

Executes the destructor for this type. Read more

Auto Trait Implementations§

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> CloneToUninit for T
where T: Clone,

Source§

unsafe fn clone_to_uninit(&self, dest: *mut u8)

🔬This is a nightly-only experimental API. (clone_to_uninit)
Performs copy-assignment from self to dest. 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> ToOwned for T
where T: Clone,

Source§

type Owned = T

The resulting type after obtaining ownership.
Source§

fn to_owned(&self) -> T

Creates owned data from borrowed data, usually by cloning. Read more
Source§

fn clone_into(&self, target: &mut T)

Uses borrowed data to replace owned data, usually by cloning. 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.