Skip to content

Commit 1c40ef1

Browse files
committed
Add BufMutSlice::(has_)spare_capacity
spare_capacity returns the total amount of unused bytes in all buffers. And has_spare_capacity return true if that number is larger than zero.
1 parent 085fc02 commit 1c40ef1

File tree

3 files changed

+53
-0
lines changed

3 files changed

+53
-0
lines changed

src/io/mod.rs

Lines changed: 8 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -730,6 +730,14 @@ unsafe impl<B: BufMutSlice<N>, const N: usize> BufMutSlice<N> for ReadNBuf<B> {
730730
self.last_read = n;
731731
unsafe { self.buf.set_init(n) };
732732
}
733+
734+
fn spare_capacity(&self) -> u32 {
735+
self.buf.spare_capacity()
736+
}
737+
738+
fn has_spare_capacity(&self) -> bool {
739+
self.buf.has_spare_capacity()
740+
}
733741
}
734742

735743
/// Wrapper around a buffer `B` to skip a number of bytes.

src/io/traits.rs

Lines changed: 29 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -270,6 +270,19 @@ pub unsafe trait BufMutSlice<const N: usize>: 'static {
270270
/// buffer with `n = 8` and the second with `n = 10 - 8 = 2`.
271271
unsafe fn set_init(&mut self, n: usize);
272272

273+
/// Returns the total amount of unused bytes in all buffers.
274+
///
275+
/// This must be the same as the total length of all slices as returned by
276+
/// [`as_iovecs_mut`].
277+
///
278+
/// [`as_iovecs_mut`]: BufMutSlice::as_iovecs_mut
279+
fn spare_capacity(&self) -> u32;
280+
281+
/// Returns `true` if any of the buffers has spare capacity.
282+
fn has_spare_capacity(&self) -> bool {
283+
self.spare_capacity() != 0
284+
}
285+
273286
/// Extend the buffer with `bytes`, returns the total number of bytes
274287
/// copied.
275288
///
@@ -388,6 +401,14 @@ unsafe impl<B: BufMut, const N: usize> BufMutSlice<N> for [B; N] {
388401
n - left
389402
);
390403
}
404+
405+
fn spare_capacity(&self) -> u32 {
406+
self.iter().map(BufMut::spare_capacity).sum()
407+
}
408+
409+
fn has_spare_capacity(&self) -> bool {
410+
self.iter().any(BufMut::has_spare_capacity)
411+
}
391412
}
392413

393414
// NOTE: Also see implementation of `BufMutSlice` for tuples in the macro
@@ -788,6 +809,14 @@ macro_rules! buf_slice_for_tuple {
788809
n - left
789810
);
790811
}
812+
813+
fn spare_capacity(&self) -> u32 {
814+
0 $( + self.$index.spare_capacity())+
815+
}
816+
817+
fn has_spare_capacity(&self) -> bool {
818+
false $( || self.$index.has_spare_capacity())+
819+
}
791820
}
792821

793822
// SAFETY: `BufSlice` has the same safety requirements as `Buf` and

tests/util/mod.rs

Lines changed: 16 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -662,6 +662,14 @@ unsafe impl BufMutSlice<2> for BadReadBufSlice {
662662
}
663663
}
664664
}
665+
666+
fn spare_capacity(&self) -> u32 {
667+
unimplemented!()
668+
}
669+
670+
fn has_spare_capacity(&self) -> bool {
671+
unimplemented!()
672+
}
665673
}
666674

667675
// NOTE: this implementation is BROKEN! It's only used to test the write_all
@@ -679,6 +687,14 @@ unsafe impl BufMutSlice<2> for GrowingBufSlice {
679687
unsafe { self.data.set_init(n) };
680688
self.data[1].reserve(200);
681689
}
690+
691+
fn spare_capacity(&self) -> u32 {
692+
unimplemented!()
693+
}
694+
695+
fn has_spare_capacity(&self) -> bool {
696+
unimplemented!()
697+
}
682698
}
683699

684700
// NOTE: this implementation is BROKEN! It's only used to test the

0 commit comments

Comments
 (0)