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
use crate::Pred;
use std::borrow::Borrow;
use std::cmp::Ordering;
use std::fmt::{Debug, Display, Formatter, Result as FmtResult};
use std::hash::Hash;
use std::hash::Hasher;
std_prelude!();

#[derive(Clone, Copy)]
/// A reference to a value that is accepted by a given predicate.
pub struct PetRef<'a, T: ?Sized, P>(&'a T, P);

impl<'a, T: ?Sized, P: Pred<T>> PetRef<'a, T, P> {
    /// Attempts to create a new `PetRef<'a, T, P>` from a reference `t`.
    ///
    /// If `P` accepts `t`, then the result is a `Some(..)` referring to `t`.
    /// Otherwise, `None` is returned.
    pub fn new(t: &'a T) -> Option<Self> {
        if P::accept(t) {
            Some(Self(t, P::default()))
        } else {
            None
        }
    }

    /// Creates a new `PetRef<'a, T, P>` from a reference `t` without checking if `P` accepts `t`.
    pub unsafe fn new_unchecked(t: &'a T) -> Self {
        Self(t, P::default())
    }

    /// Retrieves the reference contained in this `PetRef<'a, T, P>`.
    pub fn value(&self) -> &'a T {
        self.0
    }
}

impl<'a, T: ?Sized, P> AsRef<T> for PetRef<'a, T, P> {
    fn as_ref(&self) -> &T {
        self.0
    }
}
impl<'a, T: ?Sized, P> Borrow<T> for PetRef<'a, T, P> {
    fn borrow(&self) -> &T {
        self.0
    }
}

impl<'a, 'b, T: ?Sized, P, Q> PartialEq<PetRef<'b, T, Q>> for PetRef<'a, T, P>
where
    T: PartialEq,
{
    fn eq(&self, o: &PetRef<'b, T, Q>) -> bool {
        self.0.eq(&o.0)
    }
    fn ne(&self, o: &PetRef<'b, T, Q>) -> bool {
        self.0.ne(&o.0)
    }
}

impl<'a, 'b, T: ?Sized, P, Q> PartialOrd<PetRef<'b, T, Q>> for PetRef<'a, T, P>
where
    T: PartialOrd,
{
    fn partial_cmp(&self, o: &PetRef<'b, T, Q>) -> Option<Ordering> {
        self.0.partial_cmp(&o.0)
    }
    fn lt(&self, o: &PetRef<'b, T, Q>) -> bool {
        self.0.lt(&o.0)
    }
    fn le(&self, o: &PetRef<'b, T, Q>) -> bool {
        self.0.le(&o.0)
    }
    fn gt(&self, o: &PetRef<'b, T, Q>) -> bool {
        self.0.gt(&o.0)
    }
    fn ge(&self, o: &PetRef<'b, T, Q>) -> bool {
        self.0.ge(&o.0)
    }
}

impl<'a, T: ?Sized, P> Eq for PetRef<'a, T, P> where T: Eq {}
impl<'a, T: ?Sized, P> Ord for PetRef<'a, T, P>
where
    T: Ord,
{
    fn cmp(&self, o: &Self) -> Ordering {
        self.0.cmp(&o.0)
    }
}

impl<'a, T, P> Hash for PetRef<'a, T, P>
where
    T: Hash,
{
    fn hash<H>(&self, state: &mut H)
    where
        H: Hasher,
    {
        self.0.hash(state)
    }
}

impl<'a, T: ?Sized, P> Debug for PetRef<'a, T, P>
where
    T: Debug,
    P: Debug,
{
    fn fmt(&self, f: &mut Formatter) -> FmtResult {
        write!(f, "PetRef<{:?}>({:?})", self.1, self.0)
    }
}

impl<'a, T, P> Display for PetRef<'a, T, P>
where
    T: Display,
{
    fn fmt(&self, f: &mut Formatter) -> FmtResult {
        self.0.fmt(f)
    }
}