//! [`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, } /// 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 Atomic for Plain { type AParseError = PlainParseError; fn a_deserialize(deserializer: &mut dyn Deserializer) -> Result { Ok(Plain::from_slice(deserializer.read_all())) } fn a_unexpected_tail(tail: &[u8]) -> Self::AParseError { panic!( "Plain must use read_all, therefore there must not be any extra tail (received {} bytes).", tail.len() ) } } 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 { 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(()) } }