tungstenite/protocol/frame/
utf8.rs

1use bytes::{Bytes, BytesMut};
2use core::str;
3use std::fmt::Display;
4
5/// Utf8 payload.
6#[derive(Debug, Default, Clone, Eq, PartialEq)]
7pub struct Utf8Bytes(Bytes);
8
9impl Utf8Bytes {
10    /// Creates from a static str.
11    #[inline]
12    pub const fn from_static(str: &'static str) -> Self {
13        Self(Bytes::from_static(str.as_bytes()))
14    }
15
16    /// Returns as a string slice.
17    #[inline]
18    pub fn as_str(&self) -> &str {
19        // SAFETY: is valid uft8
20        unsafe { str::from_utf8_unchecked(&self.0) }
21    }
22
23    /// Creates from a [`Bytes`] object without checking the encoding.
24    ///
25    /// # Safety
26    ///
27    /// The bytes passed in must be valid UTF-8.
28    pub unsafe fn from_bytes_unchecked(bytes: Bytes) -> Self {
29        Self(bytes)
30    }
31}
32
33impl std::ops::Deref for Utf8Bytes {
34    type Target = str;
35
36    /// ```
37    /// /// Example fn that takes a str slice
38    /// fn a(s: &str) {}
39    ///
40    /// let data = tungstenite::Utf8Bytes::from_static("foo123");
41    ///
42    /// // auto-deref as arg
43    /// a(&data);
44    ///
45    /// // deref to str methods
46    /// assert_eq!(data.len(), 6);
47    /// ```
48    #[inline]
49    fn deref(&self) -> &Self::Target {
50        self.as_str()
51    }
52}
53
54impl AsRef<[u8]> for Utf8Bytes {
55    #[inline]
56    fn as_ref(&self) -> &[u8] {
57        &self.0
58    }
59}
60
61impl AsRef<str> for Utf8Bytes {
62    #[inline]
63    fn as_ref(&self) -> &str {
64        self.as_str()
65    }
66}
67
68impl AsRef<Bytes> for Utf8Bytes {
69    #[inline]
70    fn as_ref(&self) -> &Bytes {
71        &self.0
72    }
73}
74
75impl std::borrow::Borrow<str> for Utf8Bytes {
76    fn borrow(&self) -> &str {
77        self.as_str()
78    }
79}
80
81impl core::hash::Hash for Utf8Bytes {
82    fn hash<H: core::hash::Hasher>(&self, state: &mut H) {
83        self.as_str().hash(state)
84    }
85}
86
87impl PartialOrd for Utf8Bytes {
88    fn partial_cmp(&self, other: &Self) -> Option<std::cmp::Ordering> {
89        Some(self.cmp(other))
90    }
91}
92
93impl Ord for Utf8Bytes {
94    fn cmp(&self, other: &Self) -> std::cmp::Ordering {
95        self.as_str().cmp(other.as_str())
96    }
97}
98
99impl<T> PartialEq<T> for Utf8Bytes
100where
101    for<'a> &'a str: PartialEq<T>,
102{
103    /// ```
104    /// let payload = tungstenite::Utf8Bytes::from_static("foo123");
105    /// assert_eq!(payload, "foo123");
106    /// assert_eq!(payload, "foo123".to_string());
107    /// assert_eq!(payload, &"foo123".to_string());
108    /// assert_eq!(payload, std::borrow::Cow::from("foo123"));
109    /// ```
110    #[inline]
111    fn eq(&self, other: &T) -> bool {
112        self.as_str() == *other
113    }
114}
115
116impl Display for Utf8Bytes {
117    #[inline]
118    fn fmt(&self, f: &mut std::fmt::Formatter<'_>) -> std::fmt::Result {
119        f.write_str(self.as_str())
120    }
121}
122
123impl TryFrom<Bytes> for Utf8Bytes {
124    type Error = str::Utf8Error;
125
126    #[inline]
127    fn try_from(bytes: Bytes) -> Result<Self, Self::Error> {
128        str::from_utf8(&bytes)?;
129        Ok(Self(bytes))
130    }
131}
132
133impl TryFrom<BytesMut> for Utf8Bytes {
134    type Error = str::Utf8Error;
135
136    #[inline]
137    fn try_from(bytes: BytesMut) -> Result<Self, Self::Error> {
138        bytes.freeze().try_into()
139    }
140}
141
142impl TryFrom<Vec<u8>> for Utf8Bytes {
143    type Error = str::Utf8Error;
144
145    #[inline]
146    fn try_from(v: Vec<u8>) -> Result<Self, Self::Error> {
147        Bytes::from(v).try_into()
148    }
149}
150
151impl From<String> for Utf8Bytes {
152    #[inline]
153    fn from(s: String) -> Self {
154        Self(s.into())
155    }
156}
157
158impl From<&str> for Utf8Bytes {
159    #[inline]
160    fn from(s: &str) -> Self {
161        Self(Bytes::copy_from_slice(s.as_bytes()))
162    }
163}
164
165impl From<&String> for Utf8Bytes {
166    #[inline]
167    fn from(s: &String) -> Self {
168        s.as_str().into()
169    }
170}
171
172impl From<Utf8Bytes> for Bytes {
173    #[inline]
174    fn from(Utf8Bytes(bytes): Utf8Bytes) -> Self {
175        bytes
176    }
177}
178
179#[cfg(test)]
180mod tests {
181    use super::*;
182
183    use std::{
184        borrow::Borrow,
185        hash::{BuildHasher, RandomState},
186    };
187
188    #[test]
189    fn hash_consistency() {
190        let bytes = Utf8Bytes::from_static("hash_consistency");
191        let hasher = RandomState::new();
192        assert_eq!(hasher.hash_one::<&str>(bytes.borrow()), hasher.hash_one(bytes));
193    }
194}