RcSupplier

Struct RcSupplier 

Source
pub struct RcSupplier<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 ArcSupplier, methods borrow &self instead of consuming self:

use prism3_function::{RcSupplier, Supplier};

let source = RcSupplier::new(|| 10);
let mapped = source.map(|x| x * 2);
// source is still usable here!

§Examples

§Shared Counter

use prism3_function::{RcSupplier, Supplier};
use std::rc::Rc;
use std::cell::RefCell;

let counter = Rc::new(RefCell::new(0));
let counter_clone = Rc::clone(&counter);

let supplier = RcSupplier::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 prism3_function::{RcSupplier, Supplier};

let base = RcSupplier::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> RcSupplier<T>
where T: 'static,

Source

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

Creates a new RcSupplier.

§Parameters
  • f - The closure to wrap
§Returns

A new RcSupplier<T> instance

§Examples
use prism3_function::{RcSupplier, Supplier};

let supplier = RcSupplier::new(|| 42);
let mut s = supplier;
assert_eq!(s.get(), 42);
Examples found in repository?
examples/supplier_demo.rs (line 213)
209fn demo_rc_supplier() {
210    println!("--- RcSupplier ---");
211
212    // Basic usage
213    let supplier = RcSupplier::new(|| 42);
214    let mut s = supplier;
215    println!("Basic: {}", s.get());
216
217    // Shared state
218    let counter = Rc::new(RefCell::new(0));
219    let counter_clone = Rc::clone(&counter);
220    let supplier = RcSupplier::new(move || {
221        let mut c = counter_clone.borrow_mut();
222        *c += 1;
223        *c
224    });
225
226    let mut s1 = supplier.clone();
227    let mut s2 = supplier.clone();
228
229    println!("First clone: {}", s1.get());
230    println!("Second clone: {}", s2.get());
231    println!("First clone again: {}", s1.get());
232
233    // Reusable transformations
234    let source = RcSupplier::new(|| 10);
235    let doubled = source.map(|x| x * 2);
236    let tripled = source.map(|x| x * 3);
237    let squared = source.map(|x| x * x);
238
239    let mut s = source;
240    let mut d = doubled;
241    let mut t = tripled;
242    let mut sq = squared;
243
244    println!("Source: {}", s.get());
245    println!("Doubled: {}", d.get());
246    println!("Tripled: {}", t.get());
247    println!("Squared: {}", sq.get());
248    println!();
249}
250
251fn demo_type_conversions() {
252    println!("--- Type Conversions ---");
253
254    // Closure to Box
255    let closure = || 42;
256    let mut boxed = closure.into_box();
257    println!("Closure -> Box: {}", boxed.get());
258
259    // Closure to Rc
260    let closure = || 100;
261    let mut rc = closure.into_rc();
262    println!("Closure -> Rc: {}", rc.get());
263
264    // Closure to Arc
265    let closure = || 200;
266    let mut arc = closure.into_arc();
267    println!("Closure -> Arc: {}", arc.get());
268
269    // Box to Rc
270    let boxed = BoxSupplier::new(|| 42);
271    let mut rc = boxed.into_rc();
272    println!("Box -> Rc: {}", rc.get());
273
274    // Arc to Box
275    let arc = ArcSupplier::new(|| 42);
276    let mut boxed = arc.into_box();
277    println!("Arc -> Box: {}", boxed.get());
278
279    // Rc to Box
280    let rc = RcSupplier::new(|| 42);
281    let mut boxed = rc.into_box();
282    println!("Rc -> Box: {}", boxed.get());
283
284    println!();
285}
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::{RcSupplier, Supplier};

let constant = RcSupplier::constant(42);
let mut s = constant;
assert_eq!(s.get(), 42);
assert_eq!(s.get(), 42);
Source

pub fn map<U, F>(&self, mapper: F) -> RcSupplier<U>
where F: FnMut(T) -> U + 'static, U: 'static,

Maps the output using a transformation function.

Borrows &self, doesn’t consume the original supplier.

§Parameters
  • mapper - The function to apply to the output
§Returns

A new mapped RcSupplier<U>

§Examples
use prism3_function::{RcSupplier, Supplier};

