22
33/// Document Content bytes (COSE payload).
44#[ derive( Debug , Clone , PartialEq , Default ) ]
5- pub struct Content ( Vec < u8 > ) ;
5+ pub struct Content ( Option < Vec < u8 > > ) ;
66
77impl Content {
88 /// Return content bytes.
99 #[ must_use]
1010 pub fn bytes ( & self ) -> & [ u8 ] {
11- self . 0 . as_slice ( )
12- }
13-
14- /// Return content byte size.
15- #[ must_use]
16- pub fn size ( & self ) -> usize {
17- self . 0 . len ( )
11+ self . 0 . as_ref ( ) . map ( |v| v. as_slice ( ) ) . unwrap_or ( & [ ] )
1812 }
1913}
2014
2115impl From < Vec < u8 > > for Content {
2216 fn from ( value : Vec < u8 > ) -> Self {
23- Self ( value)
17+ Self ( Some ( value) )
2418 }
2519}
2620
@@ -30,13 +24,16 @@ impl minicbor::Encode<()> for Content {
3024 e : & mut minicbor:: Encoder < W > ,
3125 _ctx : & mut ( ) ,
3226 ) -> Result < ( ) , minicbor:: encode:: Error < W :: Error > > {
33- if self . 0 . is_empty ( ) {
34- e. null ( ) ?;
35- } else {
36- e. bytes ( self . 0 . as_slice ( ) ) ?;
37- }
27+ match & self . 0 {
28+ Some ( bytes) => e. bytes ( & bytes) ?,
29+ None => e. null ( ) ?,
30+ } ;
3831 Ok ( ( ) )
3932 }
33+
34+ fn is_nil ( & self ) -> bool {
35+ self . 0 . is_none ( )
36+ }
4037}
4138
4239impl minicbor:: Decode < ' _ , ( ) > for Content {
@@ -46,11 +43,11 @@ impl minicbor::Decode<'_, ()> for Content {
4643 ) -> Result < Self , minicbor:: decode:: Error > {
4744 let p = d. position ( ) ;
4845 d. null ( )
49- . map ( |( ) | Self ( Vec :: new ( ) ) )
46+ . map ( |( ) | Self ( None ) )
5047 // important to use `or_else` so it will lazy evaluated at the time when it is needed
5148 . or_else ( |_| {
5249 d. set_position ( p) ;
53- d. bytes ( ) . map ( Vec :: from) . map ( Self )
50+ d. bytes ( ) . map ( Vec :: from) . map ( Some ) . map ( Self )
5451 } )
5552 }
5653}
0 commit comments