3 releases

new 0.0.2 Feb 18, 2025
0.0.1 Feb 15, 2025
0.0.0 Feb 27, 2024

#1516 in Rust patterns

Download history 1/week @ 2024-10-29 2/week @ 2024-11-05 20/week @ 2024-11-12 2/week @ 2024-12-10 1/week @ 2025-01-28 9/week @ 2025-02-04 117/week @ 2025-02-11

127 downloads per month

Apache-2.0 OR MIT

26KB
379 lines

dtor

Build Status

ctor docs.rs crates.io

dtor docs.rs crates.io

Module teardown functions for Rust (like __attribute__((destructor)) in C/C++) for Linux, OSX, FreeBSD, NetBSD, Illumos, OpenBSD, DragonFlyBSD, Android, iOS, WASM, and Windows.

Examples

Print a message at shutdown time. Note that Rust may have shut down some stdlib services at this time.

    #[dtor]
    unsafe fn shutdown() {
        // Using println or eprintln here will panic as Rust has shut down
        libc::printf("Shutting down!\n\0".as_ptr() as *const i8);
    }

Under the Hood

The #[dtor] macro effectively creates a constructor that calls libc::atexit with the provided function, ie roughly equivalent to:

    #[ctor]
    fn dtor_atexit() {
        libc::atexit(dtor);
    }

Dependencies

~55KB