pub enum ModuleState {
Active(Module),
Cleared,
FailedInit(ModuleInitError),
}
Expand description
Represents the state of the module.
This enum implements an API to manage a single global variable of internally managed module (e.g. SkyrimSE.exe
) information.
Variants§
Active(Module)
The module is successfully initialized and active.
Cleared
The module instance has been explicitly cleared and memory has been freed.
FailedInit(ModuleInitError)
The module failed to initialize.
Implementations§
Source§impl ModuleState
impl ModuleState
Sourcepub fn map_active<F, T>(f: F) -> Result<T, ModuleStateError>
pub fn map_active<F, T>(f: F) -> Result<T, ModuleStateError>
Attempts to apply a function to the active module state.
This function tries to acquire a read lock on the module state and applies
the provided function f
if the module state is ModuleState::Active
.
If you do not know when you did the Self::reset
, or if you want it to be reinitialized automatically if necessary,
Self::map_or_init
is useful.
§Example
use commonlibsse_ng::rel::module::ModuleState;
let result = ModuleState::map_active(|module| module.version.clone());
match result {
Ok(version) => println!("Module version: {}", version),
Err(err) => eprintln!("Error: {:?}", err),
}
§Errors
Returns an error if:
- The module state is
ModuleState::Cleared
. - The module state is
ModuleState::FailedInit
, in which case the initialization error is propagated. - The internal lock is poisoned.
Sourcepub fn map_or_init<F, T>(f: F) -> Result<T, ModuleStateError>
pub fn map_or_init<F, T>(f: F) -> Result<T, ModuleStateError>
Attempts to apply a function to the active module state, initializing it if necessary.
If the module state is Cleared
/FailedInit
, it will be initialized before applying the function f
.
This function also attempts a read lock first and falls back to initialization only if needed.
§Example
use commonlibsse_ng::rel::module::ModuleState;
let result = ModuleState::map_or_init(|module| module.version.clone());
match result {
Ok(version) => println!("Module version: {}", version),
Err(err) => eprintln!("Error: {:?}", err),
}
§Errors
Returns an error if:
- The module state is
ModuleState::FailedInit
, in which case the initialization error is propagated. - The internal lock is poisoned.
Sourcepub fn reset() -> Result<(), ModuleStateError>
pub fn reset() -> Result<(), ModuleStateError>
Trait Implementations§
Source§impl Clone for ModuleState
impl Clone for ModuleState
Source§fn clone(&self) -> ModuleState
fn clone(&self) -> ModuleState
1.0.0 · Source§fn clone_from(&mut self, source: &Self)
fn clone_from(&mut self, source: &Self)
source
. Read more