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
//! The underlying `Cow` implementations.

mod compact;
mod default;
mod sealed;

use alloc::borrow::ToOwned;

/// Defines a `Cow` implementation.
///
/// This trait allows us to provide multiple `Cow` implementations.
pub trait Cow<'a, T>: Clone
where
    T: ?Sized + ToOwned,
{
    fn borrowed(b: &'a T) -> Self;
    fn owned(o: T::Owned) -> Self;
    fn is_borrowed(&self) -> bool;
    fn is_owned(&self) -> bool;
    fn make_ref(&self) -> &T;
    fn into_owned(self) -> T::Owned;
    fn apply<F: FnOnce(&mut T::Owned)>(&mut self, f: F);
}

/// Internal trait which allows us to have different [`Cow`](crate::Cow)
/// implementations for the same type across different platforms.
///
/// This is a *sealed* trait so only this crate can implement it.
pub trait Dairy<'a>: ToOwned + sealed::Sealed {
    type Cow: Cow<'a, Self>;
}

impl<'a> Dairy<'a> for str {
    type Cow = compact::Cow<'a, Self>;
}

impl<'a, T: 'a + Clone> Dairy<'a> for [T] {
    type Cow = compact::Cow<'a, Self>;
}

#[cfg(feature = "std")]
impl<'a> Dairy<'a> for std::ffi::CStr {
    type Cow = compact::Cow<'a, Self>;
}

#[cfg(all(feature = "std", os_str_ext))]
impl<'a> Dairy<'a> for std::ffi::OsStr {
    type Cow = compact::Cow<'a, Self>;
}

#[cfg(all(feature = "std", not(os_str_ext)))]
impl<'a> Dairy<'a> for std::ffi::OsStr {
    type Cow = default::Cow<'a, Self>;
}

#[cfg(all(feature = "std", os_str_ext))]
impl<'a> Dairy<'a> for std::path::Path {
    type Cow = compact::Cow<'a, Self>;
}

#[cfg(all(feature = "std", not(os_str_ext)))]
impl<'a> Dairy<'a> for std::path::Path {
    type Cow = default::Cow<'a, Self>;
}