use std::{
any::{Any, TypeId},
collections::HashMap,
sync::Arc,
};
use std::sync::LazyLock;
use crate::Object;
#[derive(Default)]
pub struct CastTable {
table: HashMap<TypeId, Caster>,
}
struct Caster {
cast_to_ref: Box<dyn Any + Send + Sync>,
cast_to_arc: Box<dyn Any + Send + Sync>,
}
impl CastTable {
pub fn insert<T: 'static + ?Sized>(
&mut self,
cast_to_ref: fn(&dyn Object) -> &T,
cast_to_arc: fn(Arc<dyn Object>) -> Arc<T>,
) {
let type_id = TypeId::of::<&'static T>();
let caster = Caster {
cast_to_ref: Box::new(cast_to_ref),
cast_to_arc: Box::new(cast_to_arc),
};
self.insert_erased(type_id, caster);
}
fn insert_erased(&mut self, type_id: TypeId, caster: Caster) {
let old_val = self.table.insert(type_id, caster);
assert!(
old_val.is_none(),
"Tried to insert a duplicate entry in a cast table.",
);
}
pub fn cast_object_to<'a, T: 'static + ?Sized>(&self, obj: &'a dyn Object) -> Option<&'a T> {
let target_type = TypeId::of::<&'static T>();
let caster = self.table.get(&target_type)?;
let caster: &fn(&dyn Object) -> &T = caster
.cast_to_ref
.downcast_ref()
.expect("Incorrect cast-function type found in cast table!");
Some(caster(obj))
}
pub fn cast_object_to_arc<T: 'static + ?Sized>(
&self,
obj: Arc<dyn Object>,
) -> Result<Arc<T>, Arc<dyn Object>> {
let target_type = TypeId::of::<&'static T>();
let caster = match self.table.get(&target_type) {
Some(c) => c,
None => return Err(obj),
};
let caster: &fn(Arc<dyn Object>) -> Arc<T> = caster
.cast_to_arc
.downcast_ref()
.expect("Incorrect cast-function type found in cast table!");
Ok(caster(obj))
}
}
pub(super) static EMPTY_CAST_TABLE: LazyLock<CastTable> = LazyLock::new(|| CastTable {
table: HashMap::new(),
});
#[doc(hidden)]
#[macro_export]
macro_rules! cast_table_deftness_helper{
{ $( $traitname:tt ),* } => {
#[allow(unused_mut)]
let mut table = $crate::CastTable::default();
$({
use std::sync::Arc;
let cast_to_ref: fn(&dyn $crate::Object) -> &(dyn $traitname + 'static) = |self_| {
let self_: &Self = self_.downcast_ref().unwrap();
let self_: &dyn $traitname = self_ as _;
self_
};
let cast_to_arc: fn(Arc<dyn $crate::Object>) -> Arc<dyn $traitname> = |self_| {
let self_: Arc<Self> = self_
.downcast_arc()
.ok()
.expect("used with incorrect type");
let self_: Arc<dyn $traitname> = self_ as _;
self_
};
table.insert::<dyn $traitname>(cast_to_ref, cast_to_arc);
})*
table
}
}
#[cfg(test)]
mod test {
#![allow(clippy::bool_assert_comparison)]
#![allow(clippy::clone_on_copy)]
#![allow(clippy::dbg_macro)]
#![allow(clippy::mixed_attributes_style)]
#![allow(clippy::print_stderr)]
#![allow(clippy::print_stdout)]
#![allow(clippy::single_char_pattern)]
#![allow(clippy::unwrap_used)]
#![allow(clippy::unchecked_time_subtraction)]
#![allow(clippy::useless_vec)]
#![allow(clippy::needless_pass_by_value)]
use super::*;
use crate::templates::*;
use derive_deftly::Deftly;
trait Tr1 {}
trait Tr2: 'static {}
#[derive(Deftly)]
#[derive_deftly(Object)]
#[deftly(rpc(downcastable_to = "Tr1"))]
struct Simple;
impl Tr1 for Simple {}
#[test]
fn check_simple() {
let concrete = Simple;
let tab = Simple::make_cast_table();
let obj: &dyn Object = &concrete;
let _cast: &(dyn Tr1 + '_) = tab.cast_object_to(obj).expect("cast failed");
let arc = Arc::new(Simple);
let arc_obj: Arc<dyn Object> = arc.clone();
let _cast: Arc<dyn Tr1> = tab.cast_object_to_arc(arc_obj).ok().expect("cast failed");
}
#[derive(Deftly)]
#[derive_deftly(Object)]
#[deftly(rpc(downcastable_to = "Tr1, Tr2"))]
struct Generic<T: Send + Sync + 'static>(T);
impl<T: Send + Sync + 'static> Tr1 for Generic<T> {}
impl<T: Send + Sync + 'static> Tr2 for Generic<T> {}
#[test]
fn check_generic() {
let generic: Generic<&'static str> = Generic("foo");
let tab = Generic::<&'static str>::make_cast_table();
let obj: &dyn Object = &generic;
let _cast: &(dyn Tr1 + '_) = tab.cast_object_to(obj).expect("cast failed");
let arc = Arc::new(Generic("bar"));
let arc_obj: Arc<dyn Object> = arc.clone();
let _cast: Arc<dyn Tr2> = tab.cast_object_to_arc(arc_obj).ok().expect("cast failed");
}
}