64 lines
1.6 KiB
Rust
64 lines
1.6 KiB
Rust
use crate::rstd::inlining::*;
|
|
|
|
use super::*;
|
|
|
|
impl Serializable for u64 {
|
|
fn serialize(&self, serializer: &mut dyn Serializer) {
|
|
serializer.write(&self.to_le_bytes());
|
|
}
|
|
}
|
|
|
|
#[derive(Debug, PartialEq)]
|
|
pub enum IntParseError {
|
|
Eof,
|
|
ExtraData(usize),
|
|
}
|
|
|
|
impl Display for IntParseError {
|
|
fn fmt(&self, f: &mut std::fmt::Formatter<'_>) -> std::fmt::Result {
|
|
match self {
|
|
Self::Eof => write!(f, "encountered EOF write parsing an integer."),
|
|
Self::ExtraData(length) => write!(
|
|
f,
|
|
"encountered extra data of length {length} while parsing an integer.",
|
|
),
|
|
}
|
|
}
|
|
}
|
|
|
|
impl Error for IntParseError {}
|
|
|
|
impl From<&[u8]> for IntParseError {
|
|
fn from(_value: &[u8]) -> Self {
|
|
Self::Eof
|
|
}
|
|
}
|
|
|
|
impl Atomic for u64 {
|
|
type AParseError = IntParseError;
|
|
|
|
fn a_deserialize(inlining: impl Inlining) -> AParseResult<Self> {
|
|
let (n, _) = inlining.iread_n_const::<8>(|slice| IntParseError::from(slice))?;
|
|
Ok(u64::from_le_bytes(n))
|
|
}
|
|
|
|
fn a_extend(self, tail: &[u8]) -> AParseResult<Self> {
|
|
Err(Self::a_extension_error(tail))
|
|
}
|
|
}
|
|
|
|
impl InlineableAtomic for u64 {
|
|
fn a_extension_error(tail: &[u8]) -> Self::AParseError {
|
|
IntParseError::ExtraData(tail.len())
|
|
}
|
|
|
|
fn a_ideserialize<D: Inlining>(inlining: D) -> ADParseResult<Self, D> {
|
|
let (x, inlining) = inlining.iread_n_const::<8>(|slice| IntParseError::from(slice))?;
|
|
Ok((u64::from_le_bytes(x), inlining))
|
|
}
|
|
}
|
|
|
|
impl ConstSizeAtomic for u64 {
|
|
const SIZE: usize = 8;
|
|
}
|