Skip to main content

Handle

Struct Handle 

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

A type-safe, generation-counted handle to an engine resource.

Handles are lightweight (8 bytes) identifiers that can be safely passed across FFI boundaries. The generic type parameter T provides compile-time type safety, ensuring handles for different resource types cannot be mixed.

§FFI Safety

The #[repr(C)] attribute ensures this struct has a predictable memory layout for interoperability with C#, Python, and other languages:

  • Offset 0: index (4 bytes, u32)
  • Offset 4: generation (4 bytes, u32)
  • Total size: 8 bytes, alignment: 4 bytes

The PhantomData<T> is a zero-sized type that doesn’t affect the layout.

Implementations§

Source§

impl<T> Handle<T>

Source

pub const INVALID: Self

The invalid handle constant.

Used to represent “no resource” or “null handle”. This is distinguishable from any valid handle because:

  • index is u32::MAX, which exceeds any reasonable allocation count
  • generation is 0, which is never used for valid allocations
§Example
use goud_engine::core::handle::Handle;

struct Texture;

let handle: Handle<Texture> = Handle::INVALID;
assert!(!handle.is_valid());
assert_eq!(handle.index(), u32::MAX);
assert_eq!(handle.generation(), 0);
Source

pub const fn new(index: u32, generation: u32) -> Self

Creates a new handle with the given index and generation.

This is typically called by HandleAllocator, not by user code. Users should obtain handles through the allocator or storage APIs.

§Arguments
  • index - The slot index in the storage array
  • generation - The generation counter for this slot
§Example
use goud_engine::core::handle::Handle;

struct Shader;

let handle: Handle<Shader> = Handle::new(42, 3);
assert_eq!(handle.index(), 42);
assert_eq!(handle.generation(), 3);
Source

pub const fn index(&self) -> u32

Returns the index component of this handle.

The index is the slot number in the allocator/storage. It identifies which entry the handle refers to.

§Example
use goud_engine::core::handle::Handle;

struct Mesh;

let handle: Handle<Mesh> = Handle::new(10, 1);
assert_eq!(handle.index(), 10);
Source

pub const fn generation(&self) -> u32

Returns the generation component of this handle.

The generation is incremented each time a slot is deallocated and reused. It prevents use-after-free by invalidating old handles.

§Example
use goud_engine::core::handle::Handle;

struct Audio;

let handle: Handle<Audio> = Handle::new(5, 7);
assert_eq!(handle.generation(), 7);
Source

pub const fn is_valid(&self) -> bool

Checks if this handle is valid (not the INVALID sentinel).

A handle is considered valid if it is not equal to Handle::INVALID. Note that a “valid” handle here only means it’s not the null sentinel; it may still refer to a deallocated resource (stale handle).

To check if a handle refers to a live resource, use the allocator’s is_alive() method.

§Example
use goud_engine::core::handle::Handle;

struct Sprite;

let valid: Handle<Sprite> = Handle::new(0, 1);
assert!(valid.is_valid());

let invalid: Handle<Sprite> = Handle::INVALID;
assert!(!invalid.is_valid());
Source

pub const fn to_u64(&self) -> u64

Packs this handle into a single u64 value.

The packed format is:

  • Upper 32 bits: generation
  • Lower 32 bits: index

This is useful for FFI with languages that prefer a single integer over a struct, and for use as hash map keys.

§Example
use goud_engine::core::handle::Handle;

struct Resource;

let handle: Handle<Resource> = Handle::new(42, 7);
let packed = handle.to_u64();
let unpacked: Handle<Resource> = Handle::from_u64(packed);

assert_eq!(handle, unpacked);
Source

pub const fn from_u64(packed: u64) -> Self

Creates a handle from a packed u64 value.

This is the inverse of to_u64(). The packed format is:

  • Upper 32 bits: generation
  • Lower 32 bits: index
§Example
use goud_engine::core::handle::Handle;

struct Resource;

let packed: u64 = (7u64 << 32) | 42u64;  // gen=7, index=42
let handle: Handle<Resource> = Handle::from_u64(packed);

assert_eq!(handle.index(), 42);
assert_eq!(handle.generation(), 7);

Trait Implementations§

Source§

impl<T> Clone for Handle<T>

Source§

fn clone(&self) -> Self

Returns a duplicate of the value. Read more
1.0.0 · Source§

fn clone_from(&mut self, source: &Self)

Performs copy-assignment from source. Read more
Source§

impl<T> Debug for Handle<T>

Source§

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

Formats the value using the given formatter. Read more
Source§

impl<T> Default for Handle<T>

Source§

fn default() -> Self

Returns Handle::INVALID.

The default handle is the invalid sentinel, representing “no resource”. This is useful for struct initialization where a handle field may not yet have a valid value.

§Example
use goud_engine::core::handle::Handle;

struct Texture;

let handle: Handle<Texture> = Default::default();
assert!(!handle.is_valid());
assert_eq!(handle, Handle::INVALID);
Source§

impl<T> From<Handle<T>> for u64

Source§

fn from(handle: Handle<T>) -> u64

Converts a handle to its packed u64 representation.

