1 2 3 4 5 6 7 8 9 10 11 12 13 14 15 16 17 18 19 20 21 22 23 24 25 26 27 28 29 30 31 32 33 34 35 36 37 38 39 40 41 42 43 44 45 46 47 48 49 50 51 52 53 54 55 56 57 58 59 60 61 62 63 64 65 66
use crate::id::ID; use std::{any::Any, mem::replace}; pub type AssetID = ID<()>; pub struct Asset { id: AssetID, protocol: String, path: String, data: Box<dyn Any + Send + Sync>, } impl Asset { pub fn new(protocol: &str, path: &str, data: Box<dyn Any + Send + Sync>) -> Self { Self { id: AssetID::new(), protocol: protocol.to_owned(), path: path.to_owned(), data, } } pub fn id(&self) -> AssetID { self.id } pub fn protocol(&self) -> &str { &self.protocol } pub fn path(&self) -> &str { &self.path } pub fn to_full_path(&self) -> String { format!("{}://{}", self.protocol, self.path) } pub fn is<T>(&self) -> bool where T: Any + Send + Sync, { self.data.is::<T>() } pub fn get<T>(&self) -> Option<&T> where T: Any + Send + Sync, { self.data.downcast_ref() } pub fn get_mut<T>(&mut self) -> Option<&mut T> where T: Any + Send + Sync, { self.data.downcast_mut() } pub fn set<T>(&mut self, data: T) -> Box<dyn Any + Send + Sync> where T: Any + Send + Sync, { replace(&mut self.data, Box::new(data)) } }