custos/static_api/
macros.rs

1/// A macro that creates a `CPU` `Buffer` using the static `CPU` device.
2///
3/// # Examples
4#[cfg_attr(feature = "cpu", doc = "```")]
5#[cfg_attr(not(feature = "cpu"), doc = "```ignore")]
6/// use custos::buf;
7///
8/// let buf = buf![2.; 10];
9/// assert_eq!(buf.read(), [2.; 10]);
10///
11/// let buf = buf![5, 3, 2, 6, 2];
12/// assert_eq!(buf.read(), &[5, 3, 2, 6, 2])
13/// ```
14#[macro_export]
15macro_rules! buf {
16    ($elem:expr; $n:expr) => (
17        if $n == 0 {
18            panic!("The length of the buffer can't be 0.");
19        } else {
20            $crate::Buffer::from(vec![$elem; $n])
21        }
22    );
23
24    ($($x:expr),+ $(,)?) => (
25        $crate::Buffer::<_, $crate::CPU, ()>::from([$($x),+])
26    );
27
28    // TODO: buf![device, [...]]
29    ($device:expr, [($x:expr),+ $(,)?]) => (
30        $crate::Buffer::<_, _, 0>::from((&device, [$($x),+]))
31    )
32}
33
34#[cfg(test)]
35mod tests {
36    #[test]
37    fn test_macro_filling() {
38        let buf = buf![2.; 10];
39        assert_eq!(buf.as_slice(), &[2.; 10]);
40    }
41
42    #[test]
43    fn test_macro_from_slice() {
44        let buf = buf![5, 3, 2, 6, 2];
45        assert_eq!(buf.as_slice(), &[5, 3, 2, 6, 2])
46    }
47}