Simplify dioxus-config-macro (#2514)

* refactor config macro code

* fix clippy
This commit is contained in:
Evan Almloff 2024-06-19 01:10:23 +02:00 committed by GitHub
parent 3a4860add4
commit b0ae9be9c8
No known key found for this signature in database
GPG key ID: B5690EEEBB952194
4 changed files with 39 additions and 134 deletions

View file

@ -6,137 +6,33 @@ use proc_macro::TokenStream;
use proc_macro2::TokenStream as TokenStream2;
use quote::quote;
#[proc_macro]
pub fn server_only(input: TokenStream) -> TokenStream {
if cfg!(any(feature = "ssr", feature = "liveview")) {
let input = TokenStream2::from(input);
quote! {
#input
macro_rules! define_config_macro {
($name:ident if $($cfg:tt)+) => {
#[proc_macro]
pub fn $name(input: TokenStream) -> TokenStream {
if cfg!($($cfg)+) {
let input = TokenStream2::from(input);
quote! {
{
#input
}
}
} else {
quote! {
{}
}
}
.into()
}
} else {
quote! {
{}
}
}
.into()
};
}
#[proc_macro]
pub fn client(input: TokenStream) -> TokenStream {
if cfg!(any(feature = "desktop", feature = "web")) {
let input = TokenStream2::from(input);
quote! {
#input
}
} else {
quote! {
{}
}
}
.into()
}
#[proc_macro]
pub fn web(input: TokenStream) -> TokenStream {
if cfg!(feature = "web") {
let input = TokenStream2::from(input);
quote! {
#input
}
} else {
quote! {
{}
}
}
.into()
}
#[proc_macro]
pub fn desktop(input: TokenStream) -> TokenStream {
if cfg!(feature = "desktop") {
let input = TokenStream2::from(input);
quote! {
#input
}
} else {
quote! {
{}
}
}
.into()
}
#[proc_macro]
pub fn mobile(input: TokenStream) -> TokenStream {
if cfg!(feature = "mobile") {
let input = TokenStream2::from(input);
quote! {
#input
}
} else {
quote! {
{}
}
}
.into()
}
#[proc_macro]
pub fn fullstack(input: TokenStream) -> TokenStream {
if cfg!(feature = "fullstack") {
let input = TokenStream2::from(input);
quote! {
#input
}
} else {
quote! {
{}
}
}
.into()
}
#[proc_macro]
pub fn static_generation(input: TokenStream) -> TokenStream {
if cfg!(feature = "static-generation") {
let input = TokenStream2::from(input);
quote! {
#input
}
} else {
quote! {
{}
}
}
.into()
}
#[proc_macro]
pub fn ssr(input: TokenStream) -> TokenStream {
if cfg!(feature = "ssr") {
let input = TokenStream2::from(input);
quote! {
#input
}
} else {
quote! {
{}
}
}
.into()
}
#[proc_macro]
pub fn liveview(input: TokenStream) -> TokenStream {
if cfg!(feature = "liveview") {
let input = TokenStream2::from(input);
quote! {
#input
}
} else {
quote! {
{}
}
}
.into()
}
define_config_macro!(server_only if any(feature = "ssr", feature = "liveview"));
define_config_macro!(client if any(feature = "desktop", feature = "web"));
define_config_macro!(web if feature = "web");
define_config_macro!(desktop if feature = "desktop");
define_config_macro!(mobile if feature = "mobile");
define_config_macro!(fullstack if feature = "fullstack");
define_config_macro!(static_generation if feature = "static-generation");
define_config_macro!(ssr if feature = "ssr");
define_config_macro!(liveview if feature = "liveview");

View file

@ -433,8 +433,11 @@ impl<Args: 'static, Ret: 'static> std::ops::Deref for Callback<Args, Ret> {
// The real closure that we will never use.
&uninit_closure
},
#[allow(clippy::missing_transmute_annotations)]
// We transmute self into a reference to the closure. This is safe because we know that the closure has the same memory layout as Self so &Closure == &Self.
unsafe { std::mem::transmute(self) },
unsafe {
std::mem::transmute(self)
},
);
// Cast the closure to a trait object.

View file

@ -95,8 +95,11 @@ impl<O> std::ops::Deref for UseCallback<O> {
// The real closure that we will never use.
&uninit_closure
},
#[allow(clippy::missing_transmute_annotations)]
// We transmute self into a reference to the closure. This is safe because we know that the closure has the same memory layout as Self so &Closure == &Self.
unsafe { std::mem::transmute(self) },
unsafe {
std::mem::transmute(self)
},
);
// Cast the closure to a trait object.

View file

@ -220,8 +220,11 @@ pub trait Readable {
// The real closure that we will never use.
&uninit_closure
},
#[allow(clippy::missing_transmute_annotations)]
// We transmute self into a reference to the closure. This is safe because we know that the closure has the same memory layout as Self so &Closure == &Self.
unsafe { std::mem::transmute(self) },
unsafe {
std::mem::transmute(self)
},
);
// Cast the closure to a trait object.