Skip to content
Merged
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
50 changes: 22 additions & 28 deletions src/cursor/mod.rs
Original file line number Diff line number Diff line change
Expand Up @@ -201,6 +201,10 @@ impl<T: AsRef<BufList>> Buf for Cursor<T> {
total.saturating_sub(self.data.pos) as usize
}

fn has_remaining(&self) -> bool {
self.data.num_bytes(self.inner.as_ref()) > self.data.pos
}

fn chunk(&self) -> &[u8] {
self.data.fill_buf_impl(self.inner.as_ref())
}
Expand All @@ -210,40 +214,30 @@ impl<T: AsRef<BufList>> Buf for Cursor<T> {
}

fn chunks_vectored<'iovs>(&'iovs self, iovs: &mut [IoSlice<'iovs>]) -> usize {
if iovs.is_empty() {
let list = self.inner.as_ref();

if iovs.is_empty() || !self.has_remaining() {
return 0;
}

let list = self.inner.as_ref();
let mut filled = 0;
let mut current_chunk = self.data.chunk;
let mut current_pos = self.data.pos;

// Iterate through chunks starting from the current position
while filled < iovs.len() && current_chunk < list.num_chunks() {
if let Some(chunk) = list.get_chunk(current_chunk) {
let chunk_start_pos = list.get_start_pos()[current_chunk];
let offset_in_chunk = (current_pos - chunk_start_pos) as usize;

if offset_in_chunk < chunk.len() {
let chunk_slice = &chunk.as_ref()[offset_in_chunk..];
iovs[filled] = IoSlice::new(chunk_slice);
filled += 1;
}
let current_chunk = self.data.chunk;
let chunk_start_pos = list.get_start_pos()[current_chunk];
let offset_in_chunk = (self.data.pos - chunk_start_pos) as usize;

current_chunk += 1;
// Move to the start of the next chunk
if let Some(&next_start_pos) = list.get_start_pos().get(current_chunk) {
current_pos = next_start_pos;
} else {
break;
}
} else {
break;
}
iovs[0] = IoSlice::new(
&list.get_chunk(current_chunk).expect("chunk is in range")[offset_in_chunk..],
);
// Fill up the remaining iovs with as many slices as possible.
let to_fill = (iovs.len()).min(list.num_chunks() - current_chunk);
for (i, iov) in iovs.iter_mut().enumerate().take(to_fill).skip(1) {
*iov = IoSlice::new(
&list
.get_chunk(current_chunk + i)
.expect("chunk is in range")[..],
);
}

filled
to_fill
}
}

Expand Down
43 changes: 29 additions & 14 deletions src/cursor/tests.rs
Original file line number Diff line number Diff line change
Expand Up @@ -57,7 +57,6 @@ enum CursorOp {
// return value separately.
Consume(prop::sample::Index),
// Buf trait operations
BufRemaining,
BufChunk,
BufAdvance(prop::sample::Index),
BufChunksVectored(prop::sample::Index),
Expand Down Expand Up @@ -192,18 +191,6 @@ impl CursorOp {
buf_list.consume(amt);
oracle.consume(amt);
}
Self::BufRemaining => {
eprintln!("buf_remaining");

let buf_list_remaining = buf_list.remaining();
let oracle_remaining = oracle.remaining();
ensure!(
buf_list_remaining == oracle_remaining,
"remaining didn't match: buf_list {} == oracle {}",
buf_list_remaining,
oracle_remaining
);
}
Self::BufChunk => {
eprintln!("buf_chunk");

Expand Down Expand Up @@ -297,7 +284,8 @@ impl CursorOp {
ensure!(
!buf_list_bytes.is_empty(),
"chunks_vectored should return some data \
when remaining > 0 and num_iovs > 0"
when remaining = {buf_list_remaining} > 0 \
and num_iovs = {num_iovs} > 0"
);
ensure!(
!oracle_bytes.is_empty(),
Expand All @@ -312,6 +300,14 @@ impl CursorOp {
"buf_list chunks_vectored data should match beginning \
of oracle data"
);

// Verify that all iovs up to buf_list_filled are non-empty.
for (i, iov) in buf_list_iovs[..buf_list_filled].iter().enumerate() {
ensure!(
!iov.is_empty(),
"buf_list iov at index {i} should be non-empty",
);
}
} else if buf_list_remaining == 0 {
// If no bytes remaining, should return no data
ensure!(
Expand Down Expand Up @@ -449,6 +445,25 @@ impl CursorOp {
}
}

// Check general properties: remaining and has_remaining are the same.
let buf_list_remaining = buf_list.remaining();
let oracle_remaining = oracle.remaining();
ensure!(
buf_list_remaining == oracle_remaining,
"remaining didn't match: buf_list {} == oracle {}",
buf_list_remaining,
oracle_remaining
);

let buf_list_has_remaining = buf_list.has_remaining();
let oracle_has_remaining = oracle.has_remaining();
ensure!(
buf_list_has_remaining == oracle_has_remaining,
"has_remaining didn't match: buf_list {} == oracle {}",
buf_list_has_remaining,
oracle_has_remaining
);

// Also check that the position is the same.
let buf_list_position = buf_list.position();
ensure!(
Expand Down