Skip to content
New issue

Have a question about this project? Sign up for a free GitHub account to open an issue and contact its maintainers and the community.

By clicking “Sign up for GitHub”, you agree to our terms of service and privacy statement. We’ll occasionally send you account related emails.

Already on GitHub? Sign in to your account

stable reflect type name #5805

Closed
wants to merge 96 commits into from
Closed
Changes from 1 commit
Commits
Show all changes
96 commits
Select commit Hold shift + click to select a range
c9ad9a5
add `TypeName` and `ReflectTypeName`
tguichaoua Aug 26, 2022
bab4ae5
replace `Reflect::type_name` by `ReflectTypeName::type_name`
tguichaoua Aug 26, 2022
06e9258
fix tuple TypeName
tguichaoua Aug 26, 2022
312659a
impl_reflect_value use modul less name
tguichaoua Aug 26, 2022
6982351
update tests
tguichaoua Aug 26, 2022
317372f
update `Reflect::represents`
tguichaoua Aug 26, 2022
30a663f
fix ci
tguichaoua Aug 26, 2022
3a8958a
fix examples/3d/skybox
tguichaoua Aug 26, 2022
a710c17
Update crates/bevy_reflect/src/type_name.rs
tguichaoua Aug 27, 2022
006c8e8
Update crates/bevy_reflect/src/type_name.rs
tguichaoua Aug 27, 2022
a7e8dd6
fix the requirement of importing `ReflectTypeName`
tguichaoua Aug 27, 2022
8eeb06d
Merge branch 'reflect/type_name' of https://github.com/tguichaoua/bev…
tguichaoua Aug 27, 2022
140e009
drop Cow in TypeName
tguichaoua Aug 27, 2022
6fba583
remove majority of `any::type_name`
tguichaoua Aug 27, 2022
4f17b0c
fix ci + doc
tguichaoua Aug 27, 2022
c135dae
remove `ReflectTypeName`
tguichaoua Aug 27, 2022
11889a1
allow custom type name for impl_reflect_value
tguichaoua Aug 27, 2022
fe2ff0b
glam type name
tguichaoua Aug 27, 2022
e7fc222
fix glam tests
tguichaoua Aug 27, 2022
ca5e875
fix GenericDataCell soft lock
tguichaoua Aug 27, 2022
0563cf9
Update crates/bevy_reflect/bevy_reflect_derive/src/derive_data.rs
tguichaoua Aug 28, 2022
be2b3d5
Update crates/bevy_reflect/bevy_reflect_derive/src/reflect_value.rs
tguichaoua Aug 28, 2022
8cd535a
Update crates/bevy_reflect/src/impls/std.rs
tguichaoua Aug 28, 2022
8af97fc
Update crates/bevy_reflect/src/type_info.rs
tguichaoua Aug 28, 2022
eb1e2f6
Update crates/bevy_reflect/src/utility.rs
tguichaoua Aug 28, 2022
80b22d8
Update crates/bevy_reflect/src/utility.rs
tguichaoua Aug 28, 2022
9682cec
remove unnecessary `as_ref()`
tguichaoua Aug 28, 2022
d9d3afa
rm last usage of `ReflectTypeName`
tguichaoua Aug 28, 2022
329f248
rm old comment
tguichaoua Aug 28, 2022
e6a3ce8
refactor(reflect_derive): if-else into match
tguichaoua Aug 28, 2022
4722a19
Update crates/bevy_reflect/bevy_reflect_derive/src/impls/type_name.rs
tguichaoua Aug 28, 2022
aecba77
Update crates/bevy_reflect/bevy_reflect_derive/src/impls/type_name.rs
tguichaoua Aug 28, 2022
4fd9a53
Merge branch 'reflect/type_name' of https://github.com/tguichaoua/bev…
tguichaoua Aug 28, 2022
b2be7e9
reflect_derive: cleanup impl_type_name
tguichaoua Aug 28, 2022
9ea7f9a
reflect_derive: impl_type_name accept ReflectMeta as argument
tguichaoua Aug 28, 2022
b697bc3
NamedReflectValueDef::get_reflected_type_name
tguichaoua Aug 28, 2022
a6654c1
reflect_derive: refactoring
tguichaoua Aug 28, 2022
34f57b7
avoid usage of `stringify!`
tguichaoua Aug 28, 2022
26460dd
fix fmt in doc code
tguichaoua Aug 28, 2022
f4a6655
fix utility doc
tguichaoua Aug 28, 2022
45eca48
rm useless `as_ref()`
tguichaoua Aug 28, 2022
e2e3a02
example: add comment
tguichaoua Aug 28, 2022
3f2ee5d
reflect_derive: cleanup get_type_name_attribute
tguichaoua Aug 28, 2022
ba4da30
doc: impl_type_name
tguichaoua Aug 28, 2022
d8987f4
doc: derive_type_name
tguichaoua Aug 28, 2022
d155529
TypeName is 'static
tguichaoua Aug 28, 2022
0c25180
doc: TypeName
tguichaoua Aug 28, 2022
b0a6604
doc: GenericTypeNameCell
tguichaoua Aug 28, 2022
9ef1454
impl TypeName on `dyn Reflect`
tguichaoua Aug 28, 2022
83e3500
fix docs
tguichaoua Aug 28, 2022
b1411ab
fix test
tguichaoua Aug 28, 2022
86ab916
Update crates/bevy_reflect/bevy_reflect_derive/src/lib.rs
tguichaoua Aug 29, 2022
5e87d13
Update crates/bevy_reflect/bevy_reflect_derive/src/lib.rs
tguichaoua Aug 29, 2022
7295583
Update crates/bevy_reflect/bevy_reflect_derive/src/lib.rs
tguichaoua Aug 29, 2022
85739b8
Update crates/bevy_reflect/src/type_name.rs
tguichaoua Aug 29, 2022
c9ec3e8
Update crates/bevy_reflect/src/type_name.rs
tguichaoua Aug 29, 2022
ef4986e
Update crates/bevy_reflect/src/type_name.rs
tguichaoua Aug 29, 2022
4feef51
Update crates/bevy_reflect/src/utility.rs
tguichaoua Aug 29, 2022
6f750c6
Update crates/bevy_reflect/bevy_reflect_derive/src/lib.rs
tguichaoua Aug 29, 2022
563cb75
Update crates/bevy_reflect/bevy_reflect_derive/src/lib.rs
tguichaoua Aug 29, 2022
9c3a034
Update crates/bevy_reflect/bevy_reflect_derive/src/lib.rs
tguichaoua Aug 29, 2022
dbd4bde
Update crates/bevy_reflect/bevy_reflect_derive/src/lib.rs
tguichaoua Aug 29, 2022
f67f1af
Update crates/bevy_reflect/src/type_name.rs
tguichaoua Aug 29, 2022
3aa805a
reflect_derive: update derive_type_name doc
tguichaoua Aug 29, 2022
188b2f3
update TypeName doc
tguichaoua Aug 29, 2022
08e2c8f
fix example comment
tguichaoua Aug 29, 2022
a7aa4de
impl TypeName on Dynamic* structs
tguichaoua Aug 29, 2022
184d335
register_type_data : use TypeName
tguichaoua Aug 30, 2022
fa9bdb4
update some doc links
tguichaoua Aug 30, 2022
9dda494
TypeUuidDynamic : use TypeName
tguichaoua Aug 30, 2022
3668407
relive last std::any::type_name
tguichaoua Aug 30, 2022
b449fbe
update std types TypeName
tguichaoua Aug 30, 2022
4d42f12
Merge branch 'main' into reflect/type_name
tguichaoua Aug 30, 2022
a4c93cf
remove extra spaces
tguichaoua Aug 30, 2022
2afbd85
Merge branch 'main' into reflect/type_name
tguichaoua Aug 31, 2022
fab49b1
Merge branch 'main' into reflect/type_name
tguichaoua Sep 5, 2022
c914af0
rename `TypeName` into `TypePath`
tguichaoua Sep 5, 2022
86e12d2
rename `GenericTypeNameCell` into `GenericTypePathCell`
tguichaoua Sep 5, 2022
cb84e1e
add other methods on `TypePath`
tguichaoua Sep 5, 2022
60ff2b7
fix type path generation
tguichaoua Sep 5, 2022
e4efa1b
fix ci & doc
tguichaoua Sep 5, 2022
02aabfa
refix ci & doc
tguichaoua Sep 5, 2022
afda7b3
fix doc
tguichaoua Sep 5, 2022
aaef516
Merge branch 'main' into reflect/type_name
tguichaoua Sep 14, 2022
ddf4726
refactor: TypePathOptions::parse_meta_list
tguichaoua Sep 14, 2022
0b0cf93
fix tuple impl
tguichaoua Sep 14, 2022
88d2082
fix ci
tguichaoua Sep 14, 2022
4f9d953
minor doc fix
tguichaoua Sep 15, 2022
c28c3b4
remove empty space
tguichaoua Sep 15, 2022
a46a028
Merge branch 'main' into reflect/type_name
tguichaoua Sep 20, 2022
5303da3
fix merge
tguichaoua Sep 20, 2022
5a70e6f
Merge branch 'main' into reflect/type_name
tguichaoua Sep 21, 2022
de07bd5
fix merge error
tguichaoua Sep 21, 2022
858a6c2
fix ser/de for option
tguichaoua Sep 21, 2022
504495b
Merge branch 'main' into reflect/type_name
tguichaoua Oct 16, 2022
226dad2
fix CI
tguichaoua Oct 16, 2022
File filter

