mirror of
https://gitlab.freedesktop.org/mesa/mesa.git
synced 2026-05-08 02:38:04 +02:00
nak: Don't use the proc macro to implement auto-boxing of ops
We can do this with a generic trait implementation as long as we have something on it to keep it from recursing on Box<T>. Part-of: <https://gitlab.freedesktop.org/mesa/mesa/-/merge_requests/41410>
This commit is contained in:
parent
14fcd9b2f8
commit
933de407f6
2 changed files with 10 additions and 38 deletions
|
|
@ -8256,6 +8256,16 @@ const _: () = {
|
|||
debug_assert!(size_of::<Op>() == 16);
|
||||
};
|
||||
|
||||
// The DisplayOp constraint exists to keep the type system from recursing
|
||||
impl<T: DisplayOp> From<T> for Op
|
||||
where
|
||||
Box<T>: Into<Op>,
|
||||
{
|
||||
fn from(op: T) -> Self {
|
||||
Box::new(op).into()
|
||||
}
|
||||
}
|
||||
|
||||
impl Op {
|
||||
pub fn is_branch(&self) -> bool {
|
||||
matches!(
|
||||
|
|
|
|||
|
|
@ -145,32 +145,6 @@ pub fn enum_derive_display_op(input: TokenStream) -> TokenStream {
|
|||
}
|
||||
}
|
||||
|
||||
fn into_box_inner_type<'a>(from_type: &'a syn::Type) -> Option<&'a syn::Type> {
|
||||
let last = match from_type {
|
||||
Type::Path(TypePath { path, .. }) => path.segments.last()?,
|
||||
_ => return None,
|
||||
};
|
||||
|
||||
if last.ident != "Box" {
|
||||
return None;
|
||||
}
|
||||
|
||||
let PathArguments::AngleBracketed(AngleBracketedGenericArguments {
|
||||
args,
|
||||
..
|
||||
}) = &last.arguments
|
||||
else {
|
||||
panic!("Expected Box<T> (with angle brackets)");
|
||||
};
|
||||
|
||||
for arg in args {
|
||||
if let GenericArgument::Type(inner_type) = arg {
|
||||
return Some(inner_type);
|
||||
}
|
||||
}
|
||||
panic!("Expected Box to use a type argument");
|
||||
}
|
||||
|
||||
#[proc_macro_derive(FromVariants)]
|
||||
pub fn derive_from_variants(input: TokenStream) -> TokenStream {
|
||||
let DeriveInput { ident, data, .. } = parse_macro_input!(input);
|
||||
|
|
@ -198,18 +172,6 @@ pub fn derive_from_variants(input: TokenStream) -> TokenStream {
|
|||
};
|
||||
|
||||
impls.extend(quote);
|
||||
|
||||
if let Some(inner_type) = into_box_inner_type(from_type) {
|
||||
let quote = quote! {
|
||||
impl From<#inner_type> for #enum_type {
|
||||
fn from(value: #inner_type) -> Self {
|
||||
From::from(Box::new(value))
|
||||
}
|
||||
}
|
||||
};
|
||||
|
||||
impls.extend(quote);
|
||||
}
|
||||
}
|
||||
}
|
||||
|
||||
|
|
|
|||
Loading…
Add table
Reference in a new issue