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
use super::Image;

/// A frame backed by a function.
///
/// This might be useful if you just wanted to hack together a fractal renderer,
/// or wanted to wrap an image coming from a C library. Using `move` closures
/// with this struct essentially lets you build anonymous frames.
#[derive(Clone, Debug)]
pub struct Function<T, F>
where F: Fn(usize, usize) -> T {
    width: usize,
    height: usize,
    func: F
}

impl<T, F> Image for Function<T, F>
where F: Fn(usize, usize) -> T{
    type Pixel = T;

    fn width(&self)  -> usize { self.width }
    fn height(&self) -> usize { self.height }

    unsafe fn pixel(&self, x: usize, y: usize) -> Self::Pixel {
        (self.func)(x, y)
    }
}

impl<T, F> Function<T, F>
where F: Fn(usize, usize) -> T {
    /// Creates a new frame.
    pub fn new(width: usize, height: usize, func: F) -> Self {
        Self { width, height, func }
    }
}

#[test]
fn sollux() {
    let sollux = Function::new(2, 1, |x, _| if x == 0 { 0 } else { 1 });
    unsafe {
        assert_eq!(sollux.pixel(0, 0), 0);
        assert_eq!(sollux.pixel(1, 0), 1);
    }
}