spinoso_string/enc/utf8/borrowed/
impls.rs

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
use alloc::borrow::Cow;
use alloc::boxed::Box;
use core::array::TryFromSliceError;
use core::fmt;
use core::hash::{Hash, Hasher};
use core::ops::{Index, IndexMut};
use core::slice::SliceIndex;
use core::str;

use super::Utf8Str;

impl Default for &'_ Utf8Str {
    #[inline]
    fn default() -> Self {
        Utf8Str::empty()
    }
}

impl Hash for Utf8Str {
    fn hash<H: Hasher>(&self, hasher: &mut H) {
        self.as_bytes().hash(hasher);
    }
}

impl fmt::Debug for Utf8Str {
    fn fmt(&self, f: &mut fmt::Formatter<'_>) -> fmt::Result {
        use bstr::ByteSlice;

        f.debug_struct("Utf8Str")
            .field("bytes", &self.as_bytes().as_bstr())
            .finish()
    }
}

impl<I: SliceIndex<[u8]>> Index<I> for Utf8Str {
    type Output = I::Output;

    #[inline]
    fn index(&self, index: I) -> &Self::Output {
        Index::index(self.as_bytes(), index)
    }
}

impl<I: SliceIndex<[u8]>> IndexMut<I> for Utf8Str {
    #[inline]
    fn index_mut(&mut self, index: I) -> &mut Self::Output {
        IndexMut::index_mut(self.as_bytes_mut(), index)
    }
}

impl AsRef<[u8]> for Utf8Str {
    #[inline]
    fn as_ref(&self) -> &[u8] {
        self.as_bytes()
    }
}

impl AsMut<[u8]> for Utf8Str {
    #[inline]
    fn as_mut(&mut self) -> &mut [u8] {
        self.as_bytes_mut()
    }
}

impl AsRef<Utf8Str> for Utf8Str {
    #[inline]
    fn as_ref(&self) -> &Utf8Str {
        self
    }
}

impl AsMut<Utf8Str> for Utf8Str {
    #[inline]
    fn as_mut(&mut self) -> &mut Utf8Str {
        self
    }
}

impl<'a, const N: usize> From<&'a [u8; N]> for &'a Utf8Str {
    #[inline]
    fn from(content: &'a [u8; N]) -> Self {
        Utf8Str::new(content)
    }
}

impl<'a, const N: usize> From<&'a mut [u8; N]> for &'a mut Utf8Str {
    #[inline]
    fn from(content: &'a mut [u8; N]) -> Self {
        Utf8Str::new_mut(content)
    }
}

impl<'a> From<&'a [u8]> for &'a Utf8Str {
    #[inline]
    fn from(content: &'a [u8]) -> Self {
        Utf8Str::new(content)
    }
}

impl<'a> From<&'a mut [u8]> for &'a mut Utf8Str {
    #[inline]
    fn from(content: &'a mut [u8]) -> Self {
        Utf8Str::new_mut(content)
    }
}

impl<'a> From<&'a str> for &'a Utf8Str {
    #[inline]
    fn from(s: &'a str) -> Self {
        Utf8Str::new(s)
    }
}

impl<'a> From<&'a Utf8Str> for &'a [u8] {
    #[inline]
    fn from(content: &'a Utf8Str) -> Self {
        content.as_bytes()
    }
}

impl<'a> From<&'a mut Utf8Str> for &'a mut [u8] {
    #[inline]
    fn from(content: &'a mut Utf8Str) -> Self {
        content.as_bytes_mut()
    }
}

impl<'a, const N: usize> TryFrom<&'a Utf8Str> for &'a [u8; N] {
    type Error = &'a Utf8Str;

    #[inline]
    fn try_from(content: &'a Utf8Str) -> Result<Self, Self::Error> {
        if let Ok(arr) = content.as_bytes().try_into() {
            Ok(arr)
        } else {
            Err(content)
        }
    }
}

impl<'a, const N: usize> TryFrom<&'a mut Utf8Str> for &'a mut [u8; N] {
    type Error = TryFromSliceError;

    #[inline]
    fn try_from(content: &'a mut Utf8Str) -> Result<Self, Self::Error> {
        content.as_bytes_mut().try_into()
    }
}

impl<'a> From<&'a Utf8Str> for Cow<'a, [u8]> {
    #[inline]
    fn from(content: &'a Utf8Str) -> Self {
        Cow::Borrowed(content.as_bytes())
    }
}

impl<'a> TryFrom<&'a Utf8Str> for &'a str {
    type Error = &'a Utf8Str;

    #[inline]
    fn try_from(s: &'a Utf8Str) -> Result<Self, Self::Error> {
        if s.is_valid_encoding() {
            let slice = s.as_bytes();
            // SAFETY: `is_valid_encoding` only returns true if the byte
            // content of the `&Utf8Str` is valid UTF-8.
            let s = unsafe { str::from_utf8_unchecked(slice) };
            Ok(s)
        } else {
            Err(s)
        }
    }
}

impl<'a> TryFrom<&'a Utf8Str> for Cow<'a, str> {
    type Error = &'a Utf8Str;

    #[inline]
    fn try_from(content: &'a Utf8Str) -> Result<Self, Self::Error> {
        let s = content.try_into()?;
        Ok(Cow::Borrowed(s))
    }
}

impl<'a> From<&'a Utf8Str> for Cow<'a, Utf8Str> {
    #[inline]
    fn from(content: &'a Utf8Str) -> Self {
        Cow::Borrowed(content)
    }
}

impl From<Box<[u8]>> for Box<Utf8Str> {
    #[inline]
    fn from(s: Box<[u8]>) -> Self {
        Utf8Str::from_boxed_bytes(s)
    }
}

impl From<Box<Utf8Str>> for Box<[u8]> {
    #[inline]
    fn from(s: Box<Utf8Str>) -> Self {
        Utf8Str::into_boxed_bytes(s)
    }
}