56 lines
1.4 KiB
Rust
56 lines
1.4 KiB
Rust
|
use std::{sync::Arc, collections::{HashMap, hash_map::DefaultHasher}, hash::{Hash, Hasher}, any::Any};
|
||
|
|
||
|
use crate::{resource::Resource, loader::ResourceLoader};
|
||
|
|
||
|
pub trait ResourceStorage: Send + Sync + Any + 'static {
|
||
|
fn as_any(&self) -> &dyn Any;
|
||
|
fn as_any_mut(&mut self) -> &mut dyn Any;
|
||
|
fn as_arc_any(self: Arc<Self>) -> Arc<dyn Any + Send + Sync>;
|
||
|
}
|
||
|
|
||
|
/// Implements this trait for anything that fits the type bounds
|
||
|
impl<T: Send + Sync + 'static> ResourceStorage for T {
|
||
|
fn as_any(&self) -> &dyn Any {
|
||
|
self
|
||
|
}
|
||
|
|
||
|
fn as_any_mut(&mut self) -> &mut dyn Any {
|
||
|
self
|
||
|
}
|
||
|
|
||
|
fn as_arc_any(self: Arc<Self>) -> Arc<dyn Any + Send + Sync> {
|
||
|
self.clone()
|
||
|
}
|
||
|
}
|
||
|
|
||
|
#[derive(Default)]
|
||
|
pub struct ResourceManager {
|
||
|
resources: HashMap<String, Arc<dyn ResourceStorage>>,
|
||
|
loader: HashMap<String, Box<dyn ResourceLoader>>,
|
||
|
}
|
||
|
|
||
|
impl ResourceManager {
|
||
|
pub fn new() -> Self {
|
||
|
Self {
|
||
|
resources: HashMap::new(),
|
||
|
loader: HashMap::new(),
|
||
|
}
|
||
|
}
|
||
|
|
||
|
pub fn request<T: Send + Sync + Any + 'static>(&mut self, path: &str) -> Option<Arc<Resource<T>>> {
|
||
|
match self.resources.get(&path.to_string()) {
|
||
|
Some(res) => {
|
||
|
let res = res.clone().as_arc_any();
|
||
|
let res = res.downcast::<Resource<T>>().ok();
|
||
|
|
||
|
res
|
||
|
},
|
||
|
None => {
|
||
|
|
||
|
|
||
|
|
||
|
todo!()
|
||
|
}
|
||
|
}
|
||
|
}
|
||
|
}
|