librashader/librashader-cache/src/cacheable.rs

29 lines
592 B
Rust
Raw Normal View History

2023-02-16 09:40:24 +11:00
/// Trait for objects that are cacheable.
pub trait Cacheable {
fn from_bytes(bytes: &[u8]) -> Option<Self>
where
Self: Sized;
fn to_bytes(&self) -> Option<Vec<u8>>;
}
impl Cacheable for Vec<u8> {
fn from_bytes(bytes: &[u8]) -> Option<Self> {
Some(Vec::from(bytes))
}
fn to_bytes(&self) -> Option<Vec<u8>> {
Some(self.to_vec())
}
}
2024-08-01 16:06:28 +10:00
impl Cacheable for Option<Vec<u8>> {
fn from_bytes(bytes: &[u8]) -> Option<Self> {
Some(Some(Vec::from(bytes)))
}
fn to_bytes(&self) -> Option<Vec<u8>> {
self.clone()
}
}