@@ -53,6 +53,11 @@ mod sealed;
5353
5454pub use read:: ReadBuffer ;
5555
56+ /// Trait implemented for supported block sizes, i.e. for types from `U1` to `U255`.
57+ pub trait BlockSizes : ArraySize + sealed:: BlockSizes { }
58+
59+ impl < T : ArraySize + sealed:: BlockSizes > BlockSizes for T { }
60+
5661/// Trait for buffer kinds.
5762pub trait BufferKind : sealed:: Sealed { }
5863
@@ -86,32 +91,22 @@ impl fmt::Display for Error {
8691}
8792
8893/// Buffer for block processing of data.
89- pub struct BlockBuffer < BS : ArraySize , K : BufferKind > {
94+ pub struct BlockBuffer < BS : BlockSizes , K : BufferKind > {
9095 buffer : MaybeUninit < Array < u8 , BS > > ,
9196 pos : K :: Pos ,
9297}
9398
94- impl < BS : ArraySize , K : BufferKind > BlockBuffer < BS , K > {
95- /// This associated constant is used to assert block size correctness at compile time.
96- const BLOCK_SIZE_ASSERT : bool = {
97- assert ! ( BS :: USIZE != 0 , "Block size can not be equal to zero!" ) ;
98- assert ! ( BS :: USIZE <= 255 , "Block size can not be bigger than 255!" ) ;
99- true
100- } ;
101- }
102-
103- impl < BS : ArraySize , K : BufferKind > Default for BlockBuffer < BS , K > {
99+ impl < BS : BlockSizes , K : BufferKind > Default for BlockBuffer < BS , K > {
104100 #[ inline]
105101 fn default ( ) -> Self {
106- assert ! ( Self :: BLOCK_SIZE_ASSERT ) ;
107102 let mut buffer = MaybeUninit :: uninit ( ) ;
108103 let mut pos = Default :: default ( ) ;
109104 K :: set_pos ( & mut buffer, & mut pos, 0 ) ;
110105 Self { buffer, pos }
111106 }
112107}
113108
114- impl < BS : ArraySize , K : BufferKind > Clone for BlockBuffer < BS , K > {
109+ impl < BS : BlockSizes , K : BufferKind > Clone for BlockBuffer < BS , K > {
115110 #[ inline]
116111 fn clone ( & self ) -> Self {
117112 // SAFETY: `BlockBuffer` does not implement `Drop` (i.e. it could be a `Copy` type),
@@ -120,7 +115,7 @@ impl<BS: ArraySize, K: BufferKind> Clone for BlockBuffer<BS, K> {
120115 }
121116}
122117
123- impl < BS : ArraySize , K : BufferKind > fmt:: Debug for BlockBuffer < BS , K > {
118+ impl < BS : BlockSizes , K : BufferKind > fmt:: Debug for BlockBuffer < BS , K > {
124119 fn fmt ( & self , f : & mut fmt:: Formatter < ' _ > ) -> Result < ( ) , fmt:: Error > {
125120 f. debug_struct ( K :: NAME )
126121 . field ( "pos" , & self . get_pos ( ) )
@@ -130,7 +125,7 @@ impl<BS: ArraySize, K: BufferKind> fmt::Debug for BlockBuffer<BS, K> {
130125 }
131126}
132127
133- impl < BS : ArraySize , K : BufferKind > BlockBuffer < BS , K > {
128+ impl < BS : BlockSizes , K : BufferKind > BlockBuffer < BS , K > {
134129 /// Create new buffer from slice.
135130 ///
136131 /// # Panics
@@ -148,10 +143,6 @@ impl<BS: ArraySize, K: BufferKind> BlockBuffer<BS, K> {
148143 /// If slice length is not valid for used buffer kind.
149144 #[ inline( always) ]
150145 pub fn try_new ( buf : & [ u8 ] ) -> Result < Self , Error > {
151- const {
152- assert ! ( Self :: BLOCK_SIZE_ASSERT ) ;
153- }
154-
155146 if !K :: invariant ( buf. len ( ) , BS :: USIZE ) {
156147 return Err ( Error ) ;
157148 }
@@ -318,7 +309,7 @@ pub type SerializedBufferSize<BS, K> = Sum<BS, <K as sealed::Sealed>::Overhead>;
318309/// `BlockBuffer` serialized as a byte array.
319310pub type SerializedBuffer < BS , K > = Array < u8 , SerializedBufferSize < BS , K > > ;
320311
321- impl < BS : ArraySize , K : BufferKind > BlockBuffer < BS , K >
312+ impl < BS : BlockSizes , K : BufferKind > BlockBuffer < BS , K >
322313where
323314 BS : core:: ops:: Add < K :: Overhead > ,
324315 Sum < BS , K :: Overhead > : ArraySize ,
@@ -337,7 +328,7 @@ where
337328 /// Deserialize buffer from a byte array.
338329 ///
339330 /// # Errors
340- /// If algorithm-specific invariant fails to hold
331+ /// If `buf` does not represent a valid serialization of `BlockBuffer`.
341332 pub fn deserialize ( buf : & SerializedBuffer < BS , K > ) -> Result < Self , Error > {
342333 let ( pos, block) = buf. split_at ( 1 ) ;
343334 let pos = usize:: from ( pos[ 0 ] ) ;
@@ -358,7 +349,7 @@ where
358349 }
359350}
360351
361- impl < BS : ArraySize > BlockBuffer < BS , Eager > {
352+ impl < BS : BlockSizes > BlockBuffer < BS , Eager > {
362353 /// Compress remaining data after padding it with `delim`, zeros and
363354 /// the `suffix` bytes. If there is not enough unused space, `compress`
364355 /// will be called twice.
@@ -413,15 +404,15 @@ impl<BS: ArraySize> BlockBuffer<BS, Eager> {
413404}
414405
415406#[ cfg( feature = "zeroize" ) ]
416- impl < BS : ArraySize , K : BufferKind > Zeroize for BlockBuffer < BS , K > {
407+ impl < BS : BlockSizes , K : BufferKind > Zeroize for BlockBuffer < BS , K > {
417408 #[ inline]
418409 fn zeroize ( & mut self ) {
419410 self . buffer . zeroize ( ) ;
420411 self . pos . zeroize ( ) ;
421412 }
422413}
423414
424- impl < BS : ArraySize , K : BufferKind > Drop for BlockBuffer < BS , K > {
415+ impl < BS : BlockSizes , K : BufferKind > Drop for BlockBuffer < BS , K > {
425416 #[ inline]
426417 fn drop ( & mut self ) {
427418 #[ cfg( feature = "zeroize" ) ]
@@ -430,4 +421,4 @@ impl<BS: ArraySize, K: BufferKind> Drop for BlockBuffer<BS, K> {
430421}
431422
432423#[ cfg( feature = "zeroize" ) ]
433- impl < BS : ArraySize , K : BufferKind > ZeroizeOnDrop for BlockBuffer < BS , K > { }
424+ impl < BS : BlockSizes , K : BufferKind > ZeroizeOnDrop for BlockBuffer < BS , K > { }
0 commit comments