Trait InitWith

Source
pub trait InitWith<T> {
    // Required methods
    fn init_with<F>(init: F) -> Self
       where F: FnMut() -> T,
             Self: Sized;
    fn init_with_indices<F>(init: F) -> Self
       where F: FnMut(usize) -> T,
             Self: Sized;
}
Expand description

A trait that allows you to create an instance of a type by using a given function to generate each element.

Required Methods§

Source

fn init_with<F>(init: F) -> Self
where F: FnMut() -> T, Self: Sized,

Create a new instance of this type using the given function to fill elements.

§Examples

Prefilling an array with a Vec, with no unsafe code:

use init_with::InitWith;

let src = vec![1, 2, 3];
let dest: [i32; 3] = {
    let mut idx = 0;

    <[i32; 3]>::init_with(|| {
        let val = src[idx];
        idx += 1;
        val
    })
};

assert_eq!(src, dest);
Source

fn init_with_indices<F>(init: F) -> Self
where F: FnMut(usize) -> T, Self: Sized,

Create a new instance of this type to fill elements by mapping the given function over the new array’s indices.

§Examples

Prefilling an array of even numbers, with no unsafe code:

use init_with::InitWith;

let src = vec![0, 2, 4];
let dest = <[i32; 3]>::init_with_indices(|x| 2*x as i32);


assert_eq!(src, dest);

Implementations on Foreign Types§

Source§

impl<T> InitWith<T> for [T; 0]

Source§

fn init_with<F>(_init: F) -> Self
where F: FnMut() -> T, Self: Sized,

Source§

fn init_with_indices<F>(_: F) -> Self
where F: FnMut(usize) -> T, Self: Sized,

Source§

impl<T> InitWith<T> for [T; 1]

Source§

fn init_with<F>(init: F) -> Self
where F: FnMut() -> T, Self: Sized,

Source§

fn init_with_indices<F>(init: F) -> Self
where F: FnMut(usize) -> T, Self: Sized,

Source§

impl<T> InitWith<T> for [T; 2]

Source§

fn init_with<F>(init: F) -> Self
where F: FnMut() -> T, Self: Sized,

Source§

fn init_with_indices<F>(init: F) -> Self
where F: FnMut(usize) -> T,

Source§

impl<T> InitWith<T> for [T; 3]

Source§

fn init_with<F>(init: F) -> Self
where F: FnMut() -> T, Self: Sized,

Source§

fn init_with_indices<F>(init: F) -> Self
where F: FnMut(usize) -> T,

Source§

impl<T> InitWith<T> for [T; 4]

Source§

fn init_with<F>(init: F) -> Self
where F: FnMut() -> T, Self: Sized,

Source§

fn init_with_indices<F>(init: F) -> Self
where F: FnMut(usize) -> T,

Source§

impl<T> InitWith<T> for [T; 5]

Source§

fn init_with<F>(init: F) -> Self
where F: FnMut() -> T, Self: Sized,

Source§

fn init_with_indices<F>(init: F) -> Self
where F: FnMut(usize) -> T,

Source§

impl<T> InitWith<T> for [T; 6]

Source§

fn init_with<F>(init: F) -> Self
where F: FnMut() -> T, Self: Sized,

Source§

fn init_with_indices<F>(init: F) -> Self
where F: FnMut(usize) -> T,

Source§

impl<T> InitWith<T> for [T; 7]

Source§

fn init_with<F>(init: F) -> Self
where F: FnMut() -> T, Self: Sized,

Source§

fn init_with_indices<F>(init: F) -> Self
where F: FnMut(usize) -> T,

Source§

impl<T> InitWith<T> for [T; 8]

Source§

fn init_with<F>(init: F) -> Self
where F: FnMut() -> T, Self: Sized,

Source§

fn init_with_indices<F>(init: F) -> Self
where F: FnMut(usize) -> T,

Source§

impl<T> InitWith<T> for [T; 9]

Source§

fn init_with<F>(init: F) -> Self
where F: FnMut() -> T, Self: Sized,

Source§

fn init_with_indices<F>(init: F) -> Self
where F: FnMut(usize) -> T,

Source§

impl<T> InitWith<T> for [T; 10]

Source§

fn init_with<F>(init: F) -> Self
where F: FnMut() -> T, Self: Sized,

Source§

fn init_with_indices<F>(init: F) -> Self
where F: FnMut(usize) -> T,

Source§

impl<T> InitWith<T> for [T; 11]

Source§

fn init_with<F>(init: F) -> Self
where F: FnMut() -> T, Self: Sized,

Source§

fn init_with_indices<F>(init: F) -> Self
where F: FnMut(usize) -> T,

Source§

impl<T> InitWith<T> for [T; 12]

Source§

fn init_with<F>(init: F) -> Self
where F: FnMut() -> T, Self: Sized,

Source§

fn init_with_indices<F>(init: F) -> Self
where F: FnMut(usize) -> T,

Source§

impl<T> InitWith<T> for [T; 13]

Source§

fn init_with<F>(init: F) -> Self
where F: FnMut() -> T, Self: Sized,

Source§

fn init_with_indices<F>(init: F) -> Self
where F: FnMut(usize) -> T,

