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
139
140
141
142
143
144
145
146
147
148
149
150
151
152
153
154
155
156
157
158
159
160
161
162
163
164
165
166
167
168
169
170
171
172
173
174
175
176
177
178
179
180
181
182
183
184
185
186
187
188
189
190
191
192
193
194
195
196
197
198
199
200
201
202
203
204
205
206
207
208
209
210
211
212
213
214
215
216
217
218
219
220
221
222
223
224
225
226
227
228
229
230
231
232
233
234
235
236
237
238
239
240
241
242
243
244
245
246
247
248
249
250
251
252
253
254
255
256
257
258
259
260
261
262
263
264
265
266
267
268
269
270
271
272
273
274
275
276
277
278
279
280
281
282
283
284
285
286
287
288
289
290
291
292
293
294
295
296
297
298
299
300
301
302
303
304
305
306
307
308
309
310
311
312
313
314
315
316
317
318
319
320
321
322
323
324
325
326
327
328
329
330
331
332
333
334
335
336
337
338
339
340
341
342
343
344
345
346
347
348
349
350
351
352
353
354
355
356
357
358
359
360
361
362
363
364
365
366
367
368
369
//! A more compact, user friendly clone-on-write smart pointer.
//!
//! ```
//! use dairy::Cow;
//! let borrowed: Cow<str> = Cow::borrowed("Hello World!");
//! let owned: Cow<str> = Cow::owned(String::from("Hello World!"));
//! ```
//!
//! [`dairy::Cow`][Cow] is an improved version of the standard library
//! [`std::borrow::Cow`]. Depending on the platform and type this crate provides
//! a better underlying implementation which will be more compact. This crate
//! currently supports the following types: [`str`], [`[T]`][slice], [`CStr`],
//! [`OsStr`], and [`Path`].
//!
//! [`dairy::Cow`][Cow] is also able to provide many more [`From`]
//! implementations; some which are not possible for the standard library to
//! provide due to the `alloc`, `std` split. For example `Cow<Path>` now has
//! the useful `From<&str>` implementation.
//!
//! ### Underlying implementation
//!
//! - On 64-bit platforms the *compact* implementation of [`Cow`] is two words
//!   wide, storing the length, capacity, and the ownership tag in the same
//!   word.
//! - On 32-bit platforms the *compact* implementation of [`Cow`] is three words
//!   wide, storing the capacity and the ownership tag in the same word.
//! - The **default** implementation simply uses the the standard library
//!   implementation which is four words wide. This is typically required in
//!   cases where the standard library does not provide an `.into_raw_parts()`
//!   or equivalent method for the owned version of types.
//!
//! The following table documents how `Cow<T>` is implemented for each type on
//! depending on the platform.
//!
//! | `Cow<T>`     | Unix/WASI | Other          |
//! | ------------ | --------- | -------------- |
//! | `Cow<str>`   | *compact* | *compact*      |
//! | `Cow<[T]>`   | *compact* | *compact*      |
//! | `Cow<CStr>`  | *compact* | *compact*      |
//! | `Cow<OsStr>` | *compact* | **default**    |
//! | `Cow<Path>`  | *compact* | **default**    |

#![no_std]
#![warn(unsafe_op_in_unsafe_fn)]

extern crate alloc;

#[cfg(feature = "std")]
extern crate std;

mod as_ref;
mod cmp;
mod extend;
mod from;
mod from_iter;
mod from_str;
mod imp;
mod serde;
mod to_boxed;

use core::borrow::Borrow;
use core::fmt;
use core::hash::{Hash, Hasher};
use core::marker::Unpin;
use core::ops::Deref;

use alloc::boxed::Box;

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

use crate::imp::Cow as _;
pub use crate::imp::Dairy;
pub use crate::to_boxed::ToBoxed;

/// Convenient type alias for a clone-on-write [`str`].
pub type String<'a> = Cow<'a, str>;

