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