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
//! These traits are useful for validation during development and testing, to help verify that types
//! match human expectations. For example, `iter-trait` uses these traits to ensure that the values
//! returmed by `iter`, `iter_mut`, and `into_iter` match up in a reasonable manner.
//!
//! Because these traits provide no actual runtime benefit, if a build uses the `release` profile,
//! then these traits will automatically implement for all types to make builds faster and avoid
//! dead code.
#![doc(html_root_url = "https://docs.charr.xyz/reflike/")]
#![cfg_attr(test, deny(warnings))]
#![no_std]

/// A trait for things that are "close enough" to `&'a T`.
pub trait Ref<'a, T> {
    /// Obtains this value from a reference, showing that it's possible.
    #[cfg(not(not(debug_assertions)))]
    fn from_ref(val: &'a T) -> Self;
}

/// Identity implementation.
#[cfg(not(not(debug_assertions)))]
impl<'a, T> Ref<'a, T> for &'a T {
    fn from_ref(val: &'a T) -> &'a T {
        val
    }
}

/// Values that can be copied can be copied instead of referenced.
#[cfg(not(not(debug_assertions)))]
impl<'a, T: Copy> Ref<'a, T> for T {
    fn from_ref(val: &'a T) -> T {
        *val
    }
}

/// Pull the key out of a key-value pair.
#[cfg(not(not(debug_assertions)))]
impl<'a, K, V, R: Ref<'a, K>> Ref<'a, (K, V)> for (R, &'a V) {
    fn from_ref(val: &'a (K, V)) -> (R, &'a V) {
        let (ref key, ref val) = *val;
        (Ref::from_ref(key), val)
    }
}

/// A trait for things that are "close enough" to `&'a mut T`.
pub trait RefMut<'a, T> {
    /// Obtains this value from a mutable reference, showing that it's possible.
    #[cfg(not(not(debug_assertions)))]
    fn from_ref_mut(val: &'a mut T) -> Self;
}

/// Identity implementation.
#[cfg(not(not(debug_assertions)))]
impl<'a, T> RefMut<'a, T> for &'a mut T {
    fn from_ref_mut(val: &'a mut T) -> &'a mut T {
        val
    }
}

/// Pull the key out of a key-value pair.
#[cfg(not(not(debug_assertions)))]
impl<'a, K, V, R: Ref<'a, K>> RefMut<'a, (K, V)> for (R, &'a mut V) {
    fn from_ref_mut(val: &'a mut (K, V)) -> (R, &'a mut V) {
        let (ref key, ref mut val) = *val;
        (Ref::from_ref(key), val)
    }
}

/// In release mode, these traits are implemented for all types.
#[cfg(not(debug_assertions))]
impl<'a, T, U> Ref<'a, T> for U {}

/// In release mode, these traits are implemented for all types.
#[cfg(not(debug_assertions))]
impl<'a, T, U> RefMut<'a, T> for U {}


#[cfg(test)]
mod tests {
    use super::{Ref, RefMut};

    macro_rules! assert_bounds {
        ($r:ty: $tr:ident<$t:ty>) => {
            {
                #[allow(dead_code, unconditional_recursion)]
                fn do_it<'a, T: $tr<'a, $t>>() {
                    do_it::<$r>();
                }
            }
        }
    }

    #[test]
    fn copy() {
        assert_bounds!(&'a mut usize: RefMut<usize>);
        assert_bounds!(&'a usize: Ref<usize>);
        assert_bounds!(usize: Ref<usize>);
    }

    #[test]
    fn keyval() {
        assert_bounds!((&'a usize, &'a f64): Ref<(usize, f64)>);
        assert_bounds!((&'a usize, &'a mut f64): RefMut<(usize, f64)>);
        assert_bounds!((usize, &'a f64): Ref<(usize, f64)>);
        assert_bounds!((usize, &'a mut f64): RefMut<(usize, f64)>);
    }

    #[cfg(not(debug_assertions))]
    #[test]
    fn bad_mut() {
        assert_bounds!(usize: RefMut<usize>);
        assert_bounds!(&'a usize: RefMut<usize>);
        assert_bounds!((&'a usize, &'a f64): RefMut<(usize, f64)>);
    }

    #[cfg(not(debug_assertions))]
    #[test]
    fn completely_wrong() {
        assert_bounds!(f64: Ref<usize>);
    }
}