reflux

Struct Extractor

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

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

Using an Extractor 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 Extractor.
  • 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::Extractor;
 let stop_flag = Arc::new(AtomicBool::new(false));
 let (extractor, inlet_chan): (Extractor, Receiver<String>) = Extractor::new(
     add_routine!(#[coroutine] |_: ()| {
         yield "Hello, world".to_string()
     }), stop_flag.clone(), None, (), None);

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

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

Implementations§

Source§

impl Extractor

Source

pub fn new<F, C, T, I>( extract_fn: F, stop_sig: Arc<AtomicBool>, pause_sig: Option<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 Extractor object.

§Parameters
  • extract_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 extract_fn.
  • pause_sig - A flag to signal the Extractor object to pause execution.
  • stop_sig - A flag to signal the Extractor object to terminate execution.
§Returns
  • A Extractor object.
  • AReceiver channel to receive data from the extract_fn.
Source

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

Waits for the Extractor object to finish execution.

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