BoxReadonlyConsumer

Struct BoxReadonlyConsumer 

Source
pub struct BoxReadonlyConsumer<T> { /* private fields */ }
Expand description

BoxReadonlyConsumer struct

Readonly consumer implementation based on Box<dyn Fn(&T)> for single ownership scenarios.

§Features

  • Single Ownership: Not cloneable, transfers ownership when used
  • Zero Overhead: No reference counting or lock overhead
  • Completely Immutable: Neither modifies itself nor input
  • No Interior Mutability: No need for Mutex or RefCell

§Use Cases

Choose BoxReadonlyConsumer when:

  • Readonly consumer is used once or in a linear flow
  • No need to share consumer across contexts
  • Pure observation operations, such as logging

§Examples

use prism3_function::{ReadonlyConsumer, BoxReadonlyConsumer};

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

§Author

Hu Haixing

Implementations§

Source§

impl<T> BoxReadonlyConsumer<T>
where T: 'static,

Source

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

Create a new BoxReadonlyConsumer

§Type Parameters
  • F - Closure type
§Parameters
  • f - Closure to wrap
§Returns

Returns a new BoxReadonlyConsumer<T> instance

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

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

pub fn noop() -> Self

Create a no-op consumer

§Returns

Returns a no-op consumer

Source

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

Get the consumer’s name

Source

pub fn set_name(&mut self, name: impl Into<String>)

Set the consumer’s name

Source

pub fn and_then<C>(self, next: C) -> Self
where C: ReadonlyConsumer<T> + 'static,

Sequentially chain another readonly consumer

Returns a new consumer that executes the current operation first, then the next operation. Consumes self.

§Type Parameters
  • C - Type of the next consumer
§Parameters
  • next - Consumer to execute after the current operation. Note: This parameter is passed by value and will transfer ownership. If you need to preserve the original consumer, clone it first (if it implements Clone). Can be:
    • A closure: |x: &T|
    • A BoxReadonlyConsumer<T>
    • An RcReadonlyConsumer<T>
    • An ArcReadonlyConsumer<T>
    • Any type implementing ReadonlyConsumer<T>
§Returns

Returns a new combined BoxReadonlyConsumer<T>

§Examples
§Direct value passing (ownership transfer)
use prism3_function::{ReadonlyConsumer, BoxReadonlyConsumer};

let first = BoxReadonlyConsumer::new(|x: &i32| {
    println!("First: {}", x);
});
let second = BoxReadonlyConsumer::new(|x: &i32| {
    println!("Second: {}", x);
});

// second is moved here
let chained = first.and_then(second);
chained.accept(&5);
// second.accept(&3); // Would not compile - moved
§Preserving original with clone
use prism3_function::{ReadonlyConsumer, BoxReadonlyConsumer,
    RcReadonlyConsumer};

let first = BoxReadonlyConsumer::new(|x: &i32| {
    println!("First: {}", x);
});
let second = RcReadonlyConsumer::new(|x: &i32| {
    println!("Second: {}", x);
});

// Clone to preserve original
let chained = first.and_then(second.clone());
chained.accept(&5);

// Original still usable
second.accept(&3);

Trait Implementations§

Source§

impl<T> Debug for BoxReadonlyConsumer<T>

Source§

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

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

impl<T> Display for BoxReadonlyConsumer<T>

Source§

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

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

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

Source§

fn accept(&self, value: &T)

Execute readonly consumption operation Read more
Source§

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

Convert to BoxReadonlyConsumer Read more
Source§

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

Convert to RcReadonlyConsumer Read more
Source§

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

Convert to closure Read more
Source§

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

Convert to ArcReadonlyConsumer Read more

Auto Trait Implementations§

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> 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> 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.