@@ -118,6 +118,11 @@ impl<const LEN: usize, const VAL: u8> From<[u8; LEN]> for ReservedBytes<LEN, VAL
118118 }
119119}
120120
121+ impl < const LEN : usize , const VAL : u8 > ReservedBytes < LEN , VAL > {
122+ /// Constant constructor.
123+ pub const fn new ( ) -> Self { Self ( [ VAL ; LEN ] ) }
124+ }
125+
121126mod _reserved {
122127 use strict_encoding:: { DecodeError , ReadTuple , StrictDecode , TypedRead } ;
123128
@@ -148,44 +153,41 @@ mod _reserved {
148153 mod _serde {
149154 use std:: fmt;
150155
151- use serde:: de:: Visitor ;
152- use serde:: { de , Deserialize , Deserializer , Serialize , Serializer } ;
156+ use serde:: de:: { Error , Visitor } ;
157+ use serde:: { Deserialize , Deserializer , Serialize , Serializer } ;
153158
154159 use super :: * ;
155160
156161 impl < const LEN : usize , const VAL : u8 > Serialize for ReservedBytes < LEN , VAL > {
157162 fn serialize < S > ( & self , serializer : S ) -> Result < S :: Ok , S :: Error >
158163 where S : Serializer {
159- // Doing nothing
160- serializer. serialize_unit ( )
164+ serializer. serialize_bytes ( self . 0 . as_ref ( ) )
161165 }
162166 }
163167
164168 impl < ' de , const LEN : usize , const VAL : u8 > Deserialize < ' de > for ReservedBytes < LEN , VAL > {
165169 fn deserialize < D > ( deserializer : D ) -> Result < Self , D :: Error >
166170 where D : Deserializer < ' de > {
167171 #[ derive( Default ) ]
168- pub struct UntaggedUnitVisitor ;
172+ pub struct UntaggedUnitVisitor < const L : usize , const V : u8 > ;
169173
170- impl Visitor < ' _ > for UntaggedUnitVisitor {
171- type Value = ( ) ;
174+ impl < const L : usize , const V : u8 > Visitor < ' _ > for UntaggedUnitVisitor < L , V > {
175+ type Value = ReservedBytes < L , V > ;
172176
173177 fn expecting ( & self , formatter : & mut fmt:: Formatter ) -> fmt:: Result {
174178 write ! ( formatter, "reserved unit" )
175179 }
176180
177- fn visit_none < E > ( self ) -> Result < ( ) , E >
178- where E : de:: Error {
179- Ok ( ( ) )
180- }
181-
182- fn visit_unit < E > ( self ) -> Result < ( ) , E >
183- where E : de:: Error {
184- Ok ( ( ) )
181+ fn visit_bytes < E > ( self , v : & [ u8 ] ) -> Result < Self :: Value , E >
182+ where E : Error {
183+ if v != [ V ; L ] {
184+ return Err ( Error :: custom ( "invalid reserved value" ) ) ;
185+ }
186+ Ok ( ReservedBytes :: < L , V > :: new ( ) )
185187 }
186188 }
187189
188- deserializer. deserialize_unit ( UntaggedUnitVisitor ) ?;
190+ deserializer. deserialize_unit ( UntaggedUnitVisitor :: < LEN , VAL > ) ?;
189191 Ok ( default ! ( ) )
190192 }
191193 }
0 commit comments