blitz_ws/protocol/frame/
utf.rs

1use core::str;
2use std::{borrow::Borrow, fmt::Display, hash::Hash, ops::Deref};
3
4use bytes::{Bytes, BytesMut};
5
6/// Utf8 payload.
7#[derive(Debug, Default, Clone, PartialEq, Eq)]
8pub struct Utf8Bytes(Bytes);
9
10impl Utf8Bytes {
11    /// Creates from a static str.
12    #[inline]
13    pub const fn from_static(str: &'static str) -> Self {
14        Self(Bytes::from_static(str.as_bytes()))
15    }
16
17    /// Returns as a string slice.
18    #[inline]
19    pub fn as_str(&self) -> &str {
20        unsafe { std::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 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 Borrow<str> for Utf8Bytes {
76    fn borrow(&self) -> &str {
77        self.as_str()
78    }
79}
80
81impl Hash for Utf8Bytes {
82    fn hash<H: std::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 = std::str::Utf8Error;
125
126    #[inline]
127    fn try_from(value: Bytes) -> Result<Self, Self::Error> {
128        std::str::from_utf8(&value)?;
129        Ok(Self(value))
130    }
131}
132
133impl TryFrom<BytesMut> for Utf8Bytes {
134    type Error = std::str::Utf8Error;
135
136    #[inline]
137    fn try_from(value: BytesMut) -> Result<Self, Self::Error> {
138        value.freeze().try_into()
139    }
140}
141
142impl TryFrom<Vec<u8>> for Utf8Bytes {
143    type Error = std::str::Utf8Error;
144
145    #[inline]
146    fn try_from(value: Vec<u8>) -> Result<Self, Self::Error> {
147        Bytes::from(value).try_into()
148    }
149}
150
151impl From<String> for Utf8Bytes {
152    #[inline]
153    fn from(value: String) -> Self {
154        Self(value.into())
155    }
156}
157
158impl From<&str> for Utf8Bytes {
159    #[inline]
160    fn from(value: &str) -> Self {
161        Self(Bytes::copy_from_slice(value.as_bytes()))
162    }
163}
164
165impl From<&String> for Utf8Bytes {
166    #[inline]
167    fn from(value: &String) -> Self {
168        value.as_str().into()
169    }
170}
171
172impl From<Utf8Bytes> for Bytes {
173    #[inline]
174    fn from(Utf8Bytes(value): Utf8Bytes) -> Self {
175        value
176    }
177}