Skip to content

Instantly share code, notes, and snippets.

@GoldsteinE
Created July 26, 2022 15:16
Show Gist options
  • Star 0 You must be signed in to star a gist
  • Fork 0 You must be signed in to fork a gist
  • Save GoldsteinE/4b24e70639c1513e4bddfe5ebfa16ef2 to your computer and use it in GitHub Desktop.
Save GoldsteinE/4b24e70639c1513e4bddfe5ebfa16ef2 to your computer and use it in GitHub Desktop.
// Problem:
macro_rules! generate_impl {
($x:ident) => {
unsafe impl cxx::ExternType for $x {
// Won't work, `type_id!()` wants a literal, not a macro call
type Id = cxx::type_id!(stringify!($x));
type Kind = cxx::kind::Trivial;
}
};
}
// Solution:
macro_rules! generate_impl {
($x:ident) => {
::paste::paste! {
// `paste!()` concatenates strings with idents in doc comments
generate_impl!(@ $x #[doc = "" $x]);
}
};
(@ $x:ident #[doc = $i:literal]) => {
unsafe impl cxx::ExternType for $x {
type Id = cxx::type_id!($i);
type Kind = cxx::kind::Trivial;
}
}
}
Sign up for free to join this conversation on GitHub. Already have an account? Sign in to comment