What changed, and why it matters
This commit adds a normal feature: it lets a Cursor (a wrapper that tracks your position while reading/writing a byte buffer) support writing, not just reading. The implementation matches the standard Rust library pattern and includes unit tests. There is no indication this fixes a bug or addresses a security issue.
No security action needed. Treat as a routine feature addition.
Security signals we found
No strong security signals were identified.
Evidence from the diff
The patch implements std::io::Write for Cursor
Changed components
io/src/lib.rsCursor<T>Write trait implementationInspect captured patch +39 / −0
diff --git a/io/src/lib.rs b/io/src/lib.rs
index 15ecdb00..ae1851d8 100644
--- a/io/src/lib.rs
+++ b/io/src/lib.rs
@@ -305,6 +305,20 @@ impl<T: AsRef<[u8]>> BufRead for Cursor<T> {
fn consume(&mut self, amount: usize) { self.pos = self.pos.saturating_add(amount as u64); }
}
+impl<T: AsMut<[u8]>> Write for Cursor<T> {
+ #[inline]
+ fn write(&mut self, buf: &[u8]) -> Result<usize> {
+ let write_slice = self.inner.as_mut();
+ let pos = cmp::min(self.pos, write_slice.len() as u64);
+ let amt = (&mut write_slice[(pos as usize)..]).write(buf)?;
+ self.pos += amt as u64;
+ Ok(amt)
+ }
+
+ #[inline]
+ fn flush(&mut self) -> Result<()> { Ok(()) }
+}
+
/// A generic trait describing an output stream.
///
/// See [`std::io::Write`] for more information.
@@ -524,6 +538,31 @@ mod tests {
assert!(buf.is_empty());
}
+ #[test]
+ fn cursor_write() {
+ let data = [0x78, 0x56, 0x34, 0x12];
+
+ let mut buf = [0_u8; 4];
+ let mut cursor = Cursor::new(&mut buf);
+ let amt = cursor.write(&data).unwrap();
+
+ assert_eq!(buf, data);
+ assert_eq!(amt, 4);
+ }
+
+ #[test]
+ fn cursor_offset_write() {
+ let data = [0x78, 0x56, 0x34, 0x12];
+
+ let mut buf = [0_u8; 4];
+ let mut cursor = Cursor::new(&mut buf);
+ cursor.set_position(2);
+ let amt = cursor.write(&data).unwrap();
+
+ assert_eq!(buf, [0, 0, 0x78, 0x56]);
+ assert_eq!(amt, 2);
+ }
+
#[test]
fn cursor_consume_past_end() {
let data = [1, 2, 3];
Why this scored 15/100
Community notes
Notes can correct, qualify, or add evidence to the AI analysis. Every note shown here has been validated by a human moderator.
The AI analysis stands alone for now. Submit a note if you can add evidence or important context.