Make Deref impls of Buf and BufMut forward more methods

This commit is contained in:
Sean McArthur
2019-12-06 16:33:04 -08:00
parent 3a14876f84
commit f9a0419036
4 changed files with 220 additions and 34 deletions
+93 -17
View File
@@ -809,7 +809,8 @@ pub trait Buf {
}
}
impl<T: Buf + ?Sized> Buf for &mut T {
macro_rules! deref_forward_buf {
() => (
fn remaining(&self) -> usize {
(**self).remaining()
}
@@ -826,25 +827,100 @@ impl<T: Buf + ?Sized> Buf for &mut T {
fn advance(&mut self, cnt: usize) {
(**self).advance(cnt)
}
fn has_remaining(&self) -> bool {
(**self).has_remaining()
}
fn copy_to_slice(&mut self, dst: &mut [u8]) {
(**self).copy_to_slice(dst)
}
fn get_u8(&mut self) -> u8 {
(**self).get_u8()
}
fn get_i8(&mut self) -> i8 {
(**self).get_i8()
}
fn get_u16(&mut self) -> u16 {
(**self).get_u16()
}
fn get_u16_le(&mut self) -> u16 {
(**self).get_u16_le()
}
fn get_i16(&mut self) -> i16 {
(**self).get_i16()
}
fn get_i16_le(&mut self) -> i16 {
(**self).get_i16_le()
}
fn get_u32(&mut self) -> u32 {
(**self).get_u32()
}
fn get_u32_le(&mut self) -> u32 {
(**self).get_u32_le()
}
fn get_i32(&mut self) -> i32 {
(**self).get_i32()
}
fn get_i32_le(&mut self) -> i32 {
(**self).get_i32_le()
}
fn get_u64(&mut self) -> u64 {
(**self).get_u64()
}
fn get_u64_le(&mut self) -> u64 {
(**self).get_u64_le()
}
fn get_i64(&mut self) -> i64 {
(**self).get_i64()
}
fn get_i64_le(&mut self) -> i64 {
(**self).get_i64_le()
}
fn get_uint(&mut self, nbytes: usize) -> u64 {
(**self).get_uint(nbytes)
}
fn get_uint_le(&mut self, nbytes: usize) -> u64 {
(**self).get_uint_le(nbytes)
}
fn get_int(&mut self, nbytes: usize) -> i64 {
(**self).get_int(nbytes)
}
fn get_int_le(&mut self, nbytes: usize) -> i64 {
(**self).get_int_le(nbytes)
}
fn to_bytes(&mut self) -> crate::Bytes {
(**self).to_bytes()
}
)
}
impl<T: Buf + ?Sized> Buf for &mut T {
deref_forward_buf!();
}
impl<T: Buf + ?Sized> Buf for Box<T> {
fn remaining(&self) -> usize {
(**self).remaining()
}
fn bytes(&self) -> &[u8] {
(**self).bytes()
}
#[cfg(feature = "std")]
fn bytes_vectored<'b>(&'b self, dst: &mut [IoSlice<'b>]) -> usize {
(**self).bytes_vectored(dst)
}
fn advance(&mut self, cnt: usize) {
(**self).advance(cnt)
}
deref_forward_buf!();
}
impl Buf for &[u8] {
+68 -17
View File
@@ -871,7 +871,8 @@ pub trait BufMut {
}
}
impl<T: BufMut + ?Sized> BufMut for &mut T {
macro_rules! deref_forward_bufmut {
() => (
fn remaining_mut(&self) -> usize {
(**self).remaining_mut()
}
@@ -888,25 +889,75 @@ impl<T: BufMut + ?Sized> BufMut for &mut T {
unsafe fn advance_mut(&mut self, cnt: usize) {
(**self).advance_mut(cnt)
}
fn put_slice(&mut self, src: &[u8]) {
(**self).put_slice(src)
}
fn put_u8(&mut self, n: u8) {
(**self).put_u8(n)
}
fn put_i8(&mut self, n: i8) {
(**self).put_i8(n)
}
fn put_u16(&mut self, n: u16) {
(**self).put_u16(n)
}
fn put_u16_le(&mut self, n: u16) {
(**self).put_u16_le(n)
}
fn put_i16(&mut self, n: i16) {
(**self).put_i16(n)
}
fn put_i16_le(&mut self, n: i16) {
(**self).put_i16_le(n)
}
fn put_u32(&mut self, n: u32) {
(**self).put_u32(n)
}
fn put_u32_le(&mut self, n: u32) {
(**self).put_u32_le(n)
}
fn put_i32(&mut self, n: i32) {
(**self).put_i32(n)
}
fn put_i32_le(&mut self, n: i32) {
(**self).put_i32_le(n)
}
fn put_u64(&mut self, n: u64) {
(**self).put_u64(n)
}
fn put_u64_le(&mut self, n: u64) {
(**self).put_u64_le(n)
}
fn put_i64(&mut self, n: i64) {
(**self).put_i64(n)
}
fn put_i64_le(&mut self, n: i64) {
(**self).put_i64_le(n)
}
)
}
impl<T: BufMut + ?Sized> BufMut for &mut T {
deref_forward_bufmut!();
}
impl<T: BufMut + ?Sized> BufMut for Box<T> {
fn remaining_mut(&self) -> usize {
(**self).remaining_mut()
}
fn bytes_mut(&mut self) -> &mut [MaybeUninit<u8>] {
(**self).bytes_mut()
}
#[cfg(feature = "std")]
fn bytes_vectored_mut<'b>(&'b mut self, dst: &mut [IoSliceMut<'b>]) -> usize {
(**self).bytes_vectored_mut(dst)
}
unsafe fn advance_mut(&mut self, cnt: usize) {
(**self).advance_mut(cnt)
}
deref_forward_bufmut!();
}
impl BufMut for &mut [u8] {
+30
View File
@@ -69,3 +69,33 @@ fn test_vec_deque() {
buffer.copy_to_slice(&mut out);
assert_eq!(b"world piece", &out[..]);
}
#[test]
fn test_deref_buf_forwards() {
struct Special;
impl Buf for Special {
fn remaining(&self) -> usize {
unreachable!("remaining");
}
fn bytes(&self) -> &[u8] {
unreachable!("bytes");
}
fn advance(&mut self, _: usize) {
unreachable!("advance");
}
fn get_u8(&mut self) -> u8 {
// specialized!
b'x'
}
}
// these should all use the specialized method
assert_eq!(Special.get_u8(), b'x');
assert_eq!((&mut Special as &mut dyn Buf).get_u8(), b'x');
assert_eq!((Box::new(Special) as Box<dyn Buf>).get_u8(), b'x');
assert_eq!(Box::new(Special).get_u8(), b'x');
}
+29
View File
@@ -87,3 +87,32 @@ fn test_mut_slice() {
let mut s = &mut v[..];
s.put_u32(42);
}
#[test]
fn test_deref_bufmut_forwards() {
struct Special;
impl BufMut for Special {
fn remaining_mut(&self) -> usize {
unreachable!("remaining_mut");
}
fn bytes_mut(&mut self) -> &mut [std::mem::MaybeUninit<u8>] {
unreachable!("bytes_mut");
}
unsafe fn advance_mut(&mut self, _: usize) {
unreachable!("advance");
}
fn put_u8(&mut self, _: u8) {
// specialized!
}
}
// these should all use the specialized method
Special.put_u8(b'x');
(&mut Special as &mut dyn BufMut).put_u8(b'x');
(Box::new(Special) as Box<dyn BufMut>).put_u8(b'x');
Box::new(Special).put_u8(b'x');
}