From 9d9b344a75571e4a641278f6f50e69f42d4f751a Mon Sep 17 00:00:00 2001 From: Quaternions Date: Tue, 6 Feb 2024 19:34:25 -0800 Subject: [PATCH] Initial commit --- .gitignore | 1 + Cargo.lock | 47 +++++++++++++++++++++++++++++++++++++++++++++++ Cargo.toml | 14 ++++++++++++++ src/lib.rs | 30 ++++++++++++++++++++++++++++++ 4 files changed, 92 insertions(+) create mode 100644 .gitignore create mode 100644 Cargo.lock create mode 100644 Cargo.toml create mode 100644 src/lib.rs diff --git a/.gitignore b/.gitignore new file mode 100644 index 0000000..ea8c4bf --- /dev/null +++ b/.gitignore @@ -0,0 +1 @@ +/target diff --git a/Cargo.lock b/Cargo.lock new file mode 100644 index 0000000..5e44835 --- /dev/null +++ b/Cargo.lock @@ -0,0 +1,47 @@ +# This file is automatically @generated by Cargo. +# It is not intended for manual editing. +version = 3 + +[[package]] +name = "id" +version = "0.1.0" +dependencies = [ + "proc-macro2", + "quote", + "syn", +] + +[[package]] +name = "proc-macro2" +version = "1.0.78" +source = "registry+https://github.com/rust-lang/crates.io-index" +checksum = "e2422ad645d89c99f8f3e6b88a9fdeca7fabeac836b1002371c4367c8f984aae" +dependencies = [ + "unicode-ident", +] + +[[package]] +name = "quote" +version = "1.0.35" +source = "registry+https://github.com/rust-lang/crates.io-index" +checksum = "291ec9ab5efd934aaf503a6466c5d5251535d108ee747472c3977cc5acc868ef" +dependencies = [ + "proc-macro2", +] + +[[package]] +name = "syn" +version = "2.0.48" +source = "registry+https://github.com/rust-lang/crates.io-index" +checksum = "0f3531638e407dfc0814761abb7c00a5b54992b849452a0646b7f65c9f770f3f" +dependencies = [ + "proc-macro2", + "quote", + "unicode-ident", +] + +[[package]] +name = "unicode-ident" +version = "1.0.12" +source = "registry+https://github.com/rust-lang/crates.io-index" +checksum = "3354b9ac3fae1ff6755cb6db53683adb661634f67557942dea4facebec0fee4b" diff --git a/Cargo.toml b/Cargo.toml new file mode 100644 index 0000000..35aa554 --- /dev/null +++ b/Cargo.toml @@ -0,0 +1,14 @@ +[package] +name = "id" +version = "0.1.0" +edition = "2021" + +[lib] +proc_macro=true + +# See more keys and their definitions at https://doc.rust-lang.org/cargo/reference/manifest.html + +[dependencies] +proc-macro2 = "1.0.78" +quote = "1.0.35" +syn = "2.0.48" diff --git a/src/lib.rs b/src/lib.rs new file mode 100644 index 0000000..5c67530 --- /dev/null +++ b/src/lib.rs @@ -0,0 +1,30 @@ +extern crate proc_macro; + +use proc_macro::TokenStream; +use quote::quote; +use syn::{parse_macro_input, DeriveInput}; + +#[proc_macro_derive(Id)] +pub fn id_derive(input: TokenStream) -> TokenStream { + // Parse the input tokens into a syntax tree + let ast = parse_macro_input!(input as DeriveInput); + let name = &ast.ident; + + // Generate the code for the implementation + let expanded = quote! { + impl #name { + #[inline] + pub const fn new(id: u32) -> Self { + Self(id) + } + + #[inline] + pub const fn get(self) -> u32 { + self.0 + } + } + }; + + // Convert the generated code into a token stream and return it + TokenStream::from(expanded) +}