aboutsummaryrefslogtreecommitdiff
path: root/src/coded_input_stream/input_buf.rs
blob: d5d625225774dcf0762c4a4a5d156c89a36f45b1 (plain)
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
use std::cmp;
use std::mem::MaybeUninit;

use crate::misc::maybe_uninit_write_slice;

#[derive(Debug)]
pub(crate) struct InputBuf<'a> {
    // Invariants: `0 <= pos_within_buf <= limit_within_buf <= buf.len()`.
    buf: &'a [u8],
    pos_within_buf: usize,
    limit_within_buf: usize,
}

impl<'a> InputBuf<'a> {
    #[inline]
    pub(crate) fn assertions(&self) {
        debug_assert!(self.pos_within_buf <= self.limit_within_buf);
        debug_assert!(self.limit_within_buf <= self.buf.len());
    }

    pub(crate) fn empty() -> InputBuf<'a> {
        InputBuf {
            buf: &[],
            pos_within_buf: 0,
            limit_within_buf: 0,
        }
    }

    pub(crate) fn from_bytes(buf: &'a [u8]) -> InputBuf<'a> {
        InputBuf {
            buf,
            pos_within_buf: 0,
            limit_within_buf: buf.len(),
        }
    }

    pub(crate) unsafe fn from_bytes_ignore_lifetime(buf: &[u8]) -> InputBuf<'a> {
        let buf = &*(buf as *const [u8]);
        Self::from_bytes(buf)
    }

    pub(crate) fn update_limit(&mut self, limit: u64) {
        let limit_within_buf = cmp::min(self.buf.len() as u64, limit);
        assert!(limit_within_buf >= self.pos_within_buf as u64);
        self.limit_within_buf = limit_within_buf as usize;
    }

    pub(crate) fn pos_within_buf(&self) -> usize {
        self.pos_within_buf
    }

    #[inline(always)]
    pub(crate) fn remaining_in_buf(&self) -> &'a [u8] {
        // SAFETY: Invariants.
        unsafe {
            self.buf
                .get_unchecked(self.pos_within_buf..self.limit_within_buf)
        }
    }

    #[inline(always)]
    pub(crate) fn consume(&mut self, amt: usize) {
        assert!(amt <= self.remaining_in_buf().len());
        self.pos_within_buf += amt;
    }

    #[inline(always)]
    pub(crate) fn read_byte(&mut self) -> Option<u8> {
        let r = self.remaining_in_buf().first().copied();
        if let Some(..) = r {
            self.pos_within_buf += 1;
        }
        r
    }

    pub(crate) fn read_bytes<'b>(&mut self, dest: &'b mut [MaybeUninit<u8>]) -> &'b mut [u8] {
        // This panics if this has not enough data.
        let r = maybe_uninit_write_slice(dest, &self.remaining_in_buf()[..dest.len()]);
        self.pos_within_buf += r.len();
        r
    }
}