Reflect::assembly_name to retrieve assembly name of a type

- this method is useful in hot reloading, to understand whether a type instance belong to a plugin or not and if so - serialize and destroy the instance before reloading
This commit is contained in:
Dmitry Stepanov
2024-05-15 10:38:22 +03:00
parent 58648d649c
commit f31bbb3429
12 changed files with 80 additions and 1 deletions
@@ -54,6 +54,10 @@ macro_rules! define_two_args_node {
""
}
fn assembly_name(&self) -> &'static str {
env!("CARGO_PKG_NAME")
}
fn fields_info(&self, func: &mut dyn FnMut(&[FieldInfo])) {
func(&[
FieldInfo {
@@ -200,6 +204,10 @@ impl<T: EntityId> Reflect for NotNode<T> {
""
}
fn assembly_name(&self) -> &'static str {
env!("CARGO_PKG_NAME")
}
fn fields_info(&self, func: &mut dyn FnMut(&[FieldInfo])) {
func(&[FieldInfo {
owner_type_id: TypeId::of::<Self>(),
+5
View File
@@ -408,6 +408,7 @@ fn gen_impl(
let as_array_impl = ty_args.as_array_impl();
let doc = args::fetch_doc_comment(&ty_args.attrs);
let assembly_name = std::env::var("CARGO_PKG_NAME").unwrap_or_default();
let set_field = set_field.map(|set_field| {
quote! {
@@ -432,6 +433,10 @@ fn gen_impl(
#doc
}
fn assembly_name(&self) -> &'static str {
#assembly_name
}
fn fields_info(&self, func: &mut dyn FnMut(&[FieldInfo])) {
#metadata
}
+4
View File
@@ -110,6 +110,10 @@ where
Ok(Box::new(this))
}
fn assembly_name(&self) -> &'static str {
env!("CARGO_PKG_NAME")
}
#[inline]
fn as_array(&self, func: &mut dyn FnMut(Option<&dyn ReflectArray>)) {
func(Some(self))
+15
View File
@@ -198,6 +198,13 @@ pub trait Reflect: ReflectBase {
fn set(&mut self, value: Box<dyn Reflect>) -> Result<Box<dyn Reflect>, Box<dyn Reflect>>;
/// Returns a parent assembly name of the type that implements this trait. **WARNING:** You should use
/// proc-macro (`#[derive(Reflect)]`) to ensure that this method will return correct assembly
/// name. In other words - there's no guarantee, that any implementation other than proc-macro
/// will return a correct name of the assembly. Alternatively, you can use `env!("CARGO_PKG_NAME")`
/// as an implementation.
fn assembly_name(&self) -> &'static str;
/// Calls user method specified with `#[reflect(setter = ..)]` or falls back to
/// [`Reflect::field_mut`]
#[allow(clippy::type_complexity)]
@@ -1087,6 +1094,10 @@ macro_rules! blank_reflect {
""
}
fn assembly_name(&self) -> &'static str {
env!("CARGO_PKG_NAME")
}
fn fields_info(&self, func: &mut dyn FnMut(&[FieldInfo])) {
func(&[])
}
@@ -1141,6 +1152,10 @@ macro_rules! delegate_reflect {
self.deref().doc()
}
fn assembly_name(&self) -> &'static str {
self.deref().assembly_name()
}
fn fields_info(&self, func: &mut dyn FnMut(&[FieldInfo])) {
self.deref().fields_info(func)
}
+4
View File
@@ -292,6 +292,10 @@ macro_rules! impl_reflect_inner_mutability {
""
}
fn assembly_name(&self) -> &'static str {
env!("CARGO_PKG_NAME")
}
fn fields_info(&$self, func: &mut dyn FnMut(&[FieldInfo])) {
let guard = $acquire_lock_guard;
guard.fields_info(func)
+4
View File
@@ -364,6 +364,10 @@ where
self.value.doc()
}
fn assembly_name(&self) -> &'static str {
env!("CARGO_PKG_NAME")
}
#[inline]
fn fields_info(&self, func: &mut dyn FnMut(&[FieldInfo])) {
self.value.fields_info(func)
+2 -1
View File
@@ -1297,7 +1297,8 @@ impl Engine {
/// graphics_context_params,
/// resource_manager: ResourceManager::new(task_pool.clone()),
/// serialization_context: Arc::new(SerializationContext::new()),
/// task_pool
/// task_pool,
/// widget_constructors: Arc::new(Default::default()),
/// })
/// .unwrap();
/// ```
+22
View File
@@ -51,3 +51,25 @@ macro_rules! define_with {
}
};
}
#[cfg(test)]
mod test {
use crate::scene::base::BaseBuilder;
use fyrox_core::reflect::Reflect;
use fyrox_core::ImmutableString;
use fyrox_sound::source::Status;
use fyrox_ui::widget::WidgetBuilder;
#[test]
fn test_assembly_names() {
let var = ImmutableString::new("Foobar");
let base = BaseBuilder::new().build_base();
let widget = WidgetBuilder::new().build();
let status = Status::Stopped;
assert_eq!(var.assembly_name(), "fyrox-core");
assert_eq!(base.assembly_name(), "fyrox-impl");
assert_eq!(widget.assembly_name(), "fyrox-ui");
assert_eq!(status.assembly_name(), "fyrox-sound");
}
}
+4
View File
@@ -602,6 +602,10 @@ impl Reflect for Node {
self.0.deref().doc()
}
fn assembly_name(&self) -> &'static str {
self.0.deref().assembly_name()
}
fn fields_info(&self, func: &mut dyn FnMut(&[FieldInfo])) {
self.0.deref().fields_info(func)
}
+4
View File
@@ -49,6 +49,10 @@ impl Reflect for Limb {
""
}
fn assembly_name(&self) -> &'static str {
env!("CARGO_PKG_NAME")
}
fn fields_info(&self, func: &mut dyn FnMut(&[FieldInfo])) {
func(&[
FieldInfo {
+4
View File
@@ -633,6 +633,10 @@ impl Reflect for Script {
self.instance.doc()
}
fn assembly_name(&self) -> &'static str {
self.instance.assembly_name()
}
fn fields_info(&self, func: &mut dyn FnMut(&[FieldInfo])) {
self.instance.fields_info(func)
}
+4
View File
@@ -263,6 +263,10 @@ impl Reflect for UiNode {
self.0.deref().doc()
}
fn assembly_name(&self) -> &'static str {
self.0.deref().assembly_name()
}
fn fields_info(&self, func: &mut dyn FnMut(&[FieldInfo])) {
self.0.deref().fields_info(func)
}