ReadonlyConsumer

Trait ReadonlyConsumer 

Source
pub trait ReadonlyConsumer<T> {
    // Required method
    fn accept(&self, value: &T);

    // Provided methods
    fn into_box(self) -> BoxReadonlyConsumer<T>
       where Self: Sized + 'static,
             T: 'static { ... }
    fn into_rc(self) -> RcReadonlyConsumer<T>
       where Self: Sized + 'static,
             T: 'static { ... }
    fn into_arc(self) -> ArcReadonlyConsumer<T>
       where Self: Sized + Send + Sync + 'static,
             T: Send + Sync + 'static { ... }
    fn into_fn(self) -> impl Fn(&T)
       where Self: Sized + 'static,
             T: 'static { ... }
    fn to_box(&self) -> BoxReadonlyConsumer<T>
       where Self: Clone + 'static,
             T: 'static { ... }
    fn to_rc(&self) -> RcReadonlyConsumer<T>
       where Self: Clone + 'static,
             T: 'static { ... }
    fn to_arc(&self) -> ArcReadonlyConsumer<T>
       where Self: Clone + Send + Sync + 'static,
             T: Send + Sync + 'static { ... }
    fn to_fn(&self) -> impl Fn(&T)
       where Self: Clone + 'static,
             T: 'static { ... }
}
Expand description

ReadonlyConsumer trait - Unified readonly consumer interface

Defines the core behavior of all readonly consumer types. Unlike Consumer, ReadonlyConsumer neither modifies its own state nor modifies input values, making it a completely immutable operation.

§Auto-implementation

  • All closures implementing Fn(&T)
  • BoxReadonlyConsumer<T>, ArcReadonlyConsumer<T>, RcReadonlyConsumer<T>

§Features

  • Unified Interface: All readonly consumer types share the same accept method signature
  • Auto-implementation: Closures automatically implement this trait with zero overhead
  • Type Conversion: Easy conversion between different ownership models
  • Generic Programming: Write functions that work with any readonly consumer type
  • No Interior Mutability: No need for Mutex or RefCell, more efficient

§Examples

use prism3_function::{ReadonlyConsumer, BoxReadonlyConsumer};

fn apply_consumer<C: ReadonlyConsumer<i32>>(consumer: &C, value: &i32) {
    consumer.accept(value);
}

let box_con = BoxReadonlyConsumer::new(|x: &i32| {
    println!("Value: {}", x);
});
apply_consumer(&box_con, &5);

§Author

Hu Haixing

Required Methods§

Source

fn accept(&self, value: &T)

Execute readonly consumption operation

Performs an operation on the given reference. The operation typically reads input values or produces side effects, but neither modifies the input value nor the consumer’s own state.

§Parameters
  • value - Reference to the value to consume
§Examples
use prism3_function::{ReadonlyConsumer, BoxReadonlyConsumer};

let consumer = BoxReadonlyConsumer::new(|x: &i32| println!("{}", x));
consumer.accept(&5);

Provided Methods§

Source

fn into_box(self) -> BoxReadonlyConsumer<T>
where Self: Sized + 'static, T: 'static,

Convert to BoxReadonlyConsumer

⚠️ Consumes self: The original consumer will be unavailable after calling this method.

§Returns

Returns the wrapped BoxReadonlyConsumer<T>

Source

fn into_rc(self) -> RcReadonlyConsumer<T>
where Self: Sized + 'static, T: 'static,

Convert to RcReadonlyConsumer

⚠️ Consumes self: The original consumer will be unavailable after calling this method.

§Returns

Returns the wrapped RcReadonlyConsumer<T>

Source

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

Convert to ArcReadonlyConsumer

⚠️ Consumes self: The original consumer will be unavailable after calling this method.

§Returns

Returns the wrapped ArcReadonlyConsumer<T>

Source

fn into_fn(self) -> impl Fn(&T)
where Self: Sized + 'static, T: 'static,

Convert to closure

⚠️ Consumes self: The original consumer will be unavailable after calling this method.

Converts a readonly consumer to a closure that can be used directly in places where the standard library requires Fn.

§Returns

Returns a closure implementing Fn(&T)

§Examples
use prism3_function::{ReadonlyConsumer, BoxReadonlyConsumer};

let consumer = BoxReadonlyConsumer::new(|x: &i32| {
    println!("Value: {}", x);
});
let func = consumer.into_fn();
func(&5);
Source

fn to_box(&self) -> BoxReadonlyConsumer<T>
where Self: Clone + 'static, T: 'static,

Non-consuming conversion to BoxReadonlyConsumer

⚠️ Does NOT consume self: This method clones self and returns a boxed readonly consumer that calls the cloned consumer. Requires Self: Clone so it can be called through an immutable reference.

§Returns

Returns the wrapped BoxReadonlyConsumer<T>

Source

fn to_rc(&self) -> RcReadonlyConsumer<T>
where Self: Clone + 'static, T: 'static,

Non-consuming conversion to RcReadonlyConsumer

⚠️ Does NOT consume self: Clones self and returns an RcReadonlyConsumer that forwards to the cloned consumer. Requires Self: Clone.

§Returns

Returns the wrapped RcReadonlyConsumer<T>

Source

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

Non-consuming conversion to ArcReadonlyConsumer

⚠️ Does NOT consume self: Clones self and returns an ArcReadonlyConsumer. Requires Self: Clone + Send + Sync and T: Send + Sync so the result is thread-safe.

§Returns

Returns the wrapped ArcReadonlyConsumer<T>

Source

fn to_fn(&self) -> impl Fn(&T)
where Self: Clone + 'static, T: 'static,

Non-consuming conversion to a boxed closure

⚠️ Does NOT consume self: Returns a closure which calls a cloned copy of the consumer. Requires Self: Clone.

§Returns

Returns a closure implementing Fn(&T) which forwards to the cloned consumer.

Implementors§

Source§

impl<T> ReadonlyConsumer<T> for ArcReadonlyConsumer<T>

Source§

impl<T> ReadonlyConsumer<T> for BoxReadonlyConsumer<T>

Source§

impl<T> ReadonlyConsumer<T> for RcReadonlyConsumer<T>

Source§

impl<T, F> ReadonlyConsumer<T> for F
where F: Fn(&T),

Implement ReadonlyConsumer for all Fn(&T)