[][src]Trait tuple_combinator::TupleReducer

pub trait TupleReducer: Sized {
    fn fold<U, F: Fn(Option<U>, &dyn Any) -> Option<U>>(
        &self,
        init: U,
        f: F
    ) -> Option<U>;
fn fold_strict<U: Any, F: Fn(Option<U>, &U) -> Option<U>>(
        &self,
        init: U,
        f: F
    ) -> Option<U>;
fn ref_slice(&self) -> Box<[&dyn Any]>;
fn strict_ref_slice<T: Any>(&self) -> Box<[&Option<T>]>;
fn mut_slice(&mut self) -> Box<[&mut dyn Any]>;
fn strict_mut_slice<T: Any>(&mut self) -> Box<[&mut Option<T>]>; }

Reduce tuples of Options into results of various form, act in comparable to the iterators.

use tuple_combinator::TupleReducer;

let res = (Some(1), Some(5), Some("rust_tuple")).fold(0, |sum, item| {
    sum.and_then(|s| {
        if let Some(raw_i32) = item.downcast_ref::<Option<i32>>() {
            return raw_i32.as_ref()
                .and_then(|val| {
                    Some(s + val)
                });
        }

        if let Some(raw_str) = item.downcast_ref::<Option<&str>>() {
            return raw_str.as_ref()
                .and_then(|val| {
                    Some(s + val.len() as i32)
                });
        }

        Some(s)
    })
});

assert_eq!(res, Some(16));

Required methods

fn fold<U, F: Fn(Option<U>, &dyn Any) -> Option<U>>(
    &self,
    init: U,
    f: F
) -> Option<U>

Fold the tuple to obtain a final outcome. Depending on the implementation of the handler function, the fold can behave differently on various option types or values.

Examples

Reduce tuples of i32 options to the sum of the contained values:

use tuple_combinator::TupleReducer;

let res = (Some(17), Some(20)).fold(5, |sum, item| {
    sum.and_then(|s| {
        item.downcast_ref::<Option<i32>>()
            .and_then(|raw| raw.as_ref())
            .and_then(|val| {
                Some(s + val)
             })
    })
});

assert_eq!(res, Some(42));

fn fold_strict<U: Any, F: Fn(Option<U>, &U) -> Option<U>>(
    &self,
    init: U,
    f: F
) -> Option<U>

fold_strict works very much like fold, except that only options with the same wrapped data type as the output type will be "folded", i.e. invoking the supplied folding function. This function will come into handy when the caller only care about the options in the tuples that match the output type.

Examples

use tuple_combinator::TupleReducer;

let res =  (Some(40), Some("noise"), None as Option<i32>, Some(2))
    .fold_strict(0i32, |sum, item| {
        sum.and_then(|s| {
            Some(s + item)
        })
    });

assert_eq!(res, Some(42));

fn ref_slice(&self) -> Box<[&dyn Any]>

Convert the tuples to a reference slice, where caller can use native iteration tools. Note that this is re-export of the tuples' internal content, hence the slice can't live longer than the tuple self.

Examples

use std::any::Any;
use tuple_combinator::TupleReducer;

let mut src = (Some(1), None as Option<&str>, Some(2), None as Option<i32>, Some(()));

// convert the tuples to a slice of `Any` type
let slice: Box<[&dyn Any]> = src.ref_slice();

// the slice has the same amount of elements as in the tuples.
assert_eq!(slice.len(), 5);

// downcast the element to its actual type; wrong type cast will be rejected with a `None`
// output from the API call.
assert_eq!(slice[0].downcast_ref::<Option<i32>>().unwrap(), &Some(1));
assert_eq!(slice[0].downcast_ref::<Option<&str>>(), None);
assert_eq!(slice[1].downcast_ref::<Option<&str>>().unwrap(), &None);

// unlike `mut_slice` API, the line below won't compile even if adding the `mut` keyword
// to the `slice` variable, because the source slice is immutable.
// let first = slice[0].downcast_mut::<Option<i32>>().unwrap().take();

fn strict_ref_slice<T: Any>(&self) -> Box<[&Option<T>]>

Convert the tuples to a reference slice which only include options wrapping the data of the desired type [T]. Options with types other than the given one will be excluded from the slice. Note that if the slice length is 0, it means the source tuple does not contain elements in options that can be converted to type ['T'].

Examples

use tuple_combinator::TupleReducer;

let src = (Some(1), None as Option<&str>, Some(2), None as Option<i32>, Some(()));
let slice = src.strict_ref_slice::<i32>();

// The above variable initiation is equivalent to the following:
// let slice: Box<[&i32]> = src.strict_ref_slice();

assert_eq!(slice.len(), 3);
assert_eq!(
    slice,
    vec![&Some(1), &Some(2), &None as &Option<i32>].into_boxed_slice()
);

// The line below won't compile because the immutability of the slice.
// let first = slice[0].downcast_mut::<Option<i32>>().unwrap().take();

fn mut_slice(&mut self) -> Box<[&mut dyn Any]>

This method works similar to ref_slice, except that the members of the slice are mutable, such that it is possible to make updates, or taking ownership from the underlying tuples data. Note that modifying or altering the slice data will also cause the same data in the tuples to be altered.

Examples

use std::any::Any;
use tuple_combinator::TupleReducer;

let mut src = (Some(1), None as Option<&str>, Some(2), None as Option<i32>, Some(()));
let slice: Box<[&mut dyn Any]> = src.mut_slice();

assert_eq!(slice.len(), 5);
assert_eq!(slice[0].downcast_ref::<Option<i32>>().unwrap(), &Some(1));
assert_eq!(slice[1].downcast_ref::<Option<&str>>().unwrap(), &None);

let first = slice[0].downcast_mut::<Option<i32>>().unwrap().take();
assert_eq!(first, Some(1));

fn strict_mut_slice<T: Any>(&mut self) -> Box<[&mut Option<T>]>

This method works similar to strict_ref_slice, except that the members of the slice are mutable, such that it is possible to make updates, or taking ownership from the underlying tuples data. Note that modifying or altering the slice data will also cause the same data in the tuples to be altered.

Examples

use tuple_combinator::TupleReducer;

let mut src = (Some(1), None as Option<&str>, Some(2), None as Option<i32>, Some(()));
let slice = src.strict_mut_slice::<i32>();

// The above variable initiation is equivalent to the following:
// let slice: Box<[&mut i32]> = src.strict_mut_slice();

assert_eq!(slice.len(), 3);
assert_eq!(
    slice,
    vec![&mut Some(1), &mut Some(2), &mut None as &mut Option<i32>].into_boxed_slice()
);

// Now you can take the wrapped content out of the tuples/slice and operate on the element.
// Note that operations on the slice element will take the same effect on the origin tuples,
// since slice elements are merely mutable borrows.
let first = slice[0].take();
assert_eq!(first, Some(1));
assert_eq!(slice[0], &mut None);
Loading content...

Implementations on Foreign Types

impl<T0> TupleReducer for (Option<T0>,) where
    T0: Any
[src]

impl<T0, T1> TupleReducer for (Option<T0>, Option<T1>) where
    T0: Any,
    T1: Any
[src]

impl<T0, T1, T2> TupleReducer for (Option<T0>, Option<T1>, Option<T2>) where
    T0: Any,
    T1: Any,
    T2: Any
[src]

impl<T0, T1, T2, T3> TupleReducer for (Option<T0>, Option<T1>, Option<T2>, Option<T3>) where
    T0: Any,
    T1: Any,
    T2: Any,
    T3: Any
[src]

impl<T0, T1, T2, T3, T4> TupleReducer for (Option<T0>, Option<T1>, Option<T2>, Option<T3>, Option<T4>) where
    T0: Any,
    T1: Any,
    T2: Any,
    T3: Any,
    T4: Any
[src]

impl<T0, T1, T2, T3, T4, T5> TupleReducer for (Option<T0>, Option<T1>, Option<T2>, Option<T3>, Option<T4>, Option<T5>) where
    T0: Any,
    T1: Any,
    T2: Any,
    T3: Any,
    T4: Any,
    T5: Any
[src]

impl<T0, T1, T2, T3, T4, T5, T6> TupleReducer for (Option<T0>, Option<T1>, Option<T2>, Option<T3>, Option<T4>, Option<T5>, Option<T6>) where
    T0: Any,
    T1: Any,
    T2: Any,
    T3: Any,
    T4: Any,
    T5: Any,
    T6: Any
[src]

impl<T0, T1, T2, T3, T4, T5, T6, T7> TupleReducer for (Option<T0>, Option<T1>, Option<T2>, Option<T3>, Option<T4>, Option<T5>, Option<T6>, Option<T7>) where
    T0: Any,
    T1: Any,
    T2: Any,
    T3: Any,
    T4: Any,
    T5: Any,
    T6: Any,
    T7: Any
[src]

impl<T0, T1, T2, T3, T4, T5, T6, T7, T8> TupleReducer for (Option<T0>, Option<T1>, Option<T2>, Option<T3>, Option<T4>, Option<T5>, Option<T6>, Option<T7>, Option<T8>) where
    T0: Any,
    T1: Any,
    T2: Any,
    T3: Any,
    T4: Any,
    T5: Any,
    T6: Any,
    T7: Any,
    T8: Any
[src]

impl<T0, T1, T2, T3, T4, T5, T6, T7, T8, T9> TupleReducer for (Option<T0>, Option<T1>, Option<T2>, Option<T3>, Option<T4>, Option<T5>, Option<T6>, Option<T7>, Option<T8>, Option<T9>) where
    T0: Any,
    T1: Any,
    T2: Any,
    T3: Any,
    T4: Any,
    T5: Any,
    T6: Any,
    T7: Any,
    T8: Any,
    T9: Any
[src]

Loading content...

Implementors

Loading content...