pub struct ArcReadonlySupplier<T> { /* private fields */ }Expand description
Thread-safe shared ownership read-only supplier.
Uses Arc<dyn Fn() -> T + Send + Sync> for thread-safe shared
ownership. Lock-free - no Mutex needed! Can be cloned and
sent across threads with excellent concurrent performance.
§Ownership Model
Methods borrow &self instead of consuming self. The
original supplier remains usable after method calls:
use prism3_function::{ArcReadonlySupplier, ReadonlySupplier};
let source = ArcReadonlySupplier::new(|| 10);
let mapped = source.map(|x| x * 2);
// source is still usable here!§Lock-Free Performance
Unlike ArcSupplier, this implementation doesn’t need Mutex.
Multiple threads can call get() concurrently without lock
contention, making it ideal for high-concurrency scenarios.
§Examples
§Thread-safe Factory
use prism3_function::{ArcReadonlySupplier, ReadonlySupplier};
use std::thread;
let factory = ArcReadonlySupplier::new(|| {
String::from("Hello")
});
let f1 = factory.clone();
let f2 = factory.clone();
let h1 = thread::spawn(move || f1.get());
let h2 = thread::spawn(move || f2.get());
assert_eq!(h1.join().unwrap(), "Hello");
assert_eq!(h2.join().unwrap(), "Hello");§Reusable Transformations
use prism3_function::{ArcReadonlySupplier, ReadonlySupplier};
let base = ArcReadonlySupplier::new(|| 10);
let doubled = base.map(|x| x * 2);
let tripled = base.map(|x| x * 3);
// All remain usable
assert_eq!(base.get(), 10);
assert_eq!(doubled.get(), 20);
assert_eq!(tripled.get(), 30);§Author
Haixing Hu
Implementations§
Source§impl<T> ArcReadonlySupplier<T>where
T: Send + 'static,
impl<T> ArcReadonlySupplier<T>where
T: Send + 'static,
Sourcepub fn map<U, M>(&self, mapper: M) -> ArcReadonlySupplier<U>
pub fn map<U, M>(&self, mapper: M) -> ArcReadonlySupplier<U>
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 ArcReadonlySupplier<U>
§Examples
use prism3_function::{ArcReadonlySupplier, ReadonlySupplier};
let source = ArcReadonlySupplier::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) -> ArcReadonlySupplier<Option<T>>
pub fn filter<P>(&self, predicate: P) -> ArcReadonlySupplier<Option<T>>
Filters output based on a predicate.
§Parameters
predicate- The predicate to test the supplied value
§Returns
A new filtered ArcReadonlySupplier<Option<T>>
§Examples
use prism3_function::{ArcReadonlySupplier, ReadonlySupplier};
let source = ArcReadonlySupplier::new(|| 42);
let filtered = source.filter(|x| x % 2 == 0);
assert_eq!(filtered.get(), Some(42));Sourcepub fn zip<U>(
&self,
other: &ArcReadonlySupplier<U>,
) -> ArcReadonlySupplier<(T, U)>where
U: Send + 'static,
pub fn zip<U>(
&self,
other: &ArcReadonlySupplier<U>,
) -> ArcReadonlySupplier<(T, U)>where
U: Send + '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 ArcReadonlySupplier<(T, U)>
§Examples
use prism3_function::{ArcReadonlySupplier, ReadonlySupplier};
let first = ArcReadonlySupplier::new(|| 42);
let second = ArcReadonlySupplier::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 ArcReadonlySupplier<T>
impl<T> Clone for ArcReadonlySupplier<T>
Source§impl<T> ReadonlySupplier<T> for ArcReadonlySupplier<T>
impl<T> ReadonlySupplier<T> for ArcReadonlySupplier<T>
Source§fn into_box(self) -> BoxReadonlySupplier<T>where
T: 'static,
fn into_box(self) -> BoxReadonlySupplier<T>where
T: 'static,
BoxReadonlySupplier. Read moreSource§fn into_rc(self) -> RcReadonlySupplier<T>where
T: 'static,
fn into_rc(self) -> RcReadonlySupplier<T>where
T: 'static,
RcReadonlySupplier. Read moreSource§fn into_arc(self) -> ArcReadonlySupplier<T>where
T: Send + 'static,
fn into_arc(self) -> ArcReadonlySupplier<T>where
T: Send + 'static,
ArcReadonlySupplier. Read moreSource§fn into_fn(self) -> impl FnMut() -> T
fn into_fn(self) -> impl FnMut() -> T
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,
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,
RcReadonlySupplier by cloning. Read moreSource§fn to_arc(&self) -> ArcReadonlySupplier<T>
fn to_arc(&self) -> ArcReadonlySupplier<T>
ArcReadonlySupplier by cloning. Read more