Skip to content

mmastrac/rust-ctor

Repository files navigation

linktime

Cross-platform libraries for link-time initialization, finalization and collection in Rust.

Build Status

crate docs version
ctor docs.rs crates.io
dtor docs.rs crates.io
link-section docs.rs crates.io

Crates

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);        
    }
}

Contributing

Contributions are welcome!

License

These projects are dual-licensed under the Apache License, Version 2.0 and the MIT License.

About

Module initialization/global constructor functions for Rust

Topics

Resources

License

Apache-2.0, MIT licenses found

Licenses found

Apache-2.0
LICENSE-APACHE
MIT
LICENSE-MIT

Stars

Watchers

Forks

Releases

No releases published

Packages

 
 
 

Contributors

Languages