-
Notifications
You must be signed in to change notification settings - Fork 59
/
Copy pathseq.rs
62 lines (54 loc) · 1.28 KB
/
seq.rs
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
extern crate libc;
use crate::num::{One, Zero};
use std::default::Default;
use std::fmt;
/// Sequences are used for indexing Arrays
#[derive(Copy, Clone)]
#[repr(C)]
pub struct Seq<T> {
begin: T,
end: T,
step: T,
}
/// Default `Seq` spans all the elements along a dimension
impl<T: One + Zero> Default for Seq<T> {
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> fmt::Display for Seq<T> {
fn fmt(&self, f: &mut fmt::Formatter) -> fmt::Result {
write!(
f,
"[begin: {}, end: {}, step: {}]",
self.begin, self.end, self.step
)
}
}
impl<T: Copy> Seq<T> {
/// 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: begin,
end: end,
step: 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
}
}