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
 47
 48
 49
 50
 51
 52
 53
 54
 55
 56
 57
 58
 59
 60
 61
 62
 63
 64
 65
 66
 67
 68
 69
 70
 71
 72
 73
 74
 75
 76
 77
 78
 79
 80
 81
 82
 83
 84
 85
 86
 87
 88
 89
 90
 91
 92
 93
 94
 95
 96
 97
 98
 99
100
101
102
103
104
105
106
107
108
109
110
111
112
113
114
115
116
117
118
119
120
121
122
123
124
125
126
127
128
129
130
131
132
133
134
135
136
137
138
139
140
141
142
143
144
145
146
147
148
149
150
151
152
153
154
155
156
157
158
159
160
161
162
163
164
165
166
167
168
169
170
171
172
173
174
175
176
177
178
179
180
181
182
183
184
185
186
187
188
189
190
191
192
193
194
195
196
197
198
199
200
use super::BaseAnimation;
use crate::DURATION_ZERO;
use std::time::Duration;

/// like `Iterator`, but does not consume any element
///
/// built-in types that derives [`Cursor`]
/// - `Vec<T>`
/// - `[T]`
/// - `&[T]`
/// - `Box<T>` where `T:Cursor`
/// - `&T` where `T:Cursor`
pub trait Cursor {
    /// item of the cursor
    type Item;
    /// none means that it's infinite
    fn size(&self) -> Option<usize>;

    /// seek to specified element
    fn index(&self, n: usize) -> Self::Item;
}

impl<T: Clone> Cursor for [T] {
    type Item = T;
    #[inline]
    fn size(&self) -> Option<usize> {
        Some(self.len())
    }
    #[inline]
    fn index(&self, n: usize) -> T {
        self[n].to_owned()
    }
}

impl<T: Clone> Cursor for &[T] {
    type Item = T;
    #[inline]
    fn size(&self) -> Option<usize> {
        Some(self.len())
    }
    #[inline]
    fn index(&self, n: usize) -> T {
        self[n].to_owned()
    }
}

impl<T: Clone> Cursor for Vec<T> {
    type Item = T;
    #[inline]
    fn size(&self) -> Option<usize> {
        Some(self.len())
    }
    #[inline]
    fn index(&self, n: usize) -> T {
        self[n].to_owned()
    }
}

impl<T: Cursor> Cursor for &T {
    type Item = T::Item;
    #[inline]
    fn size(&self) -> Option<usize> {
        (**self).size()
    }
    #[inline]
    fn index(&self, n: usize) -> Self::Item {
        (**self).index(n)
    }
}

impl<T: Cursor> Cursor for Box<T> {
    type Item = T::Item;
    #[inline]
    fn size(&self) -> Option<usize> {
        (**self).size()
    }
    #[inline]
    fn index(&self, n: usize) -> Self::Item {
        (**self).index(n)
    }
}

struct Finite<T> {
    src: T,
}

impl<T> Cursor for Finite<T>
where
    T: ExactSizeIterator + Clone,
{
    type Item = <T as Iterator>::Item;

    #[inline]
    fn size(&self) -> Option<usize> {
        self.src.len().into()
    }

    #[inline]
    fn index(&self, n: usize) -> Self::Item {
        let mut src = self.src.clone();
        src.nth(n).unwrap()
    }
}

impl<T> From<T> for Finite<T>
where
    T: ExactSizeIterator + Clone,
{
    #[inline]
    fn from(src: T) -> Self {
        Finite { src }
    }
}

#[derive(Clone)]
pub struct Infinite<F: Fn(usize) -> T, T> {
    f: F,
}

impl<F, T> Infinite<F, T>
where
    F: Fn(usize) -> T,
{
    #[inline]
    pub(super) fn new(f: F) -> Self {
        Self { f }
    }
}

impl<F, T> Cursor for Infinite<F, T>
where
    F: Fn(usize) -> T,
{
    type Item = T;
    #[inline]
    fn size(&self) -> Option<usize> {
        None
    }
    #[inline]
    fn index(&self, n: usize) -> Self::Item {
        (self.f)(n)
    }
}

#[derive(Debug, Clone)]
pub struct StepAnimation<T: Cursor> {
    src: T,
    interval: Duration,
}

impl<T> StepAnimation<T>
where
    T: Cursor,
{
    /// create animation
    #[inline]
    pub(super) fn new(src: T) -> Self {
        Self {
            src,
            interval: DURATION_ZERO,
        }
    }

    /// set duration of the animation
    #[inline]
    pub fn interval(mut self, interval: Duration) -> Self {
        self.interval = interval;
        self
    }
}

impl<T> BaseAnimation for StepAnimation<T>
where
    T: Cursor,
{
    type Item = <T as Cursor>::Item;

    #[inline]
    fn duration(&self) -> Option<Duration> {
        if self.interval == DURATION_ZERO {
            return Some(DURATION_ZERO);
        }
        self.src
            .size()
            .map(|size| self.interval.mul_f64(size as f64))
    }

    #[inline]
    fn animate(&self, elapsed: Duration) -> Self::Item {
        let n = match self.duration() {
            Some(duration) if duration == DURATION_ZERO => 0,
            Some(duration) if elapsed >= duration => self.src.size().unwrap(),
            _ => {
                let n = elapsed.as_secs_f64() / self.interval.as_secs_f64();
                n as usize
            }
        };
        self.src.index(n)
    }
}