Filter by extension

Filter by extension

Conversations
Failed to load comments.
Loading
Jump to
Jump to file
Failed to load files.
Loading
Diff view
Diff view
Prev Previous commit
Next Next commit
Update crates/bevy_reflect/src/type_name.rs
Co-authored-by: Gino Valente <49806985+MrGVSV@users.noreply.github.com>
  • Loading branch information
tguichaoua and MrGVSV committed Aug 29, 2022
commit ef4986e3b52e3eb9af23fc78028a5f9d108a51c4
2 changes: 1 addition & 1 deletion crates/bevy_reflect/src/type_name.rs
Original file line number Diff line number Diff line change
Expand Up @@ -44,7 +44,7 @@ use crate::utility::GenericTypeNameCell;
pub trait TypeName: 'static {
tguichaoua marked this conversation as resolved.
Show resolved Hide resolved
/// Returns the name of the type.
///
/// This is a stable alternative to [`std::any::type_name`] whose output isn't guarentee
/// This is a stable alternative to [`std::any::type_name`] whose output isn't guarenteed
/// and may change between versions of the compiler.
fn name() -> &'static str;
tguichaoua marked this conversation as resolved.
Show resolved Hide resolved
Copy link
Member

Choose a reason for hiding this comment

The reason will be displayed to describe this comment to others. Learn more.

