gen_caster.rs 2.4 KB

12345678910111213141516171819202122232425262728293031323334353637383940414243444546474849505152535455565758596061626364
  1. use core::str::from_utf8_unchecked;
  2. use proc_macro2::TokenStream;
  3. use uuid::adapter::Simple;
  4. use uuid::Uuid;
  5. use quote::format_ident;
  6. use quote::quote;
  7. use quote::ToTokens;
  8. pub fn generate_caster(ty: &impl ToTokens, trait_: &impl ToTokens, sync: bool) -> TokenStream {
  9. let mut fn_buf = [0u8; FN_BUF_LEN];
  10. let fn_ident = format_ident!("{}", new_fn_name(&mut fn_buf));
  11. // 生成从dyn trait转换为具体类型结构体ty的caster
  12. let new_caster = if sync {
  13. quote! {
  14. ::intertrait::Caster::<dyn #trait_>::new_sync(
  15. |from| from.downcast_ref::<#ty>().unwrap(),
  16. |from| from.downcast_mut::<#ty>().unwrap(),
  17. |from| from.downcast::<#ty>().unwrap(),
  18. |from| from.downcast::<#ty>().unwrap(),
  19. |from| from.downcast::<#ty>().unwrap()
  20. )
  21. }
  22. } else {
  23. quote! {
  24. ::intertrait::Caster::<dyn #trait_>::new(
  25. |from| from.downcast_ref::<#ty>().unwrap(),
  26. |from| from.downcast_mut::<#ty>().unwrap(),
  27. |from| from.downcast::<#ty>().unwrap(),
  28. |from| from.downcast::<#ty>().unwrap(),
  29. )
  30. }
  31. };
  32. // 由于过程宏是在预编译期执行的,这里的target_os是linux。
  33. // 编译完成的proc macro会交给下一阶段进行编译,因此,#[cfg(target_os)]会在下一阶段生效。
  34. // 我们必须在预处理阶段把两种代码的token stream都生成出来,然后在下一阶段选择性地使用其中一种。
  35. quote! {
  36. #[cfg(not(target_os = "none"))]
  37. #[::linkme::distributed_slice(::intertrait::CASTERS)]
  38. fn #fn_ident() -> (::std::any::TypeId, ::intertrait::BoxedCaster) {
  39. (::std::any::TypeId::of::<#ty>(), Box::new(#new_caster))
  40. }
  41. #[cfg(target_os = "none")]
  42. #[::linkme::distributed_slice(::intertrait::CASTERS)]
  43. fn #fn_ident() -> (::core::any::TypeId, ::intertrait::BoxedCaster) {
  44. (::core::any::TypeId::of::<#ty>(), alloc::boxed::Box::new(#new_caster))
  45. }
  46. }
  47. }
  48. const FN_PREFIX: &[u8] = b"__";
  49. const FN_BUF_LEN: usize = FN_PREFIX.len() + Simple::LENGTH;
  50. fn new_fn_name(buf: &mut [u8]) -> &str {
  51. buf[..FN_PREFIX.len()].copy_from_slice(FN_PREFIX);
  52. Uuid::new_v4()
  53. .to_simple()
  54. .encode_lower(&mut buf[FN_PREFIX.len()..]);
  55. unsafe { from_utf8_unchecked(&buf[..FN_BUF_LEN]) }
  56. }