Format: upper 32 bits = generation, lower 32 bits = index.

Source§

impl<T> From<u64> for Handle<T>

Source§

fn from(packed: u64) -> Self

Creates a handle from a packed u64 representation.

Format: upper 32 bits = generation, lower 32 bits = index.

Source§

impl<T> Hash for Handle<T>

Source§

fn hash<H: Hasher>(&self, state: &mut H)

Feeds this value into the given Hasher. Read more
1.3.0 · Source§

fn hash_slice<H>(data: &[Self], state: &mut H)
where H: Hasher, Self: Sized,

Feeds a slice of this type into the given Hasher. Read more
Source§

impl<T> PartialEq for Handle<T>

Source§

fn eq(&self, other: &Self) -> bool

Tests for self and other values to be equal, and is used by ==.
1.0.0 · Source§

fn ne(&self, other: &Rhs) -> bool

Tests for !=. The default implementation is almost always sufficient, and should not be overridden without very good reason.
Source§

impl<T> Copy for Handle<T>

Source§

impl<T> Eq for Handle<T>

Auto Trait Implementations§

§

impl<T> Freeze for Handle<T>

§

impl<T> RefUnwindSafe for Handle<T>
where T: RefUnwindSafe,

§

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

§

impl<T> Sync for Handle<T>
where T: Sync,

§

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

§

impl<T> UnsafeUnpin for Handle<T>

§

impl<T> UnwindSafe for Handle<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> CloneToUninit for T
where T: Clone,

Source§

unsafe fn clone_to_uninit(&self, dest: *mut u8)

🔬This is a nightly-only experimental API. (clone_to_uninit)
Performs copy-assignment from self to dest. Read more
Source§

impl<Q, K> Equivalent<K> for Q
where Q: Eq + ?Sized, K: Borrow<Q> + ?Sized,

Source§

fn equivalent(&self, key: &K) -> bool

Checks if this value is equivalent to the given key. Read more
Source§

impl<Q, K> Equivalent<K> for Q
where Q: Eq + ?Sized, K: Borrow<Q> + ?Sized,

Source§

fn equivalent(&self, key: &K) -> bool

Compare self to key and return true if they are equal.
Source§

impl<T> From<T> for T

Source§

fn from(t: T) -> T

Returns the argument unchanged.

Source§

impl<S> FromSample<S> for S

Source§

fn from_sample_(s: S) -> S

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> IntoEither for T

Source§

fn into_either(self, into_left: bool) -> Either<Self, Self>

Converts self into a Left variant of Either<Self, Self> if into_left is true. Converts self into a Right variant of Either<Self, Self> otherwise. Read more
Source§

fn into_either_with<F>(self, into_left: F) -> Either<Self, Self>
where F: FnOnce(&Self) -> bool,

Converts self into a Left variant of Either<Self, Self> if into_left(&self) returns true. Converts self into a Right variant of Either<Self, Self> otherwise. Read more
Source§

impl<F, T> IntoSample<T> for F
where T: FromSample<F>,

Source§

fn into_sample(self) -> T

Source§

impl<T> Pointable for T

Source§

const ALIGN: usize

The alignment of pointer.
Source§

type Init = T

The type for initializers.
Source§

unsafe fn init(init: <T as Pointable>::Init) -> usize

Initializes a with the given initializer. Read more
Source§

unsafe fn deref<'a>(ptr: usize) -> &'a T

Dereferences the given pointer. Read more
Source§

unsafe fn deref_mut<'a>(ptr: usize) -> &'a mut T

Mutably dereferences the given pointer. Read more
Source§

unsafe fn drop(ptr: usize)

Drops the object pointed to by the given pointer. Read more
Source§

impl<R, P> ReadPrimitive<R> for P
where R: Read + ReadEndian<P>, P: Default,

Source§

fn read_from_little_endian(read: &mut R) -> Result<Self, Error>

Read this value from the supplied reader. Same as ReadEndian::read_from_little_endian().
Source§

fn read_from_big_endian(read: &mut R) -> Result<Self, Error>

Read this value from the supplied reader. Same as ReadEndian::read_from_big_endian().
Source§

fn read_from_native_endian(read: &mut R) -> Result<Self, Error>

Read this value from the supplied reader. Same as ReadEndian::read_from_native_endian().
Source§

impl<T> ToOwned for T
where T: Clone,

Source§

type Owned = T

The resulting type after obtaining ownership.
Source§

fn to_owned(&self) -> T

Creates owned data from borrowed data, usually by cloning. Read more
Source§

fn clone_into(&self, target: &mut T)

Uses borrowed data to replace owned data, usually by cloning. Read more
Source§

impl<T, U> ToSample<U> for T
where U: FromSample<T>,

Source§

fn to_sample_(self) -> U

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.
Source§

impl<S, T> Duplex<S> for T
where T: FromSample<S> + ToSample<S>,

Source§

impl<T> Event for T
where T: Send + Sync + 'static,

Source§

impl<T> QueryState for T
where T: Send + Sync + Clone + 'static,

Source§

impl<T> Resource for T
where T: Send + Sync + 'static,