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
Refluxmodules.
§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
impl Inlet
sourcepub fn new<F, C, T, I>(
inlet_fn: F,
stop_sig: Arc<AtomicBool>,
init_data: I,
) -> (Self, Receiver<T>)
pub fn new<F, C, T, I>( inlet_fn: F, stop_sig: Arc<AtomicBool>, init_data: I, ) -> (Self, Receiver<T>)
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 theadd_routine!macro is necessary when passing in aninlet_fn.stop_sig- A flag to signal theInletobject to terminate
§Returns
A Inlet object and a Receiver to receive data from the inlet_fn
sourcepub fn new_bounded<F, C, T, I>(
inlet_fn: F,
stop_sig: Arc<AtomicBool>,
init_data: I,
data_limit: Option<usize>,
) -> (Self, Receiver<T>)
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>)
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 theadd_routine!macro is necessary when passing in aninlet_fn.stop_sig- A flag to signal theInletobject to terminatedata_limit- An optional parameter to limit channel capacity.
§Returns
A Inlet object and a Receiver to receive data from the inlet_fn
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> BorrowMut<T> for Twhere
T: ?Sized,
impl<T> BorrowMut<T> for Twhere
T: ?Sized,
source§fn borrow_mut(&mut self) -> &mut T
fn borrow_mut(&mut self) -> &mut T
Mutably borrows from an owned value. Read more