use super::*;
pub(crate) trait SliceObj: Send + Sync {
fn clone_box(&self) -> Box<dyn SliceObj>;
fn to_bytes(&self) -> Result<Vec<u8>, ()>;
fn as_any(&self) -> &dyn Any;
fn as_any_mut(&mut self) -> &mut dyn Any;
}
pub(super) struct Typed<T>(T);
impl<T: Clone + Serialize + Send + Sync + 'static> SliceObj for Typed<T> {
fn clone_box(&self) -> Box<dyn SliceObj> {
Box::new(Typed(self.0.clone()))
}
fn to_bytes(&self) -> Result<Vec<u8>, ()> {
postcard::to_stdvec(&self.0).map_err(|_| ())
}
fn as_any(&self) -> &dyn Any {
&self.0
}
fn as_any_mut(&mut self) -> &mut dyn Any {
&mut self.0
}
}
pub(crate) struct SliceBox {
pub(super) inner: Box<dyn SliceObj>,
}
impl SliceBox {
pub(crate) fn new<T: Clone + Serialize + Send + Sync + 'static>(value: T) -> Self {
Self {
inner: Box::new(Typed(value)),
}
}
pub(crate) fn to_bytes(&self) -> Result<Vec<u8>, ()> {
self.inner.to_bytes()
}
pub(crate) fn downcast_ref<T: 'static>(&self) -> Option<&T> {
self.inner.as_any().downcast_ref::<T>()
}
pub(crate) fn downcast_mut<T: 'static>(&mut self) -> Option<&mut T> {
self.inner.as_any_mut().downcast_mut::<T>()
}
}
impl Clone for SliceBox {
fn clone(&self) -> Self {
Self {
inner: self.inner.clone_box(),
}
}
}
impl std::fmt::Debug for SliceBox {
fn fmt(&self, f: &mut std::fmt::Formatter<'_>) -> std::fmt::Result {
write!(f, "SliceBox(..)")
}
}
pub(crate) type LoadSliceFn = fn(u32, &[u8]) -> Result<(SliceBox, bool), MigrateError>;
#[derive(Debug, Clone)]
pub(crate) struct PackageEntry {
pub name: String,
pub version: u32,
pub persistent_version: u32,
pub rules_version: u32,
pub depends: Vec<String>,
pub std: bool,
pub unit_state: bool,
pub unit_persistent: bool,
pub new_state: fn() -> SliceBox,
pub new_persistent: fn() -> SliceBox,
pub load_state: LoadSliceFn,
pub load_persistent: LoadSliceFn,
pub clear_cues: fn(&mut SliceBox),
pub cue_len: fn(&SliceBox) -> usize,
}
impl PackageEntry {
pub(crate) fn decl_only(name: &str, version: u32, depends: Vec<String>, std: bool) -> Self {
Self {
name: name.to_string(),
version,
persistent_version: 1,
rules_version: 1,
depends,
std,
unit_state: true,
unit_persistent: true,
new_state: || SliceBox::new(()),
new_persistent: || SliceBox::new(()),
load_state: |_, _| Ok((SliceBox::new(()), false)),
load_persistent: |_, _| Ok((SliceBox::new(()), false)),
clear_cues: |_| {},
cue_len: |_| 0,
}
}
pub(crate) fn of<P: Package>() -> Self {
Self {
name: P::NAME.to_string(),
version: P::VERSION,
persistent_version: P::PERSISTENT_VERSION,
rules_version: P::RULES_VERSION,
depends: P::DEPENDS.iter().map(|d| (*d).to_string()).collect(),
std: P::STD,
unit_state: std::any::TypeId::of::<P::State>() == std::any::TypeId::of::<()>(),
unit_persistent: std::any::TypeId::of::<P::Persistent>()
== std::any::TypeId::of::<()>(),
new_state: || SliceBox::new(P::State::default()),
new_persistent: || SliceBox::new(P::Persistent::default()),
load_state: |version, raw| {
if version == P::VERSION {
let v: P::State = decode_slice(raw)?;
Ok((SliceBox::new(v), false))
} else {
Ok((SliceBox::new(P::migrate(version, raw)?), true))
}
},
clear_cues: |slice| {
if let Some(state) = slice.downcast_mut::<P::State>() {
P::clear_cues(state);
}
},
cue_len: |slice| {
slice
.downcast_ref::<P::State>()
.map_or(0, |state| P::cue_len(state))
},
load_persistent: |version, raw| {
if version == P::PERSISTENT_VERSION {
let v: P::Persistent = decode_slice(raw)?;
Ok((SliceBox::new(v), false))
} else {
Ok((SliceBox::new(P::migrate_persistent(version, raw)?), true))
}
},
}
}
}