Skip to content

Commit

Permalink
perf(macros/msg): use cached type_id
Browse files Browse the repository at this point in the history
  • Loading branch information
loyd committed Apr 16, 2024
1 parent 3d9bfe2 commit 4198532
Show file tree
Hide file tree
Showing 3 changed files with 14 additions and 2 deletions.
5 changes: 5 additions & 0 deletions elfo-core/src/envelope.rs
Original file line number Diff line number Diff line change
Expand Up @@ -98,6 +98,11 @@ impl Envelope {
self.message.is::<M>()
}

#[inline]
pub fn type_id(&self) -> std::any::TypeId {
self.message.type_id()
}

#[doc(hidden)]
#[stability::unstable]
pub fn duplicate(&self) -> Self {
Expand Down
5 changes: 5 additions & 0 deletions elfo-core/src/message.rs
Original file line number Diff line number Diff line change
Expand Up @@ -86,6 +86,11 @@ impl AnyMessage {
self.data.is::<M>()
}

#[inline]
pub fn type_id(&self) -> std::any::TypeId {
(*self.data).type_id()
}

#[inline]
pub fn downcast_ref<M: Message>(&self) -> Option<&M> {
self.data.downcast_ref::<M>().map(|message| {
Expand Down
6 changes: 4 additions & 2 deletions elfo-macros-impl/src/msg.rs
Original file line number Diff line number Diff line change
Expand Up @@ -226,6 +226,7 @@ pub fn msg_impl(input: proc_macro::TokenStream, path_to_elfo: Path) -> proc_macr
}

let envelope_ident = quote! { _elfo_envelope };
let type_id_ident = quote! { _type_id_envelope };

// println!(">>> HERE {:#?}", groups);

Expand All @@ -236,7 +237,7 @@ pub fn msg_impl(input: proc_macro::TokenStream, path_to_elfo: Path) -> proc_macr
// - used the regular syntax while the request one is expected
// - unexhaustive match
(GroupKind::Regular(path), arms) => quote_spanned! { path.span()=>
else if #envelope_ident.is::<#path>() {
else if #type_id_ident == std::any::TypeId::of::<#path>() {
// Ensure it's not a request, or a request but only in a borrowed context.
// We cannot use `static_assertions` here because it wraps the check into
// a closure that forbids us to use generic `msg!`: (`msg!(match e { M => .. })`).
Expand Down Expand Up @@ -264,7 +265,7 @@ pub fn msg_impl(input: proc_macro::TokenStream, path_to_elfo: Path) -> proc_macr
}
},
(GroupKind::Request(path), arms) => quote_spanned! { path.span()=>
else if #envelope_ident.is::<#path>() {
else if #type_id_ident == std::any::TypeId::of::<#path>() {
// Ensure it's a request. We cannot use `static_assertions` here
// because it wraps the check into a closure that forbids us to
// use generic `msg!`: (`msg!(match e { (R, token) => .. })`).
Expand Down Expand Up @@ -308,6 +309,7 @@ pub fn msg_impl(input: proc_macro::TokenStream, path_to_elfo: Path) -> proc_macr
// TODO: propagate `input.attrs`?
let expanded = quote! {{
let #envelope_ident = #match_expr;
let #type_id_ident = #envelope_ident.type_id();
#[allow(clippy::suspicious_else_formatting)]
if false { unreachable!(); }
#(#groups)*
Expand Down

0 comments on commit 4198532

Please sign in to comment.