Source§

impl<T> InitWith<T> for [T; 14]

Source§

fn init_with<F>(init: F) -> Self
where F: FnMut() -> T, Self: Sized,

Source§

fn init_with_indices<F>(init: F) -> Self
where F: FnMut(usize) -> T,

Source§

impl<T> InitWith<T> for [T; 15]

Source§

fn init_with<F>(init: F) -> Self
where F: FnMut() -> T, Self: Sized,

Source§

fn init_with_indices<F>(init: F) -> Self
where F: FnMut(usize) -> T,

Source§

impl<T> InitWith<T> for [T; 16]

Source§

fn init_with<F>(init: F) -> Self
where F: FnMut() -> T, Self: Sized,

Source§

fn init_with_indices<F>(init: F) -> Self
where F: FnMut(usize) -> T,

Source§

impl<T> InitWith<T> for [T; 17]

Source§

fn init_with<F>(init: F) -> Self
where F: FnMut() -> T, Self: Sized,

Source§

fn init_with_indices<F>(init: F) -> Self
where F: FnMut(usize) -> T,

Source§

impl<T> InitWith<T> for [T; 18]

Source§

fn init_with<F>(init: F) -> Self
where F: FnMut() -> T, Self: Sized,

Source§

fn init_with_indices<F>(init: F) -> Self
where F: FnMut(usize) -> T,

Source§

impl<T> InitWith<T> for [T; 19]

Source§

fn init_with<F>(init: F) -> Self
where F: FnMut() -> T, Self: Sized,

Source§

fn init_with_indices<F>(init: F) -> Self
where F: FnMut(usize) -> T,

Source§

impl<T> InitWith<T> for [T; 20]

Source§

fn init_with<F>(init: F) -> Self
where F: FnMut() -> T, Self: Sized,

Source§

fn init_with_indices<F>(init: F) -> Self
where F: FnMut(usize) -> T,

Source§

impl<T> InitWith<T> for [T; 21]

Source§

fn init_with<F>(init: F) -> Self
where F: FnMut() -> T, Self: Sized,

Source§

fn init_with_indices<F>(init: F) -> Self
where F: FnMut(usize) -> T,

Source§

impl<T> InitWith<T> for [T; 22]

Source§

fn init_with<F>(init: F) -> Self
where F: FnMut() -> T, Self: Sized,

Source§

fn init_with_indices<F>(init: F) -> Self
where F: FnMut(usize) -> T,

Source§

impl<T> InitWith<T> for [T; 23]

Source§

fn init_with<F>(init: F) -> Self
where F: FnMut() -> T, Self: Sized,

Source§

fn init_with_indices<F>(init: F) -> Self
where F: FnMut(usize) -> T,

Source§

impl<T> InitWith<T> for [T; 24]

Source§

fn init_with<F>(init: F) -> Self
where F: FnMut() -> T, Self: Sized,

Source§

fn init_with_indices<F>(init: F) -> Self
where F: FnMut(usize) -> T,

Source§

impl<T> InitWith<T> for [T; 25]

Source§

fn init_with<F>(init: F) -> Self
where F: FnMut() -> T, Self: Sized,

Source§

fn init_with_indices<F>(init: F) -> Self
where F: FnMut(usize) -> T,

Source§

impl<T> InitWith<T> for [T; 26]

Source§

fn init_with<F>(init: F) -> Self
where F: FnMut() -> T, Self: Sized,

Source§

fn init_with_indices<F>(init: F) -> Self
where F: FnMut(usize) -> T,

Source§

impl<T> InitWith<T> for [T; 27]

Source§

fn init_with<F>(init: F) -> Self
where F: FnMut() -> T, Self: Sized,

Source§

fn init_with_indices<F>(init: F) -> Self
where F: FnMut(usize) -> T,

Source§

impl<T> InitWith<T> for [T; 28]

Source§

fn init_with<F>(init: F) -> Self
where F: FnMut() -> T, Self: Sized,

Source§

fn init_with_indices<F>(init: F) -> Self
where F: FnMut(usize) -> T,

Source§

impl<T> InitWith<T> for [T; 29]

Source§

fn init_with<F>(init: F) -> Self
where F: FnMut() -> T, Self: Sized,

Source§

fn init_with_indices<F>(init: F) -> Self
where F: FnMut(usize) -> T,

Source§

impl<T> InitWith<T> for [T; 30]

Source§

fn init_with<F>(init: F) -> Self
where F: FnMut() -> T, Self: Sized,

Source§

fn init_with_indices<F>(init: F) -> Self
where F: FnMut(usize) -> T,

Source§

impl<T> InitWith<T> for [T; 31]

Source§

fn init_with<F>(init: F) -> Self
where F: FnMut() -> T, Self: Sized,

Source§

fn init_with_indices<F>(init: F) -> Self
where F: FnMut(usize) -> T,

Source§

impl<T> InitWith<T> for [T; 32]

Source§

fn init_with<F>(init: F) -> Self
where F: FnMut() -> T, Self: Sized,

Source§

fn init_with_indices<F>(init: F) -> Self
where F: FnMut(usize) -> T,

Implementors§