-
Notifications
You must be signed in to change notification settings - Fork 107
New issue
Have a question about this project? Sign up for a free GitHub account to open an issue and contact its maintainers and the community.
By clicking “Sign up for GitHub”, you agree to our terms of service and privacy statement. We’ll occasionally send you account related emails.
Already on GitHub? Sign in to your account
unsafe lazy_static that's not Sync ? #135
Comments
Hi @asomers 👋 An unsafe variant of struct Pointers {
a: *mut u8,
b: *mut u8,
}
impl Pointers {
const UNINIT: Self = Pointers {
a: std::ptr::null_mut(),
b: std::ptr::null_mut(),
};
}
static mut POINTERS: Pointers = Pointers::UNINIT;
fn main() {
// Working with 'static mut is unsafe
// You're responsible for ensuring the mutable state is
// only aliased and accessed correctly
unsafe {
let pointers = &mut POINTERS;
// Pass through FFI
}
} |
That helps! It makes it possible, but not easy, to do what I want. Using that technique, I can't initialize the variable with non- |
Ok, I found a great hack. I can run code in a constructor function as soon as my library gets loaded, before the application can access it with #[export_name = "mysymbol"]
pub static mut MYSYMBOL: Option<Foo> = None;
#[link_section = ".init_array"]
pub static INITIALIZE: extern "C" fn() = rust_ctor;
#[no_mangle]
pub extern "C" fn rust_ctor() {
unsafe {
MYSYMBOL = Some(Foo::new())
}
} Thanks for the help. I'm going to close the issue now. |
Thank you for this; this really saved me from a multi-day bind. Still, why not just use a mutex around your global mutable var and ditch the unsafe keyword when accessing it? |
would it be possible to create an
unsafe
version of the macro that allows the type to be non-Sync
? I would like to use it in an FFI context. The calling C code requires me to provide a global mutable structure containing raw pointers.The text was updated successfully, but these errors were encountered: