petra/decode/
signed.rs

1use crate::decode::{__read_marker, read_arg};
2use crate::{Error, Marker, ReadBuffer, ReadBufferBorrowed};
3
4/// Decode a signed value given any suitable representation.
5#[inline(always)]
6pub fn read_signed(buf: &mut ReadBuffer) -> Result<isize, Error> {
7    __read_signed(&buf.borrow())
8}
9
10/// Decode an 8-bit signed value.
11#[inline(always)]
12pub fn read_i8(buf: &mut ReadBuffer) -> Result<i8, Error> {
13    __read_i8(&buf.borrow())
14}
15
16/// Decode a 16-bit signed value.
17#[inline(always)]
18pub fn read_i16(buf: &mut ReadBuffer) -> Result<i16, Error> {
19    __read_i16(&buf.borrow())
20}
21
22/// Decode a 32-bit signed value.
23#[inline(always)]
24pub fn read_i32(buf: &mut ReadBuffer) -> Result<i32, Error> {
25    __read_i32(&buf.borrow())
26}
27
28/// Decode a 64-bit signed value.
29#[inline(always)]
30pub fn read_i64(buf: &mut ReadBuffer) -> Result<i64, Error> {
31    __read_i64(&buf.borrow())
32}
33
34pub(crate) fn __read_signed(buf: &ReadBufferBorrowed) -> Result<isize, Error> {
35    let snapshot = buf.snapshot();
36    match __read_marker(buf)? {
37        Marker::ImmPos { value } => Ok(value as _),
38        Marker::ImmNeg { value } => Ok(value as _),
39
40        Marker::I8 => read_arg!(buf as i8).map(|v| v as _),
41        Marker::I16 => read_arg!(buf as i16).map(|v| v as _),
42        Marker::I32 => read_arg!(buf as i32).map(|v| v as _),
43        Marker::I64 => read_arg!(buf as i64).map(|v| v as _),
44
45        _ => {
46            // Undo the read_marker call.
47            buf.restore(snapshot);
48
49            Err(Error::Unexpected)
50        }
51    }
52}
53
54pub(crate) fn __read_i8(buf: &ReadBufferBorrowed) -> Result<i8, Error> {
55    let marker = buf.peek(1, |buf| {
56        // Safety: 1 byte available in buf.
57        unsafe { *buf.get_unchecked(0) }
58    })?;
59
60    if marker != Marker::I8.encoding() {
61        return Err(Error::Unexpected);
62    }
63
64    // Discard the marker.
65    // Safety: there is at least one byte in the buffer because peek returned.
66    let _ = buf.discard(1);
67
68    read_arg!(buf as i8)
69}
70
71pub(crate) fn __read_i16(buf: &ReadBufferBorrowed) -> Result<i16, Error> {
72    let marker = buf.peek(1, |buf| {
73        // Safety: 1 byte available in buf.
74        unsafe { *buf.get_unchecked(0) }
75    })?;
76
77    if marker != Marker::I16.encoding() {
78        return Err(Error::Unexpected);
79    }
80
81    // Discard the marker.
82    // Safety: there is at least one byte in the buffer because peek returned.
83    let _ = buf.discard(1);
84
85    read_arg!(buf as i16)
86}
87
88pub(crate) fn __read_i32(buf: &ReadBufferBorrowed) -> Result<i32, Error> {
89    let marker = buf.peek(1, |buf| {
90        // Safety: 1 byte available in buf.
91        unsafe { *buf.get_unchecked(0) }
92    })?;
93
94    if marker != Marker::I32.encoding() {
95        return Err(Error::Unexpected);
96    }
97
98    // Discard the marker.
99    // Safety: there is at least one byte in the buffer because peek returned.
100    let _ = buf.discard(1);
101
102    read_arg!(buf as i32)
103}
104
105pub(crate) fn __read_i64(buf: &ReadBufferBorrowed) -> Result<i64, Error> {
106    let marker = buf.peek(1, |buf| {
107        // Safety: 1 byte available in buf.
108        unsafe { *buf.get_unchecked(0) }
109    })?;
110
111    if marker != Marker::I64.encoding() {
112        return Err(Error::Unexpected);
113    }
114
115    // Discard the marker.
116    // Safety: there is at least one byte in the buffer because peek returned.
117    let _ = buf.discard(1);
118
119    read_arg!(buf as i64)
120}