Cross-platform libraries for link-time initialization, finalization and collection in Rust.
| crate | docs | version |
|---|---|---|
ctor |
||
dtor |
||
link-section |
This project is made up of three crates.
Module initialization functions for Rust (like __attribute__((constructor)) in C/C++).
Run code before main to initialize data, external resources, or other state.
use ctor::ctor;
#[ctor(unsafe)]
fn foo() {
println!("Life before main!");
}Module shutdown functions for Rust (like __attribute__((destructor))).
Run code after main to clean up resources, or perform other final operations.
use dtor::dtor;
#[dtor(unsafe)]
fn foo() {
println!("Life after main!");
}Typed and untyped link section support for Rust.
Collect related items from an entire linked binary into a single link section.
use link_section::{section, in_section, TypedSection};
use ctor::ctor;
#[section]
static FOO: TypedSection<u32>;
#[in_section(FOO)]
fn foo() {
println!("Hello, world!");
}
#[ctor(unsafe)]
fn print_numbers() {
for i in *FOO {
println!("{}", i);
}
}Contributions are welcome!
These projects are dual-licensed under the Apache License, Version 2.0 and the MIT License.