#![forbid(unsafe_code)]
#![deny(clippy::let_underscore_must_use)]
#![cfg_attr(doctest, doc = include_str!("../README.md"))]
mod flat_op;
mod flat_op_str;
mod reader;
mod traits;
pub mod transport;
mod writer;
pub use traits::{NvDecode, NvEncode};
#[cfg(test)]
mod tests;
#[cfg(test)]
mod tests_words;
use core::fmt;
use core::ops::{Deref, DerefMut};
pub use flat_op::{
FlatOpDesc, FlatOpFieldDesc, FlatOpFieldType, FlatOpScopeDesc, FlatOpStreamDesc,
FloatConstraint,
};
pub use flat_op_str::{
check_flat_op_bytes_words, flat_op_str_pad_mask, for_each_padded_le_word, flat_op_str_padded_bytes,
flat_op_str_word_len, read_flat_op_padded_bytes, read_flat_op_str_bytes, read_flat_op_str_words,
read_flat_op_str_words_into, validate_flat_op_str_bytes, validate_flat_op_str_words,
write_flat_op_bytes_words, write_flat_op_str_bytes, write_flat_op_str_words, FlatOpStrError,
};
pub use nativeview_abi_macros::{NvCodec, ReprEnum};
pub use reader::{ByteReader, DEFAULT_MAX_DEPTH};
pub use transport::{
call_sync_decode, decode_opt_raw_str, encode_f32_stack, encode_f64_stack, encode_opt_raw_str,
try_with_stack_writer, with_stack_writer, ByteTransport, StackWriter, TransportDoneFn,
TransportError,
};
pub use writer::{try_write_fixed, write_fixed, write_fixed_into, ByteSink, ByteWriter, SliceWriter};
pub const DEFAULT_MAX_ITEMS: usize = 65_536;
pub const DEFAULT_MAX_BYTES: usize = 16 * 1024 * 1024;
pub const CANONICAL_F32_NAN_BITS: u32 = 0x7FC0_0000;
pub const CANONICAL_F64_NAN_BITS: u64 = 0x7FF8_0000_0000_0000;
#[inline]
pub const fn canonical_f32_bits(v: f32) -> u32 {
if v.is_nan() {
CANONICAL_F32_NAN_BITS
} else if v == 0.0 {
0
} else {
v.to_bits()
}
}
#[inline]
pub const fn canonical_f64_bits(v: f64) -> u64 {
if v.is_nan() {
CANONICAL_F64_NAN_BITS
} else if v == 0.0 {
0
} else {
v.to_bits()
}
}
#[inline]
pub const fn hex_nibble(b: u8) -> Option<u8> {
match b {
b'0'..=b'9' => Some(b - b'0'),
b'a'..=b'f' => Some(b - b'a' + 10),
b'A'..=b'F' => Some(b - b'A' + 10),
_ => None,
}
}
#[derive(Debug, Clone, PartialEq, Eq)]
pub enum CodecError {
Truncated { needed: usize, remaining: usize },
BadMagic,
BadVersion(u32),
BadEnum { ty: &'static str, raw: i64 },
BadBool(u8),
NonZeroReserved(u64),
LenOverflow { len: usize, max: usize },
TrailingBytes(usize),
InvalidUtf8,
DepthExceeded { max: usize },
}
impl fmt::Display for CodecError {
fn fmt(&self, f: &mut fmt::Formatter<'_>) -> fmt::Result {
match self {
Self::Truncated { needed, remaining } => {
write!(f, "truncated binary input: needed {needed}B, remaining {remaining}B")
}
Self::BadMagic => f.write_str("invalid binary magic header"),
Self::BadVersion(v) => write!(f, "unsupported binary format version {v}"),
Self::BadEnum { ty, raw } => write!(f, "invalid enum discriminant {raw} for `{ty}`"),
Self::BadBool(b) => write!(f, "invalid boolean byte {b} (expected 0 or 1)"),
Self::NonZeroReserved(v) => write!(f, "non-zero reserved field {v:#x}"),
Self::LenOverflow { len, max } => {
write!(f, "binary length {len} exceeds maximum {max}")
}
Self::TrailingBytes(n) => write!(f, "unexpected {n} trailing byte(s)"),
Self::InvalidUtf8 => f.write_str("invalid UTF-8 string in binary payload"),
Self::DepthExceeded { max } => {
write!(f, "binary container nesting exceeded maximum depth {max}")
}
}
}
}
impl std::error::Error for CodecError {}
#[derive(Debug, Clone, Default, PartialEq, Eq, Hash)]
pub struct ByteBuf(pub Vec<u8>);
impl ByteBuf {
#[inline]
pub const fn new() -> Self {
Self(Vec::new())
}
#[inline]
pub fn into_vec(self) -> Vec<u8> {
self.0
}
}
impl Deref for ByteBuf {
type Target = [u8];
#[inline]
fn deref(&self) -> &[u8] {
&self.0
}
}
impl DerefMut for ByteBuf {
#[inline]
fn deref_mut(&mut self) -> &mut [u8] {
&mut self.0
}
}
impl AsRef<[u8]> for ByteBuf {
#[inline]
fn as_ref(&self) -> &[u8] {
&self.0
}
}
impl From<Vec<u8>> for ByteBuf {
#[inline]
fn from(v: Vec<u8>) -> Self {
Self(v)
}
}
impl From<ByteBuf> for Vec<u8> {
#[inline]
fn from(b: ByteBuf) -> Self {
b.0
}
}