Skip to content

Commit

Permalink
Make Deref impls of Buf and BufMut forward more methods
Browse files Browse the repository at this point in the history
  • Loading branch information
seanmonstar committed Dec 10, 2019
1 parent 7e80f3b commit a7fc527
Show file tree
Hide file tree
Showing 4 changed files with 210 additions and 24 deletions.
100 changes: 88 additions & 12 deletions src/buf/buf_impl.rs
Original file line number Diff line number Diff line change
Expand Up @@ -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()
}
Expand All @@ -826,25 +827,100 @@ impl<T: Buf + ?Sized> Buf for &mut T {
fn advance(&mut self, cnt: usize) {
(**self).advance(cnt)
}
}

impl<T: Buf + ?Sized> Buf for Box<T> {
fn remaining(&self) -> usize {
(**self).remaining()
fn has_remaining(&self) -> bool {
(**self).has_remaining()
}

fn bytes(&self) -> &[u8] {
(**self).bytes()
fn copy_to_slice(&mut self, dst: &mut [u8]) {
(**self).copy_to_slice(dst)
}

#[cfg(feature = "std")]
fn bytes_vectored<'b>(&'b self, dst: &mut [IoSlice<'b>]) -> usize {
(**self).bytes_vectored(dst)
fn get_u8(&mut self) -> u8 {
(**self).get_u8()
}

fn advance(&mut self, cnt: usize) {
(**self).advance(cnt)
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> {
deref_forward_buf!();
}

impl Buf for &[u8] {
Expand Down
75 changes: 63 additions & 12 deletions src/buf/buf_mut.rs
Original file line number Diff line number Diff line change
Expand Up @@ -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()
}
Expand All @@ -888,25 +889,75 @@ impl<T: BufMut + ?Sized> BufMut for &mut T {
unsafe fn advance_mut(&mut self, cnt: usize) {
(**self).advance_mut(cnt)
}
}

impl<T: BufMut + ?Sized> BufMut for Box<T> {
fn remaining_mut(&self) -> usize {
(**self).remaining_mut()
fn put_slice(&mut self, src: &[u8]) {
(**self).put_slice(src)
}

fn bytes_mut(&mut self) -> &mut [MaybeUninit<u8>] {
(**self).bytes_mut()
fn put_u8(&mut self, n: u8) {
(**self).put_u8(n)
}

#[cfg(feature = "std")]
fn bytes_vectored_mut<'b>(&'b mut self, dst: &mut [IoSliceMut<'b>]) -> usize {
(**self).bytes_vectored_mut(dst)
fn put_i8(&mut self, n: i8) {
(**self).put_i8(n)
}

unsafe fn advance_mut(&mut self, cnt: usize) {
(**self).advance_mut(cnt)
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> {
deref_forward_bufmut!();
}

impl BufMut for &mut [u8] {
Expand Down
30 changes: 30 additions & 0 deletions tests/test_buf.rs
Original file line number Diff line number Diff line change
Expand Up @@ -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 changes: 29 additions & 0 deletions tests/test_buf_mut.rs
Original file line number Diff line number Diff line change
Expand Up @@ -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');
}

0 comments on commit a7fc527

Please sign in to comment.