RcMutatingFunction

Struct RcMutatingFunction 

Source
pub struct RcMutatingFunction<T, R> { /* private fields */ }
Expand description

RcMutatingFunction struct

A mutating function implementation based on Rc<dyn Fn(&mut T) -> R> for single-threaded shared ownership scenarios. This type allows multiple references to the same function without the overhead of thread safety.

§Features

  • Shared Ownership: Cloneable via Rc, multiple owners allowed
  • Single-Threaded: Not thread-safe, cannot be sent across threads
  • Stateless: Cannot modify captured environment (uses Fn not FnMut)
  • Chainable: Method chaining via &self (non-consuming)
  • Performance: More efficient than ArcMutatingFunction (no locking)

§Use Cases

Choose RcMutatingFunction when:

  • The function needs to be shared within a single thread for stateless operations
  • Thread safety is not required
  • Performance is important (avoiding lock overhead)

§Examples

use prism3_function::{MutatingFunction, RcMutatingFunction};

let func = RcMutatingFunction::new(|x: &mut i32| {
    *x *= 2;
    *x
});
let clone = func.clone();

let mut value = 5;
assert_eq!(func.apply(&mut value), 10);

§Author

Haixing Hu

Implementations§

Source§

impl<T, R> RcMutatingFunction<T, R>
where T: 'static, R: 'static,

Source

pub fn new<F>(f: F) -> Self
where F: Fn(&mut T) -> R + 'static,

Creates a new function.

Wraps the provided closure in the appropriate smart pointer type for this function implementation.

Source

pub fn new_with_name<F>(name: &str, f: F) -> Self
where F: Fn(&mut T) -> R + 'static,

Creates a new named function.

Wraps the provided closure and assigns it a name, which is useful for debugging and logging purposes.

Source

pub fn new_with_optional_name<F>(f: F, name: Option<String>) -> Self
where F: Fn(&mut T) -> R + 'static,

Creates a new named function with an optional name.

Wraps the provided closure and assigns it an optional name.

Source

pub fn name(&self) -> Option<&str>

Gets the name of this function.

§Returns

Returns Some(&str) if a name was set, None otherwise.

Source

pub fn set_name(&mut self, name: &str)

Sets the name of this function.

§Parameters
  • name - The name to set for this function
Source

pub fn when<P>(&self, predicate: P) -> RcConditionalMutatingFunction<T, R>
where P: Predicate<T> + 'static,

Creates a conditional function that executes based on predicate result.

§Parameters
  • predicate - The predicate to determine whether to execute the function operation
§Returns

Returns a conditional function that only executes when the predicate returns true.

§Examples
use prism3_function::{ArcFunction, Function};
use std::sync::Arc;

let double = ArcFunction::new(|x: i32| x * 2);
let conditional = double.when(|value: &i32| *value > 0);
assert_eq!(conditional.or_else(|_| 0).apply(5), 10);  // executed
assert_eq!(conditional.or_else(|_| 0).apply(-3), 0);  // not executed
Source

pub fn and_then<S, F>(&self, after: F) -> RcMutatingFunction<T, S>
where S: 'static, F: Function<R, S> + 'static,

Chains execution with another function, executing the current function first, then the subsequent function.

§Parameters
  • after - The subsequent function to execute after the current function completes
§Returns

Returns a new function that executes the current function and the subsequent function in sequence.

§Examples
use prism3_function::{ArcFunction, Function};
use std::sync::Arc;

let double = ArcFunction::new(|x: i32| x * 2);
let to_string = ArcFunction::new(|x: i32| x.to_string());

let chained = double.and_then(to_string);
assert_eq!(chained.apply(5), "10".to_string());
Source§

impl<T> RcMutatingFunction<T, T>
where T: Clone + 'static,

Source

pub fn identity() -> RcMutatingFunction<T, T>

Creates an identity function

§Examples

/// rust /// use prism3_function::RcMutatingFunction; /// /// let mut identity = RcMutatingFunction::<i32, i32>::identity(); /// let mut value = 42; /// assert_eq!(identity.apply(&mut value), 42); ///

Trait Implementations§

Source§

impl<T, R> Clone for RcMutatingFunction<T, R>

Source§

fn clone(&self) -> Self

Returns a duplicate of the value. Read more
1.0.0 · Source§

fn clone_from(&mut self, source: &Self)

Performs copy-assignment from source. Read more
Source§

impl<T, R> Debug for RcMutatingFunction<T, R>

Source§

