43 lines
728 B
Rust
43 lines
728 B
Rust
|
use napi::bindgen_prelude::*;
|
||
|
|
||
|
#[napi(iterator)]
|
||
|
pub struct Fib {
|
||
|
current: u32,
|
||
|
next: u32,
|
||
|
}
|
||
|
|
||
|
#[napi]
|
||
|
impl Generator for Fib {
|
||
|
type Yield = u32;
|
||
|
type Next = i32;
|
||
|
type Return = ();
|
||
|
|
||
|
fn next(&mut self, value: Option<Self::Next>) -> Option<Self::Yield> {
|
||
|
match value {
|
||
|
Some(n) => {
|
||
|
self.current = n as u32;
|
||
|
self.next = n as u32 + 1;
|
||
|
}
|
||
|
None => {
|
||
|
let next = self.next;
|
||
|
let current = self.current;
|
||
|
self.current = next;
|
||
|
self.next = current + next;
|
||
|
}
|
||
|
};
|
||
|
Some(self.current)
|
||
|
}
|
||
|
}
|
||
|
|
||
|
#[napi]
|
||
|
#[allow(clippy::new_without_default)]
|
||
|
impl Fib {
|
||
|
#[napi(constructor)]
|
||
|
pub fn new() -> Self {
|
||
|
Fib {
|
||
|
current: 0,
|
||
|
next: 1,
|
||
|
}
|
||
|
}
|
||
|
}
|