Skip to content

Commit fa9ad2e

Browse files
chore: remove unnecessary MQTT trait impls
1 parent b51f3b4 commit fa9ad2e

File tree

2 files changed

+58
-112
lines changed

2 files changed

+58
-112
lines changed

mqrstt/src/packets/connect/mod.rs

+4-2
Original file line numberDiff line numberDiff line change
@@ -71,8 +71,10 @@ impl Default for Connect {
7171

7272
impl PacketRead for Connect {
7373
fn read(_: u8, _: usize, mut buf: Bytes) -> Result<Self, DeserializeError> {
74-
if String::read(&mut buf)? != "MQTT" {
75-
return Err(DeserializeError::MalformedPacketWithInfo("Protocol not MQTT".to_string()));
74+
let expected_protocol = [b'M', b'Q', b'T', b'T'];
75+
let received_protocol = Vec::<u8>::read(&mut buf)?;
76+
if &received_protocol != &expected_protocol {
77+
return Err(DeserializeError::MalformedPacketWithInfo("Protocol not MQTT".to_owned()));
7678
}
7779

7880
let protocol_version = ProtocolVersion::read(&mut buf)?;

mqrstt/src/packets/mqtt_trait/primitive_impl.rs

+54-110
Original file line numberDiff line numberDiff line change
@@ -10,10 +10,10 @@ use super::MqttAsyncWrite;
1010
impl MqttRead for Box<str> {
1111
#[inline]
1212
fn read(buf: &mut Bytes) -> Result<Self, DeserializeError> {
13-
let content = Bytes::read(buf)?;
13+
let content = Vec::<u8>::read(buf)?;
1414

15-
match String::from_utf8(content.to_vec()) {
16-
Ok(s) => Ok(s.into()),
15+
match String::from_utf8(content) {
16+
Ok(s) => Ok(s.into_boxed_str()),
1717
Err(e) => Err(DeserializeError::Utf8Error(e)),
1818
}
1919
}
@@ -86,117 +86,61 @@ impl WireLength for &str {
8686
}
8787
}
8888

89-
impl MqttRead for String {
90-
#[inline]
91-
fn read(buf: &mut Bytes) -> Result<Self, DeserializeError> {
92-
let content = Bytes::read(buf)?;
89+
// impl MqttRead for Bytes {
90+
// #[inline]
91+
// fn read(buf: &mut Bytes) -> Result<Self, DeserializeError> {
92+
// if buf.len() < 2 {
93+
// return Err(DeserializeError::InsufficientData(std::any::type_name::<Bytes>(), buf.len(), 2));
94+
// }
95+
// let len = buf.get_u16() as usize;
9396

94-
match String::from_utf8(content.to_vec()) {
95-
Ok(s) => Ok(s),
96-
Err(e) => Err(DeserializeError::Utf8Error(e)),
97-
}
98-
}
99-
}
97+
// if len > buf.len() {
98+
// return Err(DeserializeError::InsufficientData(std::any::type_name::<Bytes>(), buf.len(), len));
99+
// }
100100

101-
impl<T> MqttAsyncRead<T> for String
102-
where
103-
T: tokio::io::AsyncReadExt + std::marker::Unpin,
104-
{
105-
async fn async_read(buf: &mut T) -> Result<(Self, usize), ReadError> {
106-
let (content, read_bytes) = Bytes::async_read(buf).await?;
107-
match String::from_utf8(content.to_vec()) {
108-
Ok(s) => Ok((s, read_bytes)),
109-
Err(e) => Err(ReadError::DeserializeError(DeserializeError::Utf8Error(e))),
110-
}
111-
}
112-
}
101+
// Ok(buf.split_to(len))
102+
// }
103+
// }
104+
// impl<S> MqttAsyncRead<S> for Bytes
105+
// where
106+
// S: tokio::io::AsyncReadExt + std::marker::Unpin,
107+
// {
108+
// async fn async_read(stream: &mut S) -> Result<(Self, usize), ReadError> {
109+
// let size = stream.read_u16().await? as usize;
110+
// // let mut data = BytesMut::with_capacity(size);
111+
// let mut data = Vec::with_capacity(size);
112+
// let read_bytes = stream.read_exact(&mut data).await?;
113+
// assert_eq!(size, read_bytes);
114+
// Ok((data.into(), 2 + size))
115+
// }
116+
// }
117+
// impl MqttWrite for Bytes {
118+
// #[inline]
119+
// fn write(&self, buf: &mut BytesMut) -> Result<(), SerializeError> {
120+
// buf.put_u16(self.len() as u16);
121+
// buf.extend(self);
113122

114-
impl MqttWrite for String {
115-
#[inline]
116-
fn write(&self, buf: &mut BytesMut) -> Result<(), SerializeError> {
117-
if self.len() > 65535 {
118-
return Err(SerializeError::StringTooLong(self.len()));
119-
}
123+
// Ok(())
124+
// }
125+
// }
126+
// impl<S> MqttAsyncWrite<S> for Bytes
127+
// where
128+
// S: tokio::io::AsyncWrite + Unpin,
129+
// {
130+
// async fn async_write(&self, stream: &mut S) -> Result<usize, crate::packets::error::WriteError> {
131+
// let size = (self.len() as u16).to_be_bytes();
132+
// stream.write_all(&size).await?;
133+
// stream.write_all(self.as_ref()).await?;
134+
// Ok(2 + self.len())
135+
// }
136+
// }
120137

121-
buf.put_u16(self.len() as u16);
122-
buf.extend(self.as_bytes());
123-
Ok(())
124-
}
125-
}
126-
impl<S> MqttAsyncWrite<S> for String
127-
where
128-
S: tokio::io::AsyncWrite + Unpin,
129-
{
130-
async fn async_write(&self, stream: &mut S) -> Result<usize, crate::packets::error::WriteError> {
131-
let size = (self.len() as u16).to_be_bytes();
132-
stream.write_all(&size).await?;
133-
stream.write_all(self.as_bytes()).await?;
134-
Ok(2 + self.len())
135-
}
136-
}
137-
138-
impl WireLength for String {
139-
#[inline(always)]
140-
fn wire_len(&self) -> usize {
141-
self.len() + 2
142-
}
143-
}
144-
145-
impl MqttRead for Bytes {
146-
#[inline]
147-
fn read(buf: &mut Bytes) -> Result<Self, DeserializeError> {
148-
if buf.len() < 2 {
149-
return Err(DeserializeError::InsufficientData(std::any::type_name::<Bytes>(), buf.len(), 2));
150-
}
151-
let len = buf.get_u16() as usize;
152-
153-
if len > buf.len() {
154-
return Err(DeserializeError::InsufficientData(std::any::type_name::<Bytes>(), buf.len(), len));
155-
}
156-
157-
Ok(buf.split_to(len))
158-
}
159-
}
160-
impl<S> MqttAsyncRead<S> for Bytes
161-
where
162-
S: tokio::io::AsyncReadExt + std::marker::Unpin,
163-
{
164-
async fn async_read(stream: &mut S) -> Result<(Self, usize), ReadError> {
165-
let size = stream.read_u16().await? as usize;
166-
// let mut data = BytesMut::with_capacity(size);
167-
let mut data = Vec::with_capacity(size);
168-
let read_bytes = stream.read_exact(&mut data).await?;
169-
assert_eq!(size, read_bytes);
170-
Ok((data.into(), 2 + size))
171-
}
172-
}
173-
impl MqttWrite for Bytes {
174-
#[inline]
175-
fn write(&self, buf: &mut BytesMut) -> Result<(), SerializeError> {
176-
buf.put_u16(self.len() as u16);
177-
buf.extend(self);
178-
179-
Ok(())
180-
}
181-
}
182-
impl<S> MqttAsyncWrite<S> for Bytes
183-
where
184-
S: tokio::io::AsyncWrite + Unpin,
185-
{
186-
async fn async_write(&self, stream: &mut S) -> Result<usize, crate::packets::error::WriteError> {
187-
let size = (self.len() as u16).to_be_bytes();
188-
stream.write_all(&size).await?;
189-
stream.write_all(self.as_ref()).await?;
190-
Ok(2 + self.len())
191-
}
192-
}
193-
194-
impl WireLength for Bytes {
195-
#[inline(always)]
196-
fn wire_len(&self) -> usize {
197-
self.len() + 2
198-
}
199-
}
138+
// impl WireLength for Bytes {
139+
// #[inline(always)]
140+
// fn wire_len(&self) -> usize {
141+
// self.len() + 2
142+
// }
143+
// }
200144

201145
impl MqttRead for Vec<u8> {
202146
#[inline]

0 commit comments

Comments
 (0)