non_empty_slice/
cow.rs

1//! Non-empty [`Cow<'_, [T]>`](Cow).
2
3#[cfg(not(any(feature = "std", feature = "alloc")))]
4compile_error!("expected either `std` or `alloc` to be enabled");
5
6#[cfg(feature = "std")]
7use std::borrow::Cow;
8
9#[cfg(all(not(feature = "std"), feature = "alloc"))]
10use alloc::borrow::Cow;
11
12use crate::{boxed::NonEmptyBoxedSlice, slice::NonEmptySlice, vec::NonEmptyVec};
13
14/// Represents non-empty clone-on-write slices, [`Cow<'a, NonEmptySlice<T>>`](Cow).
15pub type NonEmptyCowSlice<'a, T> = Cow<'a, NonEmptySlice<T>>;
16
17impl<T: Clone> From<NonEmptyCowSlice<'_, T>> for NonEmptyVec<T> {
18    fn from(non_empty: NonEmptyCowSlice<'_, T>) -> Self {
19        non_empty.into_owned()
20    }
21}
22
23impl<T: Clone> From<NonEmptyCowSlice<'_, T>> for NonEmptyBoxedSlice<T> {
24    fn from(non_empty: NonEmptyCowSlice<'_, T>) -> Self {
25        non_empty.into_owned().into_non_empty_boxed_slice()
26    }
27}
28
29impl<'a, T: Clone> From<&'a NonEmptySlice<T>> for NonEmptyCowSlice<'a, T> {
30    fn from(non_empty: &'a NonEmptySlice<T>) -> Self {
31        Self::Borrowed(non_empty)
32    }
33}
34
35impl<T: Clone> From<NonEmptyVec<T>> for NonEmptyCowSlice<'_, T> {
36    fn from(non_empty: NonEmptyVec<T>) -> Self {
37        Self::Owned(non_empty)
38    }
39}
40
41impl<'a, T: Clone> From<&'a NonEmptyVec<T>> for NonEmptyCowSlice<'a, T> {
42    fn from(non_empty: &'a NonEmptyVec<T>) -> Self {
43        Self::Borrowed(non_empty.as_non_empty_slice())
44    }
45}