pub struct ArcSupplier<T> { /* private fields */ }Expand description
Thread-safe shared ownership stateless 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 qubit_function::{ArcSupplier, Supplier};
let source = ArcSupplier::new(|| 10);
let mapped = source.map(|x| x * 2);
// source is still usable here!§Lock-Free Performance
Unlike ArcStatefulSupplier, 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 qubit_function::{ArcSupplier, Supplier};
use std::thread;
let factory = ArcSupplier::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 qubit_function::{ArcSupplier, Supplier};
let base = ArcSupplier::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);Implementations§
Source§impl<T> ArcSupplier<T>
impl<T> ArcSupplier<T>
Sourcepub fn new<F>(f: F) -> Self
pub fn new<F>(f: F) -> Self
Creates a new supplier.
Wraps the provided closure in the appropriate smart pointer type for this supplier implementation.
Examples found in repository?
154fn demo_arc_supplier() {
155 println!("--- ArcSupplier ---");
156
157 // Basic usage (Fn)
158 let supplier = ArcSupplier::new(|| 42);
159 let s = supplier;
160 println!("Basic: {}", s.get());
161
162 // Reusable transformations (Fn)
163 let source = ArcSupplier::new(|| 10);
164 let doubled = source.map(|x| x * 2);
165 let tripled = source.map(|x| x * 3);
166
167 let s = source;
168 let d = doubled;
169 let t = tripled;
170 println!("Source: {}", s.get());
171 println!("Doubled: {}", d.get());
172 println!("Tripled: {}", t.get());
173
174 // Stateful with ArcStatefulSupplier (FnMut)
175 let call_count = Arc::new(Mutex::new(0));
176 let call_count_clone = Arc::clone(&call_count);
177 let source = ArcStatefulSupplier::new(move || {
178 let mut c = call_count_clone.lock().unwrap();
179 *c += 1;
180 println!(" Computation #{}", *c);
181 42
182 });
183
184 // Memoization with ArcStatefulSupplier
185 let memoized = source.memoize();
186 let mut m = memoized;
187 println!("First call: {}", m.get());
188 println!("Second call (cached): {}", m.get());
189 println!("Call count: {}", *call_count.lock().unwrap());
190 println!();
191}
192
193fn demo_arc_supplier_threading() {
194 println!("--- ArcStatefulSupplier Threading ---");
195
196 let counter = Arc::new(Mutex::new(0));
197 let counter_clone = Arc::clone(&counter);
198
199 let supplier = ArcStatefulSupplier::new(move || {
200 let mut c = counter_clone.lock().unwrap();
201 *c += 1;
202 *c
203 });
204
205 let mut s1 = supplier.clone();
206 let mut s2 = supplier.clone();
207 let mut s3 = supplier;
208
209 let h1 = thread::spawn(move || {
210 let v1 = s1.get();
211 let v2 = s1.get();
212 println!("Thread 1: {} {}", v1, v2);
213 (v1, v2)
214 });
215
216 let h2 = thread::spawn(move || {
217 let v1 = s2.get();
218 let v2 = s2.get();
219 println!("Thread 2: {} {}", v1, v2);
220 (v1, v2)
221 });
222
223 let v1 = s3.get();
224 let v2 = s3.get();
225 println!("Main thread: {} {}", v1, v2);
226
227 h1.join().unwrap();
228 h2.join().unwrap();
229
230 println!("Final counter: {}", *counter.lock().unwrap());
231 println!();
232}
233
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) -> Self
pub fn new_with_name<F>(name: &str, f: F) -> Self
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>) -> Self
pub fn new_with_optional_name<F>(f: F, name: Option<String>) -> Self
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) -> ArcSupplier<U>
pub fn map<U, M>(&self, mapper: M) -> ArcSupplier<U>
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::{ArcSupplier, Supplier};
let source = ArcSupplier::new(|| 10);
let mapped = source.map(|x| x * 2);
// source is still usable
assert_eq!(mapped.get(), 20);Examples found in repository?
154fn demo_arc_supplier() {
155 println!("--- ArcSupplier ---");
156
157 // Basic usage (Fn)
158 let supplier = ArcSupplier::new(|| 42);
159 let s = supplier;
160 println!("Basic: {}", s.get());
161
162 // Reusable transformations (Fn)
163 let source = ArcSupplier::new(|| 10);
164 let doubled = source.map(|x| x * 2);
165 let tripled = source.map(|x| x * 3);
166
167 let s = source;
168 let d = doubled;
169 let t = tripled;
170 println!("Source: {}", s.get());
171 println!("Doubled: {}", d.get());
172 println!("Tripled: {}", t.get());
173
174 // Stateful with ArcStatefulSupplier (FnMut)
175 let call_count = Arc::new(Mutex::new(0));
176 let call_count_clone = Arc::clone(&call_count);
177 let source = ArcStatefulSupplier::new(move || {
178 let mut c = call_count_clone.lock().unwrap();
179 *c += 1;
180 println!(" Computation #{}", *c);
181 42
182 });
183
184 // Memoization with ArcStatefulSupplier
185 let memoized = source.memoize();
186 let mut m = memoized;
187 println!("First call: {}", m.get());
188 println!("Second call (cached): {}", m.get());
189 println!("Call count: {}", *call_count.lock().unwrap());
190 println!();
191}Sourcepub fn filter<P>(&self, predicate: P) -> ArcSupplier<Option<T>>
pub fn filter<P>(&self, predicate: P) -> ArcSupplier<Option<T>>
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::{ArcSupplier, Supplier};
let source = ArcSupplier::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) -> ArcSupplier<(T, U)>
pub fn zip<U, S>(&self, other: S) -> ArcSupplier<(T, U)>
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::{ArcSupplier, Supplier};
let first = ArcSupplier::new(|| 42);
let second = ArcSupplier::new(|| "hello");
let zipped = first.zip(second);
assert_eq!(zipped.get(), (42, "hello"));Source§impl<T> ArcSupplier<T>
impl<T> ArcSupplier<T>
Sourcepub fn constant(value: T) -> Self
pub fn constant(value: T) -> Self
Creates a supplier that returns a constant value.
Creates a supplier that always returns the same value. Useful for default values or placeholder implementations.
Note: This method requires T: Sync because the constant value
is captured by a Fn closure which needs to be Sync for Arc.
§Parameters
value- The constant value to return
§Returns
Returns a new supplier instance that returns the constant value.
§Examples
use qubit_function::{ArcSupplier, Supplier};
let supplier = ArcSupplier::constant(42);
assert_eq!(supplier.get(), 42);
assert_eq!(supplier.get(), 42); // Can be called multiple timesTrait Implementations§
Source§impl<T> Clone for ArcSupplier<T>
impl<T> Clone for ArcSupplier<T>
Source§impl<T> Debug for ArcSupplier<T>
impl<T> Debug for ArcSupplier<T>
Source§impl<T> Display for ArcSupplier<T>
impl<T> Display for ArcSupplier<T>
Source§impl<T> Supplier<T> for ArcSupplier<T>
impl<T> Supplier<T> for ArcSupplier<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>where
Self: 'static,
fn into_rc(self) -> RcSupplier<T>where
Self: 'static,
RcSupplier. Read moreSource§fn into_arc(self) -> ArcSupplier<T>
fn into_arc(self) -> ArcSupplier<T>
ArcSupplier. 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_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>where
Self: 'static,
fn to_rc(&self) -> RcSupplier<T>where
Self: 'static,
RcSupplier by cloning. Read moreSource§fn to_arc(&self) -> ArcSupplier<T>
fn to_arc(&self) -> ArcSupplier<T>
ArcSupplier by cloning. 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 more