@@ -10,10 +10,10 @@ use super::MqttAsyncWrite;
10
10
impl MqttRead for Box < str > {
11
11
#[ inline]
12
12
fn read ( buf : & mut Bytes ) -> Result < Self , DeserializeError > {
13
- let content = Bytes :: read ( buf) ?;
13
+ let content = Vec :: < u8 > :: read ( buf) ?;
14
14
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 ( ) ) ,
17
17
Err ( e) => Err ( DeserializeError :: Utf8Error ( e) ) ,
18
18
}
19
19
}
@@ -86,117 +86,61 @@ impl WireLength for &str {
86
86
}
87
87
}
88
88
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;
93
96
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
+ // }
100
100
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);
113
122
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
+ // }
120
137
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
+ // }
200
144
201
145
impl MqttRead for Vec < u8 > {
202
146
#[ inline]
0 commit comments