Skip to content

Commit fdd42b0

Browse files
committed
Rename capacity -> max_capacity; add truncate method
1 parent 72f9051 commit fdd42b0

1 file changed

Lines changed: 21 additions & 13 deletions

File tree

  • rust/bufferfish-core/src

rust/bufferfish-core/src/lib.rs

Lines changed: 21 additions & 13 deletions
Original file line numberDiff line numberDiff line change
@@ -111,18 +111,18 @@ impl std::error::Error for BufferfishError {
111111
pub struct Bufferfish {
112112
inner: Cursor<Vec<u8>>,
113113
reading: bool,
114-
capacity: usize,
114+
max_capacity: usize,
115115
}
116116

117117
impl Write for Bufferfish {
118118
fn write(&mut self, bf: &[u8]) -> std::io::Result<usize> {
119-
if self.capacity > 0
120-
&& (bf.len() >= self.capacity || self.as_ref().len() + bf.len() > self.capacity)
119+
if self.max_capacity > 0
120+
&& (bf.len() > self.max_capacity || self.len() + bf.len() > self.max_capacity)
121121
{
122122
return Err(std::io::Error::other(format!(
123123
"write of {} bytes exceeds the max capacity of {} bytes on this Bufferfish",
124124
bf.len(),
125-
self.capacity
125+
self.max_capacity
126126
)));
127127
}
128128

@@ -203,7 +203,7 @@ impl Bufferfish {
203203
Self {
204204
inner: Cursor::new(Vec::new()),
205205
reading: false,
206-
capacity: 1024,
206+
max_capacity: 1024,
207207
}
208208
}
209209

@@ -213,7 +213,7 @@ impl Bufferfish {
213213
Self {
214214
inner: Cursor::new(Vec::with_capacity(capacity)),
215215
reading: false,
216-
capacity,
216+
max_capacity: capacity,
217217
}
218218
}
219219

@@ -246,6 +246,14 @@ impl Bufferfish {
246246
self.reading = false;
247247
}
248248

249+
/// Resizes the internal buffer to the given size (in bytes).
250+
/// This resets the buffer state and clears any existing data.
251+
pub fn truncate(&mut self, len: usize) {
252+
self.inner.get_mut().truncate(len);
253+
self.inner.set_position(0);
254+
self.reading = false;
255+
}
256+
249257
/// Returns a `Vec<u8>` of the internal byte buffer.
250258
pub fn into_vec(self) -> Vec<u8> {
251259
self.inner.into_inner()
@@ -260,7 +268,7 @@ impl Bufferfish {
260268
/// Set the max capacity (in bytes) for the internal buffer.
261269
/// A value of 0 will allow the buffer to grow indefinitely.
262270
pub fn set_max_capacity(&mut self, capacity: usize) {
263-
self.capacity = capacity;
271+
self.max_capacity = capacity;
264272
}
265273

266274
/// Adds a `Bufferfish` or `Vec<u8>` to the end of the buffer.
@@ -291,7 +299,7 @@ impl Bufferfish {
291299
let Some(byte) = self.inner.get_ref().get(pos as usize) else {
292300
return Err(std::io::Error::other(format!(
293301
"peek of 1 byte exceeds the max capacity of {} bytes on this Bufferfish",
294-
self.capacity
302+
self.max_capacity
295303
)))?;
296304
};
297305

@@ -311,7 +319,7 @@ impl Bufferfish {
311319
let Some(bytes) = self.inner.get_ref().get(pos as usize..pos as usize + n) else {
312320
return Err(std::io::Error::other(format!(
313321
"peek of {} bytes exceeds the max capacity of {} bytes on this Bufferfish",
314-
n, self.capacity
322+
n, self.max_capacity
315323
)))?;
316324
};
317325

@@ -674,19 +682,19 @@ impl From<&[u8]> for Bufferfish {
674682
Self {
675683
inner: Cursor::new(slice.to_vec()),
676684
reading: false,
677-
capacity: slice.len(),
685+
max_capacity: slice.len(),
678686
}
679687
}
680688
}
681689

682690
impl From<Vec<u8>> for Bufferfish {
683691
fn from(vec: Vec<u8>) -> Self {
684-
let capacity = vec.len();
692+
let max_capacity = vec.len();
685693

686694
Self {
687695
inner: Cursor::new(vec),
688696
reading: false,
689-
capacity,
697+
max_capacity,
690698
}
691699
}
692700
}
@@ -702,7 +710,7 @@ impl From<bytes::Bytes> for Bufferfish {
702710
Self {
703711
inner: Cursor::new(bytes.to_vec()),
704712
reading: false,
705-
capacity: bytes.len(),
713+
max_capacity: bytes.len(),
706714
}
707715
}
708716
}

0 commit comments

Comments
 (0)