1+ use bytes:: { Buf , BufMut } ;
2+ use std:: io:: { Read , Write } ;
3+ use std:: io:: Error ;
4+
5+ #[ derive( Clone , Debug ) ]
6+ pub struct ByteBuf {
7+ inner : Vec < u8 > ,
8+ read_cursor_position : usize ,
9+ write_cursor_position : usize ,
10+
11+ marked_read_position : usize ,
12+ marked_write_position : usize ,
13+ }
14+
15+ impl ByteBuf {
16+ pub fn with_capacity ( capacity : usize ) -> Self {
17+ Self {
18+ inner : Vec :: with_capacity ( capacity) ,
19+ read_cursor_position : 0 ,
20+ write_cursor_position : 0 ,
21+
22+ marked_read_position : 0 ,
23+ marked_write_position : 0 ,
24+ }
25+ }
26+
27+ pub fn new ( ) -> Self {
28+ Self {
29+ inner : vec ! [ ] ,
30+ read_cursor_position : 0 ,
31+ write_cursor_position : 0 ,
32+
33+ marked_read_position : 0 ,
34+ marked_write_position : 0 ,
35+ }
36+ }
37+
38+ pub fn read_position ( & self ) -> usize {
39+ self . read_cursor_position
40+ }
41+
42+ pub fn write_position ( & self ) -> usize {
43+ self . write_cursor_position
44+ }
45+
46+ pub fn mark_read_position ( & mut self ) {
47+ self . marked_read_position = self . read_cursor_position ;
48+ }
49+
50+ pub fn mark_write_position ( & mut self ) {
51+ self . marked_write_position = self . write_cursor_position ;
52+ }
53+
54+ pub fn reset_read_position ( & mut self ) {
55+ self . read_cursor_position = self . marked_read_position ;
56+ }
57+
58+ pub fn reset_write_position ( & mut self ) {
59+ self . write_cursor_position = self . marked_write_position ;
60+ }
61+
62+ pub fn inner ( & self ) -> & [ u8 ] {
63+ & self . inner [ self . read_cursor_position ..]
64+ }
65+
66+ pub fn inner_mut ( & mut self ) -> & mut [ u8 ] {
67+ & mut self . inner [ self . write_cursor_position ..]
68+ }
69+
70+ pub fn reserve ( & mut self , additional : usize ) {
71+ self . inner . reserve ( additional) ;
72+ }
73+
74+ /// Removes all bytes prior to the current read position.
75+ pub fn remove_prior ( & mut self ) {
76+ let new_capacity = self . inner . capacity ( ) - self . read_cursor_position ;
77+ let mut new_inner = Vec :: with_capacity ( new_capacity) ;
78+
79+ new_inner. extend_from_slice ( & self . inner ) ;
80+
81+ self . inner = new_inner;
82+ }
83+
84+ pub fn len ( & self ) -> usize {
85+ self . inner . len ( )
86+ }
87+ }
88+
89+ impl Buf for ByteBuf {
90+ fn remaining ( & self ) -> usize {
91+ self . inner . capacity ( ) - self . read_cursor_position
92+ }
93+
94+ fn bytes ( & self ) -> & [ u8 ] {
95+ & self . inner [ self . read_cursor_position ..]
96+ }
97+
98+ fn advance ( & mut self , cnt : usize ) {
99+ self . read_cursor_position += cnt;
100+ }
101+ }
102+
103+ impl BufMut for ByteBuf {
104+ fn remaining_mut ( & self ) -> usize {
105+ self . inner . capacity ( ) - self . write_cursor_position
106+ }
107+
108+ unsafe fn advance_mut ( & mut self , cnt : usize ) {
109+ self . write_cursor_position += cnt;
110+ }
111+
112+ unsafe fn bytes_mut ( & mut self ) -> & mut [ u8 ] {
113+ & mut self . inner [ self . write_cursor_position ..]
114+ }
115+ }
116+
117+ impl Read for ByteBuf {
118+ fn read ( & mut self , buf : & mut [ u8 ] ) -> Result < usize , Error > {
119+ let mut amount_read = 0 ;
120+
121+ while amount_read < buf. len ( ) {
122+ let self_index = self . read_cursor_position + amount_read;
123+ if let Some ( val) = self . inner . get ( self_index) {
124+ buf[ amount_read] = val. clone ( ) ;
125+ } else {
126+ break ;
127+ }
128+
129+ amount_read += 1 ;
130+ }
131+
132+ Ok ( amount_read)
133+ }
134+ }
0 commit comments