dairy/imp/
mod.rs

1//! The underlying `Cow` implementations.
2
3mod compact;
4mod default;
5mod sealed;
6
7use alloc::borrow::ToOwned;
8
9/// Defines a `Cow` implementation.
10///
11/// This trait allows us to provide multiple `Cow` implementations.
12pub trait Cow<'a, T>: Clone
13where
14    T: ?Sized + ToOwned,
15{
16    fn borrowed(b: &'a T) -> Self;
17    fn owned(o: T::Owned) -> Self;
18    fn is_borrowed(&self) -> bool;
19    fn is_owned(&self) -> bool;
20    fn make_ref(&self) -> &T;
21    fn into_owned(self) -> T::Owned;
22    fn apply<F: FnOnce(&mut T::Owned)>(&mut self, f: F);
23}
24
25/// Internal trait which allows us to have different [`Cow`](crate::Cow)
26/// implementations for the same type across different platforms.
27///
28/// This is a *sealed* trait so only this crate can implement it.
29pub trait Dairy<'a>: ToOwned + sealed::Sealed {
30    type Cow: Cow<'a, Self>;
31}
32
33impl<'a> Dairy<'a> for str {
34    type Cow = compact::Cow<'a, Self>;
35}
36
37impl<'a, T: 'a + Clone> Dairy<'a> for [T] {
38    type Cow = compact::Cow<'a, Self>;
39}
40
41#[cfg(feature = "std")]
42impl<'a> Dairy<'a> for std::ffi::CStr {
43    type Cow = compact::Cow<'a, Self>;
44}
45
46#[cfg(all(feature = "std", os_str_ext))]
47impl<'a> Dairy<'a> for std::ffi::OsStr {
48    type Cow = compact::Cow<'a, Self>;
49}
50
51#[cfg(all(feature = "std", not(os_str_ext)))]
52impl<'a> Dairy<'a> for std::ffi::OsStr {
53    type Cow = default::Cow<'a, Self>;
54}
55
56#[cfg(all(feature = "std", os_str_ext))]
57impl<'a> Dairy<'a> for std::path::Path {
58    type Cow = compact::Cow<'a, Self>;
59}
60
61#[cfg(all(feature = "std", not(os_str_ext)))]
62impl<'a> Dairy<'a> for std::path::Path {
63    type Cow = default::Cow<'a, Self>;
64}