FromStatic

Trait FromStatic 

Source
pub trait FromStatic {
    // Required methods
    fn name() -> Cow<'static, str>;
    unsafe fn instance() -> InstanceResult<&'static mut Self>;
}
Expand description

A trait for all objects that are instantiated a single time at a fixed point in memory.

This is automatically implemented for FromSingletons generated using the singleton attribute macro, and may be manually implemented for other types that have different ways of looking up their locations in-memory.

Required Methods§

Source

fn name() -> Cow<'static, str>

The name of this object. Used for debugging purposes.

Source

unsafe fn instance() -> InstanceResult<&'static mut Self>

Looks up the single global instance of this object.

Implementations may cache information about the object’s location to make this more efficient in future calls.

§Safety

The caller must ensure that access to the static object is exclusive, both with Rust and the game’s code. For single-threaded objects, this means ensuring that this is only called from the task system or from hooked functions running in the game’s main thread. For multi-threaded objects, it’s sufficient to ensure you have mutex ownership before accessing any locked fields.

Individual implementations may add additional safety requirements.

Dyn Compatibility§

This trait is not dyn compatible.

In older versions of Rust, dyn compatibility was called "object safety", so this trait is not object safe.

Implementors§

Source§

impl<T: FromSingleton> FromStatic for T

Looks up instances of singleton instances by their name. Some singletons aren’t necessarily always instanciated and available. Discovered singletons are cached so invokes after the first will be much faster.

Note: currently this never returns InstanceError::NotFound, but callers shouldn’t rely on that being true into the future.