Skip to main content

NamespaceExt

Trait NamespaceExt 

Source
pub trait NamespaceExt: Namespace {
    // Provided methods
    fn get<T>() -> &'static T
       where T: 'static + Zeroable { ... }
    unsafe fn get_mut<T>() -> *mut T
       where T: 'static + Zeroable { ... }
    fn once<T>(init: impl FnOnce() -> T) -> &'static T
       where T: 'static + Send + Sync { ... }
    fn get_const<T, const N: usize>() -> &'static T
       where T: 'static + Zeroable { ... }
    unsafe fn get_const_mut<T, const N: usize>() -> *mut T
       where T: 'static + Zeroable { ... }
    fn once_const<T, const N: usize>(init: impl FnOnce() -> T) -> &'static T
       where T: 'static + Send + Sync { ... }
}
Expand description

Extensions on top of Namespace::generic_static to make using generic statics easier.

Provided Methods§

Source

fn get<T>() -> &'static T
where T: 'static + Zeroable,

Source

unsafe fn get_mut<T>() -> *mut T
where T: 'static + Zeroable,

Source

fn once<T>(init: impl FnOnce() -> T) -> &'static T
where T: 'static + Send + Sync,

Lazily-initialized generic static for types that are not Zeroable.

Backed by crate::once::OnceSlot: the first call runs init and later calls (with the same NS and T) return the same address.

use static_generics::{define_namespace, namespace::NamespaceExt};

define_namespace!(MyNs);

let one = MyNs::once::<String>(|| String::from("hello"));
let two = MyNs::once::<String>(|| String::from("ignored"));
assert_eq!(one.as_str(), "hello");
assert!(core::ptr::eq(one, two));
Source

fn get_const<T, const N: usize>() -> &'static T
where T: 'static + Zeroable,

Source

unsafe fn get_const_mut<T, const N: usize>() -> *mut T
where T: 'static + Zeroable,

Source

fn once_const<T, const N: usize>(init: impl FnOnce() -> T) -> &'static T
where T: 'static + Send + Sync,

Lazily-initialized generic static keyed by (T, const N: usize).

Like NamespaceExt::once, but the same T with a different N is a different address. Backed by crate::once::OnceSlot.

Dyn Compatibility§

This trait is not dyn compatible.

In older versions of Rust, dyn compatibility was called "object safety".

Implementors§