Skip to main content

ModuleGlobals

Struct ModuleGlobals 

Source
pub struct ModuleGlobals<T: ModuleGlobal> { /* private fields */ }
Expand description

Thread-safe handle to PHP module globals.

Declare as a static and pass to ModuleBuilder::globals().

In ZTS (thread-safe) builds, PHP’s TSRM allocates per-thread storage and manages the lifetime via GINIT/GSHUTDOWN callbacks. In non-ZTS builds, the globals live inline in this struct as a plain static.

§Examples

use ext_php_rs::zend::{ModuleGlobal, ModuleGlobals};

#[derive(Default)]
struct MyGlobals {
    counter: i64,
}

impl ModuleGlobal for MyGlobals {}

static MY_GLOBALS: ModuleGlobals<MyGlobals> = ModuleGlobals::new();

Implementations§

Source§

impl<T: ModuleGlobal> ModuleGlobals<T>

Source

pub const fn new() -> Self

Creates an uninitialized globals handle.

Must be passed to ModuleBuilder::globals() for PHP to allocate and initialize the storage.

Source

pub fn get(&self) -> &T

Returns a shared reference to the current thread’s globals.

Safe because PHP guarantees single-threaded request processing: only one request handler runs per thread at a time, and module globals are initialized before any request begins.

§Panics

Debug-asserts that the globals have been registered. In release builds, calling this before module init is undefined behavior.

Source

pub unsafe fn get_mut(&self) -> &mut T

Returns a mutable reference to the current thread’s globals.

§Safety

Caller must ensure exclusive access. Typically safe within RINIT/RSHUTDOWN or from a #[php_function] handler (PHP runs one request per thread), but NOT from background Rust threads.

Source

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

Returns a raw pointer to the globals for the current thread.

Escape hatch for power users who need direct access without lifetime constraints.

Trait Implementations§

Source§

impl<T: ModuleGlobal> Default for ModuleGlobals<T>

Source§

fn default() -> Self

Returns the “default value” for a type. Read more
Source§

impl<T: ModuleGlobal> Sync for ModuleGlobals<T>

Auto Trait Implementations§

§

impl<T> !Freeze for ModuleGlobals<T>

§

impl<T> !RefUnwindSafe for ModuleGlobals<T>

§

impl<T> Send for ModuleGlobals<T>
where T: Send,

§

impl<T> Unpin for ModuleGlobals<T>
where T: Unpin,

§

impl<T> UnsafeUnpin for ModuleGlobals<T>
where T: UnsafeUnpin,

§

impl<T> UnwindSafe for ModuleGlobals<T>
where T: UnwindSafe,

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.