I think some more structure here would benefit other parts of the engine (and reflection as a whole):

trait TypePath {
  /// The full type path
  /// "my_crate::my_mod::MyType<Generics>"
  fn type_path() -> &'static str;
  /// The short type name, without generics
  /// "MyType"
  fn short_type_name_base() -> &'static str;
  /// The short type name, with generics
  /// "MyType<Generics>"
  fn short_type_name() -> &'static str;
  /// The full type path, minus the actual type:
  /// "my_crate::my_mod"
  fn module_path() -> &'static str;
  /// The crate name
  /// "my_crate"
  fn crate() -> &'static str;
}

Ideally, these could all be predefined "slices" of the same static str (to avoid multiple string allocations). I don't think this is "easy", but this type of hackery feels possible, given that workarounds for const-slicing arrays and const concatenating static strs are a thing.

I also think we should force some consistency here as part of the type name derive. (and consider manual impls invalid if they dont follow the rules). ex:

  • A crate name is required and it must match the actual name of the crate
  • Valid "rust module path syntax" must be used. Ex: my_crate::my_module::MyType.
  • You are allowed to "shorten" crate+module paths, but you can't remove them entirely. Ex: using MyType as the full path is not allowed.

Copy link
Member

Choose a reason for hiding this comment

The reason will be displayed to describe this comment to others. Learn more.

One area that would benefit from this structure is #5723, which currently embeds the short type name in the reflect info, which feels redundant with this effort.

Copy link
Contributor Author

Choose a reason for hiding this comment

The reason will be displayed to describe this comment to others. Learn more.

Having a single static str and slicing it is possible.

In the example bellow, PATH and IDENT can be provided by the user and other consts are computed by the derive macro (in real case, the macro don't create those consts and use directly the value, it's to make the example clearer).

The only value that is tricky to compute is LIB_NAME_LEN. If the user provides a custom path, we got a literal string, so the macro can compute the LIB_NAME_LEN value. But if not provided, we use module_path and we have no way except by using const_str.
Actually, there is a unstable feature TokenStream::expend_expr (rust-lang/rust#90765) that expend macro inside it, so we could get the result of module_path as a literal string. But since this feature is pretty recent, I don't think we can relied on it.

pub struct Foo<T>(T);

// The path is by default `module_path!()`
// If the user override it, it must start with the lib name
// and must be a valid rust path (e.g. `my_lib::my_mod`).
const PATH: &'static str = module_path!();

// By default the ident of the type.
// If the user override it, it must be a valid ident.
const IDENT: &'static str = "Foo";

// Values computed by the derive macro
const IDENT_POS: usize = PATH.len() + 2;
const GENERIC_POS: usize = IDENT_POS + IDENT.len();
const LIB_NAME_LEN: usize = const_str::split!(PATH, "::")[0].len();

impl<T: TypePath> TypePath for Foo<T> {
    fn type_path() -> &'static str {
        // if non generic, `concat!(PATH, "::", IDENT)` is used instead.
        let type_path = format!("{}::{}<{}>", PATH, IDENT, T::type_path());
        // In real case the value is stored in a static variable to avoid recomputation.
        Box::leak(Box::new(type_path))
    }

    fn short_type_name_base() -> &'static str {
        &Self::type_path()[IDENT_POS..GENERIC_POS]
    }

    fn short_type_name() -> &'static str {
        &Self::type_path()[IDENT_POS..]
    }

    fn module_path() -> &'static str {
        &Self::type_path()[..PATH.len()]
    }

    fn crate_name() -> &'static str {
        &Self::type_path()[..LIB_NAME_LEN]
    }
}

}
Expand Down