Skip to content

Commit

Permalink
impl fmt::Write for ByteChunk
Browse files Browse the repository at this point in the history
  • Loading branch information
TimLuq committed Dec 9, 2024
1 parent cfe1771 commit 0dbc8f1
Showing 1 changed file with 22 additions and 0 deletions.
22 changes: 22 additions & 0 deletions src/byte_chunk.rs
Original file line number Diff line number Diff line change
Expand Up @@ -199,3 +199,25 @@ impl core::fmt::Debug for ByteChunk {
core::fmt::Debug::fmt(&rend, f)
}
}

/// Write bytes to the `ByteChunk`.
/// The bytes are written to the end of the chunk and any write that would overflow the chunk results in an error.
impl core::fmt::Write for ByteChunk {
#[inline]
#[allow(clippy::cast_possible_truncation, clippy::min_ident_chars)]
fn write_str(&mut self, s: &str) -> core::fmt::Result {
let s = s.as_bytes();
let len = s.len();
let self_len = self.len();
if len + self_len > Self::LEN {
return Err(core::fmt::Error);
}
let mut i = 0;
while i < len {
self.data[self_len + i] = s[i];
i += 1;
}
self.len += len as u8;
Ok(())
}
}

0 comments on commit 0dbc8f1

Please sign in to comment.