computation_types/
len.rs

1use core::fmt;
2
3use crate::{
4    impl_computation_fn_for_unary, impl_core_ops,
5    peano::{One, Zero},
6    Computation, ComputationFn, NamedArgs,
7};
8
9#[derive(Clone, Copy, Debug)]
10pub struct Len<A>(pub A)
11where
12    Self: Computation;
13
14impl<A> Computation for Len<A>
15where
16    A: Computation<Dim = One>,
17{
18    type Dim = Zero;
19    type Item = usize;
20}
21
22impl_computation_fn_for_unary!(Len);
23
24impl_core_ops!(Len<A>);
25
26impl<A> fmt::Display for Len<A>
27where
28    Self: Computation,
29    A: fmt::Display,
30{
31    fn fmt(&self, f: &mut fmt::Formatter<'_>) -> fmt::Result {
32        write!(f, "{}.len()", self.0)
33    }
34}
35
36#[cfg(test)]
37mod tests {
38    use crate::val1;
39
40    use super::*;
41
42    #[test]
43    fn len_should_display() {
44        let inp = val1!(vec![0, 1]);
45        assert_eq!(inp.clone().len().to_string(), format!("{}.len()", inp));
46    }
47}