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?
234fn demo_rc_supplier() {
235 println!("--- RcSupplier ---");
236
237 // Basic usage (Fn)
238 let supplier = RcSupplier::new(|| 42);
239 let s = supplier;
240 println!("Basic: {}", s.get());
241
242 // Shared state (FnMut) - use RcStatefulSupplier
243 let counter = Rc::new(RefCell::new(0));
244 let counter_clone = Rc::clone(&counter);
245 let supplier = RcStatefulSupplier::new(move || {
246 let mut c = counter_clone.borrow_mut();
247 *c += 1;
248 *c
249 });
250
251 let mut s1 = supplier.clone();
252 let mut s2 = supplier.clone();
253
254 println!("First clone: {}", s1.get());
255 println!("Second clone: {}", s2.get());
256 println!("First clone again: {}", s1.get());
257
258 // Reusable transformations (Fn)
259 let source = RcSupplier::new(|| 10);
260 let doubled = source.map(|x| x * 2);
261 let tripled = source.map(|x| x * 3);
262 let squared = source.map(|x| x * x);
263
264 let s = source;
265 let d = doubled;
266 let t = tripled;
267 let sq = squared;
268
269 println!("Source: {}", s.get());
270 println!("Doubled: {}", d.get());
271 println!("Tripled: {}", t.get());
272 println!("Squared: {}", sq.get());
273 println!();
274}
275
276fn demo_type_conversions() {
277 println!("--- Type Conversions ---");
278
279 // Closure to Box (Fn)
280 let closure = || 42;
281 let boxed = Supplier::into_box(closure);
282 println!("Closure -> Box: {}", boxed.get());
283
284 // Closure to Rc (Fn)
285 let closure = || 100;
286 let rc = Supplier::into_rc(closure);
287 println!("Closure -> Rc: {}", rc.get());
288
289 // Closure to Arc (Fn)
290 let closure = || 200;
291 let arc = Supplier::into_arc(closure);
292 println!("Closure -> Arc: {}", arc.get());
293
294 // Box to Rc (Fn)
295 let boxed = BoxSupplier::new(|| 42);
296 let rc = boxed.into_rc();
297 println!("Box -> Rc: {}", rc.get());
298
299 // Arc to Box (Fn)
300 let arc = ArcSupplier::new(|| 42);
301 let boxed = arc.into_box();
302 println!("Arc -> Box: {}", boxed.get());
303
304 // Rc to Box (Fn)
305 let rc = RcSupplier::new(|| 42);
306 let boxed = rc.into_box();
307 println!("Rc -> Box: {}", boxed.get());
308
309 println!();
310}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?
234fn demo_rc_supplier() {
235 println!("--- RcSupplier ---");
236
237 // Basic usage (Fn)
238 let supplier = RcSupplier::new(|| 42);
239 let s = supplier;
240 println!("Basic: {}", s.get());
241
242 // Shared state (FnMut) - use RcStatefulSupplier
243 let counter = Rc::new(RefCell::new(0));
244 let counter_clone = Rc::clone(&counter);
245 let supplier = RcStatefulSupplier::new(move || {
246 let mut c = counter_clone.borrow_mut();
247 *c += 1;
248 *c
249 });
250
251 let mut s1 = supplier.clone();
252 let mut s2 = supplier.clone();
253
254 println!("First clone: {}", s1.get());
255 println!("Second clone: {}", s2.get());
256 println!("First clone again: {}", s1.get());
257
258 // Reusable transformations (Fn)
259 let source = RcSupplier::new(|| 10);
260 let doubled = source.map(|x| x * 2);
261 let tripled = source.map(|x| x * 3);
262 let squared = source.map(|x| x * x);
263
264 let s = source;
265 let d = doubled;
266 let t = tripled;
267 let sq = squared;
268
269 println!("Source: {}", s.get());
270 println!("Doubled: {}", d.get());
271 println!("Tripled: {}", t.get());
272 println!("Squared: {}", sq.get());
273 println!();
274}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