fn fmt(&self, f: &mut Formatter<'_>) -> Result

Formats the value using the given formatter. Read more
Source§

impl<T, R> Display for RcMutatingFunction<T, R>

Source§

fn fmt(&self, f: &mut Formatter<'_>) -> Result

Formats the value using the given formatter. Read more
Source§

impl<T, R> MutatingFunction<T, R> for RcMutatingFunction<T, R>

Source§

fn apply(&self, input: &mut T) -> R

Applies the function to the mutable reference and returns a result Read more
Source§

fn into_box(self) -> BoxMutatingFunction<T, R>
where T: 'static, R: 'static,

Convert this mutating function into a BoxMutatingFunction<T, R>. Read more
Source§

fn into_rc(self) -> RcMutatingFunction<T, R>
where T: 'static, R: 'static,

Convert this mutating function into an RcMutatingFunction<T, R>. Read more
Source§

fn into_fn(self) -> impl Fn(&mut T) -> R

Consume the function and return an Fn(&mut T) -> R closure. Read more
Source§

fn to_box(&self) -> BoxMutatingFunction<T, R>
where T: 'static, R: 'static,

Create a non-consuming BoxMutatingFunction<T, R> that forwards to self. Read more
Source§

fn to_rc(&self) -> RcMutatingFunction<T, R>
where T: 'static, R: 'static,

Create a non-consuming RcMutatingFunction<T, R> that forwards to self. Read more
Source§

fn to_fn(&self) -> impl Fn(&mut T) -> R

Create a boxed Fn(&mut T) -> R closure that forwards to self. Read more
Source§

fn into_once(self) -> BoxMutatingFunctionOnce<T, R>
where T: 'static, R: 'static,

Convert to MutatingFunctionOnce Read more
Source§

fn to_once(&self) -> BoxMutatingFunctionOnce<T, R>
where T: 'static, R: 'static,

Convert to MutatingFunctionOnce without consuming self Read more
Source§

fn into_arc(self) -> ArcMutatingFunction<T, R>
where Self: Sized + Send + Sync + 'static, T: Send + 'static, R: Send + 'static,

Convert this mutating function into an ArcMutatingFunction<T, R>. Read more
Source§

fn to_arc(&self) -> ArcMutatingFunction<T, R>
where Self: Sized + Clone + Send + Sync + 'static, T: Send + 'static, R: Send + 'static,

Create a non-consuming ArcMutatingFunction<T, R> that forwards to self. Read more

Auto Trait Implementations§

§

impl<T, R> Freeze for RcMutatingFunction<T, R>

§

impl<T, R> !RefUnwindSafe for RcMutatingFunction<T, R>

§

impl<T, R> !Send for RcMutatingFunction<T, R>

§

impl<T, R> !Sync for RcMutatingFunction<T, R>

§

impl<T, R> Unpin for RcMutatingFunction<T, R>

§

impl<T, R> !UnwindSafe for RcMutatingFunction<T, R>

Blanket Implementations§

Source§

impl<T> Any for T
where T: 'static + ?Sized,

Source§

fn type_id(&self) -> TypeId

Gets the TypeId of self. Read more
Source§

impl<T> Borrow<T> for T
where T: ?Sized,

Source§

fn borrow(&self) -> &T

Immutably borrows from an owned value. Read more
Source§

impl<T> BorrowMut<T> for T
where T: ?Sized,

Source§

fn borrow_mut(&mut self) -> &mut T

Mutably borrows from an owned value. Read more
Source§

impl<T> CloneToUninit for T
where T: Clone,

Source§

unsafe fn clone_to_uninit(&self, dest: *mut u8)

🔬This is a nightly-only experimental API. (clone_to_uninit)
Performs copy-assignment from self to dest. Read more
Source§

impl<T> From<T> for T

Source§

fn from(t: T) -> T

Returns the argument unchanged.

Source§

impl<T, U> Into<U> for T
where U: From<T>,

Source§

fn into(self) -> U

Calls U::from(self).

That is, this conversion is whatever the implementation of From<T> for U chooses to do.

Source§

impl<T> ToOwned for T
where T: Clone,

Source§

type Owned = T

The resulting type after obtaining ownership.
Source§

fn to_owned(&self) -> T

Creates owned data from borrowed data, usually by cloning. Read more
Source§

fn clone_into(&self, target: &mut T)

Uses borrowed data to replace owned data, usually by cloning. Read more
Source§

impl<T> ToString for T
where T: Display + ?Sized,

Source§

fn to_string(&self) -> String

Converts the given value to a String. Read more
Source§

impl<T, U> TryFrom<U> for T
where U: Into<T>,

Source§

type Error = Infallible

The type returned in the event of a conversion error.
Source§

fn try_from(value: U) -> Result<T, <T as TryFrom<U>>::Error>

Performs the conversion.
Source§

impl<T, U> TryInto<U> for T
where U: TryFrom<T>,

Source§

type Error = <U as TryFrom<T>>::Error

The type returned in the event of a conversion error.
Source§

fn try_into(self) -> Result<U, <U as TryFrom<T>>::Error>

Performs the conversion.