Improved impl_cast
All checks were successful
continuous-integration/drone/push Build is passing

This commit is contained in:
Dreaded_X 2023-04-12 05:38:15 +02:00
parent f8d1975f85
commit 33fcb95dfa
Signed by: Dreaded_X
GPG Key ID: FA5F485356B0D2D4

View File

@ -90,6 +90,26 @@ pub fn device(attr: TokenStream, item: TokenStream) -> TokenStream {
.iter() .iter()
.map(|device_trait| { .map(|device_trait| {
quote! { quote! {
// Default impl
impl<T> #name<dyn #device_trait> for T
where
T: #interface_ident + 'static,
{
default fn consume(self: Box<Self>) -> Option<Box<dyn #device_trait>> {
None
}
default fn cast(&self) -> Option<&(dyn #device_trait + 'static)> {
None
}
default fn cast_mut(&mut self) -> Option<&mut (dyn #device_trait + 'static)> {
None
}
}
// Specialization, should not cause any unsoundness as we dispatch based on
// #device_trait
impl<T> #name<dyn #device_trait> for T impl<T> #name<dyn #device_trait> for T
where where
T: #interface_ident + #device_trait + 'static, T: #interface_ident + #device_trait + 'static,
@ -106,23 +126,6 @@ pub fn device(attr: TokenStream, item: TokenStream) -> TokenStream {
Some(self) Some(self)
} }
} }
impl<T> #name<dyn #device_trait> for T
where
T: #interface_ident + ?Sized,
{
default fn consume(self: Box<Self>) -> Option<Box<dyn #device_trait>> {
None
}
default fn cast(&self) -> Option<&(dyn #device_trait + 'static)> {
None
}
default fn cast_mut(&mut self) -> Option<&mut (dyn #device_trait + 'static)> {
None
}
}
} }
}) })
.fold(quote! {}, |acc, x| { .fold(quote! {}, |acc, x| {
@ -149,7 +152,7 @@ pub fn device_trait(_attr: TokenStream, item: TokenStream) -> TokenStream {
let mut interface: ItemTrait = parse_macro_input!(item); let mut interface: ItemTrait = parse_macro_input!(item);
interface.supertraits.push(TypeParamBound::Verbatim(quote! { interface.supertraits.push(TypeParamBound::Verbatim(quote! {
::core::marker::Sync + ::core::marker::Send + 'static ::core::marker::Sync + ::core::marker::Send
})); }));
#[cfg(feature = "debug")] #[cfg(feature = "debug")]