Skip to main content

ketos/
bytes.rs

1//! Implements a reference-counted byte string supporting efficient subslicing.
2
3use std::cmp::Ordering;
4use std::fmt;
5use std::ops;
6use std::slice::Iter;
7
8use crate::rc_vec::{RangeArgument, RcVec};
9
10/// Shared byte string container
11#[derive(Clone)]
12pub struct Bytes(RcVec<u8>);
13
14impl Bytes {
15    /// Creates an empty byte string.
16    pub fn new(v: Vec<u8>) -> Bytes {
17        Bytes(RcVec::new(v))
18    }
19
20    /// Returns whether the byte string is empty.
21    pub fn is_empty(&self) -> bool {
22        self.0.is_empty()
23    }
24
25    /// Returns the number of bytes contained in the byte string.
26    pub fn len(&self) -> usize {
27        self.0.len()
28    }
29
30    /// Returns a subslice of the byte string.
31    pub fn slice<R: RangeArgument<usize>>(&self, range: R) -> Bytes {
32        Bytes(self.0.slice(range))
33    }
34
35    /// Consumes the container and returns a `Vec<u8>`.
36    pub fn into_bytes(self) -> Vec<u8> {
37        self.0.into_vec()
38    }
39
40    /// Inserts a byte at the end of the byte string.
41    pub fn push(&mut self, b: u8) {
42        self.0.push(b);
43    }
44}
45
46impl AsRef<[u8]> for Bytes {
47    fn as_ref(&self) -> &[u8] {
48        self.0.as_ref()
49    }
50}
51
52impl ops::Deref for Bytes {
53    type Target = [u8];
54
55    fn deref(&self) -> &[u8] {
56        self.0.deref()
57    }
58}
59
60impl ops::DerefMut for Bytes {
61    fn deref_mut(&mut self) -> &mut [u8] {
62        self.0.deref_mut()
63    }
64}
65
66impl fmt::Debug for Bytes {
67    fn fmt(&self, f: &mut fmt::Formatter) -> fmt::Result {
68        self.0.fmt(f)
69    }
70}
71
72impl Extend<u8> for Bytes {
73    fn extend<I>(&mut self, iterable: I) where I: IntoIterator<Item=u8> {
74        self.0.extend(iterable);
75    }
76}
77
78impl<'a> Extend<&'a u8> for Bytes {
79    fn extend<I>(&mut self, iterable: I) where I: IntoIterator<Item=&'a u8> {
80        self.0.extend(iterable);
81    }
82}
83
84impl<'a> IntoIterator for &'a Bytes {
85    type Item = &'a u8;
86    type IntoIter = Iter<'a, u8>;
87
88    fn into_iter(self) -> Iter<'a, u8> {
89        self.0.into_iter()
90    }
91}
92
93macro_rules! impl_eq_vec {
94    ( $lhs:ty, $rhs:ty ) => {
95        impl<'a> PartialEq<$rhs> for $lhs {
96            fn eq(&self, rhs: &$rhs) -> bool { self[..] == rhs[..] }
97        }
98    }
99}
100
101macro_rules! impl_eq_array {
102    ( $( $n:expr )+ ) => {
103        $(
104            impl PartialEq<[u8; $n]> for Bytes {
105                fn eq(&self, rhs: &[u8; $n]) -> bool { self[..] == rhs[..] }
106            }
107
108            impl<'a> PartialEq<&'a [u8; $n]> for Bytes {
109                fn eq(&self, rhs: &&'a [u8; $n]) -> bool { self[..] == rhs[..] }
110            }
111        )+
112    }
113}
114
115impl_eq_vec!{ Bytes, Bytes }
116impl_eq_vec!{ Bytes, Vec<u8> }
117impl_eq_vec!{ Bytes, [u8] }
118impl_eq_vec!{ Bytes, &'a [u8] }
119
120impl_eq_array!{
121    0 1 2 3 4 5 6 7 8 9 10 11 12 13 14 15 16
122    17 18 19 20 21 22 23 24 25 26 27 28 29 30 31 32
123}
124
125impl Eq for Bytes {}
126
127impl PartialOrd for Bytes {
128    fn partial_cmp(&self, rhs: &Bytes) -> Option<Ordering> { self.0.partial_cmp(&rhs.0) }
129
130    fn lt(&self, rhs: &Bytes) -> bool { self.0 < rhs.0 }
131    fn le(&self, rhs: &Bytes) -> bool { self.0 <= rhs.0 }
132    fn gt(&self, rhs: &Bytes) -> bool { self.0 > rhs.0 }
133    fn ge(&self, rhs: &Bytes) -> bool { self.0 >= rhs.0 }
134}
135
136impl Ord for Bytes {
137    fn cmp(&self, rhs: &Bytes) -> Ordering { self.0.cmp(&rhs.0) }
138}
139
140impl From<String> for Bytes {
141    fn from(s: String) -> Bytes {
142        Bytes(RcVec::new(s.into_bytes()))
143    }
144}
145
146impl<'a> From<&'a str> for Bytes {
147    fn from(s: &str) -> Bytes {
148        Bytes::from(s.to_owned())
149    }
150}
151
152impl From<Vec<u8>> for Bytes {
153    fn from(b: Vec<u8>) -> Bytes {
154        Bytes(RcVec::new(b))
155    }
156}
157
158impl<'a> From<&'a [u8]> for Bytes {
159    fn from(b: &[u8]) -> Bytes {
160        Bytes(RcVec::new(b.to_vec()))
161    }
162}