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, Hash)]
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 PartialOrd for Utf8Bytes {
82    fn partial_cmp(&self, other: &Self) -> Option<std::cmp::Ordering> {
83        Some(self.cmp(other))
84    }
85}
86
87impl Ord for Utf8Bytes {
88    fn cmp(&self, other: &Self) -> std::cmp::Ordering {
89        self.as_str().cmp(other.as_str())
90    }
91}
92
93impl<T> PartialEq<T> for Utf8Bytes
94where
95    for<'a> &'a str: PartialEq<T>,
96{
97    /// ```
98    /// let payload = tungstenite::Utf8Bytes::from_static("foo123");
99    /// assert_eq!(payload, "foo123");
100    /// assert_eq!(payload, "foo123".to_string());
101    /// assert_eq!(payload, &"foo123".to_string());
102    /// assert_eq!(payload, std::borrow::Cow::from("foo123"));
103    /// ```
104    #[inline]
105    fn eq(&self, other: &T) -> bool {
106        self.as_str() == *other
107    }
108}
109
110impl Display for Utf8Bytes {
111    #[inline]
112    fn fmt(&self, f: &mut std::fmt::Formatter<'_>) -> std::fmt::Result {
113        f.write_str(self.as_str())
114    }
115}
116
117impl TryFrom<Bytes> for Utf8Bytes {
118    type Error = str::Utf8Error;
119
120    #[inline]
121    fn try_from(bytes: Bytes) -> Result<Self, Self::Error> {
122        str::from_utf8(&bytes)?;
123        Ok(Self(bytes))
124    }
125}
126
127impl TryFrom<BytesMut> for Utf8Bytes {
128    type Error = str::Utf8Error;
129
130    #[inline]
131    fn try_from(bytes: BytesMut) -> Result<Self, Self::Error> {
132        bytes.freeze().try_into()
133    }
134}
135
136impl TryFrom<Vec<u8>> for Utf8Bytes {
137    type Error = str::Utf8Error;
138
139    #[inline]
140    fn try_from(v: Vec<u8>) -> Result<Self, Self::Error> {
141        Bytes::from(v).try_into()
142    }
143}
144
145impl From<String> for Utf8Bytes {
146    #[inline]
147    fn from(s: String) -> Self {
148        Self(s.into())
149    }
150}
151
152impl From<&str> for Utf8Bytes {
153    #[inline]
154    fn from(s: &str) -> Self {
155        Self(Bytes::copy_from_slice(s.as_bytes()))
156    }
157}
158
159impl From<&String> for Utf8Bytes {
160    #[inline]
161    fn from(s: &String) -> Self {
162        s.as_str().into()
163    }
164}
165
166impl From<Utf8Bytes> for Bytes {
167    #[inline]
168    fn from(Utf8Bytes(bytes): Utf8Bytes) -> Self {
169        bytes
170    }
171}