Prism3 Function
Comprehensive functional programming abstractions for Rust, providing Java-style functional interfaces adapted to Rust's ownership model.
Overview
This crate provides a complete set of functional programming abstractions inspired by Java's functional interfaces, carefully adapted to Rust's ownership system. It offers multiple implementations for each abstraction (Box/Arc/Rc) to cover various use cases from simple single-threaded scenarios to complex multi-threaded applications.
Key Features
- Complete Functional Interface Suite: 24 core functional abstractions with multiple variants
- High-Performance Concurrency: Uses parking_lot Mutex for superior thread synchronization performance
- Multiple Ownership Models: Box-based single ownership, Arc-based thread-safe sharing, and Rc-based single-threaded sharing
- Flexible API Design: Trait-based unified interface with concrete implementations optimized for different scenarios
- Method Chaining: All types support fluent API and functional composition
- Thread-Safety Options: Choose between thread-safe (Arc) and efficient single-threaded (Rc) implementations
- Zero-Cost Abstractions: Efficient implementations with minimal runtime overhead
Installation
Add this to your Cargo.toml:
[]
= "0.5.0"
Core Abstractions
This crate provides 24 core functional abstractions, each with multiple implementations:
1. Predicate - Condition Testing
Tests whether a value satisfies a condition, returning bool.
Trait: Predicate<T>
Core Method: test(&self, value: &T) -> bool
Closure Equivalent: Fn(&T) -> bool
Implementations:
BoxPredicate<T>- Single ownership, non-cloneableArcPredicate<T>- Thread-safe, cloneableRcPredicate<T>- Single-threaded, cloneable
Example:
use ;
let is_even = new;
let is_positive = new;
let combined = is_even.and;
assert!;
assert!;
2. BiPredicate - Two-Value Condition Testing
Tests whether two values satisfy a condition, returning bool.
Trait: BiPredicate<T, U>
Core Method: test(&self, first: &T, second: &U) -> bool
Closure Equivalent: Fn(&T, &U) -> bool
Implementations:
BoxBiPredicate<T, U>- Single ownershipArcBiPredicate<T, U>- Thread-safeRcBiPredicate<T, U>- Single-threaded
Example:
use ;
let sum_positive = new;
assert!;
assert!;
3. Consumer - Value Observation
Accepts a value reference and performs operations without returning a result.
Trait: Consumer<T>
Core Method: accept(&self, value: &T)
Closure Equivalent: Fn(&T)
Implementations:
BoxConsumer<T>- Single ownershipArcConsumer<T>- Thread-safeRcConsumer<T>- Single-threaded
Example:
use ;
let logger = new;
logger.accept;
4. ConsumerOnce - One-Time Value Observation
Accepts a value reference and performs operations once.
Trait: ConsumerOnce<T>
Core Method: accept_once(self, value: &T)
Closure Equivalent: FnOnce(&T)
Implementations:
BoxConsumerOnce<T>- Single ownership, one-time use
5. BiConsumer - Two-Value Observation
Accepts two value references and performs operations without returning a result.
Trait: BiConsumer<T, U>
Core Method: accept(&self, first: &T, second: &U)
Closure Equivalent: Fn(&T, &U)
Implementations:
BoxBiConsumer<T, U>- Single ownershipArcBiConsumer<T, U>- Thread-safeRcBiConsumer<T, U>- Single-threaded
Example:
use ;
let sum_logger = new;
sum_logger.accept;
6. BiConsumerOnce - One-Time Two-Value Observation
Accepts two value references and performs operations once.
Trait: BiConsumerOnce<T, U>
Core Method: accept_once(self, first: &T, second: &U)
Closure Equivalent: FnOnce(&T, &U)
Implementations:
BoxBiConsumerOnce<T, U>- Single ownership, one-time use
7. Mutator - In-Place Value Modification
Modifies values in-place by accepting mutable references.
Trait: Mutator<T>
Core Method: mutate(&mut self, value: &mut T)
Closure Equivalent: FnMut(&mut T)
Implementations:
BoxMutator<T>- Single ownershipArcMutator<T>- Thread-safeRcMutator<T>- Single-threaded
Example:
use ;
let mut doubler = new;
let mut value = 10;
doubler.mutate;
assert_eq!;
8. MutatorOnce - One-Time In-Place Modification
Modifies a value in-place once.
Trait: MutatorOnce<T>
Core Method: apply(self, value: &mut T)
Closure Equivalent: FnOnce(&mut T)
Implementations:
BoxMutatorOnce<T>- Single ownership, one-time use
9. Supplier - Value Generation
Generates values without input parameters.
Trait: Supplier<T>
Core Method: get(&self) -> T
Closure Equivalent: Fn() -> T
Implementations:
BoxSupplier<T>- Single ownership, lock-freeArcSupplier<T>- Thread-safe, lock-freeRcSupplier<T>- Single-threaded
Example:
use ;
let factory = new;
assert_eq!;
10. SupplierOnce - One-Time Value Generation
Generates a value once without input parameters.
Trait: SupplierOnce<T>
Core Method: get(self) -> T
Closure Equivalent: FnOnce() -> T
Implementations:
BoxSupplierOnce<T>- Single ownership, one-time use
11. StatefulSupplier - Stateful Value Generation
Generates values with mutable state.
Trait: StatefulSupplier<T>
Core Method: get(&mut self) -> T
Closure Equivalent: FnMut() -> T
Implementations:
BoxStatefulSupplier<T>- Single ownershipArcStatefulSupplier<T>- Thread-safe with parking_lot::MutexRcStatefulSupplier<T>- Single-threaded with RefCell
Example:
use ;
let mut counter = ;
assert_eq!;
assert_eq!;
12. Function - Reference Transformation
Transforms a value reference to produce a result without consuming the input.
Trait: Function<T, R>
Core Method: apply(&self, input: &T) -> R
Closure Equivalent: Fn(&T) -> R
Implementations:
BoxFunction<T, R>- Single ownershipArcFunction<T, R>- Thread-safeRcFunction<T, R>- Single-threaded
Example:
use ;
let to_string = new;
assert_eq!;
13. FunctionOnce - One-Time Reference Transformation
Transforms a value reference once to produce a result.
Trait: FunctionOnce<T, R>
Core Method: apply_once(self, input: &T) -> R
Closure Equivalent: FnOnce(&T) -> R
Implementations:
BoxFunctionOnce<T, R>- Single ownership, one-time use
14. StatefulFunction - Stateful Reference Transformation
Transforms a value reference with mutable state.
Trait: StatefulFunction<T, R>
Core Method: apply(&mut self, input: &T) -> R
Closure Equivalent: FnMut(&T) -> R
Implementations:
BoxStatefulFunction<T, R>- Single ownershipArcStatefulFunction<T, R>- Thread-safe with parking_lot::MutexRcStatefulFunction<T, R>- Single-threaded with RefCell
15. Transformer - Value Transformation by Consumption
Transforms values from type T to type R by consuming input.
Trait: Transformer<T, R>
Core Method: transform(&self, input: T) -> R
Closure Equivalent: Fn(T) -> R
Implementations:
BoxTransformer<T, R>- Single ownershipArcTransformer<T, R>- Thread-safeRcTransformer<T, R>- Single-threaded
Type Alias: UnaryOperator<T> = Transformer<T, T>
Example:
use ;
let parse = new;
assert_eq!;
16. TransformerOnce - One-Time Value Transformation
Transforms a value once by consuming both the transformer and input.
Trait: TransformerOnce<T, R>
Core Method: transform_once(self, input: T) -> R
Closure Equivalent: FnOnce(T) -> R
Implementations:
BoxTransformerOnce<T, R>- Single ownership, one-time use
Type Alias: UnaryOperatorOnce<T> = TransformerOnce<T, T>
17. StatefulTransformer - Stateful Value Transformation
Transforms values with mutable state by consuming input.
Trait: StatefulTransformer<T, R>
Core Method: transform(&mut self, input: T) -> R
Closure Equivalent: FnMut(T) -> R
Implementations:
BoxStatefulTransformer<T, R>- Single ownershipArcStatefulTransformer<T, R>- Thread-safe with parking_lot::MutexRcStatefulTransformer<T, R>- Single-threaded with RefCell
18. BiTransformer - Two-Value Transformation
Transforms two input values to produce a result by consuming inputs.
Trait: BiTransformer<T, U, R>
Core Method: transform(&self, first: T, second: U) -> R
Closure Equivalent: Fn(T, U) -> R
Implementations:
BoxBiTransformer<T, U, R>- Single ownershipArcBiTransformer<T, U, R>- Thread-safeRcBiTransformer<T, U, R>- Single-threaded
Type Alias: BinaryOperator<T> = BiTransformer<T, T, T>
Example:
use ;
let add = new;
assert_eq!;
20. BiTransformerOnce - One-Time Two-Value Transformation
Transforms two values once by consuming everything.
Trait: BiTransformerOnce<T, U, R>
Core Method: transform_once(self, first: T, second: U) -> R
Closure Equivalent: FnOnce(T, U) -> R
Implementations:
BoxBiTransformerOnce<T, U, R>- Single ownership, one-time use
Type Alias: BinaryOperatorOnce<T> = BiTransformerOnce<T, T, T>
21. StatefulConsumer - Stateful Value Observation
Accepts a value reference with mutable state.
Trait: StatefulConsumer<T>
Core Method: accept(&mut self, value: &T)
Closure Equivalent: FnMut(&T)
Implementations:
BoxStatefulConsumer<T>- Single ownershipArcStatefulConsumer<T>- Thread-safe with parking_lot::MutexRcStatefulConsumer<T>- Single-threaded with RefCell
22. StatefulBiConsumer - Stateful Two-Value Observation
Accepts two value references with mutable state.
Trait: StatefulBiConsumer<T, U>
Core Method: accept(&mut self, first: &T, second: &U)
Closure Equivalent: FnMut(&T, &U)
Implementations:
BoxStatefulBiConsumer<T, U>- Single ownershipArcStatefulBiConsumer<T, U>- Thread-safe with parking_lot::MutexRcStatefulBiConsumer<T, U>- Single-threaded with RefCell
23. Comparator - Value Comparison
Compares two values and returns an Ordering.
Trait: Comparator<T>
Core Method: compare(&self, a: &T, b: &T) -> Ordering
Closure Equivalent: Fn(&T, &T) -> Ordering
Implementations:
BoxComparator<T>- Single ownershipArcComparator<T>- Thread-safeRcComparator<T>- Single-threaded
Example:
use ;
use Ordering;
let cmp = new;
assert_eq!;
24. Tester - Condition Testing Without Input
Tests whether a state or condition holds without accepting input.
Trait: Tester
Core Method: test(&self) -> bool
Closure Equivalent: Fn() -> bool
Implementations:
BoxTester- Single ownershipArcTester- Thread-safeRcTester- Single-threaded
Example:
use ;
use ;
let flag = new;
let flag_clone = flag.clone;
let tester = new;
assert!;
flag.store;
assert!;
Trait and Closure Correspondence Table
| Trait | Core Method Signature | Equivalent Closure Type |
|---|---|---|
Predicate<T> |
test(&self, value: &T) -> bool |
Fn(&T) -> bool |
BiPredicate<T, U> |
test(&self, first: &T, second: &U) -> bool |
Fn(&T, &U) -> bool |
Consumer<T> |
accept(&self, value: &T) |
Fn(&T) |
ConsumerOnce<T> |
accept_once(self, value: &T) |
FnOnce(&T) |
StatefulConsumer<T> |
accept(&mut self, value: &T) |
FnMut(&T) |
BiConsumer<T, U> |
accept(&self, first: &T, second: &U) |
Fn(&T, &U) |
BiConsumerOnce<T, U> |
accept_once(self, first: &T, second: &U) |
FnOnce(&T, &U) |
StatefulBiConsumer<T, U> |
accept(&mut self, first: &T, second: &U) |
FnMut(&T, &U) |
Mutator<T> |
mutate(&mut self, value: &mut T) |
FnMut(&mut T) |
MutatorOnce<T> |
apply(self, value: &mut T) |
FnOnce(&mut T) |
Supplier<T> |
get(&self) -> T |
Fn() -> T |
SupplierOnce<T> |
get(self) -> T |
FnOnce() -> T |
StatefulSupplier<T> |
get(&mut self) -> T |
FnMut() -> T |
Function<T, R> |
apply(&self, input: &T) -> R |
Fn(&T) -> R |
FunctionOnce<T, R> |
apply_once(self, input: &T) -> R |
FnOnce(&T) -> R |
StatefulFunction<T, R> |
apply(&mut self, input: &T) -> R |
FnMut(&T) -> R |
Transformer<T, R> |
transform(&self, input: T) -> R |
Fn(T) -> R |
TransformerOnce<T, R> |
transform_once(self, input: T) -> R |
FnOnce(T) -> R |
StatefulTransformer<T, R> |
transform(&mut self, input: T) -> R |
FnMut(T) -> R |
BiTransformer<T, U, R> |
transform(&self, first: T, second: U) -> R |
Fn(T, U) -> R |
BiTransformerOnce<T, U, R> |
transform_once(self, first: T, second: U) -> R |
FnOnce(T, U) -> R |
Comparator<T> |
compare(&self, a: &T, b: &T) -> Ordering |
Fn(&T, &T) -> Ordering |
Tester |
test(&self) -> bool |
Fn() -> bool |
Implementation Types Comparison
Each trait has multiple implementations based on ownership model:
| Trait | Box (Single) | Arc (Thread-Safe) | Rc (Single-Thread) |
|---|---|---|---|
| Predicate | BoxPredicate | ArcPredicate | RcPredicate |
| BiPredicate | BoxBiPredicate | ArcBiPredicate | RcBiPredicate |
| Consumer | BoxConsumer | ArcConsumer | RcConsumer |
| ConsumerOnce | BoxConsumerOnce | - | - |
| StatefulConsumer | BoxStatefulConsumer | ArcStatefulConsumer | RcStatefulConsumer |
| BiConsumer | BoxBiConsumer | ArcBiConsumer | RcBiConsumer |
| BiConsumerOnce | BoxBiConsumerOnce | - | - |
| StatefulBiConsumer | BoxStatefulBiConsumer | ArcStatefulBiConsumer | RcStatefulBiConsumer |
| Mutator | BoxMutator | ArcMutator | RcMutator |
| MutatorOnce | BoxMutatorOnce | - | - |
| Supplier | BoxSupplier | ArcSupplier | RcSupplier |
| SupplierOnce | BoxSupplierOnce | - | - |
| StatefulSupplier | BoxStatefulSupplier | ArcStatefulSupplier | RcStatefulSupplier |
| Function | BoxFunction | ArcFunction | RcFunction |
| FunctionOnce | BoxFunctionOnce | - | - |
| StatefulFunction | BoxStatefulFunction | ArcStatefulFunction | RcStatefulFunction |
| Transformer | BoxTransformer | ArcTransformer | RcTransformer |
| TransformerOnce | BoxTransformerOnce | - | - |
| StatefulTransformer | BoxStatefulTransformer | ArcStatefulTransformer | RcStatefulTransformer |
| BiTransformer | BoxBiTransformer | ArcBiTransformer | RcBiTransformer |
| BiTransformerOnce | BoxBiTransformerOnce | - | - |
| Comparator | BoxComparator | ArcComparator | RcComparator |
| Tester | BoxTester | ArcTester | RcTester |
Legend:
- Box: Single ownership, cannot be cloned, consumes self
- Arc: Shared ownership, thread-safe, cloneable
- Rc: Shared ownership, single-threaded, cloneable
- -: Not applicable (Once types don't need sharing)
Design Philosophy
This crate adopts the Trait + Multiple Implementations pattern:
- Unified Interface: Each functional type has a trait defining core behavior
- Specialized Implementations: Multiple concrete types optimized for different scenarios
- Type Preservation: Composition methods return the same concrete type
- Ownership Flexibility: Choose between single ownership, thread-safe sharing, or single-threaded sharing
- High-Performance Concurrency: Uses parking_lot Mutex for superior synchronization performance
- Ergonomic API: Natural method chaining and functional composition
Examples
The examples/ directory contains comprehensive demonstrations for each type. Run examples with:
Documentation
Detailed design documents are available in the doc/ directory for each major abstraction.
License
Licensed under Apache License, Version 2.0.
Author
Haixing Hu starfish.hu@gmail.com