pub struct RcConditionalStatefulMutator<T> { /* private fields */ }Expand description
RcConditionalMutator struct
A single-threaded conditional mutator that only executes when a predicate is
satisfied. Uses RcMutator and RcPredicate for shared ownership within a
single thread.
This type is typically created by calling RcMutator::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 mutates when predicate returns
true - No Lock Overhead: More efficient than
ArcConditionalMutator
§Examples
use prism3_function::{Mutator, RcMutator};
let conditional = RcMutator::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> RcConditionalStatefulMutator<T>where
T: 'static,
impl<T> RcConditionalStatefulMutator<T>where
T: 'static,
Sourcepub fn and_then<M>(&self, next: M) -> RcStatefulMutator<T>where
M: StatefulMutator<T> + 'static,
pub fn and_then<M>(&self, next: M) -> RcStatefulMutator<T>where
M: StatefulMutator<T> + 'static,
Chains another mutator in sequence
Combines the current conditional mutator with another mutator into a new mutator that implements the following semantics:
When the returned mutator is called with an argument:
- First, it checks the predicate of this conditional mutator
- If the predicate is satisfied, it executes the internal mutator of this conditional mutator
- Then, regardless of whether the predicate was satisfied,
it unconditionally executes the
nextmutator
In other words, this creates a mutator that conditionally executes the first action (based on the predicate), and then always executes the second action.
§Parameters
next- The next mutator to execute (always executed)
§Returns
Returns a new combined mutator
§Examples
ⓘ
use std::sync::atomic::{AtomicI32, Ordering};
let result = AtomicI32::new(0);
let mutator1 = ArcMutator::new(|x: &mut i32| {
*x += 1;
});
let mutator2 = ArcMutator::new(|x: &mut i32| {
*x += 2;
});
let conditional = mutator1.when(|x| *x > 0);
let chained = conditional.and_then(mutator2);
let mut val = 5;
chained.apply(&mut val); // val = 5 + 1 + 2 = 8
let mut val2 = -1;
chained.apply(&mut val2); // val2 = -1 + 2 = 1 (not -1 + 1 + 2!)Sourcepub fn or_else<M>(&self, else_mutator: M) -> RcStatefulMutator<T>where
M: StatefulMutator<T> + 'static,
pub fn or_else<M>(&self, else_mutator: M) -> RcStatefulMutator<T>where
M: StatefulMutator<T> + 'static,
Trait Implementations§
Source§impl<T> Clone for RcConditionalStatefulMutator<T>
impl<T> Clone for RcConditionalStatefulMutator<T>
Source§impl<T> Debug for RcConditionalStatefulMutator<T>
impl<T> Debug for RcConditionalStatefulMutator<T>
Source§impl<T> Display for RcConditionalStatefulMutator<T>
impl<T> Display for RcConditionalStatefulMutator<T>
Source§impl<T> StatefulMutator<T> for RcConditionalStatefulMutator<T>where
T: 'static,
impl<T> StatefulMutator<T> for RcConditionalStatefulMutator<T>where
T: 'static,
Source§fn into_box(self) -> BoxStatefulMutator<T>
fn into_box(self) -> BoxStatefulMutator<T>
Convert this mutator into a
BoxMutator<T>. Read moreSource§fn into_rc(self) -> RcStatefulMutator<T>
fn into_rc(self) -> RcStatefulMutator<T>
Convert this mutator into an
RcMutator<T>. Read moreSource§fn into_fn(self) -> impl FnMut(&mut T)
fn into_fn(self) -> impl FnMut(&mut T)
Consume the mutator and return an
FnMut(&mut T) closure. Read moreSource§fn into_arc(self) -> ArcStatefulMutator<T>
fn into_arc(self) -> ArcStatefulMutator<T>
Convert this mutator into an
ArcMutator<T>. Read moreSource§fn to_box(&self) -> BoxStatefulMutator<T>
fn to_box(&self) -> BoxStatefulMutator<T>
Source§fn to_rc(&self) -> RcStatefulMutator<T>
fn to_rc(&self) -> RcStatefulMutator<T>
Source§fn to_arc(&self) -> ArcStatefulMutator<T>
fn to_arc(&self) -> ArcStatefulMutator<T>
Auto Trait Implementations§
impl<T> Freeze for RcConditionalStatefulMutator<T>
impl<T> !RefUnwindSafe for RcConditionalStatefulMutator<T>
impl<T> !Send for RcConditionalStatefulMutator<T>
impl<T> !Sync for RcConditionalStatefulMutator<T>
impl<T> Unpin for RcConditionalStatefulMutator<T>
impl<T> !UnwindSafe for RcConditionalStatefulMutator<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