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,
impl<T> RcSupplier<T>where
T: 'static,
Sourcepub fn new<F>(f: F) -> Selfwhere
F: FnMut() -> T + 'static,
pub fn new<F>(f: F) -> Selfwhere
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 = Supplier::into_box(closure);
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}Sourcepub fn map<U, F>(&self, mapper: F) -> RcSupplier<U>where
F: Mapper<T, U> + 'static,
U: 'static,
pub fn map<U, F>(&self, mapper: F) -> RcSupplier<U>where
F: Mapper<T, U> + 'static,
U: 'static,
Maps the output using a transformation function.
Borrows &self, doesn’t consume the original supplier.
§Parameters
mapper- The mapper to apply to the output. Can be:- A closure:
|x: T| -> U - A function pointer:
fn(T) -> U - A
BoxMapper<T, U>,RcMapper<T, U>,ArcMapper<T, U> - Any type implementing
Mapper<T, U>
- A closure:
§Returns
A new mapped RcSupplier<U>
§Examples
§Using with closure
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);§Using with Mapper object
use prism3_function::{RcSupplier, RcMapper, Supplier, Mapper};
let mapper = RcMapper::new(|x: i32| x * 2);
let source = RcSupplier::new(|| 10);
let mut supplier = source.map(mapper);
assert_eq!(supplier.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}Sourcepub fn filter<P>(&self, predicate: P) -> RcSupplier<Option<T>>
pub fn filter<P>(&self, predicate: P) -> RcSupplier<Option<T>>
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 evenSourcepub fn zip<U>(&self, other: &RcSupplier<U>) -> RcSupplier<(T, U)>where
U: 'static,
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>
- An
§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");Sourcepub fn memoize(&self) -> RcSupplier<T>where
T: Clone + 'static,
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>
impl<T> Clone 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
T: 'static,
fn into_box(self) -> BoxSupplier<T>where
T: 'static,
Converts to
BoxSupplier. Read moreSource§fn into_rc(self) -> RcSupplier<T>where
T: 'static,
fn into_rc(self) -> RcSupplier<T>where
T: 'static,
Converts to
RcSupplier. Read moreSource§fn to_box(&self) -> BoxSupplier<T>
fn to_box(&self) -> BoxSupplier<T>
Creates a
BoxSupplier from a cloned supplier. Read moreSource§fn to_rc(&self) -> RcSupplier<T>
fn to_rc(&self) -> RcSupplier<T>
Creates an
RcSupplier from a cloned supplier. Read moreSource§fn into_arc(self) -> ArcSupplier<T>
fn into_arc(self) -> ArcSupplier<T>
Converts to
ArcSupplier. Read moreAuto 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> BorrowMut<T> for Twhere
T: ?Sized,
impl<T> BorrowMut<T> for Twhere
T: ?Sized,
Source§fn borrow_mut(&mut self) -> &mut T
fn borrow_mut(&mut self) -> &mut T
Mutably borrows from an owned value. Read more