2023-09-12 18:25:33 +00:00
|
|
|
use std::sync::Arc;
|
|
|
|
|
|
|
|
use uuid::Uuid;
|
|
|
|
|
2023-09-21 13:36:44 +00:00
|
|
|
#[derive(Copy, Clone, Debug, PartialEq, Eq, PartialOrd, Ord)]
|
2023-09-12 18:25:33 +00:00
|
|
|
pub enum ResourceState {
|
|
|
|
Loading,
|
|
|
|
Ready,
|
|
|
|
}
|
|
|
|
|
|
|
|
#[derive(Clone)]
|
|
|
|
pub struct Resource<T: Send + Sync + 'static> {
|
|
|
|
pub path: String,
|
|
|
|
pub data: Option<Arc<T>>,
|
|
|
|
pub uuid: Uuid,
|
|
|
|
pub state: ResourceState,
|
|
|
|
}
|
|
|
|
|
2023-09-29 17:00:33 +00:00
|
|
|
/// A helper type to make it easier to use resources
|
|
|
|
pub type ResHandle<T> = Arc<Resource<T>>;
|
|
|
|
|
2023-09-12 18:25:33 +00:00
|
|
|
impl<T: Send + Sync + 'static> Resource<T> {
|
|
|
|
/// Create the resource with data, its assumed the state is `Ready`
|
|
|
|
pub fn with_data(path: &str, data: T) -> Self {
|
|
|
|
Self {
|
|
|
|
path: path.to_string(),
|
|
|
|
data: Some(Arc::new(data)),
|
|
|
|
uuid: Uuid::new_v4(),
|
|
|
|
state: ResourceState::Ready,
|
|
|
|
}
|
|
|
|
}
|
|
|
|
}
|