qubit_function/suppliers/supplier.rs
1/*******************************************************************************
2 *
3 * Copyright (c) 2025 - 2026.
4 * Haixing Hu, Qubit Co. Ltd.
5 *
6 * All rights reserved.
7 *
8 ******************************************************************************/
9//! # Read-only Supplier Types
10//!
11//! Provides read-only supplier implementations that generate and
12//! return values without modifying their own state.
13//!
14//! # Overview
15//!
16//! A **Supplier** is a functional abstraction that
17//! generates values without accepting input or modifying its own
18//! state. Unlike `Supplier`, it uses `&self` instead of `&mut
19//! self`, enabling usage in read-only contexts and lock-free
20//! concurrent access.
21//!
22//! # Key Differences from Supplier
23//!
24//! | Aspect | Supplier | Supplier |
25//! |--------|----------|------------------|
26//! | self signature | `&mut self` | `&self` |
27//! | Closure type | `FnMut() -> T` | `Fn() -> T` |
28//! | Can modify state | Yes | No |
29//! | Arc implementation | `Arc<Mutex<FnMut>>` | `Arc<Fn>` (lock-free!) |
30//! | Use cases | Counter, generator | Factory, constant, high concurrency |
31//!
32//! # Three Implementations
33//!
34//! - **`BoxSupplier<T>`**: Single ownership using `Box<dyn
35//! Fn() -> T>`. Zero overhead, cannot be cloned. Best for
36//! one-time use in read-only contexts.
37//!
38//! - **`ArcSupplier<T>`**: Thread-safe shared ownership
39//! using `Arc<dyn Fn() -> T + Send + Sync>`. **Lock-free** - no
40//! Mutex needed! Can be cloned and sent across threads with
41//! excellent performance.
42//!
43//! - **`RcSupplier<T>`**: Single-threaded shared ownership
44//! using `Rc<dyn Fn() -> T>`. Can be cloned but not sent across
45//! threads. Lightweight alternative to `ArcSupplier`.
46//!
47//! # Use Cases
48//!
49//! ## 1. Calling in `&self` Methods
50//!
51//! ```rust
52//! use qubit_function::{ArcSupplier, Supplier};
53//!
54//! struct Executor<E> {
55//! error_supplier: ArcSupplier<E>,
56//! }
57//!
58//! impl<E> Executor<E> {
59//! fn execute(&self) -> Result<(), E> {
60//! // Can call directly in &self method!
61//! Err(self.error_supplier.get())
62//! }
63//! }
64//! ```
65//!
66//! ## 2. High-Concurrency Lock-Free Access
67//!
68//! ```rust
69//! use qubit_function::{ArcSupplier, Supplier};
70//! use std::thread;
71//!
72//! let factory = ArcSupplier::new(|| {
73//! String::from("Hello, World!")
74//! });
75//!
76//! let handles: Vec<_> = (0..10)
77//! .map(|_| {
78//! let f = factory.clone();
79//! thread::spawn(move || f.get()) // Lock-free!
80//! })
81//! .collect();
82//!
83//! for h in handles {
84//! assert_eq!(h.join().unwrap(), "Hello, World!");
85//! }
86//! ```
87//!
88//! ## 3. Fixed Factories
89//!
90//! ```rust
91//! use qubit_function::{BoxSupplier, Supplier};
92//!
93//! #[derive(Clone)]
94//! struct Config {
95//! timeout: u64,
96//! }
97//!
98//! let config_factory = BoxSupplier::new(|| Config {
99//! timeout: 30,
100//! });
101//!
102//! assert_eq!(config_factory.get().timeout, 30);
103//! assert_eq!(config_factory.get().timeout, 30);
104//! ```
105//!
106//! # Performance Comparison
107//!
108//! For stateless scenarios in multi-threaded environments:
109//!
110//! - `ArcSupplier<T>`: Requires `Mutex`, lock contention on
111//! every `get()` call
112//! - `ArcSupplier<T>`: Lock-free, can call `get()`
113//! concurrently without contention
114//!
115//! Benchmark results show `ArcSupplier` can be **10x
116//! faster** than `ArcSupplier` in high-concurrency scenarios.
117//!
118//! # Author
119//!
120//! Haixing Hu
121
122use std::rc::Rc;
123use std::sync::Arc;
124
125use crate::macros::{
126 impl_arc_conversions,
127 impl_box_conversions,
128 impl_closure_trait,
129 impl_rc_conversions,
130};
131use crate::predicates::predicate::Predicate;
132use crate::suppliers::macros::{
133 impl_box_supplier_methods,
134 impl_shared_supplier_methods,
135 impl_supplier_clone,
136 impl_supplier_common_methods,
137 impl_supplier_debug_display,
138};
139use crate::transformers::transformer::Transformer;
140use crate::BoxSupplierOnce;
141
142// ======================================================================
143// Supplier Trait
144// ======================================================================
145
146/// Read-only supplier trait: generates values without modifying
147/// state.
148///
149/// The core abstraction for stateless value generation. Unlike
150/// `Supplier<T>`, it uses `&self` instead of `&mut self`, enabling
151/// usage in read-only contexts and lock-free concurrent access.
152///
153/// # Key Characteristics
154///
155/// - **No input parameters**: Pure value generation
156/// - **Read-only access**: Uses `&self`, doesn't modify state
157/// - **Returns ownership**: Returns `T` (not `&T`) to avoid
158/// lifetime issues
159/// - **Lock-free concurrency**: `Arc` implementation doesn't need
160/// `Mutex`
161///
162/// # Automatically Implemented for Closures
163///
164/// All `Fn() -> T` closures automatically implement this trait,
165/// enabling seamless integration with both raw closures and
166/// wrapped supplier types.
167///
168/// # Examples
169///
170/// ## Using with Generic Functions
171///
172/// ```rust
173/// use qubit_function::{Supplier, BoxSupplier};
174///
175/// fn call_twice<S: Supplier<i32>>(supplier: &S)
176/// -> (i32, i32)
177/// {
178/// (supplier.get(), supplier.get())
179/// }
180///
181/// let s = BoxSupplier::new(|| 42);
182/// assert_eq!(call_twice(&s), (42, 42));
183///
184/// let closure = || 100;
185/// assert_eq!(call_twice(&closure), (100, 100));
186/// ```
187///
188/// ## Stateless Factory
189///
190/// ```rust
191/// use qubit_function::Supplier;
192///
193/// struct User {
194/// name: String,
195/// }
196///
197/// impl User {
198/// fn new() -> Self {
199/// User {
200/// name: String::from("Default"),
201/// }
202/// }
203/// }
204///
205/// let factory = || User::new();
206/// let user1 = factory.get();
207/// let user2 = factory.get();
208/// // Each call creates a new User instance
209/// ```
210///
211/// # Author
212///
213/// Haixing Hu
214pub trait Supplier<T> {
215 /// Generates and returns a value.
216 ///
217 /// Executes the underlying function and returns the generated
218 /// value. Uses `&self` because the supplier doesn't modify its
219 /// own state.
220 ///
221 /// # Returns
222 ///
223 /// The generated value of type `T`
224 ///
225 /// # Examples
226 ///
227 /// ```rust
228 /// use qubit_function::{Supplier, BoxSupplier};
229 ///
230 /// let supplier = BoxSupplier::new(|| 42);
231 /// assert_eq!(supplier.get(), 42);
232 /// assert_eq!(supplier.get(), 42);
233 /// ```
234 fn get(&self) -> T;
235
236 /// Converts to `BoxSupplier`.
237 ///
238 /// This method has a default implementation that wraps the
239 /// supplier in a `BoxSupplier`. Custom implementations
240 /// can override this method for optimization purposes.
241 ///
242 /// # Returns
243 ///
244 /// A new `BoxSupplier<T>` instance
245 ///
246 /// # Examples
247 ///
248 /// ```rust
249 /// use qubit_function::Supplier;
250 ///
251 /// let closure = || 42;
252 /// let boxed = closure.into_box();
253 /// assert_eq!(boxed.get(), 42);
254 /// ```
255 fn into_box(self) -> BoxSupplier<T>
256 where
257 Self: Sized + 'static,
258 T: 'static,
259 {
260 BoxSupplier::new(move || self.get())
261 }
262
263 /// Converts to `RcSupplier`.
264 ///
265 /// This method has a default implementation that wraps the
266 /// supplier in an `RcSupplier`. Custom implementations
267 /// can override this method for optimization purposes.
268 ///
269 /// # Returns
270 ///
271 /// A new `RcSupplier<T>` instance
272 ///
273 /// # Examples
274 ///
275 /// ```rust
276 /// use qubit_function::Supplier;
277 ///
278 /// let closure = || 42;
279 /// let rc = closure.into_rc();
280 /// assert_eq!(rc.get(), 42);
281 /// ```
282 fn into_rc(self) -> RcSupplier<T>
283 where
284 Self: Sized + 'static,
285 T: 'static,
286 {
287 RcSupplier::new(move || self.get())
288 }
289
290 /// Converts to `ArcSupplier`.
291 ///
292 /// This method has a default implementation that wraps the
293 /// supplier in an `ArcSupplier`. Custom implementations
294 /// can override this method for optimization purposes.
295 ///
296 /// # Returns
297 ///
298 /// A new `ArcSupplier<T>` instance
299 ///
300 /// # Examples
301 ///
302 /// ```rust
303 /// use qubit_function::Supplier;
304 ///
305 /// let closure = || 42;
306 /// let arc = closure.into_arc();
307 /// assert_eq!(arc.get(), 42);
308 /// ```
309 fn into_arc(self) -> ArcSupplier<T>
310 where
311 Self: Sized + Send + Sync + 'static,
312 T: 'static,
313 {
314 ArcSupplier::new(move || self.get())
315 }
316
317 /// Converts to a closure implementing `Fn() -> T`.
318 ///
319 /// This method has a default implementation that wraps the
320 /// supplier in a closure. Custom implementations can override
321 /// this method for optimization purposes.
322 ///
323 /// # Returns
324 ///
325 /// A closure implementing `Fn() -> T`
326 ///
327 /// # Examples
328 ///
329 /// ```rust
330 /// use qubit_function::Supplier;
331 ///
332 /// let closure = || 42;
333 /// let fn_closure = closure.into_fn();
334 /// assert_eq!(fn_closure(), 42);
335 /// assert_eq!(fn_closure(), 42);
336 /// ```
337 fn into_fn(self) -> impl Fn() -> T
338 where
339 Self: Sized + 'static,
340 {
341 move || self.get()
342 }
343
344 /// Converts to `BoxSupplierOnce`.
345 ///
346 /// This method has a default implementation that wraps the
347 /// supplier in a `BoxSupplierOnce`. Custom implementations
348 /// can override this method for optimization purposes.
349 ///
350 /// # Returns
351 ///
352 /// A new `BoxSupplierOnce<T>` instance
353 ///
354 /// # Examples
355 ///
356 /// ```rust
357 /// use qubit_function::Supplier;
358 ///
359 /// let closure = || 42;
360 /// let once = closure.into_once();
361 /// assert_eq!(once.get(), 42);
362 /// ```
363 fn into_once(self) -> BoxSupplierOnce<T>
364 where
365 Self: Sized + 'static,
366 T: 'static,
367 {
368 BoxSupplierOnce::new(move || self.get())
369 }
370
371 /// Converts to `BoxSupplier` by cloning.
372 ///
373 /// This method clones the supplier and wraps it in a
374 /// `BoxSupplier`. Requires `Self: Clone`. Custom
375 /// implementations can override this method for optimization.
376 ///
377 /// # Returns
378 ///
379 /// A new `BoxSupplier<T>` instance
380 ///
381 /// # Examples
382 ///
383 /// ```rust
384 /// use qubit_function::Supplier;
385 ///
386 /// let closure = || 42;
387 /// let boxed = closure.to_box();
388 /// assert_eq!(boxed.get(), 42);
389 /// ```
390 fn to_box(&self) -> BoxSupplier<T>
391 where
392 Self: Clone + 'static,
393 T: 'static,
394 {
395 self.clone().into_box()
396 }
397
398 /// Converts to `RcSupplier` by cloning.
399 ///
400 /// This method clones the supplier and wraps it in an
401 /// `RcSupplier`. Requires `Self: Clone`. Custom
402 /// implementations can override this method for optimization.
403 ///
404 /// # Returns
405 ///
406 /// A new `RcSupplier<T>` instance
407 ///
408 /// # Examples
409 ///
410 /// ```rust
411 /// use qubit_function::Supplier;
412 ///
413 /// let closure = || 42;
414 /// let rc = closure.to_rc();
415 /// assert_eq!(rc.get(), 42);
416 /// ```
417 fn to_rc(&self) -> RcSupplier<T>
418 where
419 Self: Clone + 'static,
420 T: 'static,
421 {
422 self.clone().into_rc()
423 }
424
425 /// Converts to `ArcSupplier` by cloning.
426 ///
427 /// This method clones the supplier and wraps it in an
428 /// `ArcSupplier`. Requires `Self: Clone + Send + Sync`.
429 /// Custom implementations can override this method for
430 /// optimization.
431 ///
432 /// # Returns
433 ///
434 /// A new `ArcSupplier<T>` instance
435 ///
436 /// # Examples
437 ///
438 /// ```rust
439 /// use qubit_function::Supplier;
440 ///
441 /// let closure = || 42;
442 /// let arc = closure.to_arc();
443 /// assert_eq!(arc.get(), 42);
444 /// ```
445 fn to_arc(&self) -> ArcSupplier<T>
446 where
447 Self: Clone + Send + Sync + 'static,
448 T: 'static,
449 {
450 self.clone().into_arc()
451 }
452
453 /// Converts to a closure by cloning.
454 ///
455 /// This method clones the supplier and wraps it in a closure
456 /// implementing `Fn() -> T`. Requires `Self: Clone`. Custom
457 /// implementations can override this method for optimization.
458 ///
459 /// # Returns
460 ///
461 /// A closure implementing `Fn() -> T`
462 ///
463 /// # Examples
464 ///
465 /// ```rust
466 /// use qubit_function::Supplier;
467 ///
468 /// let closure = || 42;
469 /// let fn_closure = closure.to_fn();
470 /// assert_eq!(fn_closure(), 42);
471 /// assert_eq!(fn_closure(), 42);
472 /// ```
473 fn to_fn(&self) -> impl Fn() -> T
474 where
475 Self: Clone + 'static,
476 {
477 self.clone().into_fn()
478 }
479
480 /// Converts to `BoxSupplierOnce` without consuming self
481 ///
482 /// **⚠️ Requires Clone**: This method requires `Self` to implement `Clone`.
483 /// Clones the current supplier and converts the clone to a one-time supplier.
484 ///
485 /// # Returns
486 ///
487 /// Returns a `BoxSupplierOnce<T>`
488 fn to_once(&self) -> BoxSupplierOnce<T>
489 where
490 Self: Clone + 'static,
491 T: 'static,
492 {
493 self.clone().into_once()
494 }
495}
496
497// ======================================================================
498// BoxSupplier - Single Ownership Implementation
499// ======================================================================
500
501/// Box-based single ownership read-only supplier.
502///
503/// Uses `Box<dyn Fn() -> T>` for single ownership scenarios. This
504/// is the most lightweight read-only supplier with zero reference
505/// counting overhead.
506///
507/// # Ownership Model
508///
509/// Methods consume `self` (move semantics) or borrow `&self` for
510/// read-only operations. When you call methods like `map()`, the
511/// original supplier is consumed and you get a new one:
512///
513/// ```rust
514/// use qubit_function::{BoxSupplier, Supplier};
515///
516/// let supplier = BoxSupplier::new(|| 10);
517/// let mapped = supplier.map(|x| x * 2);
518/// // supplier is no longer usable here
519/// ```
520///
521/// # Examples
522///
523/// ## Constant Factory
524///
525/// ```rust
526/// use qubit_function::{BoxSupplier, Supplier};
527///
528/// let factory = BoxSupplier::new(|| 42);
529/// assert_eq!(factory.get(), 42);
530/// assert_eq!(factory.get(), 42);
531/// ```
532///
533/// ## Method Chaining
534///
535/// ```rust
536/// use qubit_function::{BoxSupplier, Supplier};
537///
538/// let pipeline = BoxSupplier::new(|| 10)
539/// .map(|x| x * 2)
540/// .map(|x| x + 5);
541///
542/// assert_eq!(pipeline.get(), 25);
543/// ```
544///
545/// # Author
546///
547/// Haixing Hu
548pub struct BoxSupplier<T> {
549 function: Box<dyn Fn() -> T>,
550 name: Option<String>,
551}
552
553impl<T> BoxSupplier<T>
554where
555 T: 'static,
556{
557 // Generates: new(), new_with_name(), name(), set_name(), constant()
558 impl_supplier_common_methods!(BoxSupplier<T>, (Fn() -> T + 'static), |f| Box::new(f));
559
560 // Generates: map(), filter(), zip()
561 impl_box_supplier_methods!(BoxSupplier<T>, Supplier);
562}
563
564// Generates: Debug and Display implementations for BoxSupplier<T>
565impl_supplier_debug_display!(BoxSupplier<T>);
566
567impl<T> Supplier<T> for BoxSupplier<T> {
568 fn get(&self) -> T {
569 (self.function)()
570 }
571
572 // Generates: into_box(), into_rc(), into_fn(), into_once()
573 impl_box_conversions!(
574 BoxSupplier<T>,
575 RcSupplier,
576 Fn() -> T,
577 BoxSupplierOnce
578 );
579}
580
581// ======================================================================
582// ArcSupplier - Thread-safe Shared Ownership Implementation
583// ======================================================================
584
585/// Thread-safe shared ownership read-only supplier.
586///
587/// Uses `Arc<dyn Fn() -> T + Send + Sync>` for thread-safe shared
588/// ownership. **Lock-free** - no `Mutex` needed! Can be cloned and
589/// sent across threads with excellent concurrent performance.
590///
591/// # Ownership Model
592///
593/// Methods borrow `&self` instead of consuming `self`. The
594/// original supplier remains usable after method calls:
595///
596/// ```rust
597/// use qubit_function::{ArcSupplier, Supplier};
598///
599/// let source = ArcSupplier::new(|| 10);
600/// let mapped = source.map(|x| x * 2);
601/// // source is still usable here!
602/// ```
603///
604/// # Lock-Free Performance
605///
606/// Unlike `ArcSupplier`, this implementation doesn't need `Mutex`.
607/// Multiple threads can call `get()` concurrently without lock
608/// contention, making it ideal for high-concurrency scenarios.
609///
610/// # Examples
611///
612/// ## Thread-safe Factory
613///
614/// ```rust
615/// use qubit_function::{ArcSupplier, Supplier};
616/// use std::thread;
617///
618/// let factory = ArcSupplier::new(|| {
619/// String::from("Hello")
620/// });
621///
622/// let f1 = factory.clone();
623/// let f2 = factory.clone();
624///
625/// let h1 = thread::spawn(move || f1.get());
626/// let h2 = thread::spawn(move || f2.get());
627///
628/// assert_eq!(h1.join().unwrap(), "Hello");
629/// assert_eq!(h2.join().unwrap(), "Hello");
630/// ```
631///
632/// ## Reusable Transformations
633///
634/// ```rust
635/// use qubit_function::{ArcSupplier, Supplier};
636///
637/// let base = ArcSupplier::new(|| 10);
638/// let doubled = base.map(|x| x * 2);
639/// let tripled = base.map(|x| x * 3);
640///
641/// // All remain usable
642/// assert_eq!(base.get(), 10);
643/// assert_eq!(doubled.get(), 20);
644/// assert_eq!(tripled.get(), 30);
645/// ```
646///
647/// # Author
648///
649/// Haixing Hu
650pub struct ArcSupplier<T> {
651 function: Arc<dyn Fn() -> T + Send + Sync>,
652 name: Option<String>,
653}
654
655impl<T> ArcSupplier<T>
656where
657 T: 'static,
658{
659 // Generates: new(), new_with_name(), name(), set_name()
660 // Note: constant() is NOT generated here, implemented separately below
661 crate::macros::impl_common_new_methods!(
662 (Fn() -> T + Send + Sync + 'static),
663 |f| Arc::new(f),
664 "supplier"
665 );
666
667 crate::macros::impl_common_name_methods!("supplier");
668
669 // Generates: map(), filter(), zip()
670 impl_shared_supplier_methods!(ArcSupplier<T>, Supplier, (arc));
671}
672
673// Separate impl block for constant() with stricter T: Sync bound
674impl<T> ArcSupplier<T>
675where
676 T: Send + Sync + 'static,
677{
678 /// Creates a supplier that returns a constant value.
679 ///
680 /// Creates a supplier that always returns the same value. Useful for
681 /// default values or placeholder implementations.
682 ///
683 /// **Note:** This method requires `T: Sync` because the constant value
684 /// is captured by a `Fn` closure which needs to be `Sync` for `Arc`.
685 ///
686 /// # Parameters
687 ///
688 /// * `value` - The constant value to return
689 ///
690 /// # Returns
691 ///
692 /// Returns a new supplier instance that returns the constant value.
693 ///
694 /// # Examples
695 ///
696 /// ```rust
697 /// use qubit_function::{ArcSupplier, Supplier};
698 ///
699 /// let supplier = ArcSupplier::constant(42);
700 /// assert_eq!(supplier.get(), 42);
701 /// assert_eq!(supplier.get(), 42); // Can be called multiple times
702 /// ```
703 pub fn constant(value: T) -> Self
704 where
705 T: Clone,
706 {
707 Self::new(move || value.clone())
708 }
709}
710
711// Generates: Debug and Display implementations for ArcSupplier<T>
712impl_supplier_debug_display!(ArcSupplier<T>);
713
714// Generates: Clone implementation for ArcSupplier<T>
715impl_supplier_clone!(ArcSupplier<T>);
716
717impl<T> Supplier<T> for ArcSupplier<T> {
718 fn get(&self) -> T {
719 (self.function)()
720 }
721
722 // Use macro to implement conversion methods
723 impl_arc_conversions!(
724 ArcSupplier<T>,
725 BoxSupplier,
726 RcSupplier,
727 BoxSupplierOnce,
728 Fn() -> T
729 );
730}
731
732// ======================================================================
733// RcSupplier - Single-threaded Shared Ownership
734// ======================================================================
735
736/// Single-threaded shared ownership read-only supplier.
737///
738/// Uses `Rc<dyn Fn() -> T>` for single-threaded shared ownership.
739/// Can be cloned but not sent across threads.
740///
741/// # Ownership Model
742///
743/// Like `ArcSupplier`, methods borrow `&self` instead of
744/// consuming `self`:
745///
746/// ```rust
747/// use qubit_function::{RcSupplier, Supplier};
748///
749/// let source = RcSupplier::new(|| 10);
750/// let mapped = source.map(|x| x * 2);
751/// // source is still usable here!
752/// ```
753///
754/// # Examples
755///
756/// ## Shared Factory
757///
758/// ```rust
759/// use qubit_function::{RcSupplier, Supplier};
760///
761/// let factory = RcSupplier::new(|| {
762/// String::from("Hello")
763/// });
764///
765/// let f1 = factory.clone();
766/// let f2 = factory.clone();
767/// assert_eq!(f1.get(), "Hello");
768/// assert_eq!(f2.get(), "Hello");
769/// ```
770///
771/// ## Reusable Transformations
772///
773/// ```rust
774/// use qubit_function::{RcSupplier, Supplier};
775///
776/// let base = RcSupplier::new(|| 10);
777/// let doubled = base.map(|x| x * 2);
778/// let tripled = base.map(|x| x * 3);
779///
780/// assert_eq!(base.get(), 10);
781/// assert_eq!(doubled.get(), 20);
782/// assert_eq!(tripled.get(), 30);
783/// ```
784///
785/// # Author
786///
787/// Haixing Hu
788pub struct RcSupplier<T> {
789 function: Rc<dyn Fn() -> T>,
790 name: Option<String>,
791}
792
793impl<T> RcSupplier<T>
794where
795 T: 'static,
796{
797 // Generates: new(), new_with_name(), name(), set_name(), constant()
798 impl_supplier_common_methods!(RcSupplier<T>, (Fn() -> T + 'static), |f| Rc::new(f));
799
800 // Generates: map(), filter(), zip()
801 impl_shared_supplier_methods!(
802 RcSupplier<T>,
803 Supplier,
804 ('static)
805 );
806}
807
808// Generates: Debug and Display implementations for RcSupplier<T>
809impl_supplier_debug_display!(RcSupplier<T>);
810
811// Generates: Clone implementation for RcSupplier<T>
812impl_supplier_clone!(RcSupplier<T>);
813
814impl<T> Supplier<T> for RcSupplier<T> {
815 fn get(&self) -> T {
816 (self.function)()
817 }
818
819 // Generate all conversion methods using the unified macro
820 impl_rc_conversions!(
821 RcSupplier<T>,
822 BoxSupplier,
823 BoxSupplierOnce,
824 Fn() -> T
825 );
826}
827
828// ======================================================================
829// Implement Supplier for Closures
830// ======================================================================
831
832// Implement Supplier<T> for any type that implements Fn() -> T
833impl_closure_trait!(
834 Supplier<T>,
835 get,
836 BoxSupplierOnce,
837 Fn() -> T
838);
839
840// ======================================================================
841// Note on Extension Traits for Closures
842// ======================================================================
843//
844// We don't provide `FnSupplierOps` trait for `Fn() -> T` closures
845// because:
846//
847// 1. All `Fn` closures also implement `FnMut`, so they can use `FnSupplierOps`
848// from the `supplier` module
849// 2. Providing both would cause ambiguity errors due to overlapping trait impls
850// 3. Rust doesn't support negative trait bounds to exclude `FnMut`
851//
852// Users of `Fn` closures should use `FnSupplierOps` from `supplier` module,
853// or explicitly convert to `BoxSupplier` using `.into_box()` first.