@@ -353,6 +353,7 @@ pub trait Decodable: Sized {
353353 /// should also implement it applying same rules, and in addition make sure to call
354354 /// `consensus_decode_from_finite_reader` on all members, to avoid creating redundant
355355 /// `Take` wrappers. Failure to do so might result only in a tiny performance hit.
356+ #[ inline]
356357 fn consensus_decode_from_finite_reader < R : io:: Read > ( reader : & mut R ) -> Result < Self , Error > {
357358 // This method is always strictly less general than, `consensus_decode`,
358359 // so it's safe and make sense to default to just calling it.
@@ -361,8 +362,18 @@ pub trait Decodable: Sized {
361362 Self :: consensus_decode ( reader)
362363 }
363364
364- /// Decode an object with a well-defined format
365- fn consensus_decode < R : io:: Read > ( reader : & mut R ) -> Result < Self , Error > ;
365+ /// Decode an object with a well-defined format.
366+ ///
367+ /// This is the method that should be implemented for a typical, fixed sized type
368+ /// implementing this trait. Default implementation is wrapping the reader
369+ /// in [`crate::io::Take`] to limit the input size to [`MAX_VEC_SIZE`], and forwards the call to
370+ /// [`Self::consensus_decode_from_finite_reader`], which is convenient
371+ /// for types that override [`Self::consensus_decode_from_finite_reader`]
372+ /// instead.
373+ #[ inline]
374+ fn consensus_decode < R : io:: Read > ( reader : & mut R ) -> Result < Self , Error > {
375+ Self :: consensus_decode_from_finite_reader ( reader. take ( MAX_VEC_SIZE as u64 ) . by_ref ( ) )
376+ }
366377}
367378
368379/// A variable-length unsigned integer
@@ -616,11 +627,6 @@ macro_rules! impl_vec {
616627 }
617628 Ok ( ret)
618629 }
619-
620- #[ inline]
621- fn consensus_decode<R : io:: Read >( d: & mut R ) -> Result <Self , Error > {
622- Self :: consensus_decode_from_finite_reader( & mut d. take( MAX_VEC_SIZE as u64 ) )
623- }
624630 }
625631 }
626632}
@@ -687,11 +693,6 @@ impl Decodable for Vec<u8> {
687693 // most real-world vec of bytes data, wouldn't be larger than 128KiB
688694 read_bytes_from_finite_reader ( r, ReadBytesFromFiniteReaderOpts { len, chunk_size : 128 * 1024 } )
689695 }
690-
691- #[ inline]
692- fn consensus_decode < R : io:: Read > ( r : & mut R ) -> Result < Self , Error > {
693- Self :: consensus_decode_from_finite_reader ( & mut r. take ( MAX_VEC_SIZE as u64 ) )
694- }
695696}
696697
697698impl Encodable for Box < [ u8 ] > {
@@ -706,11 +707,6 @@ impl Decodable for Box<[u8]> {
706707 fn consensus_decode_from_finite_reader < R : io:: Read > ( r : & mut R ) -> Result < Self , Error > {
707708 <Vec < u8 > >:: consensus_decode_from_finite_reader ( r) . map ( From :: from)
708709 }
709-
710- #[ inline]
711- fn consensus_decode < R : io:: Read > ( r : & mut R ) -> Result < Self , Error > {
712- Self :: consensus_decode_from_finite_reader ( & mut r. take ( MAX_VEC_SIZE as u64 ) )
713- }
714710}
715711
716712
@@ -748,10 +744,6 @@ impl Decodable for CheckedData {
748744 Ok ( CheckedData ( ret) )
749745 }
750746 }
751-
752- fn consensus_decode < R : io:: Read > ( d : & mut R ) -> Result < Self , Error > {
753- Self :: consensus_decode_from_finite_reader ( & mut d. take ( MAX_VEC_SIZE as u64 ) )
754- }
755747}
756748
757749// References
0 commit comments