{"id":13595105,"url":"https://github.com/mmastrac/rust-ctor","last_synced_at":"2025-05-13T17:13:42.573Z","repository":{"id":33681831,"uuid":"160615620","full_name":"mmastrac/rust-ctor","owner":"mmastrac","description":"Module initialization/global constructor functions for Rust","archived":false,"fork":false,"pushed_at":"2025-04-21T18:07:32.000Z","size":352,"stargazers_count":846,"open_issues_count":12,"forks_count":58,"subscribers_count":7,"default_branch":"master","last_synced_at":"2025-05-03T02:50:29.805Z","etag":null,"topics":["constructor","dylib","macros","rust"],"latest_commit_sha":null,"homepage":"","language":"Rust","has_issues":true,"has_wiki":null,"has_pages":null,"mirror_url":null,"source_name":null,"license":"apache-2.0","status":null,"scm":"git","pull_requests_enabled":true,"icon_url":"https://github.com/mmastrac.png","metadata":{"files":{"readme":"README.md","changelog":null,"contributing":null,"funding":null,"license":"LICENSE-APACHE","code_of_conduct":null,"threat_model":null,"audit":null,"citation":null,"codeowners":null,"security":null,"support":null,"governance":null,"roadmap":null,"authors":null,"dei":null,"publiccode":null,"codemeta":null,"zenodo":null}},"created_at":"2018-12-06T03:50:42.000Z","updated_at":"2025-04-28T15:10:35.000Z","dependencies_parsed_at":"2022-08-07T23:00:18.780Z","dependency_job_id":"1d49fd6a-1357-418e-84ca-615fc096986f","html_url":"https://github.com/mmastrac/rust-ctor","commit_stats":{"total_commits":241,"total_committers":29,"mean_commits":8.310344827586206,"dds":0.3817427385892116,"last_synced_commit":"fc9cd685f881b6a4055fddc9da3a28779bad6e10"},"previous_names":[],"tags_count":0,"template":false,"template_full_name":null,"repository_url":"https://repos.ecosyste.ms/api/v1/hosts/GitHub/repositories/mmastrac%2Frust-ctor","tags_url":"https://repos.ecosyste.ms/api/v1/hosts/GitHub/repositories/mmastrac%2Frust-ctor/tags","releases_url":"https://repos.ecosyste.ms/api/v1/hosts/GitHub/repositories/mmastrac%2Frust-ctor/releases","manifests_url":"https://repos.ecosyste.ms/api/v1/hosts/GitHub/repositories/mmastrac%2Frust-ctor/manifests","owner_url":"https://repos.ecosyste.ms/api/v1/hosts/GitHub/owners/mmastrac","download_url":"https://codeload.github.com/mmastrac/rust-ctor/tar.gz/refs/heads/master","host":{"name":"GitHub","url":"https://github.com","kind":"github","repositories_count":253990498,"owners_count":21995776,"icon_url":"https://github.com/github.png","version":null,"created_at":"2022-05-30T11:31:42.601Z","updated_at":"2022-07-04T15:15:14.044Z","host_url":"https://repos.ecosyste.ms/api/v1/hosts/GitHub","repositories_url":"https://repos.ecosyste.ms/api/v1/hosts/GitHub/repositories","repository_names_url":"https://repos.ecosyste.ms/api/v1/hosts/GitHub/repository_names","owners_url":"https://repos.ecosyste.ms/api/v1/hosts/GitHub/owners"}},"keywords":["constructor","dylib","macros","rust"],"created_at":"2024-08-01T16:01:44.033Z","updated_at":"2025-05-13T17:13:37.561Z","avatar_url":"https://github.com/mmastrac.png","language":"Rust","funding_links":[],"categories":["Rust"],"sub_categories":[],"readme":"# rust-ctor\n\n![Build Status](https://github.com/mmastrac/rust-ctor/actions/workflows/rust.yml/badge.svg)\n\n`ctor` [![docs.rs](https://docs.rs/ctor/badge.svg)](https://docs.rs/ctor)\n[![crates.io](https://img.shields.io/crates/v/ctor.svg)](https://crates.io/crates/ctor)\n\n`dtor` [![docs.rs](https://docs.rs/dtor/badge.svg)](https://docs.rs/dtor)\n[![crates.io](https://img.shields.io/crates/v/dtor.svg)](https://crates.io/crates/dtor)\n\nModule initialization/teardown functions for Rust (like\n`__attribute__((constructor))` in C/C++) for Linux, OSX, FreeBSD, NetBSD,\nIllumos, OpenBSD, DragonFlyBSD, Android, iOS, WASM, and Windows.\n\nThis library currently requires **Rust \u003e 1.56.0** at a minimum for edition\n2021 support. Library versions 0.2.x should work for edition 2018, and 1.0\nis planned to be released as 2021-only.\n\n## Zero Dependency\n\nAs of `ctor 0.3.0+`, `ctor` has no dependencies (other than the\n`ctor-proc-macro` crate). The proc macro in this crate calls into the\ndeclarative macro that does the majority of the work.\n\n## Support\n\nThis library works and is regularly tested on Linux, OSX, Windows, and FreeBSD,\nwith both `+crt-static` and `-crt-static` where possible. Other platforms are\nsupported but not tested as part of the automatic builds. This library will also\nwork as expected in both `bin` and `cdylib` outputs, ie: the `ctor` and `dtor`\nwill run at executable or library startup/shutdown respectively.\n\nThis library supports WASM targets, but the MSRV for this target is 1.85.\n\n## Warnings\n\nRust's philosophy is that nothing happens before or after main and\nthis library explicitly subverts that. The code that runs in the `ctor`\nand `dtor` functions should be careful to limit itself to `libc`\nfunctions and code that does not rely on Rust's stdlib services.\n\nFor example, using stdout in a `dtor` function is a guaranteed panic. Consider\nusing the [`libc-print` crate](https://crates.io/crates/libc-print) for output\nto stderr/stdout during `#[ctor]` and `#[dtor]` methods. Other issues\nmay involve signal processing or panic handling in that early code.\n\nSome linker configurations may cause `#[ctor]` and `#[dtor]` functions to be\nstripped from the final binary. The `used_linker` feature may prevent this, but\nis not supported outside of nightly Rust. Often, a simple `use module_with_ctor`\nis sufficient to ensure the linker does not strip the function.\n\nOn some platforms, unloading of shared libraries may not actually happen until\nprocess exit, even if explicitly unloaded. The rules for this are arcane and\ndifficult to understand. For example, thread-local storage on OSX will affect\nthis (see [this\ncomment](https://github.com/rust-lang/rust/issues/28794#issuecomment-368693049)).\n\n## Examples\n\nMarks the function `foo` as a module constructor, called when a static\nlibrary is loaded or an executable is started:\n\n```rust\n    static INITED: AtomicBool = AtomicBool::new(false);\n\n    #[ctor]\n    fn foo() {\n        INITED.store(true, Ordering::SeqCst);\n    }\n```\n\nCreates a `HashMap` populated with strings when a static\nlibrary is loaded or an executable is started (new in `0.1.7`):\n\n`static` items are equivalent to `std::sync::OnceLock`, but with\nan automatic deref implementation and eager initialization at\nstartup time.\n\n```rust\n    #[ctor]\n    /// This is an immutable static, evaluated at init time\n    static STATIC_CTOR: HashMap\u003cu32, \u0026'static str\u003e = {\n        let mut m = HashMap::new();\n        m.insert(0, \"foo\");\n        m.insert(1, \"bar\");\n        m.insert(2, \"baz\");\n        m\n    };\n```\n\nPrint a message at shutdown time. Note that Rust may have shut down\nsome stdlib services at this time.\n\n```rust\n    #[dtor]\n    unsafe fn shutdown() {\n        // Using println or eprintln here will panic as Rust has shut down\n        libc::printf(\"Shutting down!\\n\\0\".as_ptr() as *const i8);\n    }\n```\n\n## Under the Hood\n\nThe `#[ctor]` macro makes use of linker sections to ensure that a\nfunction is run at startup time.\n\nThe above example translates into the following Rust code (approximately):\n\n```rust\n    #[used]\n    #[cfg_attr(target_os = \"linux\"), link_section = \".init_array\")]\n    #[cfg_attr(target_vendor = \"apple\", link_section = \"__DATA,__mod_init_func\")]\n    #[cfg_attr(target_os = \"windows\", link_section = \".CRT$XCU\")]\n    /* ... other platforms elided ... */\n    static FOO: extern fn() = {\n        extern fn foo() { /* ... */ };\n        foo\n    };\n```\n\nThe `#[dtor]` macro effectively creates a constructor that calls `libc::atexit`\nwith the provided function, ie roughly equivalent to:\n\n```rust\n    #[ctor]\n    fn dtor_atexit() {\n        libc::atexit(dtor);\n    }\n```\n\n## Inspiration\n\nIdea inspired by [this\ncode](https://github.com/neon-bindings/neon/blob/2277e943a619579c144c1da543874f4a7ec39879/src/lib.rs#L42)\nin the Neon project.\n","project_url":"https://awesome.ecosyste.ms/api/v1/projects/github.com%2Fmmastrac%2Frust-ctor","html_url":"https://awesome.ecosyste.ms/projects/github.com%2Fmmastrac%2Frust-ctor","lists_url":"https://awesome.ecosyste.ms/api/v1/projects/github.com%2Fmmastrac%2Frust-ctor/lists"}