Skip to main content

Global

Struct Global 

Source
pub struct Global<T> { /* private fields */ }
Expand description

A global variable of type T at an address in a Module.

Generated bindings declare one per IR global; it holds only the address, one per build when they were generated from several. When T is Plain, read and write access it safely; otherwise use ptr.

§Data races

Each read or write is a single volatile access that doesn’t synchronise with the target. If the target uses the global on another thread at the same time, a value larger than a machine word can be seen half-updated.

§Examples

use imping_runtime::{Build, Global, Module};

static TARGET: Module = Module::new(
    "target.exe",
    &[Build { timestamp: 0x64b8f2a1, image_size: 0xa2b000 }],
);
static GRAVITY: Global<f32> = Global::new(&TARGET, 0x3a1200);

// SAFETY: this build of `target.exe` keeps an `f32` at RVA 0x3a1200.
unsafe { TARGET.init() }?;
let gravity = GRAVITY.read();
GRAVITY.write(gravity / 2.0);

Implementations§

Source§

impl<T> Global<T>

Source

pub const fn new(module: &'static Module, rva: u32) -> Self

Describes the global at rva, an address relative to module’s base.

Source

pub fn ptr(&self) -> *mut T

Returns a pointer to the global in this process.

§Panics

If the module’s init has not succeeded, or the loaded build’s IR lacks the global; try_ptr checks first.

Source

pub fn try_ptr(&self) -> Option<*mut T>

Returns a pointer to the global in this process, or None if the module’s init has not succeeded or the loaded build’s IR lacks the global.

On a build none of the IRs is, after init_any_build, it’s the first build’s address that has the global, unchecked, as nothing can search for a global.

Source

pub fn rva(&self) -> Option<u32>

Returns the global’s address relative to its module’s base in the loaded build, as its IR has it: None before init, where that build’s IR lacks the global, or on a build none of the IRs is.

A global made with new has one address, which this always returns.

Source

pub const fn module(&self) -> &'static Module

Returns the module the global is in.

Source§

impl<T: Plain> Global<T>

Source

pub fn read(&self) -> T

Reads the global.

Doesn’t synchronise with the target; see Global for data races.

§Panics

As ptr does.

Source

pub fn write(&self, value: T)

Writes the global.

Doesn’t synchronise with the target; see Global for data races.

§Panics

As ptr does.

Trait Implementations§

Source§

impl<T> Debug for Global<T>

Source§

fn fmt(&self, f: &mut Formatter<'_>) -> Result

Formats the value using the given formatter. Read more

Auto Trait Implementations§

§

impl<T> Freeze for Global<T>

§

impl<T> RefUnwindSafe for Global<T>

§

impl<T> Send for Global<T>

§

impl<T> Sync for Global<T>

§

impl<T> Unpin for Global<T>

§

impl<T> UnsafeUnpin for Global<T>

§

impl<T> UnwindSafe for Global<T>

Blanket Implementations§

Source§

impl<T> Any for T
where T: 'static + ?Sized,

Source§

fn type_id(&self) -> TypeId

Gets the TypeId of self. Read more
Source§

impl<T> Borrow<T> for T
where T: ?Sized,

Source§

fn borrow(&self) -> &T

Immutably borrows from an owned value. Read more
Source§

impl<T> BorrowMut<T> for T
where T: ?Sized,

Source§

fn borrow_mut(&mut self) -> &mut T

Mutably borrows from an owned value. Read more
Source§

impl<T> From<T> for T

Source§

fn from(t: T) -> T

Returns the argument unchanged.

Source§

impl<T, U> Into<U> for T
where U: From<T>,

Source§

fn into(self) -> U

Calls U::from(self).

That is, this conversion is whatever the implementation of From<T> for U chooses to do.

Source§

impl<T, U> TryFrom<U> for T
where U: Into<T>,

Source§

type Error = Infallible

The type returned in the event of a conversion error.
Source§

fn try_from(value: U) -> Result<T, <T as TryFrom<U>>::Error>

Performs the conversion.
Source§

impl<T, U> TryInto<U> for T
where U: TryFrom<T>,

Source§

type Error = <U as TryFrom<T>>::Error

The type returned in the event of a conversion error.
Source§

fn try_into(self) -> Result<U, <U as TryFrom<T>>::Error>

Performs the conversion.