1
2
3
4
5
6
7
8
9
10
11
12
13
14
15
16
17
18
19
20
21
22
23
24
25
26
27
28
29
30
31
32
33
34
35
36
37
38
39
40
41
42
43
44
45
46
use crate::{Slice, SliceOwned};

/// A slice calling a closure on index; see [`from_fn`](crate::from_fn).
#[derive(Clone, Copy, Hash)]
pub struct FromFn<F> {
    /// The inner closure of the slice.
    pub f: F,
    len: usize,
}

impl<F, T> FromFn<F>
where
    F: Fn(usize) -> Option<T>,
{
    /// Create a slice from a closure; see [`from_fn`](crate::from_fn).
    pub fn new(f: F, len: Option<usize>) -> Self {
        Self {
            f,
            len: len.unwrap_or(usize::MAX),
        }
    }
}

impl<F, T> Slice for FromFn<F>
where
    F: Fn(usize) -> Option<T>,
{
    type Output = T;

    fn len(&self) -> usize {
        self.len
    }

    fn get_with<W: FnMut(&Self::Output) -> R, R>(&self, index: usize, f: &mut W) -> Option<R> {
        Some(f(&self.get_owned(index)?))
    }
}

impl<F, T> SliceOwned for FromFn<F>
where
    F: Fn(usize) -> Option<T>,
{
    fn get_owned(&self, index: usize) -> Option<Self::Output> {
        (self.f)(index)
    }
}