pub struct RcSupplier<T> { /* private fields */ }Expand description
Single-threaded shared ownership stateless supplier.
Uses Rc<dyn Fn() -> 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 qubit_function::{RcSupplier, Supplier};
let source = RcSupplier::new(|| 10);
let mapped = source.map(|x| x * 2);
// source is still usable here!§Examples
§Shared Factory
use qubit_function::{RcSupplier, Supplier};
let factory = RcSupplier::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 qubit_function::{RcSupplier, Supplier};
let base = RcSupplier::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);Implementations§
Source§impl<T> RcSupplier<T>
impl<T> RcSupplier<T>
Sourcepub fn new<F>(f: F) -> Selfwhere
F: Fn() -> T + 'static,
pub fn new<F>(f: F) -> Selfwhere
F: Fn() -> T + 'static,
Creates a new supplier.
Wraps the provided closure in the appropriate smart pointer type for this supplier implementation.
Examples found in repository?
242fn demo_rc_supplier() {
243 println!("--- RcSupplier ---");
244
245 // Basic usage (Fn)
246 let supplier = RcSupplier::new(|| 42);
247 let s = supplier;
248 println!("Basic: {}", s.get());
249
250 // Shared state (FnMut) - use RcStatefulSupplier
251 let counter = Rc::new(RefCell::new(0));
252 let counter_clone = Rc::clone(&counter);
253 let supplier = RcStatefulSupplier::new(move || {
254 let mut c = counter_clone.borrow_mut();
255 *c += 1;
256 *c
257 });
258
259 let mut s1 = supplier.clone();
260 let mut s2 = supplier.clone();
261
262 println!("First clone: {}", s1.get());
263 println!("Second clone: {}", s2.get());
264 println!("First clone again: {}", s1.get());
265
266 // Reusable transformations (Fn)
267 let source = RcSupplier::new(|| 10);
268 let doubled = source.map(|x| x * 2);
269 let tripled = source.map(|x| x * 3);
270 let squared = source.map(|x| x * x);
271
272 let s = source;
273 let d = doubled;
274 let t = tripled;
275 let sq = squared;
276
277 println!("Source: {}", s.get());
278 println!("Doubled: {}", d.get());
279 println!("Tripled: {}", t.get());
280 println!("Squared: {}", sq.get());
281 println!();
282}
283
284fn demo_type_conversions() {
285 println!("--- Type Conversions ---");
286
287 // Closure to Box (Fn)
288 let closure = || 42;
289 let boxed = Supplier::into_box(closure);
290 println!("Closure -> Box: {}", boxed.get());
291
292 // Closure to Rc (Fn)
293 let closure = || 100;
294 let rc = Supplier::into_rc(closure);
295 println!("Closure -> Rc: {}", rc.get());
296
297 // Closure to Arc (Fn)
298 let closure = || 200;
299 let arc = Supplier::into_arc(closure);
300 println!("Closure -> Arc: {}", arc.get());
301
302 // Box to Rc (Fn)
303 let boxed = BoxSupplier::new(|| 42);
304 let rc = boxed.into_rc();
305 println!("Box -> Rc: {}", rc.get());
306
307 // Arc to Box (Fn)
308 let arc = ArcSupplier::new(|| 42);
309 let boxed = arc.into_box();
310 println!("Arc -> Box: {}", boxed.get());
311
312 // Rc to Box (Fn)
313 let rc = RcSupplier::new(|| 42);
314 let boxed = rc.into_box();
315 println!("Rc -> Box: {}", boxed.get());
316
317 println!();
318}Sourcepub fn new_with_name<F>(name: &str, f: F) -> Selfwhere
F: Fn() -> T + 'static,
pub fn new_with_name<F>(name: &str, f: F) -> Selfwhere
F: Fn() -> 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: Fn() -> T + 'static,
pub fn new_with_optional_name<F>(f: F, name: Option<String>) -> Selfwhere
F: Fn() -> T + 'static,
Creates a new named supplier with an optional name.
Wraps the provided closure and assigns it an optional name.
Sourcepub fn clear_name(&mut self)
pub fn clear_name(&mut self)
Clears the name of this supplier.
Sourcepub fn map<U, M>(&self, mapper: M) -> RcSupplier<U>where
T: 'static,
M: Transformer<T, U> + 'static,
U: 'static,
pub fn map<U, M>(&self, mapper: M) -> RcSupplier<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::{RcSupplier, Supplier};
let source = RcSupplier::new(|| 10);
let mapped = source.map(|x| x * 2);
// source is still usable
assert_eq!(mapped.get(), 20);Examples found in repository?
242fn demo_rc_supplier() {
243 println!("--- RcSupplier ---");
244
245 // Basic usage (Fn)
246 let supplier = RcSupplier::new(|| 42);
247 let s = supplier;
248 println!("Basic: {}", s.get());
249
250 // Shared state (FnMut) - use RcStatefulSupplier
251 let counter = Rc::new(RefCell::new(0));
252 let counter_clone = Rc::clone(&counter);
253 let supplier = RcStatefulSupplier::new(move || {
254 let mut c = counter_clone.borrow_mut();
255 *c += 1;
256 *c
257 });
258
259 let mut s1 = supplier.clone();
260 let mut s2 = supplier.clone();
261
262 println!("First clone: {}", s1.get());
263 println!("Second clone: {}", s2.get());
264 println!("First clone again: {}", s1.get());
265
266 // Reusable transformations (Fn)
267 let source = RcSupplier::new(|| 10);
268 let doubled = source.map(|x| x * 2);
269 let tripled = source.map(|x| x * 3);
270 let squared = source.map(|x| x * x);
271
272 let s = source;
273 let d = doubled;
274 let t = tripled;
275 let sq = squared;
276
277 println!("Source: {}", s.get());
278 println!("Doubled: {}", d.get());
279 println!("Tripled: {}", t.get());
280 println!("Squared: {}", sq.get());
281 println!();
282}Sourcepub fn filter<P>(&self, predicate: P) -> RcSupplier<Option<T>>where
T: 'static,
P: Predicate<T> + 'static,
pub fn filter<P>(&self, predicate: P) -> RcSupplier<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::{RcSupplier, Supplier};
let source = RcSupplier::new(|| 42);
let filtered = source.filter(|x: &i32| x % 2 == 0);
assert_eq!(filtered.get(), Some(42));Sourcepub fn zip<U, S>(&self, other: S) -> RcSupplier<(T, U)>where
T: 'static,
S: Supplier<U> + 'static,
U: 'static,
pub fn zip<U, S>(&self, other: S) -> RcSupplier<(T, U)>where
T: 'static,
S: Supplier<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::{RcSupplier, Supplier};
let first = RcSupplier::new(|| 42);
let second = RcSupplier::new(|| "hello");
let zipped = first.zip(second);
assert_eq!(zipped.get(), (42, "hello"));Trait Implementations§
Source§impl<T> Clone for RcSupplier<T>
impl<T> Clone for RcSupplier<T>
Source§impl<T> Debug for RcSupplier<T>
impl<T> Debug for RcSupplier<T>
Source§impl<T> Display for RcSupplier<T>
impl<T> Display for RcSupplier<T>
Source§impl<T> Supplier<T> for RcSupplier<T>
impl<T> Supplier<T> for RcSupplier<T>
Source§fn into_box(self) -> BoxSupplier<T>where
Self: 'static,
fn into_box(self) -> BoxSupplier<T>where
Self: 'static,
BoxSupplier. Read moreSource§fn into_rc(self) -> RcSupplier<T>
fn into_rc(self) -> RcSupplier<T>
RcSupplier. Read moreSource§fn to_box(&self) -> BoxSupplier<T>where
Self: 'static,
fn to_box(&self) -> BoxSupplier<T>where
Self: 'static,
BoxSupplier by cloning. Read moreSource§fn to_rc(&self) -> RcSupplier<T>
fn to_rc(&self) -> RcSupplier<T>
RcSupplier by cloning. 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 without consuming self Read moreSource§fn into_arc(self) -> ArcSupplier<T>
fn into_arc(self) -> ArcSupplier<T>
ArcSupplier. Read more