Skip to content

Commit

Permalink
Yeah this probs won't work
Browse files Browse the repository at this point in the history
  • Loading branch information
oscartbeaumont committed Dec 25, 2023
1 parent d6259a7 commit c2099c1
Show file tree
Hide file tree
Showing 3 changed files with 17 additions and 7 deletions.
2 changes: 1 addition & 1 deletion macros/src/type/mod.rs
Original file line number Diff line number Diff line change
Expand Up @@ -44,7 +44,7 @@ pub fn derive(input: proc_macro::TokenStream) -> syn::Result<proc_macro::TokenSt
unraw_raw_ident(&format_ident!("{}", raw_ident.to_string())).to_token_stream()
});

let sid = quote!(#crate_ref::internal::construct::sid(#name, concat!("::", module_path!(), ":", line!(), ":", column!())));
let sid = quote!(#crate_ref::SpectaID::from::<Self>());
let (inlines, reference, can_flatten) = match data {
Data::Struct(data) => {
parse_struct(&name, &container_attrs, generics, &crate_ref, &sid, data)
Expand Down
18 changes: 14 additions & 4 deletions src/type/specta_id.rs
Original file line number Diff line number Diff line change
Expand Up @@ -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<T>() -> Self {
let type_name = std::any::type_name::<T>();
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::<T>(); // Current output is in the form `some::Type<some::Generic>`

// Strip from `<` to the end of the string
let end_offset = type_name.rfind("<").unwrap_or(type_name.len());

Check warning on line 30 in src/type/specta_id.rs

View workflow job for this annotation

GitHub Actions / clippy

single-character string constant used as pattern

warning: single-character string constant used as pattern --> src/type/specta_id.rs:30:42 | 30 | let end_offset = type_name.rfind("<").unwrap_or(type_name.len()); | ^^^ help: try using a `char` instead: `'<'` | = help: for further information visit https://rust-lang.github.io/rust-clippy/master/index.html#single_char_pattern = note: `#[warn(clippy::single_char_pattern)]` implied by `#[warn(clippy::all)]`

// 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::<T>(),
}
}
Expand Down
4 changes: 2 additions & 2 deletions tests/duplicate_ty_name.rs
Original file line number Diff line number Diff line change
Expand Up @@ -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::<Demo>(&Default::default()), err);
Expand Down

0 comments on commit c2099c1

Please sign in to comment.