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().expect("thread should not panic"), "Hello");
assert_eq!(h2.join().expect("thread should not panic"), "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
179 .lock()
180 .expect("mutex should not be poisoned");
181 *c += 1;
182 println!(" Computation #{}", *c);
183 42
184 });
185
186 // Memoization with ArcStatefulSupplier
187 let memoized = source.memoize();
188 let mut m = memoized;
189 println!("First call: {}", m.get());
190 println!("Second call (cached): {}", m.get());
191 println!(
192 "Call count: {}",
193 *call_count.lock().expect("mutex should not be poisoned")
194 );
195 println!();
196}
197
198fn demo_arc_supplier_threading() {
199 println!("--- ArcStatefulSupplier Threading ---");
200
201 let counter = Arc::new(Mutex::new(0));
202 let counter_clone = Arc::clone(&counter);
203
204 let supplier = ArcStatefulSupplier::new(move || {
205 let mut c = counter_clone.lock().expect("mutex should not be poisoned");
206 *c += 1;
207 *c
208 });
209
210 let mut s1 = supplier.clone();
211 let mut s2 = supplier.clone();
212 let mut s3 = supplier;
213
214 let h1 = thread::spawn(move || {
215 let v1 = s1.get();
216 let v2 = s1.get();
217 println!("Thread 1: {} {}", v1, v2);
218 (v1, v2)
219 });
220
221 let h2 = thread::spawn(move || {
222 let v1 = s2.get();
223 let v2 = s2.get();
224 println!("Thread 2: {} {}", v1, v2);
225 (v1, v2)
226 });
227
228 let v1 = s3.get();
229 let v2 = s3.get();
230 println!("Main thread: {} {}", v1, v2);
231
232 h1.join().expect("thread should not panic");
233 h2.join().expect("thread should not panic");
234
235 println!(
236 "Final counter: {}",
237 *counter.lock().expect("mutex should not be poisoned")
238 );
239 println!();
240}
241
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) -> 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
179 .lock()
180 .expect("mutex should not be poisoned");
181 *c += 1;
182 println!(" Computation #{}", *c);
183 42
184 });
185
186 // Memoization with ArcStatefulSupplier
187 let memoized = source.memoize();
188 let mut m = memoized;
189 println!("First call: {}", m.get());
190 println!("Second call (cached): {}", m.get());
191 println!(
192 "Call count: {}",
193 *call_count.lock().expect("mutex should not be poisoned")
194 );
195 println!();
196}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