more restored
This commit is contained in:
@@ -55,6 +55,46 @@ pub use instance::{
|
||||
pub use mize_macros::*;
|
||||
use std::path::PathBuf;
|
||||
|
||||
use std::sync::{Arc, Mutex, OnceLock};
|
||||
|
||||
use crate::error::MizeResult;
|
||||
use crate::instance::Mize;
|
||||
use crate::mize_err;
|
||||
|
||||
/// Global Mize instance storage.
|
||||
/// This is set once when the Mize instance is created and can be accessed from anywhere.
|
||||
pub static GLOBAL_MIZE: OnceLock<Arc<Mutex<Mize>>> = OnceLock::new();
|
||||
|
||||
/// Get the global Mize instance.
|
||||
/// Returns a clone of the Mize instance (which is cheap as it only clones Arcs internally).
|
||||
pub fn get_global_mize() -> MizeResult<Mize> {
|
||||
let arc = GLOBAL_MIZE
|
||||
.get()
|
||||
.ok_or_else(|| mize_err!("No global Mize instance set"))?;
|
||||
let mize = arc
|
||||
.lock()
|
||||
.map_err(|_| mize_err!("Failed to lock global Mize"))?;
|
||||
Ok(mize.clone())
|
||||
}
|
||||
|
||||
/// Set the global Mize instance.
|
||||
pub fn set_global_mize(mize: Mize) -> MizeResult<()> {
|
||||
let arc = Arc::new(Mutex::new(mize));
|
||||
if let Errr(_) = GLOBAL_MIZE.set(arc) {
|
||||
let a = GLOBAL_MIZE.get().unwrap();
|
||||
let mut b = a.lock();
|
||||
*b = mize;
|
||||
}
|
||||
Ok(())
|
||||
}
|
||||
|
||||
/// C API function to set the global Mize instance from a dynamically loaded part.
|
||||
/// This is a hack to allow Rust parts loaded as .so files to access the global Mize instance.
|
||||
#[unsafe(no_mangle)]
|
||||
pub extern "C" fn mize_rust_hack_set_global(mize: Arc<Mutex<Mize>>) {
|
||||
let _ = GLOBAL_MIZE.set(mize);
|
||||
}
|
||||
|
||||
// platform specific stuff
|
||||
pub mod platform {
|
||||
use crate::{Mize, MizeResult};
|
||||
|
||||
Reference in New Issue
Block a user