diff --git a/macros/src/type/mod.rs b/macros/src/type/mod.rs index 4f00ee20..aaf8a3b6 100644 --- a/macros/src/type/mod.rs +++ b/macros/src/type/mod.rs @@ -44,7 +44,7 @@ pub fn derive(input: proc_macro::TokenStream) -> syn::Result()); let (inlines, reference, can_flatten) = match data { Data::Struct(data) => { parse_struct(&name, &container_attrs, generics, &crate_ref, &sid, data) diff --git a/src/type/specta_id.rs b/src/type/specta_id.rs index 9ab0729b..054fd7a2 100644 --- a/src/type/specta_id.rs +++ b/src/type/specta_id.rs @@ -21,13 +21,23 @@ pub struct SpectaID { } impl SpectaID { - // TODO: Unit test this well including with non-static types. + // TODO: Unit test this (including with non-static types) pub fn from() -> Self { - let type_name = std::any::type_name::(); - let last_segment = type_name.rfind("::").unwrap_or(type_name.len()); + // I am aware `std::any::type_name`'s format is not guaranteed but plz just let me order my types by name without a macro in peace. + let type_name = std::any::type_name::(); // Current output is in the form `some::Type` + + // Strip from `<` to the end of the string + let end_offset = type_name.rfind("<").unwrap_or(type_name.len()); + + // Strip everything before the last `::` + let start_offset = type_name[0..end_offset] + .rfind("::") + // +2 to skip the :: + .map(|v| v + 2) + .unwrap_or(0); SpectaID { - type_name: &type_name[0..last_segment], + type_name: &type_name[start_offset..end_offset], tid: non_static_type_id::(), } } diff --git a/tests/duplicate_ty_name.rs b/tests/duplicate_ty_name.rs index 48418893..1f4b3ae3 100644 --- a/tests/duplicate_ty_name.rs +++ b/tests/duplicate_ty_name.rs @@ -39,14 +39,14 @@ fn test_duplicate_ty_name() { #[cfg(not(target_os = "windows"))] let err = Err(ExportError::DuplicateTypeName( "One".into(), - impl_location("tests/duplicate_ty_name.rs:19:14"), impl_location("tests/duplicate_ty_name.rs:9:14"), + impl_location("tests/duplicate_ty_name.rs:19:14"), )); #[cfg(target_os = "windows")] let err = Err(ExportError::DuplicateTypeName( "One".into(), - impl_location("tests\\duplicate_ty_name.rs:19:14"), impl_location("tests\\duplicate_ty_name.rs:9:14"), + impl_location("tests\\duplicate_ty_name.rs:19:14"), )); assert_eq!(export::(&Default::default()), err);