yoke-derive-0.8.1/.cargo_vcs_info.json0000644000000001570000000000100132700ustar { "git": { "sha1": "29dfe2790b6cfdab94ca6a6b69f58ce54802dbf7" }, "path_in_vcs": "utils/yoke/derive" }yoke-derive-0.8.1/Cargo.lock0000644000000025500000000000100112420ustar # This file is automatically @generated by Cargo. # It is not intended for manual editing. version = 3 [[package]] name = "proc-macro2" version = "1.0.103" source = "registry+https://github.com/rust-lang/crates.io-index" checksum = "5ee95bc4ef87b8d5ba32e8b7714ccc834865276eab0aed5c9958d00ec45f49e8" dependencies = [ "unicode-ident", ] [[package]] name = "quote" version = "1.0.41" source = "registry+https://github.com/rust-lang/crates.io-index" checksum = "ce25767e7b499d1b604768e7cde645d14cc8584231ea6b295e9c9eb22c02e1d1" dependencies = [ "proc-macro2", ] [[package]] name = "syn" version = "2.0.108" source = "registry+https://github.com/rust-lang/crates.io-index" checksum = "da58917d35242480a05c2897064da0a80589a2a0476c9a3f2fdc83b53502e917" dependencies = [ "proc-macro2", "quote", "unicode-ident", ] [[package]] name = "synstructure" version = "0.13.2" source = "registry+https://github.com/rust-lang/crates.io-index" checksum = "728a70f3dbaf5bab7f0c4b1ac8d7ae5ea60a4b5549c8a5914361c99147a709d2" dependencies = [ "proc-macro2", "quote", "syn", ] [[package]] name = "unicode-ident" version = "1.0.20" source = "registry+https://github.com/rust-lang/crates.io-index" checksum = "462eeb75aeb73aea900253ce739c8e18a67423fadf006037cd3ff27e82748a06" [[package]] name = "yoke-derive" version = "0.8.1" dependencies = [ "proc-macro2", "quote", "syn", "synstructure", ] yoke-derive-0.8.1/Cargo.toml0000644000000026370000000000100112730ustar # 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 are reading this file be aware that the original Cargo.toml # will likely look very different (and much more reasonable). # See Cargo.toml.orig for the original contents. [package] edition = "2021" name = "yoke-derive" version = "0.8.1" authors = ["Manish Goregaokar "] build = false autolib = false autobins = false autoexamples = false autotests = false autobenches = false description = "Custom derive for the yoke crate" readme = "README.md" keywords = [ "zerocopy", "serialization", "lifetime", "borrow", "self-referential", ] categories = [ "data-structures", "memory-management", "caching", "no-std", ] license = "Unicode-3.0" repository = "https://github.com/unicode-org/icu4x" [package.metadata.workspaces] independent = true [lib] name = "yoke_derive" path = "src/lib.rs" proc-macro = true [[example]] name = "yoke_derive" path = "examples/yoke_derive.rs" [dependencies.proc-macro2] version = "1.0.61" [dependencies.quote] version = "1.0.28" [dependencies.syn] version = "2.0.21" features = ["fold"] [dependencies.synstructure] version = "0.13.0" [dev-dependencies] yoke-derive-0.8.1/Cargo.toml.orig000064400000000000000000000017271046102023000147530ustar 00000000000000# This file is part of ICU4X. For terms of use, please see the file # called LICENSE at the top level of the ICU4X source tree # (online at: https://github.com/unicode-org/icu4x/blob/main/LICENSE ). [package] name = "yoke-derive" description = "Custom derive for the yoke crate" version = "0.8.1" categories = ["data-structures", "memory-management", "caching", "no-std"] keywords = ["zerocopy", "serialization", "lifetime", "borrow", "self-referential"] authors = ["Manish Goregaokar "] edition.workspace = true license.workspace = true repository.workspace = true [package.metadata.workspaces] independent = true [lib] proc-macro = true path = "src/lib.rs" [dependencies] proc-macro2 = { workspace = true } quote = { workspace = true } syn = { workspace = true, features = ["fold"] } synstructure = { workspace = true } [dev-dependencies] yoke = { path = "..", features = ["derive"] } zerovec = { path = "../../../utils/zerovec", features = ["yoke"] } yoke-derive-0.8.1/LICENSE000064400000000000000000000042231046102023000130630ustar 00000000000000UNICODE LICENSE V3 COPYRIGHT AND PERMISSION NOTICE Copyright © 2020-2024 Unicode, Inc. NOTICE TO USER: Carefully read the following legal agreement. BY DOWNLOADING, INSTALLING, COPYING OR OTHERWISE USING DATA FILES, AND/OR SOFTWARE, YOU UNEQUIVOCALLY ACCEPT, AND AGREE TO BE BOUND BY, ALL OF THE TERMS AND CONDITIONS OF THIS AGREEMENT. IF YOU DO NOT AGREE, DO NOT DOWNLOAD, INSTALL, COPY, DISTRIBUTE OR USE THE DATA FILES OR SOFTWARE. Permission is hereby granted, free of charge, to any person obtaining a copy of data files and any associated documentation (the "Data Files") or software and any associated documentation (the "Software") to deal in the Data Files or Software without restriction, including without limitation the rights to use, copy, modify, merge, publish, distribute, and/or sell copies of the Data Files or Software, and to permit persons to whom the Data Files or Software are furnished to do so, provided that either (a) this copyright and permission notice appear with all copies of the Data Files or Software, or (b) this copyright and permission notice appear in associated Documentation. THE DATA FILES AND SOFTWARE ARE 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 OF THIRD PARTY RIGHTS. IN NO EVENT SHALL THE COPYRIGHT HOLDER OR HOLDERS INCLUDED IN THIS NOTICE BE LIABLE FOR ANY CLAIM, OR ANY SPECIAL INDIRECT OR CONSEQUENTIAL DAMAGES, OR ANY DAMAGES WHATSOEVER RESULTING FROM LOSS OF USE, DATA OR PROFITS, WHETHER IN AN ACTION OF CONTRACT, NEGLIGENCE OR OTHER TORTIOUS ACTION, ARISING OUT OF OR IN CONNECTION WITH THE USE OR PERFORMANCE OF THE DATA FILES OR SOFTWARE. Except as contained in this notice, the name of a copyright holder shall not be used in advertising or otherwise to promote the sale, use or other dealings in these Data Files or Software without prior written authorization of the copyright holder. SPDX-License-Identifier: Unicode-3.0 — Portions of ICU4X may have been adapted from ICU4C and/or ICU4J. ICU 1.8.1 to ICU 57.1 © 1995-2016 International Business Machines Corporation and others. yoke-derive-0.8.1/README.md000064400000000000000000000005731046102023000133410ustar 00000000000000# yoke-derive [![crates.io](https://img.shields.io/crates/v/yoke-derive)](https://crates.io/crates/yoke-derive) Custom derives for `Yokeable` from the `yoke` crate. ## More Information For more information on development, authorship, contributing etc. please visit [`ICU4X home page`](https://github.com/unicode-org/icu4x). yoke-derive-0.8.1/examples/yoke_derive.rs000064400000000000000000000056201046102023000165510ustar 00000000000000// This file is part of ICU4X. For terms of use, please see the file // called LICENSE at the top level of the ICU4X source tree // (online at: https://github.com/unicode-org/icu4x/blob/main/LICENSE ). #![allow(unused)] use std::{borrow::Cow, marker::PhantomData}; use yoke::{Yoke, Yokeable}; use zerovec::{maps::ZeroMapKV, ule::AsULE, VarZeroVec, ZeroMap, ZeroVec}; #[derive(Yokeable)] pub struct StringExample { x: String, } #[derive(Yokeable, Copy, Clone)] pub struct IntExample { x: u32, } #[derive(Yokeable, Copy, Clone)] pub struct GenericsExample { x: u32, y: T, } #[derive(Yokeable, Copy, Clone)] pub struct GenericsExampleWithDefault { x: T, y: U, } #[derive(Yokeable)] pub struct CowExample<'a> { x: u8, y: &'a str, z: Cow<'a, str>, w: Cow<'a, [u8]>, } #[derive(Yokeable)] pub struct ZeroVecExample<'a> { var: VarZeroVec<'a, str>, vec: ZeroVec<'a, u16>, } #[derive(Yokeable)] pub struct ZeroVecExampleWithGenerics<'a, T: AsULE> { gen: ZeroVec<'a, T>, vec: ZeroVec<'a, u16>, bare: T, } // Since ZeroMap has generic parameters, the Rust compiler cannot // prove the covariance of the lifetimes. To use derive(Yokeable) // with a type such as ZeroMap, you just add the attribute // yoke(prove_covariance_manually) #[derive(Yokeable)] #[yoke(prove_covariance_manually)] pub struct ZeroMapExample<'a> { map: ZeroMap<'a, str, u16>, } #[derive(Yokeable)] #[yoke(prove_covariance_manually)] pub struct ZeroMapGenericExample<'a, T: for<'b> ZeroMapKV<'b> + ?Sized> { map: ZeroMap<'a, str, T>, } #[derive(Yokeable)] pub struct MaybeSizedWrap { x: T, y: Option, ignored: PhantomData, q: Q, } // TODO(#4119): Make this example compile /* #[derive(Yokeable)] pub struct MaybeSizedWrapWithLifetime<'a, T, Q: ?Sized, U: ?Sized> { x: T, y: Option, ignored: &'a U, q: Q, } */ pub struct AssertYokeable { string: Yoke>, int: Yoke>, gen1: Yoke, Box<[u8]>>, gen2: Yoke, Box<[u8]>>, gen_default1: Yoke, Box<[u8]>>, gen_default2: Yoke, Box<[u8]>>, cow: Yoke, Box<[u8]>>, zv: Yoke, Box<[u8]>>, zv_gen1: Yoke, Box<[u8]>>, zv_gen2: Yoke, Box<[u8]>>, map: Yoke, Box<[u8]>>, map_gen1: Yoke, Box<[u8]>>, map_gen2: Yoke, Box<[u8]>>, maybe_sized_wrap: Yoke, Box<[u8]>>, // TODO(#4119): Make this example compile // maybe_sized_wrap_with_lt: Yoke, Box<[u8]>>, } fn main() {} yoke-derive-0.8.1/src/lib.rs000064400000000000000000000276601046102023000137730ustar 00000000000000// This file is part of ICU4X. For terms of use, please see the file // called LICENSE at the top level of the ICU4X source tree // (online at: https://github.com/unicode-org/icu4x/blob/main/LICENSE ). //! Custom derives for `Yokeable` from the `yoke` crate. use proc_macro::TokenStream; use proc_macro2::{Span, TokenStream as TokenStream2}; use quote::quote; use syn::spanned::Spanned; use syn::{parse_macro_input, parse_quote, DeriveInput, Ident, Lifetime, Type, WherePredicate}; use synstructure::Structure; mod visitor; /// Custom derive for `yoke::Yokeable`, /// /// If your struct contains `zerovec::ZeroMap`, then the compiler will not /// be able to guarantee the lifetime covariance due to the generic types on /// the `ZeroMap` itself. You must add the following attribute in order for /// the custom derive to work with `ZeroMap`. /// /// ```rust,ignore /// #[derive(Yokeable)] /// #[yoke(prove_covariance_manually)] /// ``` /// /// Beyond this case, if the derive fails to compile due to lifetime issues, it /// means that the lifetime is not covariant and `Yokeable` is not safe to implement. #[proc_macro_derive(Yokeable, attributes(yoke))] pub fn yokeable_derive(input: TokenStream) -> TokenStream { let input = parse_macro_input!(input as DeriveInput); TokenStream::from(yokeable_derive_impl(&input)) } fn yokeable_derive_impl(input: &DeriveInput) -> TokenStream2 { let tybounds = input .generics .type_params() .map(|ty| { // Strip out param defaults, we don't need them in the impl let mut ty = ty.clone(); ty.eq_token = None; ty.default = None; ty }) .collect::>(); let typarams = tybounds .iter() .map(|ty| ty.ident.clone()) .collect::>(); // We require all type parameters be 'static, otherwise // the Yokeable impl becomes really unweildy to generate safely let static_bounds: Vec = typarams .iter() .map(|ty| parse_quote!(#ty: 'static)) .collect(); let lts = input.generics.lifetimes().count(); if lts == 0 { let name = &input.ident; quote! { // This is safe because there are no lifetime parameters. unsafe impl<'a, #(#tybounds),*> yoke::Yokeable<'a> for #name<#(#typarams),*> where #(#static_bounds,)* Self: Sized { type Output = Self; #[inline] fn transform(&self) -> &Self::Output { self } #[inline] fn transform_owned(self) -> Self::Output { self } #[inline] unsafe fn make(this: Self::Output) -> Self { this } #[inline] fn transform_mut(&'a mut self, f: F) where F: 'static + for<'b> FnOnce(&'b mut Self::Output) { f(self) } } } } else { if lts != 1 { return syn::Error::new( input.generics.span(), "derive(Yokeable) cannot have multiple lifetime parameters", ) .to_compile_error(); } let name = &input.ident; let manual_covariance = input.attrs.iter().any(|a| { if let Ok(i) = a.parse_args::() { if i == "prove_covariance_manually" { return true; } } false }); if manual_covariance { let mut structure = Structure::new(input); let generics_env = typarams.iter().cloned().collect(); let static_bounds: Vec = typarams .iter() .map(|ty| parse_quote!(#ty: 'static)) .collect(); let mut yoke_bounds: Vec = vec![]; structure.bind_with(|_| synstructure::BindStyle::Move); let owned_body = structure.each_variant(|vi| { vi.construct(|f, i| { let binding = format!("__binding_{i}"); let field = Ident::new(&binding, Span::call_site()); let fty_static = replace_lifetime(&f.ty, static_lt()); let (has_ty, has_lt) = visitor::check_type_for_parameters(&f.ty, &generics_env); if has_ty { // For types without type parameters, the compiler can figure out that the field implements // Yokeable on its own. However, if there are type parameters, there may be complex preconditions // to `FieldTy: Yokeable` that need to be satisfied. We get them to be satisfied by requiring // `FieldTy<'static>: Yokeable>` if has_lt { let fty_a = replace_lifetime(&f.ty, custom_lt("'a")); yoke_bounds.push( parse_quote!(#fty_static: yoke::Yokeable<'a, Output = #fty_a>), ); } else { yoke_bounds.push( parse_quote!(#fty_static: yoke::Yokeable<'a, Output = #fty_static>), ); } } if has_ty || has_lt { // By calling transform_owned on all fields, we manually prove // that the lifetimes are covariant, since this requirement // must already be true for the type that implements transform_owned(). quote! { <#fty_static as yoke::Yokeable<'a>>::transform_owned(#field) } } else { // No nested lifetimes, so nothing to be done quote! { #field } } }) }); let borrowed_body = structure.each(|binding| { let f = binding.ast(); let field = &binding.binding; let (has_ty, has_lt) = visitor::check_type_for_parameters(&f.ty, &generics_env); if has_ty || has_lt { let fty_static = replace_lifetime(&f.ty, static_lt()); let fty_a = replace_lifetime(&f.ty, custom_lt("'a")); // We also must assert that each individual field can `transform()` correctly // // Even though transform_owned() does such an assertion already, CoerceUnsized // can cause type transformations that allow it to succeed where this would fail. // We need to check both. // // https://github.com/unicode-org/icu4x/issues/2928 quote! { let _: &#fty_a = &<#fty_static as yoke::Yokeable<'a>>::transform(#field); } } else { // No nested lifetimes, so nothing to be done quote! {} } }); return quote! { unsafe impl<'a, #(#tybounds),*> yoke::Yokeable<'a> for #name<'static, #(#typarams),*> where #(#static_bounds,)* #(#yoke_bounds,)* { type Output = #name<'a, #(#typarams),*>; #[inline] fn transform(&'a self) -> &'a Self::Output { // These are just type asserts, we don't need them for anything if false { match self { #borrowed_body } } unsafe { // safety: we have asserted covariance in // transform_owned ::core::mem::transmute(self) } } #[inline] fn transform_owned(self) -> Self::Output { match self { #owned_body } } #[inline] unsafe fn make(this: Self::Output) -> Self { use core::{mem, ptr}; // unfortunately Rust doesn't think `mem::transmute` is possible since it's not sure the sizes // are the same debug_assert!(mem::size_of::() == mem::size_of::()); let ptr: *const Self = (&this as *const Self::Output).cast(); // This is a noop if the struct is copy, which Clippy doesn't like // Furthermore, restriction lints like mem_forget are usually for first-party code, not custom derives #[allow(forgetting_copy_types, clippy::forget_copy, clippy::forget_non_drop, clippy::mem_forget)] mem::forget(this); ptr::read(ptr) } #[inline] fn transform_mut(&'a mut self, f: F) where F: 'static + for<'b> FnOnce(&'b mut Self::Output) { unsafe { f(core::mem::transmute::<&'a mut Self, &'a mut Self::Output>(self)) } } } }; } quote! { // This is safe because as long as `transform()` compiles, // we can be sure that `'a` is a covariant lifetime on `Self` // // This will not work for structs involving ZeroMap since // the compiler does not know that ZeroMap is covariant. // // This custom derive can be improved to handle this case when // necessary unsafe impl<'a, #(#tybounds),*> yoke::Yokeable<'a> for #name<'static, #(#typarams),*> where #(#static_bounds,)* { type Output = #name<'a, #(#typarams),*>; #[inline] fn transform(&'a self) -> &'a Self::Output { self } #[inline] fn transform_owned(self) -> Self::Output { self } #[inline] unsafe fn make(this: Self::Output) -> Self { use core::{mem, ptr}; // unfortunately Rust doesn't think `mem::transmute` is possible since it's not sure the sizes // are the same debug_assert!(mem::size_of::() == mem::size_of::()); let ptr: *const Self = (&this as *const Self::Output).cast(); // This is a noop if the struct is copy, which Clippy doesn't like // Furthermore, restriction lints like mem_forget are usually for first-party code, not custom derives #[allow(forgetting_copy_types, clippy::forget_copy, clippy::forget_non_drop, clippy::mem_forget)] mem::forget(this); ptr::read(ptr) } #[inline] fn transform_mut(&'a mut self, f: F) where F: 'static + for<'b> FnOnce(&'b mut Self::Output) { unsafe { f(core::mem::transmute::<&'a mut Self, &'a mut Self::Output>(self)) } } } } } } fn static_lt() -> Lifetime { Lifetime::new("'static", Span::call_site()) } fn custom_lt(s: &str) -> Lifetime { Lifetime::new(s, Span::call_site()) } fn replace_lifetime(x: &Type, lt: Lifetime) -> Type { use syn::fold::Fold; struct ReplaceLifetime(Lifetime); impl Fold for ReplaceLifetime { fn fold_lifetime(&mut self, _: Lifetime) -> Lifetime { self.0.clone() } } ReplaceLifetime(lt).fold_type(x.clone()) } yoke-derive-0.8.1/src/visitor.rs000064400000000000000000000073561046102023000147240ustar 00000000000000// This file is part of ICU4X. For terms of use, please see the file // called LICENSE at the top level of the ICU4X source tree // (online at: https://github.com/unicode-org/icu4x/blob/main/LICENSE ). //! Visitor for determining whether a type has type and non-static lifetime parameters use std::collections::HashSet; use syn::visit::{visit_lifetime, visit_type, visit_type_path, Visit}; use syn::{Ident, Lifetime, Type, TypePath}; struct TypeVisitor<'a> { /// The type parameters in scope typarams: &'a HashSet, /// Whether we found a type parameter found_typarams: bool, /// Whether we found a non-'static lifetime parameter found_lifetimes: bool, } impl<'ast> Visit<'ast> for TypeVisitor<'_> { fn visit_lifetime(&mut self, lt: &'ast Lifetime) { if lt.ident != "static" { self.found_lifetimes = true; } visit_lifetime(self, lt) } fn visit_type_path(&mut self, ty: &'ast TypePath) { // We only need to check ty.path.get_ident() and not ty.qself or any // generics in ty.path because the visitor will eventually visit those // types on its own if let Some(ident) = ty.path.get_ident() { if self.typarams.contains(ident) { self.found_typarams = true; } } visit_type_path(self, ty) } } /// Checks if a type has type or lifetime parameters, given the local context of /// named type parameters. Returns (has_type_params, has_lifetime_params) pub fn check_type_for_parameters(ty: &Type, typarams: &HashSet) -> (bool, bool) { let mut visit = TypeVisitor { typarams, found_typarams: false, found_lifetimes: false, }; visit_type(&mut visit, ty); (visit.found_typarams, visit.found_lifetimes) } #[cfg(test)] mod tests { use proc_macro2::Span; use std::collections::HashSet; use syn::{parse_quote, Ident}; use super::check_type_for_parameters; fn make_typarams(params: &[&str]) -> HashSet { params .iter() .map(|x| Ident::new(x, Span::call_site())) .collect() } #[test] fn test_simple_type() { let environment = make_typarams(&["T", "U", "V"]); let ty = parse_quote!(Foo<'a, T>); let check = check_type_for_parameters(&ty, &environment); assert_eq!(check, (true, true)); let ty = parse_quote!(Foo); let check = check_type_for_parameters(&ty, &environment); assert_eq!(check, (true, false)); let ty = parse_quote!(Foo<'static, T>); let check = check_type_for_parameters(&ty, &environment); assert_eq!(check, (true, false)); let ty = parse_quote!(Foo<'a>); let check = check_type_for_parameters(&ty, &environment); assert_eq!(check, (false, true)); let ty = parse_quote!(Foo<'a, Bar, Baz<(V, u8)>>); let check = check_type_for_parameters(&ty, &environment); assert_eq!(check, (true, true)); let ty = parse_quote!(Foo<'a, W>); let check = check_type_for_parameters(&ty, &environment); assert_eq!(check, (false, true)); } #[test] fn test_assoc_types() { let environment = make_typarams(&["T"]); let ty = parse_quote!(>::Output); let check = check_type_for_parameters(&ty, &environment); assert_eq!(check, (true, true)); let ty = parse_quote!(>::Output); let check = check_type_for_parameters(&ty, &environment); assert_eq!(check, (true, false)); let ty = parse_quote!(>::Output); let check = check_type_for_parameters(&ty, &environment); assert_eq!(check, (true, false)); } }