radn-rs/src/rstd/atomic/plain.rs
2023-07-29 20:14:00 +00:00

74 lines
1.6 KiB
Rust

//! [`Plain`] type for storing raw byte sequnces.
use super::*;
/// Raw bytes storage. Use [`Plain::raw`] to get the data.
///
/// Note: this type uses [`Deserializer::read_all`] for parsing.
#[derive(Clone, Debug)]
pub struct Plain {
data: Vec<u8>,
}
/// This shouldn't happen given [`Deserializer::read_all`] being used for parsing.
#[derive(Debug)]
pub enum PlainParseError {}
impl Display for PlainParseError {
fn fmt(&self, _f: &mut std::fmt::Formatter<'_>) -> std::fmt::Result {
match *self {}
}
}
impl Error for PlainParseError {}
impl Serializable for Plain {
fn serialize(&self, serializer: &mut dyn Serializer) {
serializer.write(&self.data)
}
}
impl AtomicBase for Plain {
type AParseError = PlainParseError;
}
impl ImplMode for Plain {
type Mode = RegularMode;
}
impl RegularAtomic for Plain {
fn ra_deserialize(inlining: impl Inlining) -> AParseResult<Self> {
Ok(inlining.iread_all(Plain::from_slice))
}
fn ra_extend(mut self, tail: &[u8]) -> AParseResult<Self> {
self.data.extend_from_slice(tail);
Ok(self)
}
}
impl Plain {
/// Copy bytes into a new instance.
pub fn from_slice(slice: &[u8]) -> Self {
Plain { data: slice.into() }
}
/// Copy of bytes stored inside.
pub fn raw(&self) -> Vec<u8> {
self.data.clone()
}
}
#[cfg(test)]
mod tests {
use super::*;
use crate::rstd::*;
#[test]
fn can_parse_plain_slice() -> Result<(), PlainParseError> {
let plain = Plain::parse_slice(b"slice")?;
assert_eq!(plain.bytes().as_slice(), b"slice");
Ok(())
}
}