BoxConditionalMapperOnce

Struct BoxConditionalMapperOnce 

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

BoxConditionalMapperOnce struct

A conditional consuming mapper that only executes when a predicate is satisfied. Uses BoxMapperOnce and BoxPredicate for single ownership semantics.

This type is typically created by calling BoxMapperOnce::when() and is designed to work with the or_else() method to create if-then-else logic.

§Features

  • Single Ownership: Not cloneable, consumes self on use
  • One-time Use: Can only be called once
  • Conditional Execution: Only maps when predicate returns true
  • Chainable: Can add or_else branch to create if-then-else logic

§Examples

§With or_else Branch

use prism3_function::{MapperOnce, BoxMapperOnce};

let double = BoxMapperOnce::new(|x: i32| x * 2);
let negate = BoxMapperOnce::new(|x: i32| -x);
let conditional = double.when(|x: &i32| *x > 0).or_else(negate);
assert_eq!(conditional.apply_once(5), 10); // when branch executed

let double2 = BoxMapperOnce::new(|x: i32| x * 2);
let negate2 = BoxMapperOnce::new(|x: i32| -x);
let conditional2 = double2.when(|x: &i32| *x > 0).or_else(negate2);
assert_eq!(conditional2.apply_once(-5), 5); // or_else branch executed

§Author

胡海星

Implementations§

Source§

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

Source

pub fn or_else<F>(self, else_mapper: F) -> BoxMapperOnce<T, R>
where F: MapperOnce<T, R> + 'static,

Adds an else branch

Executes the original mapper when the condition is satisfied, otherwise executes else_mapper.

§Parameters
  • else_mapper - The mapper for the else branch, can be:
    • Closure: |x: T| -> R
    • BoxMapperOnce<T, R>
    • Any type implementing MapperOnce<T, R>
§Returns

Returns the composed BoxMapperOnce<T, R>

§Examples
use prism3_function::{MapperOnce, BoxMapperOnce};

let double = BoxMapperOnce::new(|x: i32| x * 2);
let conditional = double.when(|x: &i32| *x > 0).or_else(|x: i32| -x);
assert_eq!(conditional.apply_once(5), 10); // Condition satisfied, execute double

let double2 = BoxMapperOnce::new(|x: i32| x * 2);
let conditional2 = double2.when(|x: &i32| *x > 0).or_else(|x: i32| -x);
assert_eq!(conditional2.apply_once(-5), 5); // Condition not satisfied, execute negate
Examples found in repository?
examples/mapper_once_demo.rs (line 52)
12fn main() {
13    println!("=== MapperOnce 演示 ===\n");
14
15    // 1. 基本的 BoxMapperOnce
16    println!("1. 基本的 BoxMapperOnce");
17    let parse = BoxMapperOnce::new(|s: String| s.parse::<i32>().unwrap_or(0));
18    let result = parse.apply_once("42".to_string());
19    println!("   解析 '42': {}", result);
20
21    // 2. identity mapper
22    println!("\n2. Identity Mapper");
23    let identity = BoxMapperOnce::<i32, i32>::identity();
24    let result = identity.apply_once(42);
25    println!("   identity(42): {}", result);
26
27    // 3. constant mapper
28    println!("\n3. Constant Mapper");
29    let constant = BoxMapperOnce::constant("hello");
30    let result = constant.apply_once(123);
31    println!("   constant(123): {}", result);
32
33    // 4. and_then 组合
34    println!("\n4. and_then 组合");
35    let add_one = BoxMapperOnce::new(|x: i32| x + 1);
36    let double = |x: i32| x * 2;
37    let composed = add_one.and_then(double);
38    let result = composed.apply_once(5);
39    println!("   (5 + 1) * 2 = {}", result);
40
41    // 5. compose 组合
42    println!("\n5. compose 组合");
43    let double = BoxMapperOnce::new(|x: i32| x * 2);
44    let add_one = |x: i32| x + 1;
45    let composed = double.compose(add_one);
46    let result = composed.apply_once(5);
47    println!("   double(5 + 1) = {}", result);
48
49    // 6. 条件映射
50    println!("\n6. 条件映射");
51    let double = BoxMapperOnce::new(|x: i32| x * 2);
52    let conditional = double.when(|x: &i32| *x > 0).or_else(|x: i32| -x);
53    let result1 = conditional.apply_once(5);
54    println!("   条件(5 > 0): double(5) = {}", result1);
55
56    let double2 = BoxMapperOnce::new(|x: i32| x * 2);
57    let conditional2 = double2.when(|x: &i32| *x > 0).or_else(|x: i32| -x);
58    let result2 = conditional2.apply_once(-5);
59    println!("   条件(-5 > 0): negate(-5) = {}", result2);
60
61    // 7. 管道操作
62    println!("\n7. 管道操作");
63    let add_one = BoxMapperOnce::new(|x: i32| x + 1);
64    let pipeline = add_one.and_then(|x| x * 2).and_then(|x| x - 3);
65    let result = pipeline.apply_once(5);
66    println!("   ((5 + 1) * 2) - 3 = {}", result);
67
68    // 8. 闭包扩展方法
69    println!("\n8. 闭包扩展方法 (FnMapperOnceOps)");
70    let parse = |s: String| s.parse::<i32>().unwrap_or(0);
71    let double = |x: i32| x * 2;
72    let composed = parse.and_then(double);
73    let result = composed.apply_once("21".to_string());
74    println!("   parse('21') * 2 = {}", result);
75
76    // 9. 类型转换
77    println!("\n9. 类型转换");
78    let to_string = BoxMapperOnce::new(|x: i32| x.to_string());
79    let add_suffix = to_string.and_then(|s| format!("{}_suffix", s));
80    let result = add_suffix.apply_once(42);
81    println!("   42.to_string() + '_suffix' = {}", result);
82
83    // 10. 消费所有权
84    println!("\n10. 消费所有权");
85    let vec = vec![1, 2, 3, 4, 5];
86    let sum = BoxMapperOnce::new(|v: Vec<i32>| v.iter().sum::<i32>());
87    let result = sum.apply_once(vec);
88    println!("   sum([1,2,3,4,5]) = {}", result);
89
90    println!("\n=== MapperOnce 演示完成 ===");
91}

Auto Trait Implementations§

§

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

§

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

§

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

§

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

§

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

§

impl<T, R> !UnwindSafe for BoxConditionalMapperOnce<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> 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, 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.