Skip to main content

astrelis_assets/
lib.rs

1//! Astrelis Assets - Type-safe asset management system
2//!
3//! This crate provides a comprehensive asset management system with:
4//! - Typed handles with generational IDs for O(1) access and use-after-free protection
5//! - Multiple asset sources (disk, memory, raw bytes)
6//! - Pluggable asset loaders
7//! - Async/background loading
8//! - Hot-reload support (disk and memory)
9//! - GPU resource integration hooks
10//! - Event system for change detection
11//!
12//! # Architecture Overview
13//!
14//! ```text
15//! ┌─────────────────────────────────────────────────────────────────┐
16//! │                        AssetServer                               │
17//! │  - Coordinates all asset operations                             │
18//! │  - Manages type-erased storage                                  │
19//! │  - Dispatches to loaders                                        │
20//! └───────────────────────────┬─────────────────────────────────────┘
21//!                             │
22//!          ┌──────────────────┼──────────────────┐
23//!          ▼                  ▼                  ▼
24//!   ┌─────────────┐   ┌─────────────┐   ┌─────────────┐
25//!   │ Assets<Tex> │   │Assets<Shader│   │Assets<Audio>│
26//!   │  SparseSet  │   │  SparseSet  │   │  SparseSet  │
27//!   └──────┬──────┘   └──────┬──────┘   └──────┬──────┘
28//!          │                 │                 │
29//!          ▼                 ▼                 ▼
30//!   ┌─────────────┐   ┌─────────────┐   ┌─────────────┐
31//!   │AssetEntry<T>│   │AssetEntry<T>│   │AssetEntry<T>│
32//!   │ - state     │   │ - state     │   │ - state     │
33//!   │ - version   │   │ - version   │   │ - version   │
34//!   │ - refcount  │   │ - refcount  │   │ - refcount  │
35//!   └─────────────┘   └─────────────┘   └─────────────┘
36//! ```
37//!
38//! # Quick Start
39//!
40//! ```ignore
41//! use astrelis_assets::prelude::*;
42//!
43//! // Create the asset server
44//! let mut server = AssetServer::new();
45//!
46//! // Register a loader for textures
47//! server.register_loader::<Texture>(TextureLoader::new());
48//!
49//! // Load an asset from disk
50//! let handle: Handle<Texture> = server.load("textures/player.png");
51//!
52//! // Check if ready and use
53//! if let Some(texture) = server.get(&handle) {
54//!     // Use the texture...
55//! }
56//!
57//! // Poll for events
58//! for event in server.drain_events() {
59//!     match event {
60//!         AssetEvent::Created { handle, .. } => { /* ... */ }
61//!         AssetEvent::Modified { handle, .. } => { /* ... */ }
62//!         AssetEvent::Removed { handle, .. } => { /* ... */ }
63//!     }
64//! }
65//! ```
66
67pub mod error;
68pub mod event;
69pub mod handle;
70pub mod hot_reload;
71pub mod io;
72pub mod loader;
73pub mod server;
74pub mod source;
75pub mod state;
76pub mod storage;
77
78// Re-export core types
79pub use error::*;
80pub use event::*;
81pub use handle::*;
82pub use loader::*;
83pub use server::*;
84pub use source::*;
85pub use state::*;
86pub use storage::*;
87
88/// Prelude module for convenient imports
89pub mod prelude {
90    pub use crate::{
91        Asset, AssetError, AssetEvent, AssetLoader, AssetServer, AssetSource, AssetState, Assets,
92        Handle, LoadContext, StrongHandle, UntypedHandle, WeakHandle,
93    };
94}
95
96use std::any::Any;
97
98/// Marker trait for types that can be managed as assets.
99///
100/// This trait combines `Any` (for type erasure) with `Send + Sync` (for thread safety).
101/// Types implementing this trait can be loaded, stored, and hot-reloaded by the asset system.
102///
103/// # Example
104///
105/// ```ignore
106/// use astrelis_assets::Asset;
107///
108/// #[derive(Debug)]
109/// pub struct Texture {
110///     pub width: u32,
111///     pub height: u32,
112///     pub data: Vec<u8>,
113/// }
114///
115/// impl Asset for Texture {
116///     fn type_name() -> &'static str {
117///         "Texture"
118///     }
119/// }
120/// ```
121pub trait Asset: Any + Send + Sync + 'static {
122    /// Returns a human-readable name for this asset type.
123    /// Used for logging and debugging.
124    fn type_name() -> &'static str
125    where
126        Self: Sized;
127}
128
129// Implement Asset for common types that might be useful
130impl Asset for String {
131    fn type_name() -> &'static str {
132        "String"
133    }
134}
135
136impl Asset for Vec<u8> {
137    fn type_name() -> &'static str {
138        "Bytes"
139    }
140}