{"id":15762424,"url":"https://github.com/williamvenner/magic_static","last_synced_at":"2025-04-21T03:32:36.269Z","repository":{"id":57636005,"uuid":"427429637","full_name":"WilliamVenner/magic_static","owner":"WilliamVenner","description":"✨ Global singletons initialized at program start, an alternative to lazy initialization","archived":false,"fork":false,"pushed_at":"2022-06-11T13:12:03.000Z","size":29,"stargazers_count":5,"open_issues_count":0,"forks_count":0,"subscribers_count":2,"default_branch":"master","last_synced_at":"2025-04-13T13:03:50.987Z","etag":null,"topics":["global","globals","lazy","lazy-static","macro","magic-static","no-std","rust","singleton","static"],"latest_commit_sha":null,"homepage":"https://crates.io/crates/magic_static","language":"Rust","has_issues":true,"has_wiki":null,"has_pages":null,"mirror_url":null,"source_name":null,"license":"mit","status":null,"scm":"git","pull_requests_enabled":true,"icon_url":"https://github.com/WilliamVenner.png","metadata":{"files":{"readme":"README.md","changelog":null,"contributing":null,"funding":null,"license":"LICENSE","code_of_conduct":null,"threat_model":null,"audit":null,"citation":null,"codeowners":null,"security":null,"support":null}},"created_at":"2021-11-12T16:41:15.000Z","updated_at":"2024-10-02T04:48:04.000Z","dependencies_parsed_at":"2022-09-26T20:21:38.644Z","dependency_job_id":null,"html_url":"https://github.com/WilliamVenner/magic_static","commit_stats":null,"previous_names":[],"tags_count":0,"template":false,"template_full_name":null,"repository_url":"https://repos.ecosyste.ms/api/v1/hosts/GitHub/repositories/WilliamVenner%2Fmagic_static","tags_url":"https://repos.ecosyste.ms/api/v1/hosts/GitHub/repositories/WilliamVenner%2Fmagic_static/tags","releases_url":"https://repos.ecosyste.ms/api/v1/hosts/GitHub/repositories/WilliamVenner%2Fmagic_static/releases","manifests_url":"https://repos.ecosyste.ms/api/v1/hosts/GitHub/repositories/WilliamVenner%2Fmagic_static/manifests","owner_url":"https://repos.ecosyste.ms/api/v1/hosts/GitHub/owners/WilliamVenner","download_url":"https://codeload.github.com/WilliamVenner/magic_static/tar.gz/refs/heads/master","host":{"name":"GitHub","url":"https://github.com","kind":"github","repositories_count":249991178,"owners_count":21357217,"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":["global","globals","lazy","lazy-static","macro","magic-static","no-std","rust","singleton","static"],"created_at":"2024-10-04T11:09:08.379Z","updated_at":"2025-04-21T03:32:36.015Z","avatar_url":"https://github.com/WilliamVenner.png","language":"Rust","funding_links":[],"categories":[],"sub_categories":[],"readme":"[![crates.io](https://img.shields.io/crates/v/magic_static.svg)](https://crates.io/crates/magic_static)\n[![docs.rs](https://docs.rs/magic_static/badge.svg)](https://docs.rs/magic_static/)\n[![license](https://img.shields.io/crates/l/magic_static)](https://github.com/WilliamVenner/magic_static/blob/master/LICENSE)\n\n# ✨ `magic_static`\n\nGlobal singletons initialized at program start, an alternative to lazy initialization.\n\n## Usage\n\nSimply add `magic_static` as a dependency in your `Cargo.toml` to get started:\n\n```toml\n[dependencies]\nmagic_static = \"*\"\n```\n\n### `bare-metal`\n\nIf your target doesn't support atomics or threads, enable the `bare-metal` feature flag in your `Cargo.toml`:\n\n```toml\n[dependencies]\nmagic_static = { version = \"*\", features = [\"bare-metal\"] }\n```\n\n## Example\n\n```rust\n#[macro_use]\nextern crate magic_static;\n\nmod foo {\n    magic_statics! {\n        pub(super) static ref MAGIC: usize = {\n            println!(\"Magic!\");\n            42\n        };\n\n        pub(super) static ref BAR: std::sync::Mutex\u003c()\u003e = std::sync::Mutex::new(());\n    }\n}\n\n// You can also modularize your magic statics in a group at the module level like so:\n// See `main()` for how to initialize these magic statics.\nmod baz {\n    magic_statics_mod! {\n        pub(super) static ref MAGIC: usize = {\n            println!(\"Magic!\");\n            42\n        };\n\n        pub(super) static ref BAR: std::sync::Mutex\u003c()\u003e = std::sync::Mutex::new(());\n    }\n}\n\n// You can also decorate statics to make them magic statics\n#[magic_static]\nstatic FOO_BAR: std::thread::JoinHandle\u003c()\u003e = {\n    std::thread::spawn(move || {\n        loop { println!(\"HELP I CANT STOP SPINNING\"); }\n    })\n};\n\n#[magic_static::main(\n    FOO_BAR,\n\n    foo::MAGIC,\n    foo::BAR,\n\n    mod baz // This will initialize all magic statics in the `baz` module\n)]\nfn main() {\n    println!(\"Hello, world!\");\n}\n```\n\n## Comparison to [`lazy_static`](https://crates.io/crates/lazy_static)\n\n`lazy_static`s are initialized on first-use and are targetted towards multithreaded applications.\n\nEvery time a `lazy_static` is dereferenced, it must check whether it has been initialized yet. This is usually extremely cheap, and the resulting reference can be stored for use in hot loops (for example), but in some cases you may prefer no checks at all, i.e. a more lightweight solution.\n\n`magic_static` only performs these checks in debug builds, making it a more ergonomic choice for single-threaded and performance-critical applications.\n\nThe downside of using `magic_static` is that you must manually initialize each `magic_static` in your `main` function or somewhere appropriate. See above for an example.","project_url":"https://awesome.ecosyste.ms/api/v1/projects/github.com%2Fwilliamvenner%2Fmagic_static","html_url":"https://awesome.ecosyste.ms/projects/github.com%2Fwilliamvenner%2Fmagic_static","lists_url":"https://awesome.ecosyste.ms/api/v1/projects/github.com%2Fwilliamvenner%2Fmagic_static/lists"}