pub trait ResourceReader {
type Resource: Read;
type Error: Error + Send + Sync + 'static;
// Required method
fn read_from(&mut self, path: &Path) -> Result<Self::Resource, Self::Error>;
}
Expand description
A trait defining types that can load data from a ResourcePath
.
This trait should be implemented if you wish to load data from a virtual filesystem.
§Example
use std::io::Cursor;
/// Basic example reader impl that just keeps a few resources in memory
struct MemoryReader;
impl tiled::ResourceReader for MemoryReader {
type Resource = Cursor<&'static [u8]>;
type Error = std::io::Error;
fn read_from(&mut self, path: &std::path::Path) -> std::result::Result<Self::Resource, Self::Error> {
if path == std::path::Path::new("my_map.tmx") {
Ok(Cursor::new(include_bytes!("../assets/tiled_xml.tmx")))
} else {
Err(std::io::Error::new(std::io::ErrorKind::NotFound, "file not found"))
}
}
}
Required Associated Types§
Sourcetype Resource: Read
type Resource: Read
The type of the resource that the reader provides. For example, for
FilesystemResourceReader
, this is defined as File
.
Sourcetype Error: Error + Send + Sync + 'static
type Error: Error + Send + Sync + 'static
The type that is returned if read_from()
fails. For example, for
FilesystemResourceReader
, this is defined as std::io::Error
.