pub struct RcReadonlySupplier<T> { /* private fields */ }Expand description
Single-threaded shared ownership read-only supplier.
Uses Rc<dyn Fn() -> T> for single-threaded shared ownership.
Can be cloned but not sent across threads.
§Ownership Model
Like ArcReadonlySupplier, methods borrow &self instead of
consuming self:
use prism3_function::{RcReadonlySupplier, ReadonlySupplier};
let source = RcReadonlySupplier::new(|| 10);
let mapped = source.map(|x| x * 2);
// source is still usable here!§Examples
§Shared Factory
use prism3_function::{RcReadonlySupplier, ReadonlySupplier};
let factory = RcReadonlySupplier::new(|| {
String::from("Hello")
});
let f1 = factory.clone();
let f2 = factory.clone();
assert_eq!(f1.get(), "Hello");
assert_eq!(f2.get(), "Hello");§Reusable Transformations
use prism3_function::{RcReadonlySupplier, ReadonlySupplier};
let base = RcReadonlySupplier::new(|| 10);
let doubled = base.map(|x| x * 2);
let tripled = base.map(|x| x * 3);
assert_eq!(base.get(), 10);
assert_eq!(doubled.get(), 20);
assert_eq!(tripled.get(), 30);§Author
Haixing Hu
Implementations§
Source§impl<T> RcReadonlySupplier<T>where
T: 'static,
impl<T> RcReadonlySupplier<T>where
T: 'static,
Sourcepub fn map<U, M>(&self, mapper: M) -> RcReadonlySupplier<U>where
M: Transformer<T, U> + 'static,
U: 'static,
pub fn map<U, M>(&self, mapper: M) -> RcReadonlySupplier<U>where
M: Transformer<T, U> + 'static,
U: 'static,
Maps the output using a transformation function.
Borrows &self, doesn’t consume the original supplier.
§Parameters
mapper- The transformer to apply to the output. Can be a closure, function pointer, or any type implementingTransformer<T, U>.
§Returns
A new mapped RcReadonlySupplier<U>
§Examples
use prism3_function::{RcReadonlySupplier, ReadonlySupplier};
let source = RcReadonlySupplier::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) -> RcReadonlySupplier<Option<T>>
pub fn filter<P>(&self, predicate: P) -> RcReadonlySupplier<Option<T>>
Filters output based on a predicate.
§Parameters
predicate- The predicate to test the supplied value
§Returns
A new filtered RcReadonlySupplier<Option<T>>
§Examples
use prism3_function::{RcReadonlySupplier, ReadonlySupplier};
let source = RcReadonlySupplier::new(|| 42);
let filtered = source.filter(|x| x % 2 == 0);
assert_eq!(filtered.get(), Some(42));Sourcepub fn zip<U>(
&self,
other: &RcReadonlySupplier<U>,
) -> RcReadonlySupplier<(T, U)>where
U: 'static,
pub fn zip<U>(
&self,
other: &RcReadonlySupplier<U>,
) -> RcReadonlySupplier<(T, U)>where
U: 'static,
Combines this supplier with another, producing a tuple.
§Parameters
other- The other supplier to combine with. Note: Passed by reference, so the original supplier remains usable.
§Returns
A new RcReadonlySupplier<(T, U)>
§Examples
use prism3_function::{RcReadonlySupplier, ReadonlySupplier};
let first = RcReadonlySupplier::new(|| 42);
let second = RcReadonlySupplier::new(|| "hello");
// second is passed by reference, so it remains usable
let zipped = first.zip(&second);
assert_eq!(zipped.get(), (42, "hello"));
// Both first and second still usable
assert_eq!(first.get(), 42);
assert_eq!(second.get(), "hello");Trait Implementations§
Source§impl<T> Clone for RcReadonlySupplier<T>
impl<T> Clone for RcReadonlySupplier<T>
Source§impl<T> ReadonlySupplier<T> for RcReadonlySupplier<T>
impl<T> ReadonlySupplier<T> for RcReadonlySupplier<T>
Source§fn into_box(self) -> BoxReadonlySupplier<T>where
T: 'static,
fn into_box(self) -> BoxReadonlySupplier<T>where
T: 'static,
Converts to
BoxReadonlySupplier. Read moreSource§fn into_rc(self) -> RcReadonlySupplier<T>where
T: 'static,
fn into_rc(self) -> RcReadonlySupplier<T>where
T: 'static,
Converts to
RcReadonlySupplier. Read moreSource§fn into_fn(self) -> impl FnMut() -> T
fn into_fn(self) -> impl FnMut() -> T
Converts to a closure implementing
FnMut() -> T. Read moreSource§fn to_box(&self) -> BoxReadonlySupplier<T>where
Self: Clone + 'static,
T: 'static,
fn to_box(&self) -> BoxReadonlySupplier<T>where
Self: Clone + 'static,
T: 'static,
Converts to
BoxReadonlySupplier by cloning. Read moreSource§fn to_rc(&self) -> RcReadonlySupplier<T>where
Self: Clone + 'static,
T: 'static,
fn to_rc(&self) -> RcReadonlySupplier<T>where
Self: Clone + 'static,
T: 'static,
Converts to
RcReadonlySupplier by cloning. Read moreSource§fn to_fn(&self) -> impl FnMut() -> Twhere
Self: Clone,
fn to_fn(&self) -> impl FnMut() -> Twhere
Self: Clone,
Converts to a closure by cloning. Read more
Source§fn into_arc(self) -> ArcReadonlySupplier<T>
fn into_arc(self) -> ArcReadonlySupplier<T>
Converts to
ArcReadonlySupplier. Read moreAuto Trait Implementations§
impl<T> Freeze for RcReadonlySupplier<T>
impl<T> !RefUnwindSafe for RcReadonlySupplier<T>
impl<T> !Send for RcReadonlySupplier<T>
impl<T> !Sync for RcReadonlySupplier<T>
impl<T> Unpin for RcReadonlySupplier<T>
impl<T> !UnwindSafe for RcReadonlySupplier<T>
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