1use crate::error::Result;
2use std::io::{Error, ErrorKind::InvalidData};
3
4pub trait Dynamic: Sized {
6 fn from_bytes(bytes: Vec<u8>) -> Result<Self>;
7 #[allow(clippy::wrong_self_convention)]
8 fn into_bytes(&self) -> Result<&[u8]>;
9}
10
11impl Dynamic for Vec<u8> {
12 fn from_bytes(bytes: Vec<u8>) -> Result<Self> {
13 Ok(bytes)
14 }
15 fn into_bytes(&self) -> Result<&[u8]> {
16 Ok(self)
17 }
18}
19
20impl Dynamic for String {
21 fn from_bytes(bytes: Vec<u8>) -> Result<Self> {
22 String::from_utf8(bytes).map_err(|_| Error::new(InvalidData, "Invalid UTF-8"))
23 }
24 fn into_bytes(&self) -> Result<&[u8]> {
25 Ok(self.as_bytes())
26 }
27}