aws_lc_rs/io/positive.rs
1// Copyright 2018 Brian Smith.
2// SPDX-License-Identifier: ISC
3// Modifications copyright Amazon.com, Inc. or its affiliates. All Rights Reserved.
4// SPDX-License-Identifier: Apache-2.0 OR ISC
5
6//! Serialization and deserialization.
7
8/// A serialized positive integer.
9#[derive(Copy, Clone)]
10pub struct Positive<'a>(untrusted::Input<'a>);
11
12impl<'a> Positive<'a> {
13 pub(crate) fn new_non_empty_without_leading_zeros(input: untrusted::Input<'a>) -> Self {
14 debug_assert!(!input.is_empty());
15 debug_assert!(input.len() == 1 || input.as_slice_less_safe()[0] != 0);
16 Self(input)
17 }
18
19 /// Returns the value, ordered from significant byte to least significant
20 /// byte, without any leading zeros. The result is guaranteed to be
21 /// non-empty.
22 #[inline]
23 #[must_use]
24 pub fn big_endian_without_leading_zero(&self) -> &'a [u8] {
25 self.big_endian_without_leading_zero_as_input()
26 .as_slice_less_safe()
27 }
28
29 #[inline]
30 pub(crate) fn big_endian_without_leading_zero_as_input(&self) -> untrusted::Input<'a> {
31 self.0
32 }
33}
34
35impl Positive<'_> {
36 /// Returns the first byte.
37 ///
38 /// Will not panic because the value is guaranteed to have at least one
39 /// byte.
40 #[must_use]
41 pub fn first_byte(&self) -> u8 {
42 // This won't panic because
43 self.0.as_slice_less_safe()[0]
44 }
45}