diff --git a/ntex-bytes/src/buf/buf_impl.rs b/ntex-bytes/src/buf/buf_impl.rs index 592d4e94..0fb8455a 100644 --- a/ntex-bytes/src/buf/buf_impl.rs +++ b/ntex-bytes/src/buf/buf_impl.rs @@ -858,65 +858,6 @@ impl Buf for &str { } } -impl Buf for Option<[u8; 1]> { - fn remaining(&self) -> usize { - if self.is_some() { - 1 - } else { - 0 - } - } - - fn chunk(&self) -> &[u8] { - self.as_ref().map(AsRef::as_ref).unwrap_or_default() - } - - fn advance(&mut self, cnt: usize) { - if cnt == 0 { - return; - } - - if self.is_none() { - panic!("overflow"); - } else { - assert_eq!(1, cnt); - *self = None; - } - } -} - -impl> Buf for std::io::Cursor { - fn remaining(&self) -> usize { - let len = self.get_ref().as_ref().len(); - let pos = self.position(); - - if pos >= len as u64 { - return 0; - } - - len - pos as usize - } - - fn chunk(&self) -> &[u8] { - let len = self.get_ref().as_ref().len(); - let pos = self.position(); - - if pos >= len as u64 { - return &[]; - } - - &self.get_ref().as_ref()[pos as usize..] - } - - fn advance(&mut self, cnt: usize) { - let pos = (self.position() as usize) - .checked_add(cnt) - .expect("overflow"); - - assert!(pos <= self.get_ref().as_ref().len()); - self.set_position(pos as u64); - } -} // The existence of this function makes the compiler catch if the Buf // trait is "object-safe" or not. fn _assert_trait_object(_b: &dyn Buf) {} @@ -938,6 +879,12 @@ mod tests { assert_eq!(&b"hello"[..], &dst); assert_eq!(6, buf.remaining()); + let mut buf = Box::new(buf); + assert_eq!(buf.remaining(), 6); + assert_eq!(buf.chunk(), b" world"); + buf.advance(1); + assert_eq!(buf.chunk(), b"world"); + let mut buf = &b"\x08 hello"[..]; assert_eq!(8, buf.get_u8()); diff --git a/ntex-bytes/src/buf/buf_mut.rs b/ntex-bytes/src/buf/buf_mut.rs index 90bb4c99..60cbbbd5 100644 --- a/ntex-bytes/src/buf/buf_mut.rs +++ b/ntex-bytes/src/buf/buf_mut.rs @@ -980,6 +980,16 @@ mod tests { buf.put_u8(0x01); assert_eq!(buf, b"\x01"); + assert_eq!(buf.remaining_mut(), usize::MAX - 1); + assert_eq!((&buf).remaining_mut(), usize::MAX - 1); + assert_eq!(Box::new(buf).remaining_mut(), usize::MAX - 1); + + let mut buf = [b'1'; 10]; + let mut b = buf.as_mut(); + assert_eq!(b.remaining_mut(), 10); + b.put_slice(b"123"); + assert_eq!(&buf[..], b"1231111111"); + let mut buf = BytesMut::new(); buf.put_u8(0x01); assert_eq!(buf, b"\x01"[..]); diff --git a/ntex-bytes/src/bytes.rs b/ntex-bytes/src/bytes.rs index e3cc8aba..1daca513 100644 --- a/ntex-bytes/src/bytes.rs +++ b/ntex-bytes/src/bytes.rs @@ -2528,6 +2528,15 @@ impl fmt::Write for BytesVec { } } +impl IntoIterator for BytesVec { + type Item = u8; + type IntoIter = IntoIter; + + fn into_iter(self) -> Self::IntoIter { + IntoIter::new(self) + } +} + impl<'a> IntoIterator for &'a BytesVec { type Item = &'a u8; type IntoIter = std::slice::Iter<'a, u8>; diff --git a/ntex-bytes/src/lib.rs b/ntex-bytes/src/lib.rs index 5ccdced4..c40c2693 100644 --- a/ntex-bytes/src/lib.rs +++ b/ntex-bytes/src/lib.rs @@ -51,7 +51,7 @@ //! [struct docs]: struct.Bytes.html #![deny( -// warnings, + warnings, // missing_docs, // missing_debug_implementations, rust_2018_idioms diff --git a/ntex-bytes/src/string.rs b/ntex-bytes/src/string.rs index 8e156a89..683da6c5 100644 --- a/ntex-bytes/src/string.rs +++ b/ntex-bytes/src/string.rs @@ -341,12 +341,16 @@ mod test { #[test] fn test_basics() { let s = ByteString::from_static("test"); + s.trimdown(); assert_eq!(s, "test"); assert_eq!(s, *"test"); assert_eq!(s, "test".to_owned()); + assert_eq!(s.as_slice(), b"test"); + assert_eq!(s.as_bytes(), &Bytes::copy_from_slice(b"test")); assert_eq!(format!("{}", s), "test"); assert_eq!(format!("{:?}", s), "\"test\""); + assert_eq!(s.into_bytes(), Bytes::copy_from_slice(b"test")); } #[test] diff --git a/ntex-bytes/tests/test_bytes.rs b/ntex-bytes/tests/test_bytes.rs index a77066e5..2b1d97ef 100644 --- a/ntex-bytes/tests/test_bytes.rs +++ b/ntex-bytes/tests/test_bytes.rs @@ -437,16 +437,58 @@ fn split_off_to_at_gt_len() { .is_err()); } +#[test] +fn fns_defined_for_bytes() { + let mut bytes = Bytes::from(&b"hello world"[..]); + bytes.as_ptr(); + + assert_eq!(bytes, "hello world"); + assert_eq!(bytes, "hello world".as_bytes().to_vec()); + assert_eq!(bytes, "hello world".to_string()); + assert_eq!(bytes, &"hello world"[..]); + assert_eq!(bytes, BytesVec::copy_from_slice(b"hello world")); + assert_eq!(bytes, BytesMut::copy_from_slice(b"hello world")); + + // Iterator + let v: Vec = bytes.as_ref().iter().cloned().collect(); + assert_eq!(&v[..], bytes); + + let b2: Bytes = v.iter().collect(); + assert_eq!(b2, bytes); + assert_eq!(&v[..], b2); + + bytes.truncate(5); + assert_eq!(bytes, b"hello"[..]); + + bytes.clear(); + assert!(bytes.is_empty()); +} + #[test] fn fns_defined_for_bytes_mut() { let mut bytes = BytesMut::from(&b"hello world"[..]); bytes.as_ptr(); bytes.as_mut_ptr(); + assert_eq!(bytes, "hello world"); + assert_eq!(bytes, "hello world".as_bytes().to_vec()); + assert_eq!(bytes, "hello world".to_string()); + assert_eq!(bytes, &"hello world"[..]); + assert_eq!(bytes, Bytes::copy_from_slice(b"hello world")); + assert_eq!(bytes, BytesVec::copy_from_slice(b"hello world")); + // Iterator let v: Vec = bytes.as_ref().iter().cloned().collect(); assert_eq!(&v[..], bytes); + let v: Vec = bytes.iter().cloned().collect(); + assert_eq!(&v[..], bytes); + + let v: Vec = bytes.clone().into_iter().collect(); + + let mut bytes = BytesMut::copy_from_slice(b"hello world"); + assert_eq!(&v[..], bytes); + let b2: BytesMut = v.iter().collect(); assert_eq!(b2, bytes); assert_eq!(&v[..], b2); @@ -460,15 +502,42 @@ fn fns_defined_for_bytes_mut() { bytes.resize(10, b'1'); assert_eq!(bytes, b"1111111111"[..]); + assert_eq!(bytes.remaining(), 10); + assert_eq!(bytes.chunk(), &b"1111111111"[..]); + + bytes.as_mut()[0] = b'2'; + assert_eq!(bytes, b"2111111111"[..]); + + let b = BytesMut::default(); + assert!(b.is_empty()); +} + +#[test] +fn fns_defined_for_bytes_vec() { // BytesVec let mut bytes = BytesVec::copy_from_slice(&b"hello world"[..]); bytes.as_ptr(); bytes.as_mut_ptr(); + assert_eq!(bytes, "hello world"); + assert_eq!(bytes, "hello world".as_bytes().to_vec()); + assert_eq!(bytes, "hello world".to_string()); + assert_eq!(bytes, &"hello world"[..]); + assert_eq!(bytes, Bytes::copy_from_slice(b"hello world")); + assert_eq!(bytes, BytesMut::copy_from_slice(b"hello world")); + // Iterator let v: Vec = bytes.as_ref().iter().cloned().collect(); assert_eq!(&v[..], bytes); + let v: Vec = bytes.iter().cloned().collect(); + assert_eq!(&v[..], bytes); + + let v: Vec = bytes.into_iter().collect(); + + let mut bytes = BytesVec::copy_from_slice(b"hello world"); + assert_eq!(&v[..], bytes); + let b2: BytesVec = v.iter().collect(); assert_eq!(b2, bytes); assert_eq!(&v[..], b2); @@ -481,6 +550,15 @@ fn fns_defined_for_bytes_mut() { bytes.resize(10, b'1'); assert_eq!(bytes, b"1111111111"[..]); + + assert_eq!(bytes.remaining(), 10); + assert_eq!(bytes.chunk(), &b"1111111111"[..]); + + bytes.as_mut()[0] = b'2'; + assert_eq!(bytes, b"2111111111"[..]); + + let b = BytesMut::default(); + assert!(b.is_empty()); } #[test]