| 1 | #![allow(missing_docs)] |
| 2 | |
| 3 | mod std_buf; |
| 4 | |
| 5 | #[cfg(feature = "slice-deque")] |
| 6 | mod slice_deque_buf; |
| 7 | |
| 8 | use std::mem::MaybeUninit; |
| 9 | |
| 10 | use self::std_buf::StdBuf; |
| 11 | |
| 12 | #[cfg(feature = "slice-deque")] |
| 13 | use self::slice_deque_buf::SliceDequeBuf; |
| 14 | |
| 15 | pub enum BufImpl { |
| 16 | Std(StdBuf), |
| 17 | #[cfg(feature = "slice-deque")] |
| 18 | Ringbuf(SliceDequeBuf), |
| 19 | } |
| 20 | |
| 21 | macro_rules! forward_method { |
| 22 | ($(#[$m:meta])* |
| 23 | pub fn $fnname:ident(&self $($args:tt)*) [$($passargs:tt)*] $(-> $ret:ty)*) => { |
| 24 | $(#[$m])* |
| 25 | pub fn $fnname(&self $($args)*) $(-> $ret)* { |
| 26 | match *self { |
| 27 | BufImpl::Std(ref buf) => buf.$fnname($($passargs)*), |
| 28 | #[cfg(feature = "slice-deque")] |
| 29 | BufImpl::Ringbuf(ref buf) => buf.$fnname($($passargs)*), |
| 30 | } |
| 31 | } |
| 32 | }; |
| 33 | |
| 34 | ($(#[$m:meta])* |
| 35 | pub fn $fnname:ident(&mut self $($args:tt)*) [$($passargs:tt)*] $(-> $ret:ty)*) => { |
| 36 | $(#[$m])* |
| 37 | pub fn $fnname(&mut self $($args)*) $(-> $ret)* { |
| 38 | match *self { |
| 39 | BufImpl::Std(ref mut buf) => buf.$fnname($($passargs)*), |
| 40 | #[cfg(feature = "slice-deque")] |
| 41 | BufImpl::Ringbuf(ref mut buf) => buf.$fnname($($passargs)*), |
| 42 | } |
| 43 | } |
| 44 | }; |
| 45 | |
| 46 | ($(#[$m:meta])* |
| 47 | pub unsafe fn $fnname:ident(&self $($args:tt)*) [$($passargs:tt)*] $(-> $ret:ty)*) => { |
| 48 | $(#[$m])* |
| 49 | pub unsafe fn $fnname(&self $($args)*) $(-> $ret)* { |
| 50 | match *self { |
| 51 | BufImpl::Std(ref buf) => buf.$fnname($($passargs)*), |
| 52 | #[cfg(feature = "slice-deque")] |
| 53 | BufImpl::Ringbuf(ref buf) => buf.$fnname($($passargs)*), |
| 54 | } |
| 55 | } |
| 56 | }; |
| 57 | |
| 58 | ($(#[$m:meta])* |
| 59 | pub unsafe fn $fnname:ident(&mut self $($args:tt)*) [$($passargs:tt)*] $(-> $ret:ty)*) => { |
| 60 | $(#[$m])* |
| 61 | pub unsafe fn $fnname(&mut self $($args)*) $(-> $ret)* { |
| 62 | match *self { |
| 63 | BufImpl::Std(ref mut buf) => buf.$fnname($($passargs)*), |
| 64 | #[cfg(feature = "slice-deque")] |
| 65 | BufImpl::Ringbuf(ref mut buf) => buf.$fnname($($passargs)*), |
| 66 | } |
| 67 | } |
| 68 | }; |
| 69 | } |
| 70 | |
| 71 | macro_rules! forward_methods { |
| 72 | ($($(#[$m:meta])* |
| 73 | $($qualifiers:ident)+ ($($args:tt)*) [$($passargs:tt)*] $(-> $ret:ty)*);+;) => ( |
| 74 | $(forward_method! { |
| 75 | $(#[$m])* $($qualifiers)+ ($($args)*) [$($passargs)*] $(-> $ret)* |
| 76 | })* |
| 77 | ) |
| 78 | } |
| 79 | |
| 80 | impl BufImpl { |
| 81 | pub fn with_capacity(cap: usize) -> Self { |
| 82 | BufImpl::Std(StdBuf::with_capacity(cap)) |
| 83 | } |
| 84 | |
| 85 | #[cfg(feature = "slice-deque")] |
| 86 | pub fn with_capacity_ringbuf(cap: usize) -> Self { |
| 87 | BufImpl::Ringbuf(SliceDequeBuf::with_capacity(cap)) |
| 88 | } |
| 89 | |
| 90 | pub fn is_ringbuf(&self) -> bool { |
| 91 | match *self { |
| 92 | #[cfg(feature = "slice-deque")] |
| 93 | BufImpl::Ringbuf(_) => true, |
| 94 | _ => false, |
| 95 | } |
| 96 | } |
| 97 | |
| 98 | forward_methods! { |
| 99 | pub fn capacity(&self)[] -> usize; |
| 100 | |
| 101 | pub fn len(&self)[] -> usize; |
| 102 | |
| 103 | pub fn usable_space(&self)[] -> usize; |
| 104 | |
| 105 | pub fn reserve(&mut self, additional: usize)[additional] -> bool; |
| 106 | |
| 107 | pub fn make_room(&mut self)[]; |
| 108 | |
| 109 | #[inline] |
| 110 | pub fn buf(&self)[] -> &[u8]; |
| 111 | |
| 112 | pub fn buf_mut(&mut self)[] -> &mut [u8]; |
| 113 | |
| 114 | pub unsafe fn write_buf(&mut self)[] -> &mut [MaybeUninit<u8>]; |
| 115 | |
| 116 | pub unsafe fn bytes_written(&mut self, add: usize)[add]; |
| 117 | |
| 118 | pub fn consume(&mut self, amt: usize)[amt]; |
| 119 | } |
| 120 | } |