| Crates.io | slackarc |
| lib.rs | slackarc |
| version | 0.1.0 |
| created_at | 2025-02-10 20:28:38.833144+00 |
| updated_at | 2025-02-10 20:28:38.833144+00 |
| description | Utility allowing easily define global Arc objects, lazyly initialized on demand |
| homepage | |
| repository | https://github.com/entd/slackarc |
| max_upload_size | |
| id | 1550651 |
| size | 6,989 |
Lazy on-demand static types for creating global shared pointers, that are droped when no longer needed.
example:
use slackarc::GlobalWeak;
use std::{fmt::Debug, sync::Arc};
#[derive(Debug)]
struct Foo {
a: String,
b: u64,
}
static REFERENCE: GlobalWeak<Foo> = GlobalWeak::new(|| {
println!("Building First");
Foo {
a: "Bar1".to_owned(),
b: 75,
}
});
static REFERENCE2: GlobalWeak<Foo> = GlobalWeak::new(|| {
println!("Building Second");
Foo {
a: "Bar2".to_owned(),
b: 45,
}
});
impl Drop for Foo {
fn drop(&mut self) {
println!("Bye {0}", self.a);
}
}
fn main() {
let fooarc2: Arc<Foo> = REFERENCE2.upgrade().unwrap();
let fooarc1: Arc<Foo> = REFERENCE.upgrade().unwrap();
println!("Hello, world! {fooarc1:?}");
println!("Hello, world! {fooarc2:?}");
std::mem::drop(fooarc2);
let _: Arc<Foo> = REFERENCE2.upgrade().unwrap();
}
Allocate and share global objects. When the last reference is lost entire object is lost. Next call will reinitialize object.