@@ -381,34 +381,54 @@ impl<T> StrictDecode for PhantomData<T> {
381381 fn strict_decode ( _reader : & mut impl TypedRead ) -> Result < Self , DecodeError > { Ok ( default ! ( ) ) }
382382}
383383
384- // TODO: Provide max length as a trait-level const
385- pub trait StrictSerialize : StrictEncode {
386- fn strict_serialized_len < const MAX : usize > ( & self ) -> io:: Result < usize > {
384+ pub trait StrictSerialize < const MAX_SERIALIZED_LEN : usize > : StrictEncode {
385+ fn strict_serialize ( & self , write : impl io:: Write ) -> Result < ( ) , io:: Error > {
386+ let writer = StreamWriter :: new :: < MAX_SERIALIZED_LEN > ( write) ;
387+ self . strict_write ( writer)
388+ }
389+
390+ fn to_strict_vec ( & self ) -> Result < Confined < Vec < u8 > , 0 , MAX_SERIALIZED_LEN > , SerializeError > {
391+ let ast_data = StrictWriter :: in_memory :: < MAX_SERIALIZED_LEN > ( ) ;
392+ let data = self . strict_encode ( ast_data) ?. unbox ( ) . unconfine ( ) ;
393+ Confined :: < Vec < u8 > , 0 , MAX_SERIALIZED_LEN > :: try_from ( data) . map_err ( SerializeError :: from)
394+ }
395+
396+ //#[cfg(feature = "std")]
397+ fn strict_serialize_to_path (
398+ & self ,
399+ path : impl AsRef < std:: path:: Path > ,
400+ overwrite : bool ,
401+ ) -> Result < ( ) , SerializeError > {
402+ let file = if overwrite { fs:: File :: create ( path) ? } else { fs:: File :: create_new ( path) ? } ;
403+ self . strict_serialize ( file) ?;
404+ Ok ( ( ) )
405+ }
406+
407+ #[ deprecated( since = "3.0.0" , note = "use `StrictEncode::strict_len` instead" ) ]
408+ fn strict_serialized_len < const MAX : usize > ( & self ) -> io:: Result < usize >
409+ where Self : StrictSerialize < MAX > {
387410 let counter = StrictWriter :: counter :: < MAX > ( ) ;
388411 Ok ( self . strict_encode ( counter) ?. unbox ( ) . unconfine ( ) . count )
389412 }
390413
414+ #[ deprecated( since = "3.0.0" , note = "use `to_strict_vec` instead" ) ]
391415 fn to_strict_serialized < const MAX : usize > (
392416 & self ,
393- ) -> Result < Confined < Vec < u8 > , 0 , MAX > , SerializeError > {
394- let ast_data = StrictWriter :: in_memory :: < MAX > ( ) ;
395- let data = self . strict_encode ( ast_data) ?. unbox ( ) . unconfine ( ) ;
396- Confined :: < Vec < u8 > , 0 , MAX > :: try_from ( data) . map_err ( SerializeError :: from)
397- }
398-
399- fn strict_serialize < const MAX : usize > ( & self , write : impl io:: Write ) -> Result < ( ) , io:: Error > {
400- let writer = StreamWriter :: new :: < MAX > ( write) ;
401- self . strict_write ( writer)
417+ ) -> Result < Confined < Vec < u8 > , 0 , MAX > , SerializeError >
418+ where Self : StrictSerialize < MAX > {
419+ self . to_strict_vec ( )
402420 }
403421
422+ #[ deprecated( since = "3.0.0" , note = "use `strict_serialize_to_path` instead" ) ]
404423 fn strict_serialize_to_file < const MAX : usize > (
405424 & self ,
406425 path : impl AsRef < std:: path:: Path > ,
407- ) -> Result < ( ) , SerializeError > {
426+ ) -> Result < ( ) , SerializeError >
427+ where
428+ Self : StrictSerialize < MAX > ,
429+ {
408430 let file = fs:: File :: create ( path) ?;
409- // TODO: Do FileWriter
410- let file = StrictWriter :: with ( StreamWriter :: new :: < MAX > ( file) ) ;
411- self . strict_encode ( file) ?;
431+ StrictSerialize :: < MAX_SERIALIZED_LEN > :: strict_serialize ( self , file) ?;
412432 Ok ( ( ) )
413433 }
414434}
0 commit comments