|
114 | 114 | //! } |
115 | 115 | //! ``` |
116 | 116 |
|
| 117 | +#[cfg(feature = "bytes")] |
| 118 | +mod bytes; |
| 119 | + |
117 | 120 | use std::boxed::Box; |
118 | 121 | use std::error; |
119 | 122 | use std::fmt; |
@@ -547,82 +550,6 @@ impl io::Write for CircBuf { |
547 | 550 | } |
548 | 551 | } |
549 | 552 |
|
550 | | -#[cfg(feature = "bytes")] |
551 | | -impl bytes::Buf for CircBuf { |
552 | | - fn remaining(&self) -> usize { |
553 | | - self.len() |
554 | | - } |
555 | | - |
556 | | - fn advance(&mut self, count: usize) { |
557 | | - assert!(count == 0 || count <= self.remaining()); |
558 | | - self.advance_read(count); |
559 | | - } |
560 | | - |
561 | | - fn bytes(&self) -> &[u8] { |
562 | | - let [left, right] = self.get_bytes(); |
563 | | - match (left.is_empty(), right.is_empty()) { |
564 | | - (true, true) => left, |
565 | | - (true, false) => right, |
566 | | - (false, true) => left, |
567 | | - (false, false) => left, |
568 | | - } |
569 | | - } |
570 | | - |
571 | | - fn bytes_vectored<'a>(&'a self, dst: &mut [io::IoSlice<'a>]) -> usize { |
572 | | - let [left, right] = self.get_bytes(); |
573 | | - let mut count = 0; |
574 | | - if let Some(slice) = dst.get_mut(0) { |
575 | | - count += 1; |
576 | | - *slice = io::IoSlice::new(left); |
577 | | - } |
578 | | - if let Some(slice) = dst.get_mut(1) { |
579 | | - count += 1; |
580 | | - *slice = io::IoSlice::new(right); |
581 | | - } |
582 | | - count |
583 | | - } |
584 | | -} |
585 | | - |
586 | | -#[cfg(feature = "bytes")] |
587 | | -impl bytes::BufMut for CircBuf { |
588 | | - fn remaining_mut(&self) -> usize { |
589 | | - self.avail() |
590 | | - } |
591 | | - |
592 | | - unsafe fn advance_mut(&mut self, count: usize) { |
593 | | - assert!(count == 0 || count <= self.remaining_mut()); |
594 | | - self.advance_write(count); |
595 | | - } |
596 | | - |
597 | | - fn bytes_mut<'this>(&'this mut self) -> &'this mut [std::mem::MaybeUninit<u8>] { |
598 | | - let [left, right] = self.get_avail(); |
599 | | - let slice = match (left.is_empty(), right.is_empty()) { |
600 | | - (true, true) => left, |
601 | | - (true, false) => right, |
602 | | - (false, true) => left, |
603 | | - (false, false) => left, |
604 | | - }; |
605 | | - // As far as I can tell it is perfectly safe to convert from u8 to MaybeUninit<u8>. |
606 | | - unsafe { |
607 | | - std::mem::transmute::<&'this mut [u8], &'this mut [std::mem::MaybeUninit<u8>]>(slice) |
608 | | - } |
609 | | - } |
610 | | - |
611 | | - fn bytes_vectored_mut<'a>(&'a mut self, dst: &mut [bytes::buf::IoSliceMut<'a>]) -> usize { |
612 | | - let [left, right] = self.get_avail(); |
613 | | - let mut count = 0; |
614 | | - if let Some(slice) = dst.get_mut(0) { |
615 | | - count += 1; |
616 | | - *slice = bytes::buf::IoSliceMut::from(left); |
617 | | - } |
618 | | - if let Some(slice) = dst.get_mut(1) { |
619 | | - count += 1; |
620 | | - *slice = bytes::buf::IoSliceMut::from(right); |
621 | | - } |
622 | | - count |
623 | | - } |
624 | | -} |
625 | | - |
626 | 553 | #[cfg(test)] |
627 | 554 | mod tests { |
628 | 555 | use super::{CircBuf, CircBufError, DEFAULT_CAPACITY}; |
@@ -932,108 +859,4 @@ mod tests { |
932 | 859 | assert_eq!(c.read_to_string(&mut s).unwrap(), 8); |
933 | 860 | assert_eq!(s, "fizzbuzz"); |
934 | 861 | } |
935 | | - |
936 | | - #[cfg(feature = "bytes")] |
937 | | - #[test] |
938 | | - fn bytes_buf_and_bufmut() { |
939 | | - use bytes::{Buf, BufMut}; |
940 | | - |
941 | | - let mut c = CircBuf::with_capacity(4).unwrap(); |
942 | | - |
943 | | - assert_eq!(c.remaining(), 0); |
944 | | - assert_eq!(c.remaining_mut(), 3); |
945 | | - unsafe { |
946 | | - c.advance_mut(2); |
947 | | - } |
948 | | - assert_eq!(c.remaining(), 2); |
949 | | - assert_eq!(c.remaining_mut(), 1); |
950 | | - c.advance(1); |
951 | | - assert_eq!(c.remaining(), 1); |
952 | | - assert_eq!(c.remaining_mut(), 2); |
953 | | - unsafe { |
954 | | - c.advance_mut(1); |
955 | | - } |
956 | | - assert_eq!(c.remaining(), 2); |
957 | | - assert_eq!(c.remaining_mut(), 1); |
958 | | - |
959 | | - assert_eq!(<CircBuf as Buf>::bytes(&c).len(), 2); |
960 | | - assert_eq!(c.bytes_mut().len(), 1); |
961 | | - |
962 | | - let mut dst = [std::io::IoSlice::new(&[]); 2]; |
963 | | - assert_eq!(c.bytes_vectored(&mut dst[..]), 2); |
964 | | - |
965 | | - assert_eq!(dst[0].len(), 2); |
966 | | - assert_eq!(dst[1].len(), 0); |
967 | | - |
968 | | - let b1: &mut [u8] = &mut []; |
969 | | - let b2: &mut [u8] = &mut []; |
970 | | - let mut dst_mut = [ |
971 | | - bytes::buf::IoSliceMut::from(b1), |
972 | | - bytes::buf::IoSliceMut::from(b2), |
973 | | - ]; |
974 | | - |
975 | | - assert_eq!(c.bytes_vectored_mut(&mut dst_mut[..]), 2); |
976 | | - |
977 | | - assert!(c.has_remaining()); |
978 | | - assert!(c.has_remaining_mut()); |
979 | | - } |
980 | | - |
981 | | - #[cfg(feature = "bytes")] |
982 | | - #[test] |
983 | | - fn bytes_buf_remaining() { |
984 | | - use bytes::{Buf, BufMut}; |
985 | | - |
986 | | - let mut c = CircBuf::with_capacity(4).unwrap(); |
987 | | - |
988 | | - assert_eq!(c.remaining(), 0); |
989 | | - assert_eq!(c.remaining_mut(), 3); |
990 | | - assert!(!c.has_remaining()); |
991 | | - assert!(c.has_remaining_mut()); |
992 | | - |
993 | | - unsafe { |
994 | | - c.advance_mut(3); |
995 | | - } |
996 | | - |
997 | | - assert_eq!(c.remaining(), 3); |
998 | | - assert_eq!(c.remaining_mut(), 0); |
999 | | - assert!(c.has_remaining()); |
1000 | | - assert!(!c.has_remaining_mut()); |
1001 | | - |
1002 | | - c.advance(2); |
1003 | | - |
1004 | | - assert_eq!(c.remaining(), 1); |
1005 | | - assert_eq!(c.remaining_mut(), 2); |
1006 | | - assert!(c.has_remaining()); |
1007 | | - assert!(c.has_remaining_mut()); |
1008 | | - |
1009 | | - c.advance(1); |
1010 | | - |
1011 | | - assert_eq!(c.remaining(), 0); |
1012 | | - assert_eq!(c.remaining_mut(), 3); |
1013 | | - assert!(!c.has_remaining()); |
1014 | | - assert!(c.has_remaining_mut()); |
1015 | | - } |
1016 | | - |
1017 | | - #[cfg(feature = "bytes")] |
1018 | | - #[test] |
1019 | | - fn bytes_bufmut_hello() { |
1020 | | - use bytes::BufMut; |
1021 | | - |
1022 | | - let mut c = CircBuf::with_capacity(16).unwrap(); |
1023 | | - |
1024 | | - unsafe { |
1025 | | - c.bytes_mut()[0].as_mut_ptr().write(b'h'); |
1026 | | - c.bytes_mut()[1].as_mut_ptr().write(b'e'); |
1027 | | - |
1028 | | - c.advance_mut(2); |
1029 | | - |
1030 | | - c.bytes_mut()[0].as_mut_ptr().write(b'l'); |
1031 | | - c.bytes_mut()[1].as_mut_ptr().write(b'l'); |
1032 | | - c.bytes_mut()[2].as_mut_ptr().write(b'o'); |
1033 | | - |
1034 | | - c.advance_mut(3); |
1035 | | - } |
1036 | | - |
1037 | | - assert_eq!(c.get_bytes()[0], b"hello"); |
1038 | | - } |
1039 | 862 | } |
0 commit comments