aabel_identifier_rs/
lib.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
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
91
92
93
94
95
96
97
98
99
100
101
102
103
104
105
106
107
108
109
110
111
112
113
114
115
116
117
118
119
120
121
122
123
124
125
126
127
128
129
130
131
132
133
134
135
136
137
138
139
140
141
142
143
144
145
146
147
148
149
150
151
152
153
154
155
156
157
158
159
160
161
162
163
164
165
166
167
168
169
170
171
172
173
174
175
176
177
178
179
180
181
182
183
184
185
186
187
188
189
190
191
192
193
194
195
196
197
198
199
200
201
202
203
204
205
206
207
208
209
210
211
212
213
214
215
216
217
218
219
220
221
/// Identity trait represents data which can be used
/// as identifiers. Given two identifiers we just need
/// to determine if they are equal (represent the same entity)
/// or not.
///
/// # Example
///
/// ```
/// use aabel_identifier_rs::*;
///
/// fn test_identifier(_id: impl Identifier) {
///     assert!(true);
/// }
///
/// let id = 10_u8;
/// test_identifier(id);
/// ```
pub trait Identifier: Eq {}
impl<T> Identifier for T where T: Eq {}

/// The PartialOrdIdentity trait represents data which can be
/// used for concurrent actions. Given two such identifiers, we
/// can tell if they are equal and we could potentially compare them.
pub trait PartialOrdIdentifier: Identifier + PartialOrd {}
impl<T> PartialOrdIdentifier for T where T: Eq + PartialOrd {}

/// The IntoIdentityIterator trait allows to create a sequence of
/// identifiers starting with a given identifier value and using a
/// given function which computes a new identifier.
///
/// # Example
///
/// ```
/// use aabel_identifier_rs::*;
///
/// let id = 10_u8;
/// let mut iter = id.into_ids_iterator(|id| id + 1);
/// assert_eq!(iter.next(), Some(10));
/// assert_eq!(iter.next(), Some(11));
/// ```
pub trait IntoIdentifierIterator {
    type Item: Identifier;

    fn into_ids_iterator<F>(self, next: F) -> impl Iterator<Item = Self::Item>
    where
        F: Fn(&Self::Item) -> Self::Item;
}

/// Internal implementation of the [`IntoIdentityIterator`] trait.
/// The iterator will return an [`Identifier`] instance. The identifier
/// type must also implement [`Copy`] trait.
struct IdentifierIterator<F, I>
where
    F: Fn(&I) -> I,
    I: Copy,
{
    current_id: I,
    get_next_id: F,
}

impl<F, I> Iterator for IdentifierIterator<F, I>
where
    F: Fn(&I) -> I,
    I: Copy,
{
    type Item = I;

    fn next(&mut self) -> Option<Self::Item> {
        let nxt = self.current_id;
        self.current_id = (self.get_next_id)(&self.current_id);
        Some(nxt)
    }
}

impl<T> IntoIdentifierIterator for T
where
    T: Copy + Identifier,
{
    type Item = Self;

    fn into_ids_iterator<F>(self, next: F) -> impl Iterator<Item = Self::Item>
    where
        F: Fn(&Self::Item) -> Self::Item,
    {
        IdentifierIterator {
            current_id: self,
            get_next_id: next,
        }
    }
}

#[cfg(test)]
mod tests {
    use super::*;

    fn test_identifier(_id: impl Identifier) {
        assert!(true);
    }

    fn test_partialord_identifier(_id: impl PartialOrdIdentifier) {
        assert!(true);
    }

    #[test]
    fn identifier_u8() {
        let id = 10_u8;
        test_identifier(id)
    }

    #[test]
    fn identifier_u64() {
        let id = 10_u64;
        test_identifier(id)
    }

    #[test]
    fn identifier_str() {
        let id = "test";
        test_identifier(id)
    }

    #[test]
    fn identifier_arr() {
        let id = [1, 2, 3];
        test_identifier(id)
    }

    #[test]
    fn identifier_vec() {
        let id = vec![1, 2, 3];
        test_identifier(id)
    }

    #[test]
    fn identifier_eq() {
        fn cmp_identifiers<I>(a: I, b: I) -> bool
        where
            I: Identifier,
        {
            a == b
        }

        assert!(cmp_identifiers(10_u16, 10_u16));
        assert!(!cmp_identifiers(10_u16, 20_u16));
    }

    #[test]
    fn partialord_identifier_u8() {
        let id = 10_u8;
        test_partialord_identifier(id)
    }

    #[test]
    fn partialord_identifier_u64() {
        let id = 10_u64;
        test_partialord_identifier(id)
    }

    #[test]
    fn partialord_identity_str() {
        let id = "test";
        test_partialord_identifier(id)
    }

    #[test]
    fn partialord_identifier_arr() {
        let id = [1, 2, 3];
        test_partialord_identifier(id)
    }

    #[test]
    fn partialord_identifier_vec() {
        let id = vec![1, 2, 3];
        test_partialord_identifier(id)
    }

    #[test]
    fn partialord_idenitifier_cmp() {
        fn cmp_identifiers<I>(a: I, b: &I) -> Option<std::cmp::Ordering>
        where
            I: PartialOrdIdentifier,
        {
            a.partial_cmp(b)
        }

        assert!(cmp_identifiers(10_u8, &10_u8).is_some());
        assert!(cmp_identifiers("az", &"za").is_some());
    }

    #[test]
    fn iter_u8() {
        let id = 10_u8;
        let mut iter = id.into_ids_iterator(|id| id + 1);
        assert_eq!(iter.next(), Some(10));
        assert_eq!(iter.next(), Some(11));
    }

    #[test]
    fn iter_u64() {
        let id = 10_u64;
        let mut iter = id.into_ids_iterator(|id| id + 1);
        assert_eq!(iter.next(), Some(10));
        assert_eq!(iter.next(), Some(11));
    }

    #[test]
    fn iter_str() {
        let id = "a";
        let mut iter = id.into_ids_iterator(|id| id);
        assert_eq!(iter.next(), Some("a"));
        assert_eq!(iter.next(), Some("a"));
    }

    #[test]
    fn iter_string() {
        let id = "a".to_string();
        let mut iter = id.into_ids_iterator(|id| id);
        assert_eq!(iter.next(), Some(&"a".into()));
        assert_eq!(iter.next(), Some(&"a".into()));
    }
}