buf_redux/buffer/
mod.rs

1// Copyright 2018 Austin Bonander <austin.bonander@gmail.com>
2//
3// Licensed under the Apache License, Version 2.0 <LICENSE-APACHE or
4// http://www.apache.org/licenses/LICENSE-2.0> or the MIT license
5// <LICENSE-MIT or http://opensource.org/licenses/MIT>, at your
6// option. This file may not be copied, modified, or distributed
7// except according to those terms.
8#![allow(missing_docs)]
9
10mod std_buf;
11
12#[cfg(feature = "slice-deque")]
13mod slice_deque_buf;
14
15use self::std_buf::StdBuf;
16
17#[cfg(feature = "slice-deque")]
18use self::slice_deque_buf::SliceDequeBuf;
19
20pub enum BufImpl {
21    Std(StdBuf),
22    #[cfg(feature = "slice-deque")]
23    Ringbuf(SliceDequeBuf),
24}
25
26macro_rules! forward_method {
27    (pub fn $fnname:ident(&self $($args:tt)*) [$($passargs:tt)*] $(-> $ret:ty)*) => {
28        pub fn $fnname(&self $($args)*) $(-> $ret)* {
29            match *self {
30                BufImpl::Std(ref buf) => buf.$fnname($($passargs)*),
31                #[cfg(feature = "slice-deque")]
32                BufImpl::Ringbuf(ref buf) => buf.$fnname($($passargs)*),
33            }
34        }
35    };
36
37    (pub fn $fnname:ident(&mut self $($args:tt)*) [$($passargs:tt)*] $(-> $ret:ty)*) => {
38        pub fn $fnname(&mut self $($args)*) $(-> $ret)* {
39            match *self {
40                BufImpl::Std(ref mut buf) => buf.$fnname($($passargs)*),
41                #[cfg(feature = "slice-deque")]
42                BufImpl::Ringbuf(ref mut buf) => buf.$fnname($($passargs)*),
43            }
44        }
45    };
46
47    (pub unsafe fn $fnname:ident(&self $($args:tt)*) [$($passargs:tt)*] $(-> $ret:ty)*) => {
48        pub unsafe fn $fnname(&self $($args)*) $(-> $ret)* {
49            match *self {
50                BufImpl::Std(ref buf) => buf.$fnname($($passargs)*),
51                #[cfg(feature = "slice-deque")]
52                BufImpl::Ringbuf(ref buf) => buf.$fnname($($passargs)*),
53            }
54        }
55    };
56
57    (pub unsafe fn $fnname:ident(&mut self $($args:tt)*) [$($passargs:tt)*] $(-> $ret:ty)*) => {
58        pub unsafe fn $fnname(&mut self $($args)*) $(-> $ret)* {
59            match *self {
60                BufImpl::Std(ref mut buf) => buf.$fnname($($passargs)*),
61                #[cfg(feature = "slice-deque")]
62                BufImpl::Ringbuf(ref mut buf) => buf.$fnname($($passargs)*),
63            }
64        }
65    };
66}
67
68macro_rules! forward_methods {
69    ($($($qualifiers:ident)+ ($($args:tt)*) [$($passargs:tt)*] $(-> $ret:ty)*);+;) => (
70        $(forward_method! {
71            $($qualifiers)+ ($($args)*) [$($passargs)*] $(-> $ret)*
72        })*
73    )
74}
75
76impl BufImpl {
77    pub fn with_capacity(cap: usize) -> Self {
78        BufImpl::Std(StdBuf::with_capacity(cap))
79    }
80
81    #[cfg(feature = "slice-deque")]
82    pub fn with_capacity_ringbuf(cap: usize) -> Self {
83        BufImpl::Ringbuf(SliceDequeBuf::with_capacity(cap))
84    }
85
86    pub fn is_ringbuf(&self) -> bool {
87        match *self {
88            #[cfg(feature = "slice-deque")]
89            BufImpl::Ringbuf(_) => true,
90            _ => false,
91        }
92    }
93
94    forward_methods! {
95        pub fn capacity(&self)[] -> usize;
96
97        pub fn len(&self)[] -> usize;
98
99        pub fn usable_space(&self)[] -> usize;
100
101        pub fn reserve(&mut self, additional: usize)[additional] -> bool;
102
103        pub fn make_room(&mut self)[];
104
105        pub fn buf(&self)[] -> &[u8];
106
107        pub fn buf_mut(&mut self)[] -> &mut [u8];
108
109        pub unsafe fn write_buf(&mut self)[] -> &mut [u8];
110
111        pub unsafe fn bytes_written(&mut self, add: usize)[add];
112
113        pub fn consume(&mut self, amt: usize)[amt];
114    }
115}