Skip to main content

AssetManager

Struct AssetManager 

Source
pub struct AssetManager { /* private fields */ }
Expand description

Central manager for assets in the system.

The asset manager is responsible for:

  • Registering and retrieving asset types
  • Managing asset lifecycles and hash-based unique IDs
  • Providing access to the filesystem for asset operations
  • Thread-safe storage of assets and their metadata

Implementations§

Source§

impl AssetManager

Source

pub fn new(filesystem: Arc<dyn Filesystem>) -> Self

Creates a new asset manager with the provided filesystem.

§Arguments
  • filesystem - The filesystem implementation to use for asset I/O
Source

pub fn context(&self) -> Option<Arc<dyn AssetManagerContext>>

Retrieves the current asset manager context.

§Returns

The context if one has been set, otherwise None.

Source

pub fn set_context(&mut self, context: Arc<dyn AssetManagerContext>)

Sets the asset manager context.

§Arguments
  • context - The context implementation to set
Source

pub fn set_serialization_backend( &mut self, serialization: Box<dyn AssetSerializationBackend>, )

Sets the serialization backend for the asset manager.

This allows changing how assets are serialized and deserialized. The backend determines the format used for asset persistence.

§Arguments
  • serialization - The serialization backend implementation to use
Source

pub fn asset_type_by_name(&self, name: &str) -> Option<Weak<dyn AssetType>>

Retrieves an asset type by its name.

§Arguments
  • name - The name of the asset type to look up
§Returns

The asset type if found, or None if no asset type with that name is registered.

Source

pub fn register_asset_type(&self, asset_type: Arc<dyn AssetType>)

Registers a new asset type with the manager.

This allows the asset manager to handle assets of this type. If an asset type with the same name already exists, it will be replaced.

§Arguments
  • asset_type - The asset type implementation to register
Source

pub fn asset_by_id(&self, id: AssetId) -> Option<Arc<Asset>>

Retrieves an asset by its unique ID.

§Arguments
  • id - The unique identifier of the asset
§Returns

The asset if found, or None if no asset with that ID is registered.

Source

pub fn register_asset(&self, asset_path: &str, asset: Asset) -> AssetId

Registers an asset with the manager and assigns it a deterministic ID.

If the asset’s ID is non-zero, the ID is generated from the asset path using hash-based generation, ensuring the same path always produces the same ID.

§Arguments
  • asset_path - The path string used to generate the asset ID
  • asset - The asset to register
§Returns

The deterministic ID assigned to the asset

Source

pub fn unregister_asset(&self, id: AssetId) -> bool

Unregisters an asset from the manager.

Removes the asset with the given ID from the manager’s storage. The asset will no longer be accessible through the manager after this call. The asset will be de-allocated when all Arc references to it are dropped.

§Arguments
  • id - The unique identifier of the asset to remove
§Returns

true if the asset was found and removed, false if no asset with that ID existed.

Source

pub async fn fs_read_bytes( &self, asset_path: &str, ) -> Result<Vec<u8>, FilesystemError>

Asynchronously reads the raw bytes of a file from the filesystem.

This is a convenience method that delegates to the underlying filesystem for reading asset files as byte arrays. The operation is non-blocking.

§Arguments
  • asset_path - The path to the file to read
§Returns

A future that resolves to the file contents as bytes, or a FilesystemError if reading fails.

Source

pub async fn fs_read_and_register_asset( &self, asset_path: &str, ) -> Result<AssetId>

Asynchronously reads an asset file and registers it with the manager.

This method combines file reading with asset deserialization and registration. It reads the asset file from the filesystem, deserializes it using the configured serialization backend, and registers the resulting asset with the manager. If the asset has no ID (ID is 0), it generates one deterministically from the path.

§Arguments
  • asset_path - The path to the asset file to read and register
§Returns

A future that resolves to the asset ID if successful, or an error if reading, deserialization, or asset creation fails.

§Errors

This function will return an error if:

  • The file cannot be read from the filesystem
  • The file contents cannot be deserialized
  • Asset creation from the serialized data fails

Auto Trait Implementations§

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> Downcast for T
where T: Any,

Source§

fn into_any(self: Box<T>) -> Box<dyn Any>

Converts Box<dyn Trait> (where Trait: Downcast) to Box<dyn Any>, which can then be downcast into Box<dyn ConcreteType> where ConcreteType implements Trait.
Source§

fn into_any_rc(self: Rc<T>) -> Rc<dyn Any>

Converts Rc<Trait> (where Trait: Downcast) to Rc<Any>, which can then be further downcast into Rc<ConcreteType> where ConcreteType implements Trait.
Source§

fn as_any(&self) -> &(dyn Any + 'static)

Converts &Trait (where Trait: Downcast) to &Any. This is needed since Rust cannot generate &Any’s vtable from &Trait’s.
Source§

fn as_any_mut(&mut self) -> &mut (dyn Any + 'static)

Converts &mut Trait (where Trait: Downcast) to &Any. This is needed since Rust cannot generate &mut Any’s vtable from &mut Trait’s.
Source§

impl<T> DowncastSend for T
where T: Any + Send,

Source§

fn into_any_send(self: Box<T>) -> Box<dyn Any + Send>

Converts Box<Trait> (where Trait: DowncastSend) to Box<dyn Any + Send>, which can then be downcast into Box<ConcreteType> where ConcreteType implements Trait.
Source§

impl<T> DowncastSync for T
where T: Any + Send + Sync,

Source§

fn into_any_sync(self: Box<T>) -> Box<dyn Any + Send + Sync>

Converts Box<Trait> (where Trait: DowncastSync) to Box<dyn Any + Send + Sync>, which can then be downcast into Box<ConcreteType> where ConcreteType implements Trait.
Source§

fn into_any_arc(self: Arc<T>) -> Arc<dyn Any + Send + Sync>

Converts Arc<Trait> (where Trait: DowncastSync) to Arc<Any>, which can then be downcast into Arc<ConcreteType> where ConcreteType implements Trait.
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> Same for T

Source§

type Output = T

Should always be Self
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.