Skip to main content

AssetHandle

Struct AssetHandle 

Source
pub struct AssetHandle<A: Asset> { /* private fields */ }
Expand description

A typed handle to an asset of type A.

AssetHandle is the primary way to reference loaded assets. It provides:

  • Type Safety: AssetHandle<Texture> and AssetHandle<Audio> are distinct types
  • Generation Counting: Prevents use-after-free when assets are unloaded
  • FFI Compatibility: Can be passed across language boundaries

§Handle States

An asset handle can be in several states:

  • Invalid: The sentinel value INVALID, representing “no asset”
  • Valid but Loading: Points to an asset slot where loading is in progress
  • Valid and Loaded: Points to a fully loaded, usable asset
  • Stale: The asset was unloaded, handle generation no longer matches

Use AssetServer::get_state() to check the current state of an asset.

§FFI Layout

Offset 0:  index      (u32, 4 bytes)
Offset 4:  generation (u32, 4 bytes)
Offset 8:  _marker    (PhantomData, 0 bytes)
Total:     8 bytes, alignment 4

§Example

use goud_engine::assets::{Asset, AssetHandle};

struct Shader { /* ... */ }
impl Asset for Shader {}

// Create an invalid handle (default)
let handle: AssetHandle<Shader> = AssetHandle::INVALID;
assert!(!handle.is_valid());

// Create a valid handle (normally done by AssetServer)
let handle: AssetHandle<Shader> = AssetHandle::new(42, 1);
assert!(handle.is_valid());
assert_eq!(handle.index(), 42);
assert_eq!(handle.generation(), 1);

Implementations§

Source§

impl<A: Asset> AssetHandle<A>

Source

pub const INVALID: Self

The invalid handle constant, representing “no asset”.

This is the default value for AssetHandle and is guaranteed to never match any valid asset handle.

§Example
use goud_engine::assets::{Asset, AssetHandle};

struct Mesh;
impl Asset for Mesh {}

let handle: AssetHandle<Mesh> = AssetHandle::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 asset handle with the given index and generation.

This is typically called by the asset system, not by user code.

§Arguments
  • index - Slot index in the asset storage
  • generation - Generation counter for this slot
§Example
use goud_engine::assets::{Asset, AssetHandle};

struct Audio;
impl Asset for Audio {}

let handle: AssetHandle<Audio> = AssetHandle::new(10, 3);
assert_eq!(handle.index(), 10);
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 asset storage array.

Source

pub const fn generation(&self) -> u32

Returns the generation component of this handle.

The generation is incremented each time a slot is reused, preventing stale handles from accessing wrong assets.

Source

pub const fn is_valid(&self) -> bool

Returns true if this handle is not the INVALID sentinel.

Note: A “valid” handle may still be stale if the asset was unloaded. Use AssetServer::is_alive() for definitive liveness checks.

§Example
use goud_engine::assets::{Asset, AssetHandle};

struct Font;
impl Asset for Font {}

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

let invalid: AssetHandle<Font> = AssetHandle::INVALID;
assert!(!invalid.is_valid());
Source

pub fn untyped(&self) -> UntypedAssetHandle

Converts this handle to a type-erased UntypedAssetHandle.

The untyped handle preserves the asset type ID for runtime type checking.

§Example
use goud_engine::assets::{Asset, AssetHandle, AssetId};

struct Texture;
impl Asset for Texture {}

let typed: AssetHandle<Texture> = AssetHandle::new(5, 2);
let untyped = typed.untyped();

assert_eq!(untyped.index(), 5);
assert_eq!(untyped.generation(), 2);
assert_eq!(untyped.asset_id(), AssetId::of::<Texture>());
Source

pub const fn to_u64(&self) -> u64

Packs this handle into a single u64 value for FFI.

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

Note: This does NOT preserve the asset type; use untyped() if you need type information across FFI boundaries.

Source

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

Creates a handle from a packed u64 value.

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

Source

pub fn asset_id() -> AssetId

Returns the asset type ID for this handle’s asset type.

This is a convenience method equivalent to AssetId::of::<A>().

Source

pub fn asset_type() -> AssetType

Returns the asset type category for this handle’s asset type.

This is a convenience method equivalent to A::asset_type().

Trait Implementations§

Source§

impl<A: Asset> Clone for AssetHandle<A>

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<A: Asset> Debug for AssetHandle<A>

Source§

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

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

impl<A: Asset> Default for AssetHandle<A>

Source§

fn default() -> Self

Returns AssetHandle::INVALID.

Source§

impl<A: Asset> Display for AssetHandle<A>

Source§

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

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

impl<A: Asset> From<&AssetHandle<A>> for WeakAssetHandle<A>

Source§

fn from(handle: &AssetHandle<A>) -> Self

Converts to this type from the input type.
Source§

impl<A: Asset> From<AssetHandle<A>> for u64

Source§

fn from(handle: AssetHandle<A>) -> u64

Converts to this type from the input type.
Source§

impl<A: Asset> From<u64> for AssetHandle<A>

Source§

fn from(packed: u64) -> Self

Converts to this type from the input type.
Source§

impl<A: Asset> Hash for AssetHandle<A>

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<A: Asset> Ord for AssetHandle<A>

Source§

fn cmp(&self, other: &Self) -> Ordering

This method returns an Ordering between self and other. Read more
1.21.0 · Source§

fn max(self, other: Self) -> Self
where Self: Sized,

Compares and returns the maximum of two values. Read more
1.21.0 · Source§

fn min(self, other: Self) -> Self
where Self: Sized,

Compares and returns the minimum of two values. Read more
1.50.0 · Source§

fn clamp(self, min: Self, max: Self) -> Self
where Self: Sized,

Restrict a value to a certain interval. Read more
Source§

impl<A: Asset> PartialEq for AssetHandle<A>

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<A: Asset> PartialOrd for AssetHandle<A>

Source§

fn partial_cmp(&self, other: &Self) -> Option<Ordering>

This method returns an ordering between self and other values if one exists. Read more
1.0.0 · Source§

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

Tests less than (for self and other) and is used by the < operator. Read more
1.0.0 · Source§

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

Tests less than or equal to (for self and other) and is used by the <= operator. Read more
1.0.0 · Source§

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

Tests greater than (for self and other) and is used by the > operator. Read more
1.0.0 · Source§

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

Tests greater than or equal to (for self and other) and is used by the >= operator. Read more
Source§

impl<A: Asset> Copy for AssetHandle<A>

Source§

impl<A: Asset> Eq for AssetHandle<A>

Auto Trait Implementations§

§

impl<A> Freeze for AssetHandle<A>

§

impl<A> RefUnwindSafe for AssetHandle<A>
where A: RefUnwindSafe,

§

impl<A> Send for AssetHandle<A>

§

impl<A> Sync for AssetHandle<A>

§

impl<A> Unpin for AssetHandle<A>
where A: Unpin,

§

impl<A> UnsafeUnpin for AssetHandle<A>

§

impl<A> UnwindSafe for AssetHandle<A>
where A: 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> Comparable<K> for Q
where Q: Ord + ?Sized, K: Borrow<Q> + ?Sized,

Source§

fn compare(&self, key: &K) -> Ordering

Compare self to key and return their ordering.
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> ToString for T
where T: Display + ?Sized,

Source§

fn to_string(&self) -> String

Converts the given value to a String. Read more
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,