Interplane page as plain text, for reading tools and agents. English is the reference text: translate it with your own tool. Text in backticks and in ``` blocks (commands, fingerprints, file names) and link targets in <...> are exact: copy them, never translate them. Page: /drakestapleton/aienux/blob/main/vendor/ptr_meta_derive/src/lib.rs # aienux Aienux, the AienOS kernel: boot, scheduling, IPC and capability boundaries. Pre-alpha; host, emulator and physical evidence are labelled separately. Brought by drakestapleton Sign in to star 0 stars 0 watching About Files History Releases Proposed changes Issues For developers branch `main` Branches Tags History `aienux / vendor / ptr_meta_derive / src / lib.rs` 125 lines ยท 3675 bytes Raw Blame History 1 mod attributes; 2 3 use proc_macro2::TokenStream; 4 use quote::quote; 5 use syn::{ 6 meta, parse_macro_input, parse_quote, Data, DeriveInput, Error, ItemTrait, 7 }; 8 9 use self::attributes::Attributes; 10 11 /// Derives `Pointee` for the labeled struct which has a trailing DST. 12 /// 13 /// # Attributes 14 /// 15 /// Additional arguments can be specified using attributes. 16 /// 17 /// `#[ptr_meta(...)]` accepts the following arguments: 18 /// 19 /// - `crate = ...`: Chooses an alternative crate path to import ptr_meta from. 20 #[proc_macro_derive(Pointee, attributes(ptr_meta))] 21 pub fn derive_pointee( 22 input: proc_macro::TokenStream, 23 ) -> proc_macro::TokenStream { 24 let derive_input = parse_macro_input!(input as DeriveInput); 25 26 match derive_pointee_impl(derive_input) { 27 Ok(result) => result.into(), 28 Err(e) => e.to_compile_error().into(), 29 } 30 } 31 32 fn derive_pointee_impl(mut input: DeriveInput) -> Result { 33 let attributes = Attributes::parse(&input.attrs)?; 34 let ident = &input.ident; 35 let crate_path = attributes.crate_path(); 36 37 let fields = match &input.data { 38 Data::Struct(data) => &data.fields, 39 Data::Enum(_) => { 40 return Err(Error::new( 41 ident.span(), 42 "enums always have a provided `Pointee` impl because they \ 43 cannot be dynamically-sized", 44 )) 45 } 46 Data::Union(_) => { 47 return Err(Error::new( 48 ident.span(), 49 "unions always have an provided `Pointee` impl because they \ 50 cannot be dynamically-sized", 51 )) 52 } 53 }; 54 55 let Some(last_field) = fields.iter().next_back() else { 56 return Err(Error::new( 57 ident.span(), 58 "fieldless structs always have a provided `Poitnee` impl because 59 they cannot be dynamically-sized", 60 )); 61 }; 62 let last_field_ty = &last_field.ty; 63 64 let where_clause = input.generics.make_where_clause(); 65 where_clause 66 .predicates 67 .push(parse_quote! { #last_field_ty: #crate_path::Pointee }); 68 69 let (impl_generics, ty_generics, where_clause) = 70 input.generics.split_for_impl(); 71 72 Ok(quote! { 73 unsafe impl #impl_generics #crate_path::Pointee for #ident #ty_generics 74 #where_clause 75 { 76 type Metadata = <#last_field_ty as #crate_path::Pointee>::Metadata; 77 } 78 }) 79 } 80 81 /// Generates a `Pointee` implementation for trait object of the labeled trait. 82 /// 83 /// # Arguments 84 /// 85 /// `#[pointee(...)]` takes the following arguments: 86 /// 87 /// - `crate = ...`: Chooses an alternative crate path to import ptr_meta from. 88 #[proc_macro_attribute] 89 pub fn pointee( 90 attr: proc_macro::TokenStream, 91 item: proc_macro::TokenStream, 92 ) -> proc_macro::TokenStream { 93 let mut attributes = Attributes::default(); 94 let meta_parser = meta::parser(|nested| attributes.parse_meta(nested)); 95 96 parse_macro_input!(attr with meta_parser); 97 let item = parse_macro_input!(item as ItemTrait); 98 99 match pointee_impl(attributes, item) { 100 Ok(result) => result.into(), 101 Err(e) => e.to_compile_error().into(), 102 } 103 } 104 105 fn pointee_impl( 106 attributes: Attributes, 107 item: ItemTrait, 108 ) -> Result { 109 let ident = &item.ident; 110 let crate_path = attributes.crate_path(); 111 112 let (impl_generics, ty_generics, where_clause) = 113 item.generics.split_for_impl(); 114 115 Ok(quote! { 116 #item 117 118 unsafe impl #impl_generics #crate_path::Pointee for 119 (dyn #ident #ty_generics + '_) 120 #where_clause 121 { 122 type Metadata = #crate_path::DynMetadata; 123 } 124 }) 125 }