Trait ResourceReader

Source
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§

Source

type Resource: Read

The type of the resource that the reader provides. For example, for FilesystemResourceReader, this is defined as File.

Source

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.

Required Methods§

Source

fn read_from(&mut self, path: &Path) -> Result<Self::Resource, Self::Error>

Try to return a reader object from a path into the resources filesystem.

Implementors§

Source§

impl ResourceReader for FilesystemResourceReader

Source§

impl<T, R, E> ResourceReader for T
where T: for<'a> Fn(&'a Path) -> Result<R, E>, R: Read, E: Error + Send + Sync + 'static,