1use bytes::BytesMut;
2
3#[derive(Debug)]
5pub struct Cursor<'a> {
6 inner: &'a mut BytesMut,
7 pos: usize,
8}
9
10impl<'a> Cursor<'a> {
11 pub fn new(inner: &'a mut BytesMut) -> Self {
12 Cursor {
13 inner,
14 pos: 0,
15 }
16 }
17
18 pub fn into_inner(&mut self) -> BytesMut {
19 self.inner.split()
20 }
21
22 pub const fn position(&self) -> usize {
23 self.pos
24 }
25
26 pub fn set_position(&mut self, pos: usize) {
27 self.pos = pos;
28 }
29
30 pub fn reset(&mut self) {
31 self.pos = 0;
32 }
33
34 pub fn remaining(&self) -> &[u8] {
35 self.inner[self.pos..].as_ref()
36 }
37
38 pub fn split_at_current_pos(&mut self) -> BytesMut {
40 let pos = self.pos;
41 self.reset();
42 self.inner.split_to(pos)
43 }
44
45 pub fn len(&self) -> usize {
47 self.inner.len()
48 }
49
50 pub fn is_empty(&self) -> bool {
51 self.inner.is_empty()
52 }
53}
54
55impl AsRef<[u8]> for Cursor<'_> {
56 fn as_ref(&self) -> &[u8] {
57 self.inner
58 }
59}
60
61impl AsMut<BytesMut> for Cursor<'_> {
62 fn as_mut(&mut self) -> &mut BytesMut {
63 self.inner
64 }
65}