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