Try out ctor and ctor_lite

This commit is contained in:
Fabian Schmidt 2025-03-14 13:29:25 +01:00
parent 8c2b70c8cf
commit e3a8f5d083
4 changed files with 90 additions and 3 deletions

41
Cargo.lock generated Normal file
View File

@ -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"

View File

@ -4,3 +4,5 @@ version = "0.1.0"
edition = "2024" edition = "2024"
[dependencies] [dependencies]
ctor = "0.4.1"
ctor-lite = "0.1.0"

View File

@ -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. 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. 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.

View File

@ -1,3 +1,43 @@
fn main() { use ctor::{ctor, dtor};
println!("Hello, world!");
#[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();
} }