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
use core::cmp::{Eq, Ord, Ordering, PartialEq, PartialOrd};

use alloc::string::String;
use alloc::vec::Vec;

#[cfg(feature = "std")]
use std::{
    ffi::{CStr, CString, OsStr, OsString},
    path::{Path, PathBuf},
};

use crate::{Cow, Dairy};

impl<'a, 'b, T, U> PartialEq<Cow<'b, U>> for Cow<'a, T>
where
    T: ?Sized + Dairy<'a> + PartialEq<U>,
    U: ?Sized + Dairy<'b>,
{
    #[inline]
    fn eq(&self, other: &Cow<'b, U>) -> bool {
        PartialEq::eq(&**self, &**other)
    }
}

impl<'a, T> Eq for Cow<'a, T> where T: ?Sized + Dairy<'a> + Eq {}

impl<'a, T> PartialOrd for Cow<'a, T>
where
    T: ?Sized + Dairy<'a> + PartialOrd,
{
    #[inline]
    fn partial_cmp(&self, other: &Cow<'a, T>) -> Option<Ordering> {
        PartialOrd::partial_cmp(&**self, &**other)
    }
}

impl<'a, T> Ord for Cow<'a, T>
where
    T: ?Sized + Dairy<'a> + Ord,
{
    #[inline]
    fn cmp(&self, other: &Self) -> Ordering {
        Ord::cmp(&**self, &**other)
    }
}

macro_rules! impl_basic {
    ($(
        $(#[$attrs:meta])*
        { $Ty:ty, $To:ty $(, { $($bound:tt)+ })? }
    )+) => {
        $(
            $(#[$attrs])*
            impl<'a $(, $($bound)+)?> PartialEq<$To> for Cow<'a, $Ty> {
                #[inline]
                fn eq(&self, other: &$To) -> bool {
                    PartialEq::eq(&**self, &*other)
                }
            }

            $(#[$attrs])*
            impl<'a $(, $($bound)+)?> PartialEq<&$To> for Cow<'a, $Ty> {
                #[inline]
                fn eq(&self, other: &&$To) -> bool {
                    PartialEq::eq(&**self, &**other)
                }
            }
        )+
    };
}

impl_basic! {
    ////////////////////////////////////////////////////////////////////////////
    // Cow<str>
    ////////////////////////////////////////////////////////////////////////////

    { str, str }

    { str, String }

    #[cfg(feature = "std")]
    { str, OsStr }

    #[cfg(feature = "std")]
    { str, OsString }

    ////////////////////////////////////////////////////////////////////////////
    // Cow<[T]>
    ////////////////////////////////////////////////////////////////////////////

    { [T], [U], { T: 'a + Clone + PartialEq<U>, U } }

    { [T], Vec<U>, { T: 'a + Clone + PartialEq<U>, U } }

    { [T], [U; N], { T: 'a + Clone + PartialEq<U>, U, const N: usize } }

    ////////////////////////////////////////////////////////////////////////////
    // Cow<CStr>
    ////////////////////////////////////////////////////////////////////////////

    #[cfg(feature = "std")]
    { CStr, CStr }

    #[cfg(feature = "std")]
    { CStr, CString }

    ////////////////////////////////////////////////////////////////////////////
    // Cow<OsStr>
    ////////////////////////////////////////////////////////////////////////////

    #[cfg(feature = "std")]
    { OsStr, OsStr }

    #[cfg(feature = "std")]
    { OsStr, OsString }

    #[cfg(feature = "std")]
    { OsStr, Path }

    #[cfg(feature = "std")]
    { OsStr, PathBuf }

    ////////////////////////////////////////////////////////////////////////////
    // Cow<Path>
    ////////////////////////////////////////////////////////////////////////////

    #[cfg(feature = "std")]
    { Path, Path }

    #[cfg(feature = "std")]
    { Path, PathBuf }

    #[cfg(feature = "std")]
    { Path, OsStr }

    #[cfg(feature = "std")]
    { Path, OsString }
}