From e3a8f5d083131005f1c7f1f0212d1d2eecfaffe4 Mon Sep 17 00:00:00 2001 From: Fabian Schmidt Date: Fri, 14 Mar 2025 13:29:25 +0100 Subject: [PATCH] Try out ctor and ctor_lite --- Cargo.lock | 41 +++++++++++++++++++++++++++++++++++++++++ Cargo.toml | 2 ++ Readme.md | 6 +++++- src/main.rs | 44 ++++++++++++++++++++++++++++++++++++++++++-- 4 files changed, 90 insertions(+), 3 deletions(-) create mode 100644 Cargo.lock diff --git a/Cargo.lock b/Cargo.lock new file mode 100644 index 0000000..9146b2e --- /dev/null +++ b/Cargo.lock @@ -0,0 +1,41 @@ +# This file is automatically @generated by Cargo. +# It is not intended for manual editing. +version = 4 + +[[package]] +name = "comptime_macros" +version = "0.1.0" +dependencies = [ + "ctor", +] + +[[package]] +name = "ctor" +version = "0.4.1" +source = "registry+https://github.com/rust-lang/crates.io-index" +checksum = "07e9666f4a9a948d4f1dff0c08a4512b0f7c86414b23960104c243c10d79f4c3" +dependencies = [ + "ctor-proc-macro", + "dtor", +] + +[[package]] +name = "ctor-proc-macro" +version = "0.0.5" +source = "registry+https://github.com/rust-lang/crates.io-index" +checksum = "4f211af61d8efdd104f96e57adf5e426ba1bc3ed7a4ead616e15e5881fd79c4d" + +[[package]] +name = "dtor" +version = "0.0.5" +source = "registry+https://github.com/rust-lang/crates.io-index" +checksum = "222ef136a1c687d4aa0395c175f2c4586e379924c352fd02f7870cf7de783c23" +dependencies = [ + "dtor-proc-macro", +] + +[[package]] +name = "dtor-proc-macro" +version = "0.0.5" +source = "registry+https://github.com/rust-lang/crates.io-index" +checksum = "7454e41ff9012c00d53cf7f475c5e3afa3b91b7c90568495495e8d9bf47a1055" diff --git a/Cargo.toml b/Cargo.toml index 5d4c07c..7877a66 100644 --- a/Cargo.toml +++ b/Cargo.toml @@ -4,3 +4,5 @@ version = "0.1.0" edition = "2024" [dependencies] +ctor = "0.4.1" +ctor-lite = "0.1.0" diff --git a/Readme.md b/Readme.md index 96a09f5..8d3f341 100644 --- a/Readme.md +++ b/Readme.md @@ -2,4 +2,8 @@ The goal of this crate is to find a way to annotate functions with an attribute macro like `#[get("/")]` and have it automatically added in a global registry. Actix for example still requires you to register all functions annotated in such a way with the service method, I don't need to do that in Spring Boot applications, I just use the necessary annotations. -This crate might not actually end up presenting a solution, but rather document why it's not possible to do so, only time will tell. + + +Turns out there is a way to do this with the ctor or ctor_lite crate. Seems simple enough and I need to find out which one I would prefer. +I think ctor_lite as I will wrap this in my own attribute macro anyway for my library. In an application I would probably prefer ctor as I personally find attribute macros more readable. +There seems to be no real differences between the two crates other than the macro type. diff --git a/src/main.rs b/src/main.rs index e7a11a9..f0622b7 100644 --- a/src/main.rs +++ b/src/main.rs @@ -1,3 +1,43 @@ -fn main() { - println!("Hello, world!"); +use ctor::{ctor, dtor}; + +#[ctor] +unsafe fn ctor() { + println!("Hello from ctor"); +} + +#[dtor] +unsafe fn dtor() { + println!("Goodbye from ctor"); +} + +ctor_lite::ctor! { + unsafe fn ctor_lite() { + println!("Hello from ctor_lite"); + } +} + +ctor_lite::dtor! { + unsafe fn dtor_lite() { + println!("Goodbye from ctor_lite"); + } +} + +struct HelloPrinter; + +impl HelloPrinter { + pub fn print(self) { + println!("Hello from printer"); + } +} + +impl Drop for HelloPrinter { + fn drop(&mut self) { + println!("Goodbye from printer"); + } +} + +fn main() { + println!("Hello from main"); + let printer = HelloPrinter; + printer.print(); }