@@ -10,22 +10,34 @@ use crate::{
10
10
/// Octet strings represent contiguous sequences of octets, a.k.a. bytes.
11
11
///
12
12
/// This is a zero-copy reference type which borrows from the input data.
13
- #[ derive( Copy , Clone , Debug , Eq , PartialEq , PartialOrd , Ord ) ]
14
- pub struct OctetStringRef < ' a > {
13
+ #[ derive( Debug , Eq , Hash , PartialEq , PartialOrd , Ord ) ]
14
+ #[ repr( transparent) ]
15
+ pub struct OctetStringRef {
15
16
/// Inner value
16
- inner : & ' a BytesRef ,
17
+ inner : BytesRef ,
17
18
}
18
19
19
- impl < ' a > OctetStringRef < ' a > {
20
+ impl OctetStringRef {
20
21
/// Create a new ASN.1 `OCTET STRING` from a byte slice.
21
- pub fn new ( slice : & ' a [ u8 ] ) -> Result < Self , Error > {
22
+ pub fn new < ' a > ( slice : & ' a [ u8 ] ) -> Result < & ' a Self , Error > {
22
23
BytesRef :: new ( slice)
23
- . map ( |inner| Self { inner } )
24
+ . map ( Self :: from_bytes_ref )
24
25
. map_err ( |_| ErrorKind :: Length { tag : Self :: TAG } . into ( ) )
25
26
}
26
27
28
+ /// Create an [`OctetStringRef`] from a [`BytesRef`].
29
+ ///
30
+ /// Implemented as an inherent method to keep [`BytesRef`] out of the public API.
31
+ fn from_bytes_ref ( bytes_ref : & BytesRef ) -> & Self {
32
+ // SAFETY: `Self` is a `repr(transparent)` newtype for `BytesRef`
33
+ #[ allow( unsafe_code) ]
34
+ unsafe {
35
+ & * ( bytes_ref. as_ptr ( ) as * const Self )
36
+ }
37
+ }
38
+
27
39
/// Borrow the inner byte slice.
28
- pub fn as_bytes ( & self ) -> & ' a [ u8 ] {
40
+ pub fn as_bytes ( & self ) -> & [ u8 ] {
29
41
self . inner . as_slice ( )
30
42
}
31
43
@@ -40,29 +52,28 @@ impl<'a> OctetStringRef<'a> {
40
52
}
41
53
42
54
/// Parse `T` from this `OCTET STRING`'s contents.
43
- pub fn decode_into < T : Decode < ' a > > ( & self ) -> Result < T , T :: Error > {
55
+ pub fn decode_into < ' a , T : Decode < ' a > > ( & ' a self ) -> Result < T , T :: Error > {
44
56
Decode :: from_der ( self . as_bytes ( ) )
45
57
}
46
58
}
47
59
48
- impl_any_conversions ! ( OctetStringRef <' a>, ' a) ;
60
+ // impl_any_conversions!(OctetStringRef<'a>, 'a);
49
61
50
- impl AsRef < [ u8 ] > for OctetStringRef < ' _ > {
62
+ impl AsRef < [ u8 ] > for OctetStringRef {
51
63
fn as_ref ( & self ) -> & [ u8 ] {
52
64
self . as_bytes ( )
53
65
}
54
66
}
55
67
56
- impl < ' a > DecodeValue < ' a > for OctetStringRef < ' a > {
68
+ impl < ' a > DecodeValue < ' a > for & ' a OctetStringRef {
57
69
type Error = Error ;
58
70
59
71
fn decode_value < R : Reader < ' a > > ( reader : & mut R , header : Header ) -> Result < Self , Error > {
60
- let inner = <& ' a BytesRef >:: decode_value ( reader, header) ?;
61
- Ok ( Self { inner } )
72
+ <& ' a BytesRef >:: decode_value ( reader, header) . map ( OctetStringRef :: from_bytes_ref)
62
73
}
63
74
}
64
75
65
- impl EncodeValue for OctetStringRef < ' _ > {
76
+ impl EncodeValue for OctetStringRef {
66
77
fn value_len ( & self ) -> Result < Length , Error > {
67
78
self . inner . value_len ( )
68
79
}
@@ -72,59 +83,44 @@ impl EncodeValue for OctetStringRef<'_> {
72
83
}
73
84
}
74
85
75
- impl FixedTag for OctetStringRef < ' _ > {
86
+ impl FixedTag for OctetStringRef {
76
87
const TAG : Tag = Tag :: OctetString ;
77
88
}
78
89
79
- impl OrdIsValueOrd for OctetStringRef < ' _ > { }
90
+ impl OrdIsValueOrd for OctetStringRef { }
80
91
81
- impl < ' a > From < & OctetStringRef < ' a > > for OctetStringRef < ' a > {
82
- fn from ( value : & OctetStringRef < ' a > ) -> OctetStringRef < ' a > {
83
- * value
92
+ impl < ' a > From < & ' a OctetStringRef > for AnyRef < ' a > {
93
+ fn from ( octet_string : & ' a OctetStringRef ) -> AnyRef < ' a > {
94
+ AnyRef :: from_tag_and_value ( Tag :: OctetString , & octet_string . inner )
84
95
}
85
96
}
86
97
87
- impl < ' a > From < OctetStringRef < ' a > > for AnyRef < ' a > {
88
- fn from ( octet_string : OctetStringRef < ' a > ) -> AnyRef < ' a > {
89
- AnyRef :: from_tag_and_value ( Tag :: OctetString , octet_string. inner )
90
- }
91
- }
92
-
93
- impl < ' a > From < OctetStringRef < ' a > > for & ' a [ u8 ] {
94
- fn from ( octet_string : OctetStringRef < ' a > ) -> & ' a [ u8 ] {
98
+ impl < ' a > From < & ' a OctetStringRef > for & ' a [ u8 ] {
99
+ fn from ( octet_string : & ' a OctetStringRef ) -> & ' a [ u8 ] {
95
100
octet_string. as_bytes ( )
96
101
}
97
102
}
98
103
99
- impl < ' a > TryFrom < & ' a [ u8 ] > for OctetStringRef < ' a > {
104
+ impl < ' a > TryFrom < & ' a [ u8 ] > for & ' a OctetStringRef {
100
105
type Error = Error ;
101
106
102
107
fn try_from ( byte_slice : & ' a [ u8 ] ) -> Result < Self , Error > {
103
108
OctetStringRef :: new ( byte_slice)
104
109
}
105
110
}
106
111
107
- /// Hack for simplifying the custom derive use case.
108
- impl < ' a > TryFrom < & & ' a [ u8 ] > for OctetStringRef < ' a > {
109
- type Error = Error ;
110
-
111
- fn try_from ( byte_slice : & & ' a [ u8 ] ) -> Result < Self , Error > {
112
- OctetStringRef :: new ( byte_slice)
113
- }
114
- }
115
-
116
- impl < ' a , const N : usize > TryFrom < & ' a [ u8 ; N ] > for OctetStringRef < ' a > {
112
+ impl < ' a , const N : usize > TryFrom < & ' a [ u8 ; N ] > for & ' a OctetStringRef {
117
113
type Error = Error ;
118
114
119
115
fn try_from ( byte_slice : & ' a [ u8 ; N ] ) -> Result < Self , Error > {
120
116
OctetStringRef :: new ( byte_slice)
121
117
}
122
118
}
123
119
124
- impl < ' a , const N : usize > TryFrom < OctetStringRef < ' a > > for [ u8 ; N ] {
120
+ impl < ' a , const N : usize > TryFrom < & ' a OctetStringRef > for [ u8 ; N ] {
125
121
type Error = Error ;
126
122
127
- fn try_from ( octet_string : OctetStringRef < ' a > ) -> Result < Self , Self :: Error > {
123
+ fn try_from ( octet_string : & ' a OctetStringRef ) -> Result < Self , Self :: Error > {
128
124
octet_string
129
125
. as_bytes ( )
130
126
. try_into ( )
@@ -133,10 +129,10 @@ impl<'a, const N: usize> TryFrom<OctetStringRef<'a>> for [u8; N] {
133
129
}
134
130
135
131
#[ cfg( feature = "heapless" ) ]
136
- impl < ' a , const N : usize > TryFrom < OctetStringRef < ' a > > for heapless:: Vec < u8 , N > {
132
+ impl < const N : usize > TryFrom < & OctetStringRef > for heapless:: Vec < u8 , N > {
137
133
type Error = Error ;
138
134
139
- fn try_from ( octet_string : OctetStringRef < ' a > ) -> Result < Self , Self :: Error > {
135
+ fn try_from ( octet_string : & OctetStringRef ) -> Result < Self , Self :: Error > {
140
136
octet_string
141
137
. as_bytes ( )
142
138
. try_into ( )
@@ -145,7 +141,7 @@ impl<'a, const N: usize> TryFrom<OctetStringRef<'a>> for heapless::Vec<u8, N> {
145
141
}
146
142
147
143
#[ cfg( feature = "heapless" ) ]
148
- impl < ' a , const N : usize > TryFrom < & ' a heapless:: Vec < u8 , N > > for OctetStringRef < ' a > {
144
+ impl < ' a , const N : usize > TryFrom < & ' a heapless:: Vec < u8 , N > > for & ' a OctetStringRef {
149
145
type Error = Error ;
150
146
151
147
fn try_from ( byte_vec : & ' a heapless:: Vec < u8 , N > ) -> Result < Self , Error > {
@@ -159,8 +155,13 @@ pub use self::allocating::OctetString;
159
155
#[ cfg( feature = "alloc" ) ]
160
156
mod allocating {
161
157
use super :: * ;
162
- use crate :: { BytesOwned , referenced:: * } ;
163
- use alloc:: { borrow:: Cow , boxed:: Box , vec:: Vec } ;
158
+ use crate :: BytesOwned ;
159
+ use alloc:: {
160
+ borrow:: { Borrow , Cow } ,
161
+ boxed:: Box ,
162
+ vec:: Vec ,
163
+ } ;
164
+ use std:: prelude:: rust_2015:: ToOwned ;
164
165
165
166
/// ASN.1 `OCTET STRING` type: owned form.
166
167
///
@@ -214,6 +215,12 @@ mod allocating {
214
215
}
215
216
}
216
217
218
+ impl Borrow < OctetStringRef > for OctetString {
219
+ fn borrow ( & self ) -> & OctetStringRef {
220
+ OctetStringRef :: from_bytes_ref ( self . inner . as_ref ( ) )
221
+ }
222
+ }
223
+
217
224
impl < ' a > DecodeValue < ' a > for OctetString {
218
225
type Error = Error ;
219
226
@@ -237,40 +244,30 @@ mod allocating {
237
244
const TAG : Tag = Tag :: OctetString ;
238
245
}
239
246
240
- impl < ' a > From < & ' a OctetString > for OctetStringRef < ' a > {
241
- fn from ( octet_string : & ' a OctetString ) -> OctetStringRef < ' a > {
242
- OctetStringRef {
243
- inner : octet_string. inner . as_ref ( ) ,
244
- }
245
- }
246
- }
247
-
248
247
impl OrdIsValueOrd for OctetString { }
249
248
250
- impl < ' a > RefToOwned < ' a > for OctetStringRef < ' a > {
251
- type Owned = OctetString ;
252
- fn ref_to_owned ( & self ) -> Self :: Owned {
253
- OctetString {
254
- inner : self . inner . into ( ) ,
255
- }
249
+ impl < ' a > From < & ' a OctetString > for & ' a OctetStringRef {
250
+ fn from ( octet_string : & ' a OctetString ) -> & ' a OctetStringRef {
251
+ OctetStringRef :: from_bytes_ref ( octet_string. inner . as_ref ( ) )
256
252
}
257
253
}
258
254
259
- impl OwnedToRef for OctetString {
260
- type Borrowed < ' a > = OctetStringRef < ' a > ;
261
- fn owned_to_ref ( & self ) -> Self :: Borrowed < ' _ > {
262
- self . into ( )
255
+ impl From < & OctetStringRef > for OctetString {
256
+ fn from ( octet_string_ref : & OctetStringRef ) -> OctetString {
257
+ Self {
258
+ inner : octet_string_ref. inner . to_owned ( ) ,
259
+ }
263
260
}
264
261
}
265
262
266
- impl From < OctetStringRef < ' _ > > for Vec < u8 > {
267
- fn from ( octet_string : OctetStringRef < ' _ > ) -> Vec < u8 > {
263
+ impl From < & OctetStringRef > for Vec < u8 > {
264
+ fn from ( octet_string : & OctetStringRef ) -> Vec < u8 > {
268
265
Vec :: from ( octet_string. as_bytes ( ) )
269
266
}
270
267
}
271
268
272
269
/// Hack for simplifying the custom derive use case.
273
- impl < ' a > TryFrom < & ' a Vec < u8 > > for OctetStringRef < ' a > {
270
+ impl < ' a > TryFrom < & ' a Vec < u8 > > for & ' a OctetStringRef {
274
271
type Error = Error ;
275
272
276
273
fn try_from ( byte_vec : & ' a Vec < u8 > ) -> Result < Self , Error > {
@@ -284,18 +281,26 @@ mod allocating {
284
281
}
285
282
}
286
283
287
- impl < ' a > TryFrom < & ' a Cow < ' a , [ u8 ] > > for OctetStringRef < ' a > {
284
+ impl ToOwned for OctetStringRef {
285
+ type Owned = OctetString ;
286
+
287
+ fn to_owned ( & self ) -> OctetString {
288
+ self . into ( )
289
+ }
290
+ }
291
+
292
+ impl < ' a > TryFrom < & ' a Cow < ' a , [ u8 ] > > for & ' a OctetStringRef {
288
293
type Error = Error ;
289
294
290
295
fn try_from ( byte_slice : & ' a Cow < ' a , [ u8 ] > ) -> Result < Self , Error > {
291
296
OctetStringRef :: new ( byte_slice)
292
297
}
293
298
}
294
299
295
- impl < ' a > TryFrom < OctetStringRef < ' a > > for Cow < ' a , [ u8 ] > {
300
+ impl < ' a > TryFrom < & ' a OctetStringRef > for Cow < ' a , [ u8 ] > {
296
301
type Error = Error ;
297
302
298
- fn try_from ( octet_string : OctetStringRef < ' a > ) -> Result < Self , Self :: Error > {
303
+ fn try_from ( octet_string : & ' a OctetStringRef ) -> Result < Self , Self :: Error > {
299
304
Ok ( Cow :: Borrowed ( octet_string. as_bytes ( ) ) )
300
305
}
301
306
}
@@ -345,8 +350,8 @@ mod bytes {
345
350
const TAG : Tag = Tag :: OctetString ;
346
351
}
347
352
348
- impl From < OctetStringRef < ' _ > > for Bytes {
349
- fn from ( octet_string : OctetStringRef < ' _ > ) -> Bytes {
353
+ impl From < & OctetStringRef > for Bytes {
354
+ fn from ( octet_string : & OctetStringRef ) -> Bytes {
350
355
Vec :: from ( octet_string) . into ( )
351
356
}
352
357
}
0 commit comments