summaryrefslogtreecommitdiff
path: root/src/lib.rs
blob: eecdcaf4c6fd943a9acdaf92b308f5be675fa11d (plain)
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
use num::{One, Zero};
use std::mem;
use std::ops::Add;

#[derive(Debug)]
pub struct Fib<T> {
    a: T,
    b: T,
}

impl<T: Zero + One> Fib<T> {
    pub fn new() -> Self {
        Self {
            a: T::zero(),
            b: T::one(),
        }
    }
}

impl<T: Clone + Add<Output = T>> Iterator for Fib<T> {
    type Item = T;

    fn next(&mut self) -> Option<Self::Item> {
        let res = self.a.clone();
        mem::swap(&mut self.a, &mut self.b);
        self.b = self.b.clone() + self.a.clone();

        Some(res)
    }
}