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>andAssetHandle<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>
impl<A: Asset> AssetHandle<A>
Sourcepub const INVALID: Self
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);Sourcepub const fn new(index: u32, generation: u32) -> Self
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 storagegeneration- 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);Sourcepub const fn index(&self) -> u32
pub const fn index(&self) -> u32
Returns the index component of this handle.
The index is the slot number in the asset storage array.
Sourcepub const fn generation(&self) -> u32
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.
Sourcepub const fn is_valid(&self) -> bool
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());Sourcepub fn untyped(&self) -> UntypedAssetHandle
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>());Sourcepub const fn to_u64(&self) -> u64
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.
Sourcepub const fn from_u64(packed: u64) -> Self
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.
Sourcepub fn asset_id() -> AssetId
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>().
Sourcepub fn asset_type() -> AssetType
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>
impl<A: Asset> Clone for AssetHandle<A>
Source§impl<A: Asset> Debug for AssetHandle<A>
impl<A: Asset> Debug for AssetHandle<A>
Source§impl<A: Asset> Default for AssetHandle<A>
impl<A: Asset> Default for AssetHandle<A>
Source§impl<A: Asset> Display for AssetHandle<A>
impl<A: Asset> Display for AssetHandle<A>
Source§impl<A: Asset> From<&AssetHandle<A>> for WeakAssetHandle<A>
impl<A: Asset> From<&AssetHandle<A>> for WeakAssetHandle<A>
Source§fn from(handle: &AssetHandle<A>) -> Self
fn from(handle: &AssetHandle<A>) -> Self
Source§impl<A: Asset> From<AssetHandle<A>> for u64
impl<A: Asset> From<AssetHandle<A>> for u64
Source§fn from(handle: AssetHandle<A>) -> u64
fn from(handle: AssetHandle<A>) -> u64
Source§impl<A: Asset> Hash for AssetHandle<A>
impl<A: Asset> Hash for AssetHandle<A>
Source§impl<A: Asset> Ord for AssetHandle<A>
impl<A: Asset> Ord for AssetHandle<A>
Source§impl<A: Asset> PartialEq for AssetHandle<A>
impl<A: Asset> PartialEq for AssetHandle<A>
Source§impl<A: Asset> PartialOrd for AssetHandle<A>
impl<A: Asset> PartialOrd for AssetHandle<A>
impl<A: Asset> Copy for AssetHandle<A>
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> BorrowMut<T> for Twhere
T: ?Sized,
impl<T> BorrowMut<T> for Twhere
T: ?Sized,
Source§fn borrow_mut(&mut self) -> &mut T
fn borrow_mut(&mut self) -> &mut T
Source§impl<T> CloneToUninit for Twhere
T: Clone,
impl<T> CloneToUninit for Twhere
T: Clone,
Source§impl<Q, K> Comparable<K> for Q
impl<Q, K> Comparable<K> for Q
Source§impl<Q, K> Equivalent<K> for Q
impl<Q, K> Equivalent<K> for Q
Source§impl<Q, K> Equivalent<K> for Q
impl<Q, K> Equivalent<K> for Q
Source§fn equivalent(&self, key: &K) -> bool
fn equivalent(&self, key: &K) -> bool
key and return true if they are equal.Source§impl<S> FromSample<S> for S
impl<S> FromSample<S> for S
fn from_sample_(s: S) -> S
Source§impl<T> IntoEither for T
impl<T> IntoEither for T
Source§fn into_either(self, into_left: bool) -> Either<Self, Self>
fn into_either(self, into_left: bool) -> Either<Self, Self>
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 moreSource§fn into_either_with<F>(self, into_left: F) -> Either<Self, Self>
fn into_either_with<F>(self, into_left: F) -> Either<Self, Self>
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 moreSource§impl<F, T> IntoSample<T> for Fwhere
T: FromSample<F>,
impl<F, T> IntoSample<T> for Fwhere
T: FromSample<F>,
fn into_sample(self) -> T
Source§impl<T> Pointable for T
impl<T> Pointable for T
Source§impl<R, P> ReadPrimitive<R> for P
impl<R, P> ReadPrimitive<R> for P
Source§fn read_from_little_endian(read: &mut R) -> Result<Self, Error>
fn read_from_little_endian(read: &mut R) -> Result<Self, Error>
ReadEndian::read_from_little_endian().