@@ -7,7 +7,7 @@ use crate::{
7
7
use core:: cmp:: Ordering ;
8
8
9
9
#[ cfg( feature = "alloc" ) ]
10
- use alloc :: vec :: Vec ;
10
+ use crate :: Bytes ;
11
11
12
12
#[ cfg( feature = "oid" ) ]
13
13
use crate :: asn1:: ObjectIdentifier ;
@@ -94,11 +94,6 @@ impl<'a> AnyRef<'a> {
94
94
self . try_into ( )
95
95
}
96
96
97
- /// Attempt to decode an ASN.1 `IA5String`.
98
- pub fn ia5_string ( self ) -> Result < Ia5StringRef < ' a > > {
99
- self . try_into ( )
100
- }
101
-
102
97
/// Attempt to decode an ASN.1 `OCTET STRING`.
103
98
pub fn octet_string ( self ) -> Result < OctetStringRef < ' a > > {
104
99
self . try_into ( )
@@ -123,21 +118,6 @@ impl<'a> AnyRef<'a> {
123
118
}
124
119
}
125
120
126
- /// Attempt to decode an ASN.1 `PrintableString`.
127
- pub fn printable_string ( self ) -> Result < PrintableStringRef < ' a > > {
128
- self . try_into ( )
129
- }
130
-
131
- /// Attempt to decode an ASN.1 `TeletexString`.
132
- pub fn teletex_string ( self ) -> Result < TeletexStringRef < ' a > > {
133
- self . try_into ( )
134
- }
135
-
136
- /// Attempt to decode an ASN.1 `VideotexString`.
137
- pub fn videotex_string ( self ) -> Result < VideotexStringRef < ' a > > {
138
- self . try_into ( )
139
- }
140
-
141
121
/// Attempt to decode this value an ASN.1 `SEQUENCE`, creating a new
142
122
/// nested reader and calling the provided argument with it.
143
123
pub fn sequence < F , T > ( self , f : F ) -> Result < T >
@@ -154,11 +134,6 @@ impl<'a> AnyRef<'a> {
154
134
pub fn utc_time ( self ) -> Result < UtcTime > {
155
135
self . try_into ( )
156
136
}
157
-
158
- /// Attempt to decode an ASN.1 `UTF8String`.
159
- pub fn utf8_string ( self ) -> Result < Utf8StringRef < ' a > > {
160
- self . try_into ( )
161
- }
162
137
}
163
138
164
139
impl < ' a > Choice < ' a > for AnyRef < ' a > {
@@ -194,6 +169,13 @@ impl Tagged for AnyRef<'_> {
194
169
}
195
170
}
196
171
172
+ #[ cfg( feature = "alloc" ) ]
173
+ impl ValueOrd for Any {
174
+ fn value_cmp ( & self , other : & Self ) -> Result < Ordering > {
175
+ self . value . der_cmp ( & other. value )
176
+ }
177
+ }
178
+
197
179
impl ValueOrd for AnyRef < ' _ > {
198
180
fn value_cmp ( & self , other : & Self ) -> Result < Ordering > {
199
181
self . value . der_cmp ( & other. value )
@@ -226,19 +208,35 @@ pub struct Any {
226
208
tag : Tag ,
227
209
228
210
/// Inner value encoded as bytes.
229
- value : Vec < u8 > ,
211
+ value : Bytes ,
230
212
}
231
213
232
214
#[ cfg( feature = "alloc" ) ]
233
215
impl Any {
234
216
/// Create a new [`Any`] from the provided [`Tag`] and DER bytes.
235
- pub fn new ( tag : Tag , bytes : impl Into < Vec < u8 > > ) -> Result < Self > {
236
- let value = bytes . into ( ) ;
217
+ pub fn new ( tag : Tag , bytes : & [ u8 ] ) -> Result < Self > {
218
+ let value = Bytes :: new ( bytes ) ? ;
237
219
238
220
// Ensure the tag and value are a valid `AnyRef`.
239
- AnyRef :: new ( tag, & value) ?;
221
+ AnyRef :: new ( tag, value. as_slice ( ) ) ?;
240
222
Ok ( Self { tag, value } )
241
223
}
224
+
225
+ /// Attempt to decode this [`Any`] type into the inner value.
226
+ pub fn decode_into < ' a , T > ( & ' a self ) -> Result < T >
227
+ where
228
+ T : DecodeValue < ' a > + FixedTag ,
229
+ {
230
+ self . tag . assert_eq ( T :: TAG ) ?;
231
+ let header = Header {
232
+ tag : self . tag ,
233
+ length : self . value . len ( ) ,
234
+ } ;
235
+
236
+ let mut decoder = SliceReader :: new ( self . value . as_slice ( ) ) ?;
237
+ let result = T :: decode_value ( & mut decoder, header) ?;
238
+ decoder. finish ( result)
239
+ }
242
240
}
243
241
244
242
#[ cfg( feature = "alloc" ) ]
@@ -253,26 +251,26 @@ impl<'a> Decode<'a> for Any {
253
251
fn decode < R : Reader < ' a > > ( reader : & mut R ) -> Result < Self > {
254
252
let header = Header :: decode ( reader) ?;
255
253
let value = reader. read_vec ( header. length ) ?;
256
- Self :: new ( header. tag , value)
254
+ Self :: new ( header. tag , & value)
257
255
}
258
256
}
259
257
260
258
#[ cfg( feature = "alloc" ) ]
261
259
impl EncodeValue for Any {
262
260
fn value_len ( & self ) -> Result < Length > {
263
- self . value . len ( ) . try_into ( )
261
+ Ok ( self . value . len ( ) )
264
262
}
265
263
266
264
fn encode_value ( & self , writer : & mut dyn Writer ) -> Result < ( ) > {
267
- writer. write ( & self . value )
265
+ writer. write ( self . value . as_slice ( ) )
268
266
}
269
267
}
270
268
271
269
#[ cfg( feature = "alloc" ) ]
272
270
impl < ' a > From < & ' a Any > for AnyRef < ' a > {
273
271
fn from ( any : & ' a Any ) -> AnyRef < ' a > {
274
272
// Ensured to parse successfully in constructor
275
- AnyRef :: new ( any. tag , & any. value ) . expect ( "invalid ANY" )
273
+ AnyRef :: new ( any. tag , any. value . as_slice ( ) ) . expect ( "invalid ANY" )
276
274
}
277
275
}
278
276
@@ -282,3 +280,17 @@ impl Tagged for Any {
282
280
self . tag
283
281
}
284
282
}
283
+
284
+ #[ cfg( feature = "alloc" ) ]
285
+ impl < ' a , T > From < T > for Any
286
+ where
287
+ T : Into < AnyRef < ' a > > ,
288
+ {
289
+ fn from ( input : T ) -> Any {
290
+ let anyref: AnyRef < ' a > = input. into ( ) ;
291
+ Self {
292
+ tag : anyref. tag ( ) ,
293
+ value : Bytes :: from ( anyref. value ) ,
294
+ }
295
+ }
296
+ }
0 commit comments