RcReadonlySupplier

Struct RcReadonlySupplier 

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

Source

pub fn new<F>(f: F) -> Self
where F: Fn() -> T + 'static,

Creates a new RcReadonlySupplier.

§Parameters
  • f - The closure to wrap
§Returns

A new RcReadonlySupplier<T> instance

§Examples
use prism3_function::{RcReadonlySupplier, ReadonlySupplier};

let supplier = RcReadonlySupplier::new(|| 42);
assert_eq!(supplier.get(), 42);
Source

pub fn constant(value: T) -> Self
where T: Clone + 'static,

Creates a constant supplier.

§Parameters
  • value - The constant value to return
§Returns

A constant supplier

§Examples
use prism3_function::{RcReadonlySupplier, ReadonlySupplier};

let constant = RcReadonlySupplier::constant(42);
assert_eq!(constant.get(), 42);
assert_eq!(constant.get(), 42);
Source

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 implementing Transformer<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);
Source

pub fn filter<P>(&self, predicate: P) -> RcReadonlySupplier<Option<T>>
where P: Fn(&T) -> bool + 'static,

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));
Source

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>

Source§

fn clone(&self) -> Self

Clones the RcReadonlySupplier.

Creates a new instance that shares the underlying function with the original.

1.0.0 · Source§

fn clone_from(&mut self, source: &Self)

Performs copy-assignment from source. Read more
Source§

impl<T> ReadonlySupplier<T> for RcReadonlySupplier<T>

Source§

fn get(&self) -> T

Generates and returns a value. Read more
Source§

fn into_box(self) -> BoxReadonlySupplier<T>
where T: 'static,

Converts to BoxReadonlySupplier. Read more
Source§

fn into_rc(self) -> RcReadonlySupplier<T>
where T: 'static,

Converts to RcReadonlySupplier. Read more
Source§

fn into_fn(self) -> impl FnMut() -> T

Converts to a closure implementing FnMut() -> T. Read more
Source§

fn to_box(&self) -> BoxReadonlySupplier<T>
where Self: Clone + 'static, T: 'static,

Converts to BoxReadonlySupplier by cloning. Read more
Source§

fn to_rc(&self) -> RcReadonlySupplier<T>
where Self: Clone + 'static, T: 'static,

Converts to RcReadonlySupplier by cloning. Read more
Source§

fn to_fn(&self) -> impl FnMut() -> T
where Self: Clone,

Converts to a closure by cloning. Read more
Source§

fn into_arc(self) -> ArcReadonlySupplier<T>
where Self: Sized + Send + Sync + 'static, T: Send + 'static,

Converts to ArcReadonlySupplier. Read more
Source§

fn to_arc(&self) -> ArcReadonlySupplier<T>
where Self: Clone + Send + Sync + 'static, T: Send + 'static,

Converts to ArcReadonlySupplier by cloning. Read more

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> CloneToUninit for T
where T: Clone,

Source§

unsafe fn clone_to_uninit(&self, dest: *mut u8)

🔬This is a nightly-only experimental API. (clone_to_uninit)
Performs copy-assignment from self to dest. 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> ToOwned for T
where T: Clone,

Source§

type Owned = T

The resulting type after obtaining ownership.
Source§

fn to_owned(&self) -> T

Creates owned data from borrowed data, usually by cloning. Read more
Source§

fn clone_into(&self, target: &mut T)

Uses borrowed data to replace owned data, usually by cloning. Read more
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.