let source = RcSupplier::new(|| 10);
let mapped = source.map(|x| x * 2);
// source is still usable
let mut s = mapped;
assert_eq!(s.get(), 20);
Examples found in repository?
examples/supplier_demo.rs (line 235)
209fn demo_rc_supplier() {
210    println!("--- RcSupplier ---");
211
212    // Basic usage
213    let supplier = RcSupplier::new(|| 42);
214    let mut s = supplier;
215    println!("Basic: {}", s.get());
216
217    // Shared state
218    let counter = Rc::new(RefCell::new(0));
219    let counter_clone = Rc::clone(&counter);
220    let supplier = RcSupplier::new(move || {
221        let mut c = counter_clone.borrow_mut();
222        *c += 1;
223        *c
224    });
225
226    let mut s1 = supplier.clone();
227    let mut s2 = supplier.clone();
228
229    println!("First clone: {}", s1.get());
230    println!("Second clone: {}", s2.get());
231    println!("First clone again: {}", s1.get());
232
233    // Reusable transformations
234    let source = RcSupplier::new(|| 10);
235    let doubled = source.map(|x| x * 2);
236    let tripled = source.map(|x| x * 3);
237    let squared = source.map(|x| x * x);
238
239    let mut s = source;
240    let mut d = doubled;
241    let mut t = tripled;
242    let mut sq = squared;
243
244    println!("Source: {}", s.get());
245    println!("Doubled: {}", d.get());
246    println!("Tripled: {}", t.get());
247    println!("Squared: {}", sq.get());
248    println!();
249}
Source

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

Filters output based on a predicate.

§Parameters
  • predicate - The predicate to test the supplied value
§Returns

A new filtered RcSupplier<Option<T>>

§Examples
use prism3_function::{RcSupplier, Supplier};
use std::rc::Rc;
use std::cell::RefCell;

let counter = Rc::new(RefCell::new(0));
let counter_clone = Rc::clone(&counter);
let source = RcSupplier::new(move || {
    let mut c = counter_clone.borrow_mut();
    *c += 1;
    *c
});
let filtered = source.filter(|x| x % 2 == 0);

let mut s = filtered;
assert_eq!(s.get(), None);     // 1 is odd
assert_eq!(s.get(), Some(2));  // 2 is even
Source

pub fn zip<U>(&self, other: &RcSupplier<U>) -> RcSupplier<(T, U)>
where U: 'static,

Combines this supplier with another, producing a tuple.

§Parameters
  • other - The other supplier to combine with. Note: This parameter is passed by reference, so the original supplier remains usable. Can be:
    • An RcSupplier<U> (passed by reference)
    • Any type implementing Supplier<U>
§Returns

A new RcSupplier<(T, U)>

§Examples
use prism3_function::{RcSupplier, Supplier};

let first = RcSupplier::new(|| 42);
let second = RcSupplier::new(|| "hello");

// second is passed by reference, so it remains usable
let zipped = first.zip(&second);

let mut z = zipped;
assert_eq!(z.get(), (42, "hello"));

// Both first and second still usable
let mut f = first;
let mut s = second;
assert_eq!(f.get(), 42);
assert_eq!(s.get(), "hello");
Source

pub fn memoize(&self) -> RcSupplier<T>
where T: Clone + 'static,

Creates a memoizing supplier.

§Returns

A new memoized RcSupplier<T>

§Examples
use prism3_function::{RcSupplier, 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 = RcSupplier::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 RcSupplier<T>

Source§

fn clone(&self) -> Self

Clones the RcSupplier.

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> Supplier<T> for RcSupplier<T>

Source§

fn get(&mut self) -> T

Generates and returns the next value. Read more
Source§

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

Converts to BoxSupplier. Read more
Source§

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

Converts to RcSupplier. Read more
Source§

fn into_arc(self) -> ArcSupplier<T>
where T: Send + 'static,

Converts to ArcSupplier. Read more

Auto Trait Implementations§

§

impl<T> Freeze for RcSupplier<T>

§

impl<T> !RefUnwindSafe for RcSupplier<T>

§

impl<T> !Send for RcSupplier<T>

§

impl<T> !Sync for RcSupplier<T>

§

impl<T> Unpin for RcSupplier<T>

§

impl<T> !UnwindSafe for RcSupplier<T>

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.