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
44
45
46
47
48
49
50
51
52
53
54
55
56
57
58
59
60
61
62
63
64
65
66
67
68
69
70
71
72
73
74
75
76
77
78
79
80
81
82
83
84
85
86
87
88
89
90
use num::{One, Zero};

#[cfg(feature = "afserde")]
use serde::{Deserialize, Serialize};
use std::default::Default;
use std::fmt;

use super::util::IndexableType;

/// Sequences are used for indexing Arrays
#[derive(Copy, Clone, Debug, PartialEq)]
#[cfg_attr(feature = "afserde", derive(Serialize, Deserialize))]
#[repr(C)]
pub struct Seq<T: IndexableType> {
    begin: T,
    end: T,
    step: T,
}

/// Default `Seq` spans all the elements along a dimension
impl<T> Default for Seq<T>
where
    T: One + Zero + IndexableType,
{
    fn default() -> Self {
        Self {
            begin: One::one(),
            end: One::one(),
            step: Zero::zero(),
        }
    }
}

/// Enables use of `Seq` with `{}` format in print statements
impl<T> fmt::Display for Seq<T>
where
    T: fmt::Display + IndexableType,
{
    fn fmt(&self, f: &mut fmt::Formatter) -> fmt::Result {
        write!(
            f,
            "[begin: {}, end: {}, step: {}]",
            self.begin, self.end, self.step
        )
    }
}

impl<T> Seq<T>
where
    T: Copy + IndexableType,
{
    /// Create a `Seq` that goes from `begin` to `end` at a step size of `step`
    pub fn new(begin: T, end: T, step: T) -> Self {
        Self { begin, end, step }
    }

    /// Get begin index of Seq
    pub fn begin(&self) -> T {
        self.begin
    }

    /// Get end index of Seq
    pub fn end(&self) -> T {
        self.end
    }

    /// Get step size of Seq
    pub fn step(&self) -> T {
        self.step
    }
}

#[cfg(test)]
mod tests {
    #[cfg(feature = "afserde")]
    #[test]
    fn seq_serde() {
        use super::Seq;
        use crate::seq;

        let original = seq!(1:2:1);
        let serd = match serde_json::to_string(&original) {
            Ok(serialized_str) => serialized_str,
            Err(e) => e.to_string(),
        };

        let deserd: Seq<i32> = serde_json::from_str(&serd).unwrap();
        assert_eq!(deserd, original);
    }
}