reflux

Struct Inlet

source
pub struct Inlet { /* private fields */ }
Expand description

Receives data from an external source and sends the data through a channel.

Using an Inlet yields the following benefits:

  • Abstracts away the use of channels. You only need to write a coroutine that takes a parameter and yields a result.
  • The coroutine does not have to be aware of termination signals, or joining threads. This functionality is handled by Inlet.
  • Easy integration with other Reflux modules.

§Example

 #![feature(coroutines, coroutine_trait, stmt_expr_attributes)]
 #![feature(unboxed_closures)]
 use std::sync::Arc;
 use std::sync::atomic::{AtomicBool, Ordering};
 use crossbeam_channel::Receiver;
 use reflux::add_routine;
 use reflux::Inlet;
 let stop_flag = Arc::new(AtomicBool::new(false));
 let (inlet, inlet_chan): (Inlet, Receiver<String>) = Inlet::new(
     add_routine!(#[coroutine] |_: ()| {
         yield "Hello, world".to_string()
     }), stop_flag.clone(), ());

 let data = inlet_chan.recv().unwrap();
 stop_flag.store(true, Ordering::Relaxed);
 inlet.join().unwrap();

 assert_eq!(data, "Hello, world".to_string())

Implementations§

source§

impl Inlet

source

pub fn new<F, C, T, I>( inlet_fn: F, stop_sig: Arc<AtomicBool>, init_data: I, ) -> (Self, Receiver<T>)
where F: Fn() -> C + Send + 'static, I: Send + 'static + Clone, C: Coroutine<I> + Send + 'static + Unpin, T: Send + 'static + From<<C as Coroutine<I>>::Yield>,

Creates a new Inlet object with an unbounded internal channel.

§Parameters
  • inlet_fn - A coroutine that reads and returns data from an external source. The use of the add_routine! macro is necessary when passing in an inlet_fn.
  • stop_sig - A flag to signal the Inlet object to terminate
§Returns

A Inlet object and a Receiver to receive data from the inlet_fn

source

pub fn new_bounded<F, C, T, I>( inlet_fn: F, stop_sig: Arc<AtomicBool>, init_data: I, data_limit: Option<usize>, ) -> (Self, Receiver<T>)
where F: Fn() -> C + Send + 'static, I: Send + 'static + Clone, C: Coroutine<I> + Send + 'static + Unpin, T: Send + 'static + From<<C as Coroutine<I>>::Yield>,

Creates a new Inlet object with a bounded internal channel.

§Parameters
  • inlet_fn - A coroutine that reads and returns data from an external source. The use of the add_routine! macro is necessary when passing in an inlet_fn.
  • stop_sig - A flag to signal the Inlet object to terminate
  • data_limit - An optional parameter to limit channel capacity.
§Returns

A Inlet object and a Receiver to receive data from the inlet_fn

source

pub fn join(self) -> Result<()>

Waits for the Inlet object to finish execution

Auto Trait Implementations§

§

impl Freeze for Inlet

§

impl !RefUnwindSafe for Inlet

§

impl Send for Inlet

§

impl Sync for Inlet

§

impl Unpin for Inlet

§

impl !UnwindSafe for Inlet

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, 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.