Add example using linkme

This commit is contained in:
Fabian Schmidt 2025-03-17 10:17:57 +01:00
parent e3a8f5d083
commit 5acb24f182
6 changed files with 224 additions and 0 deletions

63
Cargo.lock generated
View File

@ -7,6 +7,8 @@ name = "comptime_macros"
version = "0.1.0"
dependencies = [
"ctor",
"ctor-lite",
"linkme",
]
[[package]]
@ -19,6 +21,12 @@ dependencies = [
"dtor",
]
[[package]]
name = "ctor-lite"
version = "0.1.0"
source = "registry+https://github.com/rust-lang/crates.io-index"
checksum = "1f791803201ab277ace03903de1594460708d2d54df6053f2d9e82f592b19e3b"
[[package]]
name = "ctor-proc-macro"
version = "0.0.5"
@ -39,3 +47,58 @@ name = "dtor-proc-macro"
version = "0.0.5"
source = "registry+https://github.com/rust-lang/crates.io-index"
checksum = "7454e41ff9012c00d53cf7f475c5e3afa3b91b7c90568495495e8d9bf47a1055"
[[package]]
name = "linkme"
version = "0.3.32"
source = "registry+https://github.com/rust-lang/crates.io-index"
checksum = "22d227772b5999ddc0690e733f734f95ca05387e329c4084fe65678c51198ffe"
dependencies = [
"linkme-impl",
]
[[package]]
name = "linkme-impl"
version = "0.3.32"
source = "registry+https://github.com/rust-lang/crates.io-index"
checksum = "71a98813fa0073a317ed6a8055dcd4722a49d9b862af828ee68449adb799b6be"
dependencies = [
"proc-macro2",
"quote",
"syn",
]
[[package]]
name = "proc-macro2"
version = "1.0.94"
source = "registry+https://github.com/rust-lang/crates.io-index"
checksum = "a31971752e70b8b2686d7e46ec17fb38dad4051d94024c88df49b667caea9c84"
dependencies = [
"unicode-ident",
]
[[package]]
name = "quote"
version = "1.0.40"
source = "registry+https://github.com/rust-lang/crates.io-index"
checksum = "1885c039570dc00dcb4ff087a89e185fd56bae234ddc7f056a945bf36467248d"
dependencies = [
"proc-macro2",
]
[[package]]
name = "syn"
version = "2.0.100"
source = "registry+https://github.com/rust-lang/crates.io-index"
checksum = "b09a44accad81e1ba1cd74a32461ba89dee89095ba17b32f5d03683b1b1fc2a0"
dependencies = [
"proc-macro2",
"quote",
"unicode-ident",
]
[[package]]
name = "unicode-ident"
version = "1.0.18"
source = "registry+https://github.com/rust-lang/crates.io-index"
checksum = "5a5f39404a5da50712a4c1eecf25e90dd62b613502b7e925fd4e4d19b5c96512"

View File

@ -6,3 +6,4 @@ edition = "2024"
[dependencies]
ctor = "0.4.1"
ctor-lite = "0.1.0"
linkme = "0.3.32"

31
src/bin/ctor.rs Normal file
View File

@ -0,0 +1,31 @@
use ctor::{ctor, dtor};
#[ctor]
unsafe fn ctor() {
println!("Hello from ctor");
}
#[dtor]
unsafe fn dtor() {
println!("Goodbye from ctor");
}
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();
}

31
src/bin/ctor_lite.rs Normal file
View File

@ -0,0 +1,31 @@
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();
}

59
src/bin/linkme.rs Normal file
View File

@ -0,0 +1,59 @@
use linkme::distributed_slice;
use std::sync::Once;
#[distributed_slice]
static CTORS: [fn()] = [..];
#[distributed_slice]
static DTORS: [fn()] = [..];
static INIT: Once = Once::new();
static DROP: Once = Once::new();
fn run_ctors() {
INIT.call_once(|| {
for ctor in CTORS {
ctor();
}
});
}
fn run_dtors() {
DROP.call_once(|| {
for dtor in DTORS {
dtor();
}
});
}
#[linkme::distributed_slice(CTORS)]
fn ctor() {
println!("Hello from linkme");
}
#[linkme::distributed_slice(DTORS)]
fn dtor() {
println!("Goodbye from linkme");
}
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() {
run_ctors();
println!("Hello from main");
let printer = HelloPrinter;
printer.print();
run_dtors();
}

View File

@ -1,4 +1,41 @@
use ctor::{ctor, dtor};
use linkme::distributed_slice;
use std::sync::Once;
#[distributed_slice]
static CTORS: [fn()] = [..];
#[distributed_slice]
static DTORS: [fn()] = [..];
static INIT: Once = Once::new();
static DROP: Once = Once::new();
fn run_ctors() {
INIT.call_once(|| {
for linkme_ctor in CTORS {
linkme_ctor();
}
});
}
fn run_dtors() {
DROP.call_once(|| {
for linkme_dtor in DTORS {
linkme_dtor();
}
});
}
#[linkme::distributed_slice(CTORS)]
fn linkme_ctor() {
println!("Hello from linkme");
}
#[linkme::distributed_slice(DTORS)]
fn linkme_dtor() {
println!("Goodbye from linkme");
}
#[ctor]
unsafe fn ctor() {
@ -37,7 +74,9 @@ impl Drop for HelloPrinter {
}
fn main() {
run_ctors();
println!("Hello from main");
let printer = HelloPrinter;
printer.print();
run_dtors();
}