@@ -39,11 +39,11 @@ pub const METHOD_GET_PEERS: &[u8] = b"get_peers";
3939#[ derive( Debug , Deserialize , Serialize ) ]
4040pub struct QueryArgs < ' a > {
4141 /// The querying node's ID
42- #[ serde( borrow ) ]
43- pub id : & ' a Bytes ,
42+ #[ serde( with = "serde_bytes" ) ]
43+ pub id : & ' a [ u8 ] ,
4444 /// The `InfoHash` associated with the torrent
45- #[ serde( borrow ) ]
46- pub info_hash : & ' a Bytes ,
45+ #[ serde( with = "serde_bytes" ) ]
46+ pub info_hash : & ' a [ u8 ] ,
4747}
4848
4949impl < ' a > QueryArgs < ' a > {
@@ -52,41 +52,41 @@ impl<'a> QueryArgs<'a> {
5252 #[ inline]
5353 pub fn new ( id : & ' a LocalId , info_hash : & ' a InfoHash ) -> Self {
5454 Self {
55- id : Bytes :: new ( & ( id. 0 ) . 0 ) ,
56- info_hash : Bytes :: new ( & info_hash. 0 ) ,
55+ id : & ( id. 0 ) . 0 ,
56+ info_hash : & info_hash. 0 ,
5757 }
5858 }
5959
6060 /// Returns the querying node's ID.
6161 #[ must_use]
6262 #[ inline]
6363 pub fn id ( & self ) -> Option < Id > {
64- Id :: try_from ( self . id . as_ref ( ) ) . ok ( )
64+ Id :: try_from ( self . id ) . ok ( )
6565 }
6666
6767 /// Returns the `InfoHash` for the relevant torrent.
6868 #[ must_use]
6969 #[ inline]
7070 pub fn info_hash ( & self ) -> Option < InfoHash > {
71- InfoHash :: try_from ( self . info_hash . as_ref ( ) ) . ok ( )
71+ InfoHash :: try_from ( self . info_hash ) . ok ( )
7272 }
7373}
7474
7575/// The value for the get peers response.
7676#[ derive( Debug , Serialize , Deserialize ) ]
7777pub struct RespValues < ' a , V > {
7878 /// The queried node's ID
79- #[ serde( borrow ) ]
80- pub id : & ' a Bytes ,
79+ #[ serde( with = "serde_bytes" ) ]
80+ pub id : & ' a [ u8 ] ,
8181 /// IPv4 nodes which may have relevant information
8282 #[ serde( skip_serializing_if = "Option::is_none" , borrow) ]
8383 pub nodes : Option < & ' a Bytes > ,
8484 /// IPv6 nodes which may have relevant information
8585 #[ serde( skip_serializing_if = "Option::is_none" , borrow) ]
8686 pub nodes6 : Option < & ' a Bytes > ,
8787 /// An opaque token which can be used in an announce peer message.
88- #[ serde( borrow ) ]
89- pub token : & ' a Bytes ,
88+ #[ serde( with = "serde_bytes" ) ]
89+ pub token : & ' a [ u8 ] ,
9090 /// Peer compact addresses
9191 #[ serde( skip_serializing_if = "Option::is_none" ) ]
9292 pub values : Option < V > ,
@@ -98,13 +98,13 @@ impl<'a, V> RespValues<'a, V> {
9898 #[ inline]
9999 pub fn new (
100100 id : & ' a LocalId ,
101- token : & ' a Bytes ,
101+ token : & ' a [ u8 ] ,
102102 values : Option < V > ,
103103 nodes : Option < & ' a Bytes > ,
104104 nodes6 : Option < & ' a Bytes > ,
105105 ) -> Self {
106106 Self {
107- id : Bytes :: new ( & ( id. 0 ) . 0 ) ,
107+ id : & ( id. 0 ) . 0 ,
108108 token,
109109 values,
110110 nodes,
@@ -116,7 +116,7 @@ impl<'a, V> RespValues<'a, V> {
116116 #[ must_use]
117117 #[ inline]
118118 pub fn id ( & self ) -> Option < Id > {
119- Id :: try_from ( self . id . as_ref ( ) ) . ok ( )
119+ Id :: try_from ( self . id ) . ok ( )
120120 }
121121
122122 /// Returns the token which is used by the queried node for verification.
@@ -169,18 +169,18 @@ impl<'a, V> RespValues<'a, V> {
169169 }
170170}
171171
172- impl < ' a > RespValues < ' a , Vec < & ' a Bytes > > {
172+ impl < ' a > RespValues < ' a , Vec < & ' a [ u8 ] > > {
173173 /// Returns peers' socket addresses for the torrent.
174174 #[ must_use]
175175 #[ inline]
176176 pub fn values ( & ' a self ) -> Option < impl Iterator < Item = CompactAddr > + ' a > {
177177 self . values . as_ref ( ) . map ( |values| {
178178 values. iter ( ) . filter_map ( |& v| match v. len ( ) {
179- 6 => <[ u8 ; 6 ] >:: try_from ( v. as_ref ( ) )
179+ 6 => <[ u8 ; 6 ] >:: try_from ( v)
180180 . ok ( )
181181 . map ( CompactAddrV4 :: from)
182182 . map ( CompactAddr :: from) ,
183- 18 => <[ u8 ; 18 ] >:: try_from ( v. as_ref ( ) )
183+ 18 => <[ u8 ; 18 ] >:: try_from ( v)
184184 . ok ( )
185185 . map ( CompactAddrV6 :: from)
186186 . map ( CompactAddr :: from) ,
@@ -193,7 +193,6 @@ impl<'a> RespValues<'a, Vec<&'a Bytes>> {
193193#[ cfg( test) ]
194194mod tests {
195195 use bt_bencode:: Error ;
196- use serde_bytes:: Bytes ;
197196
198197 use super :: * ;
199198
@@ -221,9 +220,9 @@ mod tests {
221220 ) ;
222221
223222 let ser_query_msg = ser:: QueryMsg {
224- t : Bytes :: new ( b"aa" ) ,
223+ t : b"aa" ,
225224 v : None ,
226- q : Bytes :: new ( METHOD_GET_PEERS ) ,
225+ q : METHOD_GET_PEERS ,
227226 a : query_args,
228227 } ;
229228 let ser_msg = bt_bencode:: to_vec ( & ser_query_msg) ?;
@@ -252,7 +251,7 @@ mod tests {
252251 assert_eq ! ( msg. ty( ) , Ty :: Response ) ;
253252 assert_eq ! ( msg. client_version( ) , None ) ;
254253
255- let resp_values: RespValues < ' _ , Vec < & ' _ Bytes > > = msg. values ( ) . unwrap ( ) ?;
254+ let resp_values: RespValues < ' _ , Vec < & ' _ [ u8 ] > > = msg. values ( ) . unwrap ( ) ?;
256255 assert_eq ! ( resp_values. id( ) , Some ( Id :: from( * b"0123456789abcdefghij" ) ) ) ;
257256 assert ! ( resp_values. values( ) . is_none( ) ) ;
258257 assert_eq ! (
@@ -262,7 +261,7 @@ mod tests {
262261 assert ! ( resp_values. nodes6( ) . is_none( ) ) ;
263262
264263 let ser_resp_msg = ser:: RespMsg {
265- t : Bytes :: new ( b"aa" ) ,
264+ t : b"aa" ,
266265 v : None ,
267266 r : & resp_values,
268267 } ;
@@ -289,15 +288,15 @@ mod tests {
289288 let values: Vec < CompactAddr > = vec ! [ compact_addr_v4. into( ) , compact_addr_v6. into( ) ] ;
290289
291290 let resp_values: RespValues < ' _ , Vec < CompactAddr > > = RespValues {
292- id : Bytes :: new ( b"0123456789abcdefghij" ) ,
291+ id : b"0123456789abcdefghij" ,
293292 nodes : None ,
294293 nodes6 : None ,
295- token : Bytes :: new ( b"abcd1234" ) ,
294+ token : b"abcd1234" ,
296295 values : Some ( values) ,
297296 } ;
298297
299298 let ser_resp_msg = ser:: RespMsg {
300- t : Bytes :: new ( b"aa" ) ,
299+ t : b"aa" ,
301300 v : None ,
302301 r : & resp_values,
303302 } ;
@@ -308,7 +307,7 @@ mod tests {
308307 assert_eq ! ( msg. ty( ) , Ty :: Response ) ;
309308 assert_eq ! ( msg. client_version( ) , None ) ;
310309
311- let resp_values: RespValues < ' _ , Vec < & ' _ Bytes > > = msg. values ( ) . unwrap ( ) . unwrap ( ) ;
310+ let resp_values: RespValues < ' _ , Vec < & ' _ [ u8 ] > > = msg. values ( ) . unwrap ( ) . unwrap ( ) ;
312311 assert_eq ! ( resp_values. id( ) , Some ( Id :: from( * b"0123456789abcdefghij" ) ) ) ;
313312 assert_eq ! (
314313 resp_values. values( ) . unwrap( ) . collect:: <Vec <_>>( ) ,
0 commit comments