Skip to main content

qubit_function/
lib.rs

1/*******************************************************************************
2 *
3 *    Copyright (c) 2025 - 2026 Haixing Hu.
4 *
5 *    SPDX-License-Identifier: Apache-2.0
6 *
7 *    Licensed under the Apache License, Version 2.0.
8 *
9 ******************************************************************************/
10//! # Qubit Function
11//!
12//! Provides functional programming abstractions for Rust, including:
13//!
14//! - **Function types**: Compute results from borrowed values
15//! - **MutatingFunction types**: Mutate borrowed values and return results
16//! - **BiFunction types**: Compute results from two borrowed values
17//! - **BiMutatingFunction types**: Mutate two borrowed values and return results
18//! - **Transformer types**: Transform values from type T to type R
19//! - **UnaryOperator types**: Transform values of type T to the same type T
20//! - **BiTransformer types**: Transform two values to produce a result
21//! - **BinaryOperator types**: Transform two values of type T to produce a T
22//! - **StatefulBinaryOperator types**: Stateful transform two values of type T to produce a T
23//! - **Consumer types**: Functions that consume values without returning
24//! - **BiConsumer types**: Functions that consume two values without returning
25//! - **Predicate types**: Functions that test values and return boolean
26//! - **StatefulPredicate types**: Stateful functions that test values and return boolean
27//! - **BiPredicate types**: Functions that test two values and return boolean
28//! - **StatefulBiPredicate types**: Stateful functions that test two values and return boolean
29//! - **Supplier types**: Functions that produce values without input
30//! - **Mutator types**: Functions that mutate values in place
31//! - **Task types**: Fallible zero-argument and mutable-input actions and computations
32//! - **Tester types**: Functions that test zero-argument conditions
33//! - **Comparator types**: Functions that compare values and return ordering
34//!
35
36// Module declarations
37pub mod comparator;
38pub mod consumers;
39pub mod functions;
40pub(crate) mod macros;
41pub mod mutators;
42pub mod predicates;
43pub mod suppliers;
44pub mod tasks;
45pub mod testers;
46pub mod transformers;
47
48// Re-export common types from module roots for ergonomic crate-level imports.
49// Types are organized by functionality and ownership model for better readability.
50
51// =============================================================================
52// Core Functional Types
53// =============================================================================
54
55// ---- Consumer Types (Fn(&T)) ----
56pub use consumers::{
57    // Arc-based (shared multi-threaded ownership)
58    ArcConditionalConsumer,
59    ArcConditionalStatefulConsumer,
60    ArcConsumer,
61    ArcStatefulConsumer,
62
63    // Box-based (single ownership)
64    BoxConditionalConsumer,
65    BoxConditionalConsumerOnce,
66    BoxConditionalStatefulConsumer,
67    BoxConsumer,
68    BoxConsumerOnce,
69    BoxStatefulConsumer,
70
71    // Core traits
72    Consumer,
73    ConsumerOnce,
74    FnConsumerOnceOps,
75    // Extension traits
76    FnConsumerOps,
77    FnStatefulConsumerOps,
78    // Rc-based (shared single-threaded ownership)
79    RcConditionalConsumer,
80    RcConditionalStatefulConsumer,
81    RcConsumer,
82    RcStatefulConsumer,
83
84    StatefulConsumer,
85};
86
87// ---- BiConsumer Types (Fn(&T, &U)) ----
88pub use consumers::{
89    // Arc-based (shared multi-threaded ownership)
90    ArcBiConsumer,
91    ArcConditionalBiConsumer,
92    ArcConditionalStatefulBiConsumer,
93    ArcStatefulBiConsumer,
94
95    // Core traits
96    BiConsumer,
97    BiConsumerOnce,
98    // Box-based (single ownership)
99    BoxBiConsumer,
100    BoxBiConsumerOnce,
101    BoxConditionalBiConsumer,
102    BoxConditionalBiConsumerOnce,
103    BoxConditionalStatefulBiConsumer,
104    BoxStatefulBiConsumer,
105
106    FnBiConsumerOnceOps,
107    // Extension traits
108    FnBiConsumerOps,
109    FnStatefulBiConsumerOps,
110    // Rc-based (shared single-threaded ownership)
111    RcBiConsumer,
112    RcConditionalBiConsumer,
113    RcConditionalStatefulBiConsumer,
114    RcStatefulBiConsumer,
115
116    StatefulBiConsumer,
117};
118
119// ---- Function Types (Fn(&T) -> R) ----
120pub use functions::{
121    // Arc-based (shared multi-threaded ownership)
122    ArcFunction,
123    ArcMutatingFunction,
124    ArcStatefulFunction,
125    ArcStatefulMutatingFunction,
126
127    // Box-based (single ownership)
128    BoxFunction,
129    BoxFunctionOnce,
130    BoxMutatingFunction,
131    BoxMutatingFunctionOnce,
132    BoxStatefulFunction,
133    BoxStatefulMutatingFunction,
134
135    FnFunctionOnceOps,
136    // Extension traits
137    FnFunctionOps,
138    FnMutatingFunctionOnceOps,
139    FnMutatingFunctionOps,
140    FnStatefulFunctionOps,
141    FnStatefulMutatingFunctionOps,
142    // Core traits
143    Function,
144    FunctionOnce,
145    MutatingFunction,
146    MutatingFunctionOnce,
147    // Rc-based (shared single-threaded ownership)
148    RcFunction,
149    RcMutatingFunction,
150    RcStatefulFunction,
151    RcStatefulMutatingFunction,
152
153    StatefulFunction,
154    StatefulMutatingFunction,
155};
156
157// ---- BiFunction Types (Fn(&T, &U) -> R) ----
158pub use functions::{
159    // Arc-based (shared multi-threaded ownership)
160    ArcBiFunction,
161    ArcBiMutatingFunction,
162
163    // Core traits
164    BiFunction,
165    BiFunctionOnce,
166    BiMutatingFunction,
167    BiMutatingFunctionOnce,
168
169    // Box-based (single ownership)
170    BoxBiFunction,
171    BoxBiFunctionOnce,
172    BoxBiMutatingFunction,
173    BoxBiMutatingFunctionOnce,
174
175    FnBiFunctionOnceOps,
176    // Extension traits
177    FnBiFunctionOps,
178    FnBiMutatingFunctionOnceOps,
179    FnBiMutatingFunctionOps,
180    // Rc-based (shared single-threaded ownership)
181    RcBiFunction,
182    RcBiMutatingFunction,
183};
184
185// ---- Binary Function Types (Fn(&T, &T) -> R) ----
186pub use functions::{
187    // Arc-based (shared multi-threaded ownership)
188    ArcBinaryFunction,
189    ArcBinaryMutatingFunction,
190    // Box-based (single ownership)
191    BoxBinaryFunction,
192    BoxBinaryMutatingFunction,
193
194    // Rc-based (shared single-threaded ownership)
195    RcBinaryFunction,
196    RcBinaryMutatingFunction,
197};
198
199// ---- Conditional Function Types ----
200pub use functions::{
201    ArcConditionalBiFunction,
202    ArcConditionalBiMutatingFunction,
203    // Arc-based (shared multi-threaded ownership)
204    ArcConditionalFunction,
205    ArcConditionalMutatingFunction,
206    ArcConditionalStatefulFunction,
207    ArcConditionalStatefulMutatingFunction,
208    BoxConditionalBiFunction,
209    BoxConditionalBiFunctionOnce,
210    BoxConditionalBiMutatingFunction,
211    BoxConditionalBiMutatingFunctionOnce,
212    // Box-based (single ownership)
213    BoxConditionalFunction,
214    BoxConditionalFunctionOnce,
215    BoxConditionalMutatingFunction,
216    BoxConditionalMutatingFunctionOnce,
217
218    BoxConditionalStatefulFunction,
219    BoxConditionalStatefulMutatingFunction,
220    RcConditionalBiFunction,
221    RcConditionalBiMutatingFunction,
222    // Rc-based (shared single-threaded ownership)
223    RcConditionalFunction,
224    RcConditionalMutatingFunction,
225
226    RcConditionalStatefulFunction,
227    RcConditionalStatefulMutatingFunction,
228};
229
230// =============================================================================
231// Data Processing Types
232// =============================================================================
233
234// ---- Transformer Types (Fn(T) -> R) ----
235pub use transformers::{
236    ArcStatefulBiTransformer,
237
238    ArcStatefulTransformer,
239    // Arc-based (shared multi-threaded ownership)
240    ArcTransformer,
241    BoxStatefulBiTransformer,
242
243    BoxStatefulTransformer,
244    // Box-based (single ownership)
245    BoxTransformer,
246    BoxTransformerOnce,
247    FnStatefulBiTransformerOps,
248    FnStatefulTransformerOps,
249    FnTransformerOnceOps,
250    // Extension traits
251    FnTransformerOps,
252    RcStatefulBiTransformer,
253
254    RcStatefulTransformer,
255    // Rc-based (shared single-threaded ownership)
256    RcTransformer,
257    StatefulBiTransformer,
258
259    StatefulTransformer,
260    // Core traits
261    Transformer,
262    TransformerOnce,
263};
264
265// ---- BiTransformer Types (Fn(T, U) -> R) ----
266pub use transformers::{
267    // Arc-based (shared multi-threaded ownership)
268    ArcBiTransformer,
269
270    // Core traits
271    BiTransformer,
272    BiTransformerOnce,
273
274    // Box-based (single ownership)
275    BoxBiTransformer,
276    BoxBiTransformerOnce,
277
278    FnBiTransformerOnceOps,
279    // Extension traits
280    FnBiTransformerOps,
281    // Rc-based (shared single-threaded ownership)
282    RcBiTransformer,
283};
284
285// ---- Operator Types ----
286// Binary operators (Fn(T, T) -> T)
287pub use transformers::{
288    ArcBinaryOperator,
289    BinaryOperator,
290    BinaryOperatorOnce,
291    BoxBinaryOperator,
292    BoxBinaryOperatorOnce,
293    RcBinaryOperator,
294};
295
296// Stateful binary operators (FnMut(T, T) -> T)
297pub use transformers::{
298    ArcStatefulBinaryOperator,
299    BoxStatefulBinaryOperator,
300    RcStatefulBinaryOperator,
301    StatefulBinaryOperator,
302};
303
304// Unary operators (Fn(T) -> T)
305pub use transformers::{
306    ArcUnaryOperator,
307    BoxUnaryOperator,
308    BoxUnaryOperatorOnce,
309    RcUnaryOperator,
310    UnaryOperator,
311    UnaryOperatorOnce,
312};
313
314// ---- Conditional Transformer Types ----
315pub use transformers::{
316    ArcConditionalBiTransformer,
317    ArcConditionalStatefulBiTransformer,
318    ArcConditionalStatefulTransformer,
319    // Arc-based (shared multi-threaded ownership)
320    ArcConditionalTransformer,
321    BoxConditionalBiTransformer,
322    BoxConditionalBiTransformerOnce,
323    BoxConditionalStatefulBiTransformer,
324
325    BoxConditionalStatefulTransformer,
326    // Box-based (single ownership)
327    BoxConditionalTransformer,
328    BoxConditionalTransformerOnce,
329    RcConditionalBiTransformer,
330    RcConditionalStatefulBiTransformer,
331
332    RcConditionalStatefulTransformer,
333    // Rc-based (shared single-threaded ownership)
334    RcConditionalTransformer,
335};
336
337// =============================================================================
338// Utility Types
339// =============================================================================
340
341// ---- Mutator Types (Fn(&mut T)) ----
342pub use mutators::{
343    ArcConditionalMutator,
344    ArcConditionalStatefulMutator,
345
346    // Arc-based (shared multi-threaded ownership)
347    ArcMutator,
348    ArcStatefulMutator,
349
350    // Conditional types
351    BoxConditionalMutator,
352    BoxConditionalMutatorOnce,
353    BoxConditionalStatefulMutator,
354    // Box-based (single ownership)
355    BoxMutator,
356    BoxMutatorOnce,
357    BoxStatefulMutator,
358
359    FnMutStatefulMutatorOps,
360    FnMutatorOnceOps,
361    // Extension traits
362    FnMutatorOps,
363    // Core traits
364    Mutator,
365    MutatorOnce,
366    RcConditionalMutator,
367    RcConditionalStatefulMutator,
368    // Rc-based (shared single-threaded ownership)
369    RcMutator,
370    RcStatefulMutator,
371
372    StatefulMutator,
373};
374
375// ---- Predicate Types (Fn(&T) -> bool) ----
376pub use predicates::{
377    // Arc-based (shared multi-threaded ownership)
378    ArcPredicate,
379    ArcStatefulPredicate,
380
381    // Box-based (single ownership)
382    BoxPredicate,
383    BoxStatefulPredicate,
384
385    // Extension traits
386    FnPredicateOps,
387    FnStatefulPredicateOps,
388    // Core traits
389    Predicate,
390    // Rc-based (shared single-threaded ownership)
391    RcPredicate,
392    RcStatefulPredicate,
393    StatefulPredicate,
394};
395
396// ---- BiPredicate Types (Fn(&T, &U) -> bool) ----
397pub use predicates::{
398    // Arc-based (shared multi-threaded ownership)
399    ArcBiPredicate,
400    ArcStatefulBiPredicate,
401
402    // Core traits
403    BiPredicate,
404
405    // Box-based (single ownership)
406    BoxBiPredicate,
407    BoxStatefulBiPredicate,
408
409    // Extension traits
410    FnBiPredicateOps,
411    FnStatefulBiPredicateOps,
412
413    // Rc-based (shared single-threaded ownership)
414    RcBiPredicate,
415    RcStatefulBiPredicate,
416
417    // Stateful core trait
418    StatefulBiPredicate,
419};
420
421// ---- Supplier Types (Fn() -> R) ----
422pub use suppliers::{
423    ArcStatefulSupplier,
424
425    // Arc-based (shared multi-threaded ownership)
426    ArcSupplier,
427    BoxStatefulSupplier,
428
429    // Box-based (single ownership)
430    BoxSupplier,
431    BoxSupplierOnce,
432    // Extension traits
433    FnStatefulSupplierOps,
434    RcStatefulSupplier,
435
436    // Rc-based (shared single-threaded ownership)
437    RcSupplier,
438    StatefulSupplier,
439
440    // Core traits
441    Supplier,
442    SupplierOnce,
443};
444
445// ---- Task Types (fallible actions and computations) ----
446pub use tasks::{
447    ArcCallable,
448    ArcCallableWith,
449    ArcRunnable,
450    ArcRunnableWith,
451    BoxCallable,
452    BoxCallableOnce,
453    BoxCallableWith,
454    BoxRunnable,
455    BoxRunnableOnce,
456    BoxRunnableWith,
457    Callable,
458    CallableOnce,
459    CallableWith,
460    LocalBoxCallableOnce,
461    LocalBoxRunnableOnce,
462    RcCallable,
463    RcCallableWith,
464    RcRunnable,
465    RcRunnableWith,
466    Runnable,
467    RunnableOnce,
468    RunnableWith,
469};
470
471// ---- Comparator Types (Fn(&T, &T) -> Ordering) ----
472pub use comparator::{
473    // Arc-based (shared multi-threaded ownership)
474    ArcComparator,
475
476    // Box-based (single ownership)
477    BoxComparator,
478
479    // Core traits
480    Comparator,
481
482    // Extension traits
483    FnComparatorOps,
484    // Rc-based (shared single-threaded ownership)
485    RcComparator,
486};
487
488// ---- Tester Types (Fn() -> bool and FnMut() -> bool) ----
489pub use testers::{
490    stateful_tester::{
491        StatefulTester,
492        arc_stateful_tester::ArcStatefulTester,
493        box_stateful_tester::BoxStatefulTester,
494        fn_stateful_tester_ops::FnStatefulTesterOps,
495        rc_stateful_tester::RcStatefulTester,
496    },
497    tester::{
498        Tester,
499        arc_tester::ArcTester,
500        box_tester::BoxTester,
501        fn_tester_ops::FnTesterOps,
502        rc_tester::RcTester,
503    },
504};