pub struct RcStatefulSupplier<T> { /* private fields */ }Expand description
Single-threaded shared ownership supplier.
Uses Rc<RefCell<dyn FnMut() -> T>> for single-threaded shared
ownership. Can be cloned but not sent across threads.
§Ownership Model
Like ArcStatefulSupplier, methods borrow &self instead of consuming
self:
use qubit_function::{RcStatefulSupplier, Supplier};
let source = RcStatefulSupplier::new(|| 10);
let mapped = source.map(|x| x * 2);
// source is still usable here!§Examples
§Shared Counter
use qubit_function::{RcStatefulSupplier, Supplier};
use std::rc::Rc;
use std::cell::RefCell;
let counter = Rc::new(RefCell::new(0));
let counter_clone = Rc::clone(&counter);
let supplier = RcStatefulSupplier::new(move || {
let mut c = counter_clone.borrow_mut();
*c += 1;
*c
});
let mut s1 = supplier.clone();
let mut s2 = supplier.clone();
assert_eq!(s1.get(), 1);
assert_eq!(s2.get(), 2);§Reusable Transformations
use qubit_function::{RcStatefulSupplier, Supplier};
let base = RcStatefulSupplier::new(|| 10);
let doubled = base.map(|x| x * 2);
let tripled = base.map(|x| x * 3);
let mut b = base;
let mut d = doubled;
let mut t = tripled;
assert_eq!(b.get(), 10);
assert_eq!(d.get(), 20);
assert_eq!(t.get(), 30);§Author
Haixing Hu
Implementations§
Source§impl<T> RcStatefulSupplier<T>
impl<T> RcStatefulSupplier<T>
Sourcepub fn new<F>(f: F) -> Selfwhere
F: FnMut() -> T + 'static,
pub fn new<F>(f: F) -> Selfwhere
F: FnMut() -> T + 'static,
Creates a new supplier.
Wraps the provided closure in the appropriate smart pointer type for this supplier implementation.
Sourcepub fn new_with_name<F>(name: &str, f: F) -> Selfwhere
F: FnMut() -> T + 'static,
pub fn new_with_name<F>(name: &str, f: F) -> Selfwhere
F: FnMut() -> T + 'static,
Creates a new named supplier.
Wraps the provided closure and assigns it a name, which is useful for debugging and logging purposes.
Sourcepub fn new_with_optional_name<F>(f: F, name: Option<String>) -> Selfwhere
F: FnMut() -> T + 'static,
pub fn new_with_optional_name<F>(f: F, name: Option<String>) -> Selfwhere
F: FnMut() -> T + 'static,
Creates a new named supplier with an optional name.
Wraps the provided closure and assigns it an optional name.
Sourcepub fn map<U, M>(&self, mapper: M) -> RcStatefulSupplier<U>where
T: 'static,
M: Transformer<T, U> + 'static,
U: 'static,
pub fn map<U, M>(&self, mapper: M) -> RcStatefulSupplier<U>where
T: 'static,
M: Transformer<T, U> + 'static,
U: 'static,
Maps the output using a transformation function.
§Parameters
mapper- The transformation function to apply
§Returns
A new $struct_name<U> with the mapped output
§Examples
use qubit_function::{$struct_name, $supplier_trait};
let source = $struct_name::new(|| 10);
let mapped = source.map(|x| x * 2);
// source is still usable
assert_eq!(mapped.get(), 20);Sourcepub fn filter<P>(&self, predicate: P) -> RcStatefulSupplier<Option<T>>where
T: 'static,
P: Predicate<T> + 'static,
pub fn filter<P>(&self, predicate: P) -> RcStatefulSupplier<Option<T>>where
T: 'static,
P: Predicate<T> + 'static,
Filters output based on a predicate.
§Parameters
predicate- The predicate to test the supplied value
§Returns
A new filtered $struct_name<Option<$t>>
§Examples
use qubit_function::{$struct_name, $supplier_trait};
let source = $struct_name::new(|| 42);
let filtered = source.filter(|x| x % 2 == 0);
assert_eq!(filtered.get(), Some(42));Sourcepub fn zip<U, S>(&self, other: S) -> RcStatefulSupplier<(T, U)>where
T: 'static,
S: StatefulSupplier<U> + 'static,
U: 'static,
pub fn zip<U, S>(&self, other: S) -> RcStatefulSupplier<(T, U)>where
T: 'static,
S: StatefulSupplier<U> + 'static,
U: 'static,
Combines this supplier with another, producing a tuple.
§Parameters
other- The other supplier to combine with
§Returns
A new $struct_name<($t, U)>
§Examples
use qubit_function::{$struct_name, $supplier_trait};
let first = $struct_name::new(|| 42);
let second = $struct_name::new(|| "hello");
let zipped = first.zip(second);
assert_eq!(zipped.get(), (42, "hello"));Sourcepub fn memoize(&self) -> RcStatefulSupplier<T>where
T: Clone + 'static,
pub fn memoize(&self) -> RcStatefulSupplier<T>where
T: Clone + 'static,
Creates a memoizing supplier.
§Returns
A new memoized RcStatefulSupplier<T>
§Examples
use qubit_function::{RcStatefulSupplier, Supplier};
use std::rc::Rc;
use std::cell::RefCell;
let call_count = Rc::new(RefCell::new(0));
let call_count_clone = Rc::clone(&call_count);
let source = RcStatefulSupplier::new(move || {
let mut c = call_count_clone.borrow_mut();
*c += 1;
42
});
let memoized = source.memoize();
let mut s = memoized;
assert_eq!(s.get(), 42); // Calls underlying function
assert_eq!(s.get(), 42); // Returns cached value
assert_eq!(*call_count.borrow(), 1);Trait Implementations§
Source§impl<T> Clone for RcStatefulSupplier<T>
impl<T> Clone for RcStatefulSupplier<T>
Source§impl<T> Debug for RcStatefulSupplier<T>
impl<T> Debug for RcStatefulSupplier<T>
Source§impl<T> Display for RcStatefulSupplier<T>
impl<T> Display for RcStatefulSupplier<T>
Source§impl<T> StatefulSupplier<T> for RcStatefulSupplier<T>
impl<T> StatefulSupplier<T> for RcStatefulSupplier<T>
Source§fn into_box(self) -> BoxStatefulSupplier<T>where
Self: 'static,
fn into_box(self) -> BoxStatefulSupplier<T>where
Self: 'static,
BoxStatefulSupplier. Read moreSource§fn into_rc(self) -> RcStatefulSupplier<T>
fn into_rc(self) -> RcStatefulSupplier<T>
RcStatefulSupplier. Read moreSource§fn to_box(&self) -> BoxStatefulSupplier<T>where
Self: 'static,
fn to_box(&self) -> BoxStatefulSupplier<T>where
Self: 'static,
BoxStatefulSupplier from a cloned supplier. Read moreSource§fn to_rc(&self) -> RcStatefulSupplier<T>
fn to_rc(&self) -> RcStatefulSupplier<T>
RcStatefulSupplier from a cloned supplier. Read moreSource§fn into_once(self) -> BoxSupplierOnce<T>where
Self: 'static,
fn into_once(self) -> BoxSupplierOnce<T>where
Self: 'static,
BoxSupplierOnce. Read moreSource§fn to_once(&self) -> BoxSupplierOnce<T>where
Self: 'static,
fn to_once(&self) -> BoxSupplierOnce<T>where
Self: 'static,
BoxSupplierOnce from a cloned supplier Read moreSource§fn into_arc(self) -> ArcStatefulSupplier<T>
fn into_arc(self) -> ArcStatefulSupplier<T>
ArcStatefulSupplier. Read more