forked from jeromefroe/circbuf-rs
-
Notifications
You must be signed in to change notification settings - Fork 0
/
bytes.rs
executable file
·158 lines (127 loc) · 4.06 KB
/
bytes.rs
1
2
3
4
5
6
7
8
9
10
11
12
13
14
15
16
17
18
19
20
21
22
23
24
25
26
27
28
29
30
31
32
33
34
35
36
37
38
39
40
41
42
43
44
45
46
47
48
49
50
51
52
53
54
55
56
57
58
59
60
61
62
63
64
65
66
67
68
69
70
71
72
73
74
75
76
77
78
79
80
81
82
83
84
85
86
87
88
89
90
91
92
93
94
95
96
97
98
99
100
101
102
103
104
105
106
107
108
109
110
111
112
113
114
115
116
117
118
119
120
121
122
123
124
125
126
127
128
129
130
131
132
133
134
135
136
137
138
139
140
141
142
143
144
145
146
147
148
149
150
151
152
153
154
155
156
157
158
use std::io::IoSlice;
use crate::CircBuf;
use bytes::{buf::UninitSlice, Buf, BufMut};
impl Buf for CircBuf {
fn advance(&mut self, count: usize) {
assert!(count == 0 || count <= self.remaining());
self.advance_read(count);
}
fn chunk(&self) -> &[u8] {
let [left, right] = self.get_bytes();
match (left.is_empty(), right.is_empty()) {
(true, true) => left,
(true, false) => right,
(false, true) => left,
(false, false) => left,
}
}
fn remaining(&self) -> usize {
self.len()
}
fn chunks_vectored<'a>(&'a self, dst: &mut [IoSlice<'a>]) -> usize {
let [left, right] = self.get_bytes();
let mut count = 0;
if let Some(slice) = dst.get_mut(0) {
count += 1;
*slice = IoSlice::new(left);
}
if let Some(slice) = dst.get_mut(1) {
count += 1;
*slice = IoSlice::new(right);
}
count
}
}
unsafe impl BufMut for CircBuf {
unsafe fn advance_mut(&mut self, count: usize) {
assert!(count == 0 || count <= self.remaining_mut());
self.advance_write(count);
}
fn chunk_mut<'this>(&'this mut self) -> &'this mut UninitSlice {
let [left, right] = self.get_avail();
let slice = match (left.is_empty(), right.is_empty()) {
(true, true) => left,
(true, false) => right,
(false, true) => left,
(false, false) => left,
};
// https://docs.rs/bytes/latest/bytes/buf/struct.UninitSlice.html#method.from_raw_parts_mut
unsafe { UninitSlice::from_raw_parts_mut(slice.as_mut_ptr(), slice.len()) }
}
fn remaining_mut(&self) -> usize {
self.avail()
}
}
#[cfg(test)]
mod tests {
use crate::CircBuf;
use bytes::{Buf, BufMut};
use std::io::IoSlice;
#[test]
fn bytes_buf_and_bufmut() {
let mut c = CircBuf::with_capacity(4).unwrap();
assert_eq!(c.remaining(), 0);
assert_eq!(c.remaining_mut(), 3);
unsafe {
c.advance_mut(2);
}
assert_eq!(c.remaining(), 2);
assert_eq!(c.remaining_mut(), 1);
c.advance(1);
assert_eq!(c.remaining(), 1);
assert_eq!(c.remaining_mut(), 2);
unsafe {
c.advance_mut(1);
}
assert_eq!(c.remaining(), 2);
assert_eq!(c.remaining_mut(), 1);
assert_eq!(<CircBuf as Buf>::chunk(&c).len(), 2);
assert_eq!(c.chunk_mut().len(), 1);
let mut dst = [IoSlice::new(&[]); 2];
assert_eq!(c.chunks_vectored(&mut dst[..]), 2);
assert_eq!(dst[0].len(), 2);
assert_eq!(dst[1].len(), 0);
}
#[test]
fn bytes_buf_remaining() {
use bytes::{Buf, BufMut};
let mut c = CircBuf::with_capacity(4).unwrap();
assert_eq!(c.remaining(), 0);
assert_eq!(c.remaining_mut(), 3);
assert!(!c.has_remaining());
assert!(c.has_remaining_mut());
unsafe {
c.advance_mut(3);
}
assert_eq!(c.remaining(), 3);
assert_eq!(c.remaining_mut(), 0);
assert!(c.has_remaining());
assert!(!c.has_remaining_mut());
c.advance(2);
assert_eq!(c.remaining(), 1);
assert_eq!(c.remaining_mut(), 2);
assert!(c.has_remaining());
assert!(c.has_remaining_mut());
c.advance(1);
assert_eq!(c.remaining(), 0);
assert_eq!(c.remaining_mut(), 3);
assert!(!c.has_remaining());
assert!(c.has_remaining_mut());
}
#[cfg(feature = "bytes")]
#[test]
fn bytes_bufmut_hello() {
use bytes::BufMut;
let mut c = CircBuf::with_capacity(16).unwrap();
unsafe {
c.chunk_mut().write_byte(0, b'h');
c.chunk_mut().write_byte(1, b'e');
c.advance_mut(2);
c.chunk_mut().write_byte(0, b'l');
c.chunk_mut().write_byte(1, b'l');
c.chunk_mut().write_byte(2, b'o');
c.advance_mut(3);
}
assert_eq!(c.get_bytes()[0], b"hello");
}
}