pub struct RcConditionalMapper<T, R> { /* private fields */ }Expand description
RcConditionalMapper struct
A single-threaded conditional mapper that only executes when a
predicate is satisfied. Uses RcMapper and RcPredicate for shared
ownership within a single thread.
This type is typically created by calling RcMapper::when() and is
designed to work with the or_else() method to create if-then-else
logic.
§Features
- Shared Ownership: Cloneable via
Rc, multiple owners allowed - Single-Threaded: Not thread-safe, cannot be sent across threads
- Conditional Execution: Only maps when predicate returns
true - No Lock Overhead: More efficient than
ArcConditionalMapper
§Examples
use prism3_function::{Mapper, RcMapper};
let mut mapper = RcMapper::new(|x: i32| x * 2)
.when(|x: &i32| *x > 0)
.or_else(|x: i32| -x);
let mut mapper_clone = mapper.clone();
assert_eq!(mapper.apply(5), 10);
assert_eq!(mapper_clone.apply(-5), 5);§Author
Haixing Hu
Implementations§
Source§impl<T, R> RcConditionalMapper<T, R>where
T: 'static,
R: 'static,
impl<T, R> RcConditionalMapper<T, R>where
T: 'static,
R: 'static,
Sourcepub fn or_else<F>(self, else_mapper: F) -> RcMapper<T, R>where
F: Mapper<T, R> + 'static,
pub fn or_else<F>(self, else_mapper: F) -> RcMapper<T, R>where
F: Mapper<T, R> + 'static,
Adds an else branch (single-threaded shared version)
Executes the original mapper when the condition is satisfied, otherwise executes else_mapper.
§Parameters
else_mapper- The mapper for the else branch, can be:- Closure:
|x: T| -> R RcMapper<T, R>,BoxMapper<T, R>- Any type implementing
Mapper<T, R>
- Closure:
§Returns
Returns the composed RcMapper<T, R>
§Examples
use prism3_function::{Mapper, RcMapper};
let mut mapper = RcMapper::new(|x: i32| x * 2)
.when(|x: &i32| *x > 0)
.or_else(|x: i32| -x);
assert_eq!(mapper.apply(5), 10);
assert_eq!(mapper.apply(-5), 5);Trait Implementations§
Source§impl<T, R> Clone for RcConditionalMapper<T, R>
impl<T, R> Clone for RcConditionalMapper<T, R>
Auto Trait Implementations§
impl<T, R> Freeze for RcConditionalMapper<T, R>
impl<T, R> !RefUnwindSafe for RcConditionalMapper<T, R>
impl<T, R> !Send for RcConditionalMapper<T, R>
impl<T, R> !Sync for RcConditionalMapper<T, R>
impl<T, R> Unpin for RcConditionalMapper<T, R>
impl<T, R> !UnwindSafe for RcConditionalMapper<T, R>
Blanket Implementations§
Source§impl<T> BorrowMut<T> for Twhere
T: ?Sized,
impl<T> BorrowMut<T> for Twhere
T: ?Sized,
Source§fn borrow_mut(&mut self) -> &mut T
fn borrow_mut(&mut self) -> &mut T
Mutably borrows from an owned value. Read more