Trait ArrayExt

Source
pub trait ArrayExt {
    // Required method
    fn try_map_ext<F, R>(
        self,
        f: F,
    ) -> <<R as Try>::Residual as Residual<Self::TN<<R as Try>::Output>>>::TryType
       where F: FnMut(Self::T) -> R,
             R: Try,
             <R as Try>::Residual: Residual<Self::TN<<R as Try>::Output>>;
}
Expand description

A helper extension trait for arrays

Required Methods§

Source

fn try_map_ext<F, R>( self, f: F, ) -> <<R as Try>::Residual as Residual<Self::TN<<R as Try>::Output>>>::TryType
where F: FnMut(Self::T) -> R, R: Try, <R as Try>::Residual: Residual<Self::TN<<R as Try>::Output>>,

A fallible function f applied to each element on array self in order to return an array the same size as self or the first error encountered.

The return type of this function depends on the return type of the closure. If you return Result<T, E> from the closure, you’ll get a Result<[T; N], E>. If you return Option<T> from the closure, you’ll get an Option<[T; N]>.

§Examples
use array_util::ArrayExt;

let a = ["1", "2", "3"];
let b = a.try_map_ext(|v| v.parse::<u32>()).unwrap().map(|v| v + 1);
assert_eq!(b, [2, 3, 4]);

let a = ["1", "2a", "3"];
let b = a.try_map_ext(|v| v.parse::<u32>());
assert!(b.is_err());

use std::num::NonZeroU32;
let z = [1, 2, 0, 3, 4];
assert_eq!(z.try_map_ext(NonZeroU32::new), None);
let a = [1, 2, 3];
let b = a.try_map_ext(NonZeroU32::new);
let c = b.map(|x| x.map(NonZeroU32::get));
assert_eq!(c, Some(a));

Dyn Compatibility§

This trait is not dyn compatible.

In older versions of Rust, dyn compatibility was called "object safety", so this trait is not object safe.

Implementations on Foreign Types§

Source§

impl<T, const N: usize> ArrayExt for [T; N]

Source§

fn try_map_ext<F, R>( self, f: F, ) -> <<R as Try>::Residual as Residual<[<R as Try>::Output; N]>>::TryType
where F: FnMut(T) -> R, R: Try, <R as Try>::Residual: Residual<[<R as Try>::Output; N]>,

A fallible function f applied to each element on array self in order to return an array the same size as self or the first error encountered.

The return type of this function depends on the return type of the closure. If you return Result<T, E> from the closure, you’ll get a Result<[T; N], E>. If you return Option<T> from the closure, you’ll get an Option<[T; N]>.

§Examples
use array_util::ArrayExt;

let a = ["1", "2", "3"];
let b = a.try_map_ext(|v| v.parse::<u32>()).unwrap().map(|v| v + 1);
assert_eq!(b, [2, 3, 4]);

let a = ["1", "2a", "3"];
let b = a.try_map_ext(|v| v.parse::<u32>());
assert!(b.is_err());

use std::num::NonZeroU32;
let z = [1, 2, 0, 3, 4];
assert_eq!(z.try_map_ext(NonZeroU32::new), None);
let a = [1, 2, 3];
let b = a.try_map_ext(NonZeroU32::new);
let c = b.map(|x| x.map(NonZeroU32::get));
assert_eq!(c, Some(a));

Implementors§