/// Convenient type alias for a clone-on-write [`[T]`][slice].
pub type Vec<'a, T> = Cow<'a, [T]>;

/// Convenient type alias for a clone-on-write [`CStr`].
#[cfg(feature = "std")]
pub type CString<'a> = Cow<'a, CStr>;

/// Convenient type alias for a clone-on-write [`OsStr`].
#[cfg(feature = "std")]
pub type OsString<'a> = Cow<'a, OsStr>;

/// Convenient type alias for a clone-on-write [`Path`].
#[cfg(feature = "std")]
pub type PathBuf<'a> = Cow<'a, Path>;

/// A clone-on-write smart pointer.
///
/// The type `Cow` is a smart pointer providing clone-on-write functionality: it
/// can enclose and provide immutable access to borrowed data, and clone the
/// data lazily when mutation or ownership is required.
///
/// `Cow` implements [`Deref`], which means that you can call non-mutating
/// methods directly on the data it encloses.
pub struct Cow<'a, T>
where
    T: ?Sized + Dairy<'a>,
{
    inner: T::Cow,
}

impl<'a, T> Cow<'a, T>
where
    T: ?Sized + Dairy<'a>,
{
    /// Construct from borrowed data.
    ///
    /// # Examples
    ///
    /// ```
    /// use dairy::Cow;
    /// let cow: Cow<str> = Cow::borrowed("moo");
    /// ```
    #[inline]
    pub fn borrowed(b: &'a T) -> Self {
        Self {
            inner: T::Cow::borrowed(b),
        }
    }

    /// Construct from owned data.
    ///
    /// # Examples
    ///
    /// ```
    /// use dairy::Cow;
    /// let cow: Cow<str> = Cow::owned(String::from("moo"));
    /// ```
    #[inline]
    pub fn owned(o: T::Owned) -> Self {
        Self {
            inner: T::Cow::owned(o),
        }
    }

    /// Returns true if the data is borrowed.
    ///
    /// # Examples
    ///
    /// ```
    /// use dairy::Cow;
    ///
    /// let cow: Cow<str> = Cow::borrowed("moo");
    /// assert!(cow.is_borrowed());
    ///
    /// let cow: Cow<str> = Cow::owned(String::from("...moo?"));
    /// assert!(!cow.is_borrowed());
    /// ```
    #[inline]
    pub fn is_borrowed(&self) -> bool {
        self.inner.is_borrowed()
    }

    /// Returns true if the data is owned.
    ///
    /// # Examples
    ///
    /// ```
    /// use dairy::Cow;
    ///
    /// let cow: Cow<str> = Cow::owned(String::from("moo"));
    /// assert!(cow.is_owned());
    ///
    /// let cow: Cow<str> = Cow::borrowed("...moo?");
    /// assert!(!cow.is_owned());
    /// ```
    #[inline]
    pub fn is_owned(&self) -> bool {
        self.inner.is_owned()
    }

    #[inline]
    fn make_ref(&self) -> &T {
        self.inner.make_ref()
    }

    /// Converts into owned data.
    ///
    /// Clones the data if it is not already owned.
    ///
    /// # Examples
    ///
    /// Calling `.into_owned()` on a borrowed `Cow` clones the underlying data.
    ///
    /// ```
    /// use dairy::Cow;
    ///
    /// let cow = Cow::borrowed("Moo!");
    /// assert_eq!(cow.into_owned(), String::from("Moo!"));
    /// ```
    ///
    /// Calling `.into_owned()` on an owned `Cow` is a noop.
    ///
    /// ```
    /// use dairy::Cow;
    ///
    /// let cow: Cow<str> = Cow::owned(String::from("Moo!"));
    /// assert_eq!(cow.into_owned(), String::from("Moo!"));
    /// ```
    #[inline]
    pub fn into_owned(self) -> T::Owned {
        self.inner.into_owned()
    }

    /// Converts into boxed data.
    ///
    /// Clones the data if it is not already owned.
    ///
    /// # Examples
    ///
    /// Calling `.into_boxed()` on a borrowed `Cow` clones the underlying data.
    ///
    /// ```
    /// use dairy::Cow;
    ///
    /// let cow = Cow::borrowed("Moo!");
    /// assert_eq!(cow.into_boxed(), String::from("Moo!").into_boxed_str());
    /// ```
    ///
    /// Calling `.into_boxed()` on an owned `Cow` is a noop.
    ///
    /// ```
    /// use dairy::Cow;
    ///
    /// let cow: Cow<str> = Cow::owned(String::from("Moo!"));
    /// assert_eq!(cow.into_boxed(), String::from("Moo!").into_boxed_str());
    /// ```
    #[inline]
    pub fn into_boxed(self) -> Box<T>
    where
        T: ToBoxed,
    {
        T::to_boxed(self.into_owned())
    }

    /// Applies the given function to the owned data.
    ///
    /// Clones the data if it is not already owned. This is useful because the
    /// compact implementation is not able to provide a `.to_mut()` method like
    /// the standard library. This function allows you to modify the `Cow`
    /// without [moving] it.
    ///
    /// # Examples
    ///
    /// ```
    /// use dairy::Cow;
    ///
    /// let mut cow = Cow::borrowed("Moo!");
    /// cow.apply(|s| s.make_ascii_uppercase());
    /// assert_eq!(cow, "MOO!");
    /// ```
    ///
    /// [moving]: https://doc.rust-lang.org/book/ch04-01-what-is-ownership.html?#ways-variables-and-data-interact-move
    #[inline]
    pub fn apply<F>(&mut self, f: F)
    where
        F: FnOnce(&mut T::Owned),
    {
        self.inner.apply(f)
    }
}

impl<'a, T> Deref for Cow<'a, T>
where
    T: ?Sized + Dairy<'a>,
{
    type Target = T;

    #[inline]
    fn deref(&self) -> &T {
        self.make_ref()
    }
}

impl<'a, T> Borrow<T> for Cow<'a, T>
where
    T: ?Sized + Dairy<'a>,
{
    #[inline]
    fn borrow(&self) -> &T {
        self.make_ref()
    }
}

impl<'a, T> Clone for Cow<'a, T>
where
    T: ?Sized + Dairy<'a>,
{
    #[inline]
    fn clone(&self) -> Self {
        Self {
            inner: self.inner.clone(),
        }
    }
}

impl<'a, T> fmt::Debug for Cow<'a, T>
where
    T: ?Sized + Dairy<'a> + fmt::Debug,
    T::Owned: fmt::Debug,
{
    #[inline]
    fn fmt(&self, f: &mut fmt::Formatter<'_>) -> fmt::Result {
        fmt::Debug::fmt(&**self, f)
    }
}

impl<'a, T> fmt::Display for Cow<'a, T>
where
    T: ?Sized + Dairy<'a> + fmt::Display,
    T::Owned: fmt::Display,
{
    #[inline]
    fn fmt(&self, f: &mut fmt::Formatter<'_>) -> fmt::Result {
        fmt::Display::fmt(&**self, f)
    }
}

impl<'a, T> Default for Cow<'a, T>
where
    T: ?Sized + Dairy<'a>,
    T::Owned: Default,
{
    #[inline]
    fn default() -> Self {
        Self::owned(T::Owned::default())
    }
}

impl<'a, T> Hash for Cow<'a, T>
where
    T: ?Sized + Dairy<'a> + Hash,
{
    #[inline]
    fn hash<H: Hasher>(&self, state: &mut H) {
        Hash::hash(&**self, state)
    }
}

unsafe impl<'a, T> Send for Cow<'a, T>
where
    T: ?Sized + Dairy<'a> + Sync,
    T::Owned: Send,
{
}

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

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