databake-derive-0.2.0/.cargo_vcs_info.json0000644000000001630000000000100140430ustar { "git": { "sha1": "6bd4893cc44c2ca2718de47a119a31cc40045fe5" }, "path_in_vcs": "utils/databake/derive" }databake-derive-0.2.0/Cargo.toml0000644000000030730000000000100120440ustar # 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" rust-version = "1.71.1" name = "databake-derive" version = "0.2.0" authors = ["The ICU4X Project Developers"] build = false include = [ "data/**/*", "src/**/*", "examples/**/*", "benches/**/*", "tests/**/*", "Cargo.toml", "LICENSE", "README.md", ] autobins = false autoexamples = false autotests = false autobenches = false description = "Custom derive for the databake crate" readme = "README.md" keywords = [ "zerocopy", "serialization", "const", "proc", ] categories = [ "rust-patterns", "memory-management", "development-tools::procedural-macro-helpers", "development-tools::build-utils", ] license = "Unicode-3.0" repository = "https://github.com/unicode-org/icu4x" [package.metadata.workspaces] independent = true [lib] name = "databake_derive" path = "src/lib.rs" proc-macro = true [[test]] name = "derive" path = "tests/derive.rs" [dependencies.proc-macro2] version = "1.0.61" [dependencies.quote] version = "1.0.28" [dependencies.syn] version = "2.0.21" [dependencies.synstructure] version = "0.13.0" [dev-dependencies] databake-derive-0.2.0/Cargo.toml.orig000064400000000000000000000017031046102023000155230ustar 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 = "databake-derive" description = "Custom derive for the databake crate" version = "0.2.0" categories = ["rust-patterns", "memory-management", "development-tools::procedural-macro-helpers", "development-tools::build-utils"] keywords = ["zerocopy", "serialization", "const", "proc"] authors.workspace = true edition.workspace = true include.workspace = true license.workspace = true repository.workspace = true rust-version.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 } synstructure = { workspace = true } [dev-dependencies] databake = { path = "..", features = ["derive"]} databake-derive-0.2.0/LICENSE000064400000000000000000000042231046102023000136410ustar 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. databake-derive-0.2.0/README.md000064400000000000000000000005541046102023000141160ustar 00000000000000# databake-derive [![crates.io](https://img.shields.io/crates/v/databake-derive)](https://crates.io/crates/databake-derive) Custom derives for `Bake` ## More Information For more information on development, authorship, contributing etc. please visit [`ICU4X home page`](https://github.com/unicode-org/icu4x). databake-derive-0.2.0/src/lib.rs000064400000000000000000000067321046102023000145460ustar 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 `Bake` use proc_macro::TokenStream; use proc_macro2::TokenStream as TokenStream2; use quote::quote; use syn::{ parse::{Parse, ParseStream}, parse_macro_input, punctuated::Punctuated, DeriveInput, Ident, Path, PathSegment, Token, }; use synstructure::{AddBounds, Structure}; /// This custom derive auto-implements the `Bake` trait on any given type that has public /// fields that also implement `Bake`. /// /// For a type `Person` defined in the module `module` of crate `bar`, this derive /// can be used as follows: /// /// ```rust /// use databake::Bake; /// /// #[derive(Bake)] /// #[databake(path = bar::module)] /// pub struct Person<'a> { /// pub name: &'a str, /// pub age: u32, /// } /// ``` #[proc_macro_derive(Bake, attributes(databake))] pub fn bake_derive(input: TokenStream) -> TokenStream { let input = parse_macro_input!(input as DeriveInput); TokenStream::from(bake_derive_impl(&input)) } fn bake_derive_impl(input: &DeriveInput) -> TokenStream2 { let mut structure = Structure::new(input); struct PathAttr(Punctuated); impl Parse for PathAttr { fn parse(input: ParseStream<'_>) -> syn::parse::Result { let i: Ident = input.parse()?; if i != "path" { return Err(input.error(format!("expected token \"path\", found {i:?}"))); } input.parse::()?; Ok(Self(input.parse::()?.segments)) } } let path = input .attrs .iter() .find(|a| a.path().is_ident("databake")) .expect("missing databake(path = ...) attribute") .parse_args::() .unwrap() .0; let bake_body = structure.each_variant(|vi| { let recursive_calls = vi.bindings().iter().map(|b| { let ident = b.binding.clone(); quote! { let #ident = #ident.bake(env); } }); let constructor = vi.construct(|_, i| { let ident = &vi.bindings()[i].binding; quote! { # #ident } }); quote! { #(#recursive_calls)* databake::quote! { #path::#constructor } } }); let borrows_size_body = structure.each_variant(|vi| { let recursive_calls = vi.bindings().iter().map(|b| { let ident = b.binding.clone(); quote! { #ident.borrows_size() } }); quote! { 0 #(+ #recursive_calls)* } }); structure.add_bounds(AddBounds::Fields); let crate_name = path.iter().next().unwrap(); let crate_name = quote!(#crate_name).to_string(); structure.gen_impl(quote! { gen impl databake::Bake for @Self { fn bake(&self, env: &databake::CrateEnv) -> databake::TokenStream { env.insert(#crate_name); match self { #bake_body &_ => unreachable!() // ZST references aren't uninhabited } } } gen impl databake::BakeSize for @Self { fn borrows_size(&self) -> usize { match self { #borrows_size_body &_ => unreachable!() // ZST references aren't uninhabited } } } }) } databake-derive-0.2.0/tests/derive.rs000064400000000000000000000023061046102023000156220ustar 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 ). extern crate alloc; use databake::*; #[derive(Bake)] #[databake(path = test)] pub struct IntExample { x: u8, } #[test] fn test_int_example() { test_bake!(IntExample, const, crate::IntExample { x: 17u8 }, test,); } #[derive(Bake)] #[databake(path = test)] pub struct GenericsExample { x: u32, y: T, } #[test] fn test_generics_example() { test_bake!( GenericsExample, const, crate::GenericsExample { x: 17u32, y: 100isize }, test ); } #[derive(Bake)] #[databake(path = test)] pub struct CowExample<'a> { x: u8, y: &'a str, z: alloc::borrow::Cow<'a, str>, w: alloc::borrow::Cow<'a, [u8]>, } #[test] fn test_cow_example() { test_bake!( CowExample<'static>, const, crate::CowExample { x: 17u8, y: "foo", z: alloc::borrow::Cow::Borrowed("bar"), w: alloc::borrow::Cow::Borrowed(b"\x01\x02\x03"), }, test ); }