Skip to content
Open
Show file tree
Hide file tree
Changes from all commits
Commits
File filter

Filter by extension

Filter by extension

Conversations
Failed to load comments.
Loading
Jump to
Jump to file
Failed to load files.
Loading
Diff view
Diff view
177 changes: 95 additions & 82 deletions library/core/src/io/borrowed_buf.rs

Large diffs are not rendered by default.

282 changes: 273 additions & 9 deletions library/coretests/tests/io/borrowed_buf.rs
Original file line number Diff line number Diff line change
Expand Up @@ -5,7 +5,29 @@ use core::mem::MaybeUninit;
#[test]
fn new() {
let buf: &mut [_] = &mut [0; 16];
let mut rbuf: BorrowedBuf<'_> = buf.into();
let mut rbuf: BorrowedBuf<'_, u8> = buf.into();

assert_eq!(rbuf.filled().len(), 0);
assert!(rbuf.is_init());
assert_eq!(rbuf.capacity(), 16);
assert_eq!(rbuf.unfilled().capacity(), 16);
}

#[test]
fn new_u64() {
let buf: &mut [_] = &mut [0u64; 16];
let mut rbuf = BorrowedBuf::from(buf);

assert_eq!(rbuf.filled().len(), 0);
assert!(rbuf.is_init());
assert_eq!(rbuf.capacity(), 16);
assert_eq!(rbuf.unfilled().capacity(), 16);
}

#[test]
fn new_unit() {
let buf: &mut [_] = &mut [(); 16];
let mut rbuf = BorrowedBuf::from(buf);

assert_eq!(rbuf.filled().len(), 0);
assert!(rbuf.is_init());
Expand All @@ -17,7 +39,29 @@ fn new() {
#[test]
fn uninit() {
let buf: &mut [_] = &mut [MaybeUninit::uninit(); 16];
let mut rbuf: BorrowedBuf<'_> = buf.into();
let mut rbuf: BorrowedBuf<'_, u8> = buf.into();

assert_eq!(rbuf.filled().len(), 0);
assert!(!rbuf.is_init());
assert_eq!(rbuf.capacity(), 16);
assert_eq!(rbuf.unfilled().capacity(), 16);
}

#[test]
fn uninit_u64() {
let buf: &mut [_] = &mut [MaybeUninit::<u64>::uninit(); 16];
let mut rbuf = BorrowedBuf::<u64>::from(buf);

assert_eq!(rbuf.filled().len(), 0);
assert!(!rbuf.is_init());
assert_eq!(rbuf.capacity(), 16);
assert_eq!(rbuf.unfilled().capacity(), 16);
}

#[test]
fn uninit_unit() {
let buf: &mut [_] = &mut [MaybeUninit::<()>::uninit(); 16];
let mut rbuf = BorrowedBuf::<()>::from(buf);

assert_eq!(rbuf.filled().len(), 0);
assert!(!rbuf.is_init());
Expand All @@ -28,7 +72,7 @@ fn uninit() {
#[test]
fn initialize_unfilled() {
let buf: &mut [_] = &mut [MaybeUninit::uninit(); 16];
let mut rbuf: BorrowedBuf<'_> = buf.into();
let mut rbuf: BorrowedBuf<'_, u8> = buf.into();

rbuf.unfilled().ensure_init();

Expand All @@ -38,7 +82,29 @@ fn initialize_unfilled() {
#[test]
fn advance_filled() {
let buf: &mut [_] = &mut [0; 16];
let mut rbuf: BorrowedBuf<'_> = buf.into();
let mut rbuf: BorrowedBuf<'_, u8> = buf.into();

rbuf.unfilled().advance_checked(1);

assert_eq!(rbuf.filled().len(), 1);
assert_eq!(rbuf.unfilled().capacity(), 15);
}

#[test]
fn advance_filled_u64() {
let buf: &mut [_] = &mut [0u64; 16];
let mut rbuf = BorrowedBuf::from(buf);

rbuf.unfilled().advance_checked(1);

assert_eq!(rbuf.filled().len(), 1);
assert_eq!(rbuf.unfilled().capacity(), 15);
}

#[test]
fn advance_filled_unit() {
let buf: &mut [_] = &mut [(); 16];
let mut rbuf = BorrowedBuf::from(buf);

rbuf.unfilled().advance_checked(1);

Expand All @@ -49,7 +115,7 @@ fn advance_filled() {
#[test]
fn clear() {
let buf: &mut [_] = &mut [255; 16];
let mut rbuf: BorrowedBuf<'_> = buf.into();
let mut rbuf: BorrowedBuf<'_, u8> = buf.into();

rbuf.unfilled().advance_checked(16);

Expand All @@ -64,22 +130,116 @@ fn clear() {
assert_eq!(rbuf.unfilled().ensure_init(), [255; 16]);
}

#[test]
fn clear_u64() {
let buf: &mut [_] = &mut [255u64; 16];
let mut rbuf = BorrowedBuf::from(buf);

rbuf.unfilled().advance_checked(16);

assert_eq!(rbuf.filled().len(), 16);
assert_eq!(rbuf.unfilled().capacity(), 0);

rbuf.clear();

assert_eq!(rbuf.filled().len(), 0);
assert_eq!(rbuf.unfilled().capacity(), 16);

unsafe {
rbuf.set_init();
}
rbuf.unfilled().advance_checked(16);
assert_eq!(rbuf.filled(), [255; 16]);
}

#[test]
fn clear_unit() {
let buf: &mut [_] = &mut [(); 16];
let mut rbuf = BorrowedBuf::from(buf);

rbuf.unfilled().advance_checked(16);

assert_eq!(rbuf.filled().len(), 16);
assert_eq!(rbuf.unfilled().capacity(), 0);

rbuf.clear();

assert_eq!(rbuf.filled().len(), 0);
assert_eq!(rbuf.unfilled().capacity(), 16);

unsafe {
rbuf.set_init();
}
rbuf.unfilled().advance_checked(16);
assert_eq!(rbuf.filled(), [(); 16]);
}

#[test]
fn set_init() {
let buf: &mut [_] = &mut [MaybeUninit::zeroed(); 16];
let mut rbuf: BorrowedBuf<'_> = buf.into();
let mut rbuf: BorrowedBuf<'_, u8> = buf.into();

unsafe {
rbuf.set_init();
}

assert!(rbuf.is_init());
rbuf.unfilled().advance_checked(16);
assert_eq!(rbuf.filled(), [0; 16]);
}

#[test]
fn set_init_u64() {
let buf: &mut [_] = &mut [MaybeUninit::<u64>::zeroed(); 16];
let mut rbuf = BorrowedBuf::<u64>::from(buf);

unsafe {
rbuf.set_init();
}

assert!(rbuf.is_init());
rbuf.unfilled().advance_checked(16);
assert_eq!(rbuf.filled(), [0; 16]);
}

#[test]
fn set_init_unit() {
let buf: &mut [_] = &mut [MaybeUninit::<()>::zeroed(); 16];
let mut rbuf = BorrowedBuf::<()>::from(buf);

unsafe {
rbuf.set_init();
}

assert!(rbuf.is_init());
rbuf.unfilled().advance_checked(16);
assert_eq!(rbuf.filled(), [(); 16]);
}

#[test]
fn append() {
let buf: &mut [_] = &mut [MaybeUninit::new(255); 16];
let mut rbuf: BorrowedBuf<'_> = buf.into();
let mut rbuf: BorrowedBuf<'_, u8> = buf.into();

rbuf.unfilled().append(&[0; 8]);

assert!(!rbuf.is_init());
assert_eq!(rbuf.filled().len(), 8);
assert_eq!(rbuf.filled(), [0; 8]);

rbuf.clear();

rbuf.unfilled().append(&[1; 16]);

assert!(!rbuf.is_init());
assert_eq!(rbuf.filled().len(), 16);
assert_eq!(rbuf.filled(), [1; 16]);
}

#[test]
fn append_u64() {
let buf: &mut [_] = &mut [MaybeUninit::new(255u64); 16];
let mut rbuf = BorrowedBuf::<u64>::from(buf);

rbuf.unfilled().append(&[0; 8]);

Expand All @@ -96,10 +256,52 @@ fn append() {
assert_eq!(rbuf.filled(), [1; 16]);
}

#[test]
fn append_unit() {
let buf: &mut [_] = &mut [MaybeUninit::new(()); 16];
let mut rbuf = BorrowedBuf::<()>::from(buf);

rbuf.unfilled().append(&[(); 8]);

assert!(!rbuf.is_init());
assert_eq!(rbuf.filled().len(), 8);
assert_eq!(rbuf.filled(), [(); 8]);

rbuf.clear();

rbuf.unfilled().append(&[(); 16]);

assert!(!rbuf.is_init());
assert_eq!(rbuf.filled().len(), 16);
assert_eq!(rbuf.filled(), [(); 16]);
}

#[test]
fn reborrow_written() {
let buf: &mut [_] = &mut [MaybeUninit::new(0); 32];
let mut buf: BorrowedBuf<'_> = buf.into();
let mut buf: BorrowedBuf<'_, u8> = buf.into();

let mut cursor = buf.unfilled();
cursor.append(&[1; 16]);

let mut cursor2 = cursor.reborrow();
cursor2.append(&[2; 16]);

assert_eq!(cursor2.written(), 32);
assert_eq!(cursor.written(), 32);

assert_eq!(buf.unfilled().written(), 32);
assert!(!buf.is_init());
assert_eq!(buf.filled().len(), 32);
let filled = buf.filled();
assert_eq!(&filled[..16], [1; 16]);
assert_eq!(&filled[16..], [2; 16]);
}

#[test]
fn reborrow_written_u64() {
let buf: &mut [_] = &mut [MaybeUninit::new(0u64); 32];
let mut buf = BorrowedBuf::<u64>::from(buf);

let mut cursor = buf.unfilled();
cursor.append(&[1; 16]);
Expand All @@ -118,10 +320,72 @@ fn reborrow_written() {
assert_eq!(&filled[16..], [2; 16]);
}

#[test]
fn reborrow_written_unit() {
let buf: &mut [_] = &mut [MaybeUninit::new(()); 32];
let mut buf = BorrowedBuf::<()>::from(buf);

let mut cursor = buf.unfilled();
cursor.append(&[(); 16]);

let mut cursor2 = cursor.reborrow();
cursor2.append(&[(); 16]);

assert_eq!(cursor2.written(), 32);
assert_eq!(cursor.written(), 32);

assert_eq!(buf.unfilled().written(), 32);
assert!(!buf.is_init());
assert_eq!(buf.filled().len(), 32);
let filled = buf.filled();
assert_eq!(&filled[..16], [(); 16]);
assert_eq!(&filled[16..], [(); 16]);
}

#[test]
fn cursor_set_init() {
let buf: &mut [_] = &mut [MaybeUninit::zeroed(); 16];
let mut rbuf: BorrowedBuf<'_> = buf.into();
let mut rbuf: BorrowedBuf<'_, u8> = buf.into();
let mut cursor = rbuf.unfilled();

unsafe {
cursor.set_init();
}

assert!(cursor.is_init());
assert_eq!(unsafe { cursor.as_mut().len() }, 16);

cursor.advance_checked(4);

assert_eq!(unsafe { cursor.as_mut().len() }, 12);

assert!(rbuf.is_init());
}

#[test]
fn cursor_set_init_u64() {
let buf: &mut [_] = &mut [MaybeUninit::<u64>::zeroed(); 16];
let mut rbuf = BorrowedBuf::<u64>::from(buf);
let mut cursor = rbuf.unfilled();

unsafe {
cursor.set_init();
}

assert!(cursor.is_init());
assert_eq!(unsafe { cursor.as_mut().len() }, 16);

cursor.advance_checked(4);

assert_eq!(unsafe { cursor.as_mut().len() }, 12);

assert!(rbuf.is_init());
}

#[test]
fn cursor_set_init_unit() {
let buf: &mut [_] = &mut [MaybeUninit::<()>::zeroed(); 16];
let mut rbuf = BorrowedBuf::<()>::from(buf);
let mut cursor = rbuf.unfilled();

unsafe {
Expand Down
4 changes: 2 additions & 2 deletions library/std/src/fs.rs
Original file line number Diff line number Diff line change
Expand Up @@ -1354,7 +1354,7 @@ impl Read for &File {
}

#[inline]
fn read_buf(&mut self, cursor: BorrowedCursor<'_>) -> io::Result<()> {
fn read_buf(&mut self, cursor: BorrowedCursor<'_, u8>) -> io::Result<()> {
self.inner.read_buf(cursor)
}

Expand Down Expand Up @@ -1499,7 +1499,7 @@ impl Read for File {
fn read_vectored(&mut self, bufs: &mut [IoSliceMut<'_>]) -> io::Result<usize> {
(&*self).read_vectored(bufs)
}
fn read_buf(&mut self, cursor: BorrowedCursor<'_>) -> io::Result<()> {
fn read_buf(&mut self, cursor: BorrowedCursor<'_, u8>) -> io::Result<()> {
(&*self).read_buf(cursor)
}
#[inline]
Expand Down
4 changes: 2 additions & 2 deletions library/std/src/io/buffered/bufreader.rs
Original file line number Diff line number Diff line change
Expand Up @@ -350,7 +350,7 @@ impl<R: ?Sized + Read> Read for BufReader<R> {
Ok(nread)
}

fn read_buf(&mut self, mut cursor: BorrowedCursor<'_>) -> io::Result<()> {
fn read_buf(&mut self, mut cursor: BorrowedCursor<'_, u8>) -> io::Result<()> {
// If we don't have any buffered data and we're doing a massive read
// (larger than our internal buffer), bypass our internal buffer
// entirely.
Expand Down Expand Up @@ -381,7 +381,7 @@ impl<R: ?Sized + Read> Read for BufReader<R> {
crate::io::default_read_exact(self, buf)
}

fn read_buf_exact(&mut self, mut cursor: BorrowedCursor<'_>) -> io::Result<()> {
fn read_buf_exact(&mut self, mut cursor: BorrowedCursor<'_, u8>) -> io::Result<()> {
if self.buf.consume_with(cursor.capacity(), |claimed| cursor.append(claimed)) {
return Ok(());
}
Expand Down
Loading
Loading