Make Deref impls of Buf and BufMut forward more methods

This commit is contained in:
Sean McArthur
2019-12-10 13:35:26 -08:00
parent 7e80f3b646
commit a7fc5274ad
4 changed files with 220 additions and 34 deletions
+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');
}