1+ use core:: cmp;
12use core:: mem:: MaybeUninit ;
23
34pub const fn _assert_send < T : Send > ( ) { }
@@ -18,7 +19,7 @@ impl<B: AsRef<[u8]>> PartialBuffer<B> {
1819 & self . buffer . as_ref ( ) [ ..self . index ]
1920 }
2021
21- /// Convenient method for `.writen ().len()`
22+ /// Convenience method for `.written ().len()`.
2223 pub fn written_len ( & self ) -> usize {
2324 self . index
2425 }
@@ -75,14 +76,11 @@ impl<B: AsRef<[u8]> + AsMut<[u8]>> From<B> for PartialBuffer<B> {
7576
7677/// Write buffer for compression-codecs.
7778///
78- /// Currently it only supports initialized buffer, but will support uninitialized
79- /// buffer soon.
80- ///
8179/// # Layout
8280///
8381/// ```text
84- /// | buffer |
85- /// | written and initialized | unwritten but initialized | unwritten and uninitialized
82+ /// | buffer |
83+ /// | written and initialized | unwritten but initialized | unwritten and uninitialized |
8684/// ```
8785#[ derive( Debug ) ]
8886pub struct WriteBuffer < ' a > {
@@ -110,6 +108,7 @@ impl<'a> WriteBuffer<'a> {
110108 }
111109 }
112110
111+ /// Returns entire buffer capacity, including space which is not yet initialized.
113112 pub fn capacity ( & self ) -> usize {
114113 self . buffer . len ( )
115114 }
@@ -118,17 +117,23 @@ impl<'a> WriteBuffer<'a> {
118117 self . buffer . as_mut_ptr ( ) as * mut _
119118 }
120119
120+ /// Returns size of buffer's initialized portion.
121+ ///
122+ /// This will always be at least [`written_len`](Self::written_len).
121123 pub fn initialized_len ( & self ) -> usize {
122124 self . initialized
123125 }
124126
125127 pub fn written ( & self ) -> & [ u8 ] {
126128 debug_assert ! ( self . index <= self . initialized) ;
127129
130+ // SAFETY: slice up to `index` is always initialized
128131 unsafe { & * ( & self . buffer [ ..self . index ] as * const _ as * const [ u8 ] ) }
129132 }
130133
131- /// Convenient method for `.writen().len()`
134+ /// Returns size of buffer's written portion.
135+ ///
136+ /// This will always be at most [`initialized_len`](Self::initialized_len).
132137 pub fn written_len ( & self ) -> usize {
133138 self . index
134139 }
@@ -148,6 +153,7 @@ impl<'a> WriteBuffer<'a> {
148153 } ) ;
149154 self . initialized = self . buffer . len ( ) ;
150155
156+ // SAFETY: slice up to `index` is always initialized
151157 unsafe { & mut * ( & mut self . buffer [ self . index ..] as * mut _ as * mut [ u8 ] ) }
152158 }
153159
@@ -202,7 +208,7 @@ impl<'a> WriteBuffer<'a> {
202208 debug_assert ! ( self . index + n <= self . buffer. len( ) ) ;
203209
204210 self . index += n;
205- self . initialized = self . initialized . max ( self . index ) ;
211+ self . initialized = cmp :: max ( self . initialized , self . index ) ;
206212 }
207213
208214 /// Convenient function combining [`WriteBuffer::assume_init`] and [`WriteBuffer::advance`],
@@ -215,15 +221,15 @@ impl<'a> WriteBuffer<'a> {
215221 debug_assert ! ( n <= self . buffer. len( ) ) ;
216222
217223 self . index = n;
218- self . initialized = self . initialized . max ( n) ;
224+ self . initialized = cmp :: max ( self . initialized , n) ;
219225 }
220226
221227 pub fn copy_unwritten_from < C : AsRef < [ u8 ] > > ( & mut self , other : & mut PartialBuffer < C > ) -> usize {
222228 fn inner ( this : & mut WriteBuffer < ' _ > , input : & [ u8 ] ) -> usize {
223229 // Safety: We will never ever write uninitialized bytes into it
224230 let out = unsafe { this. unwritten_mut ( ) } ;
225231
226- let len = out. len ( ) . min ( input. len ( ) ) ;
232+ let len = cmp :: min ( out. len ( ) , input. len ( ) ) ;
227233
228234 out[ ..len]
229235 . iter_mut ( )
0 commit comments