pub struct ArcConditionalMutator<T> { /* private fields */ }Expand description
ArcConditionalMutator struct
A thread-safe conditional mutator that only executes when a predicate is
satisfied. Uses ArcMutator and ArcPredicate for shared ownership across
threads.
This type is typically created by calling ArcMutator::when() and is
designed to work with the or_else() method to create if-then-else logic.
§Features
- Shared Ownership: Cloneable via
Arc, multiple owners allowed - Thread-Safe: Implements
Send + Sync, safe for concurrent use - Conditional Execution: Only mutates when predicate returns
true - Chainable: Can add
or_elsebranch to create if-then-else logic
§Examples
use prism3_function::{Mutator, ArcMutator};
let conditional = ArcMutator::new(|x: &mut i32| *x *= 2)
.when(|x: &i32| *x > 0);
let conditional_clone = conditional.clone();
let mut value = 5;
let mut m = conditional;
m.mutate(&mut value);
assert_eq!(value, 10);§Author
Haixing Hu
Implementations§
Source§impl<T> ArcConditionalMutator<T>where
T: Send + 'static,
impl<T> ArcConditionalMutator<T>where
T: Send + 'static,
Sourcepub fn or_else<C>(&self, else_mutator: C) -> ArcMutator<T>
pub fn or_else<C>(&self, else_mutator: C) -> ArcMutator<T>
Adds an else branch (thread-safe version)
Executes the original mutator when the condition is satisfied, otherwise executes else_mutator.
§Parameters
else_mutator- The mutator for the else branch. Note: This parameter is passed by value and will transfer ownership. If you need to preserve the original mutator, clone it first (if it implementsClone). Must beSend, can be:- A closure:
|x: &mut T|(must beSend) - An
ArcMutator<T> - A
BoxMutator<T> - Any type implementing
Mutator<T> + Send
- A closure:
§Returns
Returns the composed ArcMutator<T>
§Examples
§Using a closure (recommended)
use prism3_function::{Mutator, ArcMutator};
let mut mutator = ArcMutator::new(|x: &mut i32| *x *= 2)
.when(|x: &i32| *x > 0)
.or_else(|x: &mut i32| *x -= 1);
let mut positive = 5;
mutator.mutate(&mut positive);
assert_eq!(positive, 10);
let mut negative = -5;
mutator.mutate(&mut negative);
assert_eq!(negative, -6);Trait Implementations§
Source§impl<T> Clone for ArcConditionalMutator<T>
impl<T> Clone for ArcConditionalMutator<T>
Source§impl<T> Mutator<T> for ArcConditionalMutator<T>where
T: Send + 'static,
impl<T> Mutator<T> for ArcConditionalMutator<T>where
T: Send + 'static,
Source§fn into_box(self) -> BoxMutator<T>where
T: 'static,
fn into_box(self) -> BoxMutator<T>where
T: 'static,
Convert this mutator into a
BoxMutator<T>. Read moreSource§fn into_rc(self) -> RcMutator<T>where
T: 'static,
fn into_rc(self) -> RcMutator<T>where
T: 'static,
Convert this mutator into an
RcMutator<T>. Read moreSource§fn into_arc(self) -> ArcMutator<T>where
T: Send + 'static,
fn into_arc(self) -> ArcMutator<T>where
T: Send + 'static,
Convert this mutator into an
ArcMutator<T>. Read moreSource§fn into_fn(self) -> impl FnMut(&mut T)where
T: 'static,
fn into_fn(self) -> impl FnMut(&mut T)where
T: 'static,
Consume the mutator and return an
FnMut(&mut T) closure. Read moreSource§fn to_box(&self) -> BoxMutator<T>where
Self: Sized + 'static,
T: 'static,
fn to_box(&self) -> BoxMutator<T>where
Self: Sized + 'static,
T: 'static,
Source§fn to_arc(&self) -> ArcMutator<T>where
Self: Sized + 'static,
T: 'static,
fn to_arc(&self) -> ArcMutator<T>where
Self: Sized + 'static,
T: 'static,
Auto Trait Implementations§
impl<T> Freeze for ArcConditionalMutator<T>
impl<T> !RefUnwindSafe for ArcConditionalMutator<T>
impl<T> Send for ArcConditionalMutator<T>
impl<T> Sync for ArcConditionalMutator<T>
impl<T> Unpin for ArcConditionalMutator<T>
impl<T> !UnwindSafe for ArcConditionalMutator<T>
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