shared_vec/impls/
convert.rs

1use crate::{Counter, String, Vec};
2use alloc::borrow::Cow;
3use alloc::boxed::Box;
4
5impl<C: Counter<usize>> From<Box<str>> for String<C> {
6    /// This takes ownership of `s` and creates a `String` without copying.
7    #[inline]
8    fn from(s: Box<str>) -> Self {
9        Self::from_boxed_str(s)
10    }
11}
12impl<C: Counter<usize>> From<&str> for String<C> {
13    /// The resulting `String` will contain a copy of `s` on the heap.
14    #[inline]
15    fn from(s: &str) -> Self {
16        Self::from_boxed_str(s.into())
17    }
18}
19impl<C: Counter<usize>> From<&mut str> for String<C> {
20    /// The resulting `String` will contain a copy of `s` on the heap.
21    #[inline]
22    fn from(s: &mut str) -> Self {
23        Self::from_boxed_str(s.into())
24    }
25}
26impl<C1: Counter<usize>, C2: Counter<usize>> From<&String<C2>> for String<C1> {
27    /// This creates a `String` with counter `C1` containing a copy of the data in `s` on the heap.
28    #[inline]
29    fn from(s: &String<C2>) -> Self {
30        Self::from_boxed_str(s.as_str().into())
31    }
32}
33impl<C: Counter<usize>> From<&alloc::string::String> for String<C> {
34    /// The resulting `String` will contain a copy of `s` on the heap.
35    #[inline]
36    fn from(s: &alloc::string::String) -> Self {
37        Self::from(s.as_str())
38    }
39}
40impl<C: Counter<usize>> From<alloc::string::String> for String<C> {
41    /// This takes ownership of `s`, shrinks it with [`alloc::string::String::into_boxed_str`],
42    /// and creates a `String` without copying.
43    #[inline]
44    fn from(s: alloc::string::String) -> Self {
45        Self::from_boxed_str(s.into_boxed_str())
46    }
47}
48impl<C: Counter<usize>> From<&String<C>> for alloc::string::String {
49    /// This creates an owned `alloc::string::String` by copying the data from `s`.
50    #[inline]
51    fn from(s: &String<C>) -> Self {
52        alloc::string::String::from(s.as_str())
53    }
54}
55impl<C: Counter<usize>> From<String<C>> for alloc::string::String {
56    /// This creates an owned `alloc::string::String` by copying the data from `s`.
57    #[inline]
58    fn from(s: String<C>) -> Self {
59        alloc::string::String::from(s.as_str())
60    }
61}
62
63impl<C: Counter<usize>> From<Cow<'_, str>> for String<C> {
64    /// This creates a `String` from either a borrowed or owned string.
65    ///
66    /// - If the `Cow` is borrowed, it will copy the data to the heap.
67    /// - If the `Cow` is owned, it will take ownership of the data without copying.
68    #[inline]
69    fn from(s: Cow<'_, str>) -> Self {
70        match s {
71            Cow::Borrowed(s) => Self::from_boxed_str(s.into()),
72            Cow::Owned(s) => Self::from_boxed_str(s.into_boxed_str()),
73        }
74    }
75}
76
77impl<C: Counter<usize>, T> From<Box<[T]>> for Vec<C, T> {
78    #[inline]
79    fn from(data: Box<[T]>) -> Self {
80        Self::from_boxed_slice(data)
81    }
82}