engineioxide_core/
str.rs

1use bytes::Bytes;
2use serde::{Deserialize, Serialize};
3use std::borrow::{Borrow, Cow};
4
5/// A custom [`Bytes`] wrapper to efficiently store string packets
6#[derive(Debug, Default, Clone, PartialEq, Eq, PartialOrd)]
7pub struct Str(Bytes);
8impl Str {
9    /// Efficiently slice string by calling [`Bytes::slice`] on the inner bytes
10    pub fn slice(&self, range: impl std::ops::RangeBounds<usize>) -> Self {
11        Str(self.0.slice(range))
12    }
13    /// Return a `&str` representation of the string
14    pub fn as_str(&self) -> &str {
15        // SAFETY: Str is always a valid utf8 string
16        unsafe { std::str::from_utf8_unchecked(&self.0) }
17    }
18    /// Return a `&[u8]` representation of the string
19    pub fn as_bytes(&self) -> &[u8] {
20        &self.0
21    }
22    /// Get the byte at the specified index
23    pub fn get(&self, index: usize) -> Option<&u8> {
24        self.0.get(index)
25    }
26    /// Creates a [`Str`] instance from str slice, by copying it.
27    pub fn copy_from_slice(data: &str) -> Self {
28        Str(Bytes::copy_from_slice(data.as_bytes()))
29    }
30    /// Creates a [`Str`] instance from a [`Bytes`] slice. It is the caller's responsibility to
31    /// ensure that the provided bytes are a valid utf8 string.
32    ///
33    /// # Safety
34    /// It is the caller's responsibility to ensure that the provided bytes are a valid utf8 string.
35    pub unsafe fn from_bytes_unchecked(data: Bytes) -> Self {
36        Str(data)
37    }
38}
39/// This custom Hash implementation as a [`str`] is made to match with the [`Borrow`]
40/// implementation as [`str`]. Otherwise [`str`] and [`Str`] won't have the same hash.
41impl std::hash::Hash for Str {
42    fn hash<H: std::hash::Hasher>(&self, state: &mut H) {
43        str::hash(self.as_str(), state);
44    }
45}
46impl std::ops::Deref for Str {
47    type Target = str;
48    fn deref(&self) -> &Self::Target {
49        self.as_str()
50    }
51}
52impl std::fmt::Display for Str {
53    fn fmt(&self, f: &mut std::fmt::Formatter<'_>) -> std::fmt::Result {
54        write!(f, "{}", self.as_str())
55    }
56}
57impl Borrow<str> for Str {
58    fn borrow(&self) -> &str {
59        self.as_str()
60    }
61}
62impl From<&'static str> for Str {
63    fn from(s: &'static str) -> Self {
64        Str(Bytes::from_static(s.as_bytes()))
65    }
66}
67impl From<String> for Str {
68    fn from(s: String) -> Self {
69        let vec = s.into_bytes();
70        Str(Bytes::from(vec))
71    }
72}
73
74impl From<Cow<'static, str>> for Str {
75    fn from(s: Cow<'static, str>) -> Self {
76        match s {
77            Cow::Borrowed(s) => Str::from(s),
78            Cow::Owned(s) => Str::from(s),
79        }
80    }
81}
82impl From<&Cow<'static, str>> for Str {
83    fn from(s: &Cow<'static, str>) -> Self {
84        match s {
85            Cow::Borrowed(s) => Str::from(*s),
86            Cow::Owned(s) => Str(Bytes::copy_from_slice(s.as_bytes())),
87        }
88    }
89}
90
91impl From<Str> for Bytes {
92    fn from(s: Str) -> Self {
93        s.0
94    }
95}
96impl From<Str> for String {
97    fn from(s: Str) -> Self {
98        let vec = s.0.into();
99        // SAFETY: Str is always a valid utf8 string
100        unsafe { String::from_utf8_unchecked(vec) }
101    }
102}
103impl From<Str> for Vec<u8> {
104    fn from(value: Str) -> Self {
105        Vec::from(value.0)
106    }
107}
108impl Serialize for Str {
109    fn serialize<S>(&self, serializer: S) -> Result<S::Ok, S::Error>
110    where
111        S: serde::Serializer,
112    {
113        serializer.serialize_str(self.as_str())
114    }
115}
116impl<'de> Deserialize<'de> for Str {
117    fn deserialize<D>(deserializer: D) -> Result<Self, D::Error>
118    where
119        D: serde::Deserializer<'de>,
120    {
121        struct StrVisitor;
122        impl serde::de::Visitor<'_> for StrVisitor {
123            type Value = Str;
124
125            fn expecting(&self, formatter: &mut std::fmt::Formatter<'_>) -> std::fmt::Result {
126                formatter.write_str("a str")
127            }
128
129            fn visit_str<E>(self, v: &str) -> Result<Self::Value, E>
130            where
131                E: serde::de::Error,
132            {
133                Ok(Str::copy_from_slice(v))
134            }
135            fn visit_string<E>(self, v: String) -> Result<Self::Value, E>
136            where
137                E: serde::de::Error,
138            {
139                Ok(Str::from(v))
140            }
141        }
142        deserializer.deserialize_str(StrVisitor)
143    }
144}
145
146impl std::cmp::PartialEq<&str> for Str {
147    fn eq(&self, other: &&str) -> bool {
148        self.as_str() == *other
149    }
150}
151impl std::cmp::PartialEq<Str> for &str {
152    fn eq(&self, other: &Str) -> bool {
153        *self == other.as_str()
154    }
155}