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,39 +90,42 @@ pub fn device(attr: TokenStream, item: TokenStream) -> TokenStream {
.iter()
.map(|device_trait| {
quote! {
// Default impl
impl<T> #name<dyn #device_trait> for T
where
T: #interface_ident + #device_trait + 'static,
{
fn consume(self: Box<Self>) -> Option<Box<dyn #device_trait>> {
Some(self)
}
fn cast(&self) -> Option<&(dyn #device_trait + 'static)> {
Some(self)
}
fn cast_mut(&mut self) -> Option<&mut (dyn #device_trait + 'static)> {
Some(self)
}
where
T: #interface_ident + 'static,
{
default fn consume(self: Box<Self>) -> Option<Box<dyn #device_trait>> {
None
}
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
}
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
where
T: #interface_ident + #device_trait + 'static,
{
fn consume(self: Box<Self>) -> Option<Box<dyn #device_trait>> {
Some(self)
}
fn cast(&self) -> Option<&(dyn #device_trait + 'static)> {
Some(self)
}
fn cast_mut(&mut self) -> Option<&mut (dyn #device_trait + 'static)> {
Some(self)
}
}
}
})
.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);
interface.supertraits.push(TypeParamBound::Verbatim(quote! {
::core::marker::Sync + ::core::marker::Send + 'static
::core::marker::Sync + ::core::marker::Send
}));
#[cfg(feature = "debug")]