iter_trait/
refs.rs

1//! Marker traits that help ensure that iterators are implemented properly.
2
3/// Marker that states that `Self` can be obtained from the reference `&'a T`.
4pub trait Ref<'a, T> {
5    /// Performs the conversion, showing that it is possible.
6    fn convert(val: &'a T) -> Self;
7}
8impl<'a, T> Ref<'a, T> for &'a T {
9    fn convert(val: &'a T) -> &'a T {
10        val
11    }
12}
13impl<'a, T: Copy> Ref<'a, T> for T {
14    fn convert(val: &'a T) -> T {
15        *val
16    }
17}
18impl<'a, K, V> Ref<'a, (K, V)> for (&'a K, &'a V) {
19    fn convert(val: &'a (K, V)) -> (&'a K, &'a V) {
20        let (ref k, ref v) = *val;
21        (k, v)
22    }
23}
24impl<'a, K: Copy, V> Ref<'a, (K, V)> for (K, &'a V) {
25    fn convert(val: &'a (K, V)) -> (K, &'a V) {
26        let (k, ref v) = *val;
27        (k, v)
28    }
29}
30
31/// Marker that states that `Self` can be obtained from the reference `&'a mut T`.
32pub trait RefMut<'a, T> {
33    /// Performs the conversion, showing that it is possible.
34    fn convert(val: &'a mut T) -> Self;
35}
36impl<'a, T> RefMut<'a, T> for &'a mut T {
37    fn convert(val: &'a mut T) -> &'a mut T {
38        val
39    }
40}
41impl<'a, K, V> RefMut<'a, (K, V)> for (&'a K, &'a mut V) {
42    fn convert(val: &'a mut (K, V)) -> (&'a K, &'a mut V) {
43        let (ref key, ref mut val) = *val;
44        (key, val)
45    }
46}
47impl<'a, K: Copy, V> RefMut<'a, (K, V)> for (K, &'a mut V) {
48    fn convert(val: &'a mut (K, V)) -> (K, &'a mut V) {
49        (val.0, &mut val.1)
50    }
51}