ptr_meta_derive-0.1.4/.cargo_vcs_info.json0000644000000001120000000000000142000ustar { "git": { "sha1": "7c10669a67324208a153343928a29b89302fe75e" } } ptr_meta_derive-0.1.4/Cargo.toml0000644000000017020000000000000122040ustar # THIS FILE IS AUTOMATICALLY GENERATED BY CARGO # # When uploading crates to the registry Cargo will automatically # "normalize" Cargo.toml files for maximal compatibility # with all versions of Cargo and also rewrite `path` dependencies # to registry (e.g., crates.io) dependencies # # If you believe there's an error in this file please file an # issue against the rust-lang/cargo repository. If you're # editing this file be aware that the upstream Cargo.toml # will likely look very different (and much more reasonable) [package] edition = "2018" name = "ptr_meta_derive" version = "0.1.4" authors = ["David Koloski "] description = "Macros for ptr_meta" documentation = "https://docs.rs/ptr_meta_derive" license = "MIT" repository = "https://github.com/djkoloski/ptr_meta" [lib] proc-macro = true [dependencies.proc-macro2] version = "1.0" [dependencies.quote] version = "1.0" [dependencies.syn] version = "1.0" features = ["full"] ptr_meta_derive-0.1.4/Cargo.toml.orig000064400000000000000000000007570000000000000156540ustar 00000000000000[package] name = "ptr_meta_derive" version = "0.1.4" authors = ["David Koloski "] edition = "2018" description = "Macros for ptr_meta" license = "MIT" documentation = "https://docs.rs/ptr_meta_derive" repository = "https://github.com/djkoloski/ptr_meta" # See more keys and their definitions at https://doc.rust-lang.org/cargo/reference/manifest.html [lib] proc-macro = true [dependencies] proc-macro2 = "1.0" syn = { version = "1.0", features = ["full"] } quote = "1.0" ptr_meta_derive-0.1.4/LICENSE000064400000000000000000000020350000000000000137610ustar 00000000000000Copyright 2021 David Koloski Permission is hereby granted, free of charge, to any person obtaining a copy of this software and associated documentation files (the "Software"), to deal in the Software without restriction, including without limitation the rights to use, copy, modify, merge, publish, distribute, sublicense, and/or sell copies of the Software, and to permit persons to whom the Software is furnished to do so, subject to the following conditions: The above copyright notice and this permission notice shall be included in all copies or substantial portions of the Software. THE SOFTWARE IS PROVIDED "AS IS", WITHOUT WARRANTY OF ANY KIND, EXPRESS OR IMPLIED, INCLUDING BUT NOT LIMITED TO THE WARRANTIES OF MERCHANTABILITY, FITNESS FOR A PARTICULAR PURPOSE AND NONINFRINGEMENT. IN NO EVENT SHALL THE AUTHORS OR COPYRIGHT HOLDERS BE LIABLE FOR ANY CLAIM, DAMAGES OR OTHER LIABILITY, WHETHER IN AN ACTION OF CONTRACT, TORT OR OTHERWISE, ARISING FROM, OUT OF OR IN CONNECTION WITH THE SOFTWARE OR THE USE OR OTHER DEALINGS IN THE SOFTWARE. ptr_meta_derive-0.1.4/src/lib.rs000064400000000000000000000053610000000000000146640ustar 00000000000000use proc_macro2::TokenStream; use quote::quote; use syn::{parse_macro_input, Data, DeriveInput, Error, Fields, ItemTrait}; /// Generates an implementation of `Pointee` for structs with a DST as its last /// field. #[proc_macro_derive(Pointee)] pub fn pointee_derive(input: proc_macro::TokenStream) -> proc_macro::TokenStream { let input = parse_macro_input!(input as DeriveInput); proc_macro::TokenStream::from(derive_pointee_impl(&input)) } fn derive_pointee_impl(input: &DeriveInput) -> TokenStream { let ident = &input.ident; let last_field_ty = match input.data { Data::Struct(ref data) => match data.fields { Fields::Named(ref fields) => { if let Some(result) = fields.named.last() { &result.ty } else { return Error::new(ident.span(), "dynamically sized structs must contain at least one field").to_compile_error(); } }, Fields::Unnamed(ref fields) => { if let Some(result) = fields.unnamed.last() { &result.ty } else { return Error::new(ident.span(), "dynamically sized structs must contain at least one field").to_compile_error(); } }, Fields::Unit => return Error::new(ident.span(), "unit structs cannot be dynamically sized").to_compile_error(), }, Data::Enum(_) => return Error::new(ident.span(), "enums cannot be dynamically sized").to_compile_error(), Data::Union(_) => return Error::new(ident.span(), "unions cannot be dynamically sized").to_compile_error(), }; let (impl_generics, ty_generics, where_clause) = input.generics.split_for_impl(); quote! { const _: () = { use ptr_meta::Pointee; impl #impl_generics Pointee for #ident #ty_generics #where_clause where #last_field_ty: Pointee, { type Metadata = <#last_field_ty as Pointee>::Metadata; } }; } } /// Generates an implementation of `Pointee` for trait objects. #[proc_macro_attribute] pub fn pointee( _attr: proc_macro::TokenStream, item: proc_macro::TokenStream, ) -> proc_macro::TokenStream { let input = parse_macro_input!(item as ItemTrait); let ident = &input.ident; let (impl_generics, ty_generics, where_clause) = input.generics.split_for_impl(); let result = quote! { #input const _: () = { use ptr_meta::{DynMetadata, Pointee}; impl #impl_generics Pointee for (dyn #ident #ty_generics #where_clause + '_) { type Metadata = DynMetadata; } }; }; proc_macro::TokenStream::from(result) }