pub struct AssetPath<'a> { /* private fields */ }Expand description
Represents an asset path, including its protocol, path, and optional metadata.
§Structure
The AssetPath is divided into three main components:
- Protocol: The scheme of the asset path (e.g.,
file,http). - Path: The main path to the asset (e.g.,
/assets/texture.png). - Meta: Optional metadata for the asset, typically a query string (e.g.,
?version=1).
Implementations§
Source§impl<'a> AssetPath<'a>
impl<'a> AssetPath<'a>
Sourcepub fn new(content: impl Into<Cow<'a, str>>) -> Self
pub fn new(content: impl Into<Cow<'a, str>>) -> Self
Creates a new AssetPath from the given content.
Sourcepub fn from_parts(protocol: &str, path: &str, meta: &str) -> Self
pub fn from_parts(protocol: &str, path: &str, meta: &str) -> Self
Constructs an AssetPath from separate protocol, path, and metadata strings.
Examples found in repository?
11fn main() -> Result<(), Box<dyn Error>> {
12 /* ANCHOR: main */
13 let language = Arc::new(RwLock::new("en"));
14 let language2 = language.clone();
15
16 let mut database = AssetDatabase::default()
17 .with_protocol(TextAssetProtocol)
18 .with_fetch(RewriteAssetFetch::new(
19 FileAssetFetch::default().with_root("resources"),
20 move |path| {
21 // Rewrite input path to localized one.
22 Ok(AssetPath::from_parts(
23 path.protocol(),
24 &format!(
25 "{}.{}{}",
26 path.path_without_extension(),
27 *language2.read().unwrap(),
28 path.path_dot_extension().unwrap_or_default()
29 ),
30 path.meta(),
31 ))
32 },
33 ));
34
35 // Gets `text://localized.en.txt`.
36 let asset = database.ensure("text://localized.txt")?;
37 println!("English: {}", asset.access::<&String>(&database));
38
39 // Change language.
40 *language.write().unwrap() = "de";
41 database.storage.clear();
42
43 // Gets `text://localized.de.txt`.
44 let asset = database.ensure("text://localized.txt")?;
45 println!("German: {}", asset.access::<&String>(&database));
46 /* ANCHOR_END: main */
47
48 Ok(())
49}More examples
11fn main() -> Result<(), Box<dyn Error>> {
12 let mut database = AssetDatabase::default()
13 .with_protocol(TextAssetProtocol)
14 // Rewrite asset fetch allows to rewrite input asset paths to some other
15 // before inner fetch tries to load it.
16 .with_fetch(RewriteAssetFetch::new(
17 FileAssetFetch::default().with_root("resources"),
18 |path| {
19 // Get version from requested asset meta items or use current version.
20 let version = path
21 .meta_items()
22 .find(|(key, _)| *key == "v")
23 .map(|(_, value)| value)
24 .unwrap_or(VERSION);
25 // Build new asset path that includes version.
26 // Example: `protocol://dir/asset.v1.ext`
27 Ok(AssetPath::from_parts(
28 path.protocol(),
29 &format!(
30 "{}.v{}{}",
31 path.path_without_extension(),
32 version,
33 path.path_dot_extension().unwrap_or_default()
34 ),
35 path.meta(),
36 ))
37 },
38 ));
39
40 // Gets `text://versioned.v1.txt`.
41 let v1 = database.ensure("text://versioned.txt?v=1")?;
42 println!("Version 1: {}", v1.access::<&String>(&database));
43
44 // Gets `text://versioned.v2.txt`.
45 let v2 = database.ensure("text://versioned.txt")?;
46 println!("Version 2: {}", v2.access::<&String>(&database));
47
48 Ok(())
49}Sourcepub fn into_static(self) -> AssetPathStatic
pub fn into_static(self) -> AssetPathStatic
Converts the AssetPath into a static version, consuming the current instance.
Sourcepub fn protocol(&self) -> &str
pub fn protocol(&self) -> &str
Returns the protocol part of the AssetPath.
Examples found in repository?
11fn main() -> Result<(), Box<dyn Error>> {
12 /* ANCHOR: main */
13 let language = Arc::new(RwLock::new("en"));
14 let language2 = language.clone();
15
16 let mut database = AssetDatabase::default()
17 .with_protocol(TextAssetProtocol)
18 .with_fetch(RewriteAssetFetch::new(
19 FileAssetFetch::default().with_root("resources"),
20 move |path| {
21 // Rewrite input path to localized one.
22 Ok(AssetPath::from_parts(
23 path.protocol(),
24 &format!(
25 "{}.{}{}",
26 path.path_without_extension(),
27 *language2.read().unwrap(),
28 path.path_dot_extension().unwrap_or_default()
29 ),
30 path.meta(),
31 ))
32 },
33 ));
34
35 // Gets `text://localized.en.txt`.
36 let asset = database.ensure("text://localized.txt")?;
37 println!("English: {}", asset.access::<&String>(&database));
38
39 // Change language.
40 *language.write().unwrap() = "de";
41 database.storage.clear();
42
43 // Gets `text://localized.de.txt`.
44 let asset = database.ensure("text://localized.txt")?;
45 println!("German: {}", asset.access::<&String>(&database));
46 /* ANCHOR_END: main */
47
48 Ok(())
49}More examples
13fn main() -> Result<(), Box<dyn Error>> {
14 let mut database = AssetDatabase::default()
15 .with_protocol(TextAssetProtocol)
16 .with_protocol(BytesAssetProtocol)
17 .with_fetch(
18 RouterAssetFetch::default()
19 .route(
20 |_| true,
21 ContainerAssetFetch::new(ZipContainerPartialFetch::new(ZipArchive::new(
22 File::open("./resources/main.zip")?,
23 )?)),
24 0,
25 )
26 .route(
27 |path| path.path().starts_with("dlc/"),
28 RewriteAssetFetch::new(
29 ContainerAssetFetch::new(ZipContainerPartialFetch::new(ZipArchive::new(
30 File::open("./resources/dlc.zip")?,
31 )?)),
32 |path| {
33 Ok(format!(
34 "{}://{}",
35 path.protocol(),
36 path.path_with_meta().strip_prefix("dlc/").unwrap()
37 )
38 .into())
39 },
40 ),
41 1,
42 ),
43 );
44
45 let lorem = database.ensure("text://lorem.txt")?;
46 println!("Lorem Ipsum: {}", lorem.access::<&String>(&database));
47
48 let trash = database.ensure("bytes://dlc/trash.bin")?;
49 println!("Bytes: {:?}", trash.access::<&Vec<u8>>(&database));
50
51 Ok(())
52}11fn main() -> Result<(), Box<dyn Error>> {
12 let mut database = AssetDatabase::default()
13 .with_protocol(TextAssetProtocol)
14 // Rewrite asset fetch allows to rewrite input asset paths to some other
15 // before inner fetch tries to load it.
16 .with_fetch(RewriteAssetFetch::new(
17 FileAssetFetch::default().with_root("resources"),
18 |path| {
19 // Get version from requested asset meta items or use current version.
20 let version = path
21 .meta_items()
22 .find(|(key, _)| *key == "v")
23 .map(|(_, value)| value)
24 .unwrap_or(VERSION);
25 // Build new asset path that includes version.
26 // Example: `protocol://dir/asset.v1.ext`
27 Ok(AssetPath::from_parts(
28 path.protocol(),
29 &format!(
30 "{}.v{}{}",
31 path.path_without_extension(),
32 version,
33 path.path_dot_extension().unwrap_or_default()
34 ),
35 path.meta(),
36 ))
37 },
38 ));
39
40 // Gets `text://versioned.v1.txt`.
41 let v1 = database.ensure("text://versioned.txt?v=1")?;
42 println!("Version 1: {}", v1.access::<&String>(&database));
43
44 // Gets `text://versioned.v2.txt`.
45 let v2 = database.ensure("text://versioned.txt")?;
46 println!("Version 2: {}", v2.access::<&String>(&database));
47
48 Ok(())
49}Sourcepub fn path(&self) -> &str
pub fn path(&self) -> &str
Returns the path part of the AssetPath.
Examples found in repository?
More examples
12fn main() -> Result<(), Box<dyn Error>> {
13 /* ANCHOR: main */
14 let mut database = AssetDatabase::default()
15 .with_protocol(TextAssetProtocol)
16 .with_protocol(BytesAssetProtocol)
17 // We start with regular fetch engine.
18 .with_fetch(FileAssetFetch::default().with_root("resources"));
19
20 // Start loading package ZIP bytes.
21 database.ensure("bytes://package.zip")?;
22
23 // Maintain database while busy.
24 while database.is_busy() {
25 database.maintain()?;
26 }
27
28 // Then we push extraction asset fetch to fetch engine stack. From now on
29 // any future asset request will be extracted from loaded ZIP archive.
30 database.push_fetch(ExtractAssetFetch::new(from_asset_extractor(
31 "bytes://package.zip",
32 |bytes: &Vec<u8>, path| {
33 let mut archive = ZipArchive::new(Cursor::new(bytes))?;
34 let mut file = archive.by_name(path.path())?;
35 let mut result = vec![];
36 file.read_to_end(&mut result)?;
37 Ok(result)
38 },
39 )));
40
41 // Extract some assets from ZIP asset.
42 let lorem = database.ensure("text://lorem.txt")?;
43 let trash = database.ensure("bytes://trash.bin")?;
44
45 // Run maintenance to process extracted asset bytes.
46 database.maintain()?;
47
48 println!("Lorem Ipsum: {}", lorem.access::<&String>(&database));
49 println!("Bytes: {:?}", trash.access::<&Vec<u8>>(&database));
50 /* ANCHOR_END: main */
51
52 Ok(())
53}13fn main() -> Result<(), Box<dyn Error>> {
14 let mut database = AssetDatabase::default()
15 .with_protocol(TextAssetProtocol)
16 .with_protocol(BytesAssetProtocol)
17 .with_fetch(
18 RouterAssetFetch::default()
19 .route(
20 |_| true,
21 ContainerAssetFetch::new(ZipContainerPartialFetch::new(ZipArchive::new(
22 File::open("./resources/main.zip")?,
23 )?)),
24 0,
25 )
26 .route(
27 |path| path.path().starts_with("dlc/"),
28 RewriteAssetFetch::new(
29 ContainerAssetFetch::new(ZipContainerPartialFetch::new(ZipArchive::new(
30 File::open("./resources/dlc.zip")?,
31 )?)),
32 |path| {
33 Ok(format!(
34 "{}://{}",
35 path.protocol(),
36 path.path_with_meta().strip_prefix("dlc/").unwrap()
37 )
38 .into())
39 },
40 ),
41 1,
42 ),
43 );
44
45 let lorem = database.ensure("text://lorem.txt")?;
46 println!("Lorem Ipsum: {}", lorem.access::<&String>(&database));
47
48 let trash = database.ensure("bytes://dlc/trash.bin")?;
49 println!("Bytes: {:?}", trash.access::<&Vec<u8>>(&database));
50
51 Ok(())
52}
53
54struct ZipContainerPartialFetch {
55 archive: ZipArchive<File>,
56}
57
58impl ZipContainerPartialFetch {
59 pub fn new(archive: ZipArchive<File>) -> Self {
60 Self { archive }
61 }
62}
63
64impl ContainerPartialFetch for ZipContainerPartialFetch {
65 fn load_bytes(&mut self, path: AssetPath) -> Result<Vec<u8>, Box<dyn Error>> {
66 let mut file = self
67 .archive
68 .by_name(path.path())
69 .map_err(|error| format!("Could not read zip file: `{}` - {}", path.path(), error))?;
70 let mut bytes = vec![];
71 file.read_to_end(&mut bytes)?;
72 Ok(bytes)
73 }Sourcepub fn path_extension(&self) -> Option<&str>
pub fn path_extension(&self) -> Option<&str>
Returns the path part extension of the AssetPath.
Sourcepub fn path_dot_extension(&self) -> Option<&str>
pub fn path_dot_extension(&self) -> Option<&str>
Returns the path part extension with preceding dot of the AssetPath.
Examples found in repository?
11fn main() -> Result<(), Box<dyn Error>> {
12 /* ANCHOR: main */
13 let language = Arc::new(RwLock::new("en"));
14 let language2 = language.clone();
15
16 let mut database = AssetDatabase::default()
17 .with_protocol(TextAssetProtocol)
18 .with_fetch(RewriteAssetFetch::new(
19 FileAssetFetch::default().with_root("resources"),
20 move |path| {
21 // Rewrite input path to localized one.
22 Ok(AssetPath::from_parts(
23 path.protocol(),
24 &format!(
25 "{}.{}{}",
26 path.path_without_extension(),
27 *language2.read().unwrap(),
28 path.path_dot_extension().unwrap_or_default()
29 ),
30 path.meta(),
31 ))
32 },
33 ));
34
35 // Gets `text://localized.en.txt`.
36 let asset = database.ensure("text://localized.txt")?;
37 println!("English: {}", asset.access::<&String>(&database));
38
39 // Change language.
40 *language.write().unwrap() = "de";
41 database.storage.clear();
42
43 // Gets `text://localized.de.txt`.
44 let asset = database.ensure("text://localized.txt")?;
45 println!("German: {}", asset.access::<&String>(&database));
46 /* ANCHOR_END: main */
47
48 Ok(())
49}More examples
11fn main() -> Result<(), Box<dyn Error>> {
12 let mut database = AssetDatabase::default()
13 .with_protocol(TextAssetProtocol)
14 // Rewrite asset fetch allows to rewrite input asset paths to some other
15 // before inner fetch tries to load it.
16 .with_fetch(RewriteAssetFetch::new(
17 FileAssetFetch::default().with_root("resources"),
18 |path| {
19 // Get version from requested asset meta items or use current version.
20 let version = path
21 .meta_items()
22 .find(|(key, _)| *key == "v")
23 .map(|(_, value)| value)
24 .unwrap_or(VERSION);
25 // Build new asset path that includes version.
26 // Example: `protocol://dir/asset.v1.ext`
27 Ok(AssetPath::from_parts(
28 path.protocol(),
29 &format!(
30 "{}.v{}{}",
31 path.path_without_extension(),
32 version,
33 path.path_dot_extension().unwrap_or_default()
34 ),
35 path.meta(),
36 ))
37 },
38 ));
39
40 // Gets `text://versioned.v1.txt`.
41 let v1 = database.ensure("text://versioned.txt?v=1")?;
42 println!("Version 1: {}", v1.access::<&String>(&database));
43
44 // Gets `text://versioned.v2.txt`.
45 let v2 = database.ensure("text://versioned.txt")?;
46 println!("Version 2: {}", v2.access::<&String>(&database));
47
48 Ok(())
49}Sourcepub fn path_without_extension(&self) -> &str
pub fn path_without_extension(&self) -> &str
Returns the path part without extension of the AssetPath.
Examples found in repository?
11fn main() -> Result<(), Box<dyn Error>> {
12 /* ANCHOR: main */
13 let language = Arc::new(RwLock::new("en"));
14 let language2 = language.clone();
15
16 let mut database = AssetDatabase::default()
17 .with_protocol(TextAssetProtocol)
18 .with_fetch(RewriteAssetFetch::new(
19 FileAssetFetch::default().with_root("resources"),
20 move |path| {
21 // Rewrite input path to localized one.
22 Ok(AssetPath::from_parts(
23 path.protocol(),
24 &format!(
25 "{}.{}{}",
26 path.path_without_extension(),
27 *language2.read().unwrap(),
28 path.path_dot_extension().unwrap_or_default()
29 ),
30 path.meta(),
31 ))
32 },
33 ));
34
35 // Gets `text://localized.en.txt`.
36 let asset = database.ensure("text://localized.txt")?;
37 println!("English: {}", asset.access::<&String>(&database));
38
39 // Change language.
40 *language.write().unwrap() = "de";
41 database.storage.clear();
42
43 // Gets `text://localized.de.txt`.
44 let asset = database.ensure("text://localized.txt")?;
45 println!("German: {}", asset.access::<&String>(&database));
46 /* ANCHOR_END: main */
47
48 Ok(())
49}More examples
11fn main() -> Result<(), Box<dyn Error>> {
12 let mut database = AssetDatabase::default()
13 .with_protocol(TextAssetProtocol)
14 // Rewrite asset fetch allows to rewrite input asset paths to some other
15 // before inner fetch tries to load it.
16 .with_fetch(RewriteAssetFetch::new(
17 FileAssetFetch::default().with_root("resources"),
18 |path| {
19 // Get version from requested asset meta items or use current version.
20 let version = path
21 .meta_items()
22 .find(|(key, _)| *key == "v")
23 .map(|(_, value)| value)
24 .unwrap_or(VERSION);
25 // Build new asset path that includes version.
26 // Example: `protocol://dir/asset.v1.ext`
27 Ok(AssetPath::from_parts(
28 path.protocol(),
29 &format!(
30 "{}.v{}{}",
31 path.path_without_extension(),
32 version,
33 path.path_dot_extension().unwrap_or_default()
34 ),
35 path.meta(),
36 ))
37 },
38 ));
39
40 // Gets `text://versioned.v1.txt`.
41 let v1 = database.ensure("text://versioned.txt?v=1")?;
42 println!("Version 1: {}", v1.access::<&String>(&database));
43
44 // Gets `text://versioned.v2.txt`.
45 let v2 = database.ensure("text://versioned.txt")?;
46 println!("Version 2: {}", v2.access::<&String>(&database));
47
48 Ok(())
49}Sourcepub fn path_parts(&self) -> impl Iterator<Item = &str>
pub fn path_parts(&self) -> impl Iterator<Item = &str>
Splits the path into its component parts.
Sourcepub fn meta(&self) -> &str
pub fn meta(&self) -> &str
Returns the metadata part of the AssetPath.
Examples found in repository?
11fn main() -> Result<(), Box<dyn Error>> {
12 /* ANCHOR: main */
13 let language = Arc::new(RwLock::new("en"));
14 let language2 = language.clone();
15
16 let mut database = AssetDatabase::default()
17 .with_protocol(TextAssetProtocol)
18 .with_fetch(RewriteAssetFetch::new(
19 FileAssetFetch::default().with_root("resources"),
20 move |path| {
21 // Rewrite input path to localized one.
22 Ok(AssetPath::from_parts(
23 path.protocol(),
24 &format!(
25 "{}.{}{}",
26 path.path_without_extension(),
27 *language2.read().unwrap(),
28 path.path_dot_extension().unwrap_or_default()
29 ),
30 path.meta(),
31 ))
32 },
33 ));
34
35 // Gets `text://localized.en.txt`.
36 let asset = database.ensure("text://localized.txt")?;
37 println!("English: {}", asset.access::<&String>(&database));
38
39 // Change language.
40 *language.write().unwrap() = "de";
41 database.storage.clear();
42
43 // Gets `text://localized.de.txt`.
44 let asset = database.ensure("text://localized.txt")?;
45 println!("German: {}", asset.access::<&String>(&database));
46 /* ANCHOR_END: main */
47
48 Ok(())
49}More examples
11fn main() -> Result<(), Box<dyn Error>> {
12 let mut database = AssetDatabase::default()
13 .with_protocol(TextAssetProtocol)
14 // Rewrite asset fetch allows to rewrite input asset paths to some other
15 // before inner fetch tries to load it.
16 .with_fetch(RewriteAssetFetch::new(
17 FileAssetFetch::default().with_root("resources"),
18 |path| {
19 // Get version from requested asset meta items or use current version.
20 let version = path
21 .meta_items()
22 .find(|(key, _)| *key == "v")
23 .map(|(_, value)| value)
24 .unwrap_or(VERSION);
25 // Build new asset path that includes version.
26 // Example: `protocol://dir/asset.v1.ext`
27 Ok(AssetPath::from_parts(
28 path.protocol(),
29 &format!(
30 "{}.v{}{}",
31 path.path_without_extension(),
32 version,
33 path.path_dot_extension().unwrap_or_default()
34 ),
35 path.meta(),
36 ))
37 },
38 ));
39
40 // Gets `text://versioned.v1.txt`.
41 let v1 = database.ensure("text://versioned.txt?v=1")?;
42 println!("Version 1: {}", v1.access::<&String>(&database));
43
44 // Gets `text://versioned.v2.txt`.
45 let v2 = database.ensure("text://versioned.txt")?;
46 println!("Version 2: {}", v2.access::<&String>(&database));
47
48 Ok(())
49}Sourcepub fn meta_items(&self) -> impl Iterator<Item = (&str, &str)>
pub fn meta_items(&self) -> impl Iterator<Item = (&str, &str)>
Parses the metadata into key-value pairs.
Examples found in repository?
11fn main() -> Result<(), Box<dyn Error>> {
12 let mut database = AssetDatabase::default()
13 .with_protocol(TextAssetProtocol)
14 // Rewrite asset fetch allows to rewrite input asset paths to some other
15 // before inner fetch tries to load it.
16 .with_fetch(RewriteAssetFetch::new(
17 FileAssetFetch::default().with_root("resources"),
18 |path| {
19 // Get version from requested asset meta items or use current version.
20 let version = path
21 .meta_items()
22 .find(|(key, _)| *key == "v")
23 .map(|(_, value)| value)
24 .unwrap_or(VERSION);
25 // Build new asset path that includes version.
26 // Example: `protocol://dir/asset.v1.ext`
27 Ok(AssetPath::from_parts(
28 path.protocol(),
29 &format!(
30 "{}.v{}{}",
31 path.path_without_extension(),
32 version,
33 path.path_dot_extension().unwrap_or_default()
34 ),
35 path.meta(),
36 ))
37 },
38 ));
39
40 // Gets `text://versioned.v1.txt`.
41 let v1 = database.ensure("text://versioned.txt?v=1")?;
42 println!("Version 1: {}", v1.access::<&String>(&database));
43
44 // Gets `text://versioned.v2.txt`.
45 let v2 = database.ensure("text://versioned.txt")?;
46 println!("Version 2: {}", v2.access::<&String>(&database));
47
48 Ok(())
49}Sourcepub fn has_meta_key(&self, key: &str) -> bool
pub fn has_meta_key(&self, key: &str) -> bool
Checks if path has specific meta key.
Sourcepub fn has_meta_key_value(&self, key: &str, value: &str) -> bool
pub fn has_meta_key_value(&self, key: &str, value: &str) -> bool
Checks if path has specific meta key-value.
Examples found in repository?
8fn main() -> Result<(), Box<dyn Error>> {
9 /* ANCHOR: main */
10 let mut database = AssetDatabase::default()
11 .with_protocol(TextAssetProtocol)
12 .with_protocol(BytesAssetProtocol)
13 .with_fetch(
14 // Router allows to combine multiple asset sources, so that proper one to use
15 // for given asset is selected by pattern in asset path.
16 RouterAssetFetch::default()
17 // Every asset that has `router=file` meta, will load asset from file.
18 .route(
19 |path| path.has_meta_key_value("router", "file"),
20 FileAssetFetch::default().with_root("resources"),
21 0,
22 )
23 // Every asset that has `memory/` path prefix, will load from in-memory collection.
24 .route(
25 |path| path.path().starts_with("memory/"),
26 vec![(
27 "memory/trash.bin".to_owned(),
28 std::fs::read("./resources/trash.bin")?,
29 )],
30 1,
31 ),
32 );
33
34 // This asset will select file router.
35 let lorem = database.ensure("text://lorem.txt?router=file")?;
36 println!("Lorem Ipsum: {}", lorem.access::<&String>(&database));
37
38 // This asset will select memory router.
39 let trash = database.ensure("bytes://memory/trash.bin")?;
40 println!("Bytes: {:?}", trash.access::<&Vec<u8>>(&database));
41 /* ANCHOR_END: main */
42
43 Ok(())
44}Sourcepub fn try_meta(&self) -> Option<&str>
pub fn try_meta(&self) -> Option<&str>
Tries to retrieve the metadata, returning None if it’s empty.
Sourcepub fn path_with_meta(&self) -> &str
pub fn path_with_meta(&self) -> &str
Returns the combined path and metadata of the AssetPath.
Examples found in repository?
13fn main() -> Result<(), Box<dyn Error>> {
14 let mut database = AssetDatabase::default()
15 .with_protocol(TextAssetProtocol)
16 .with_protocol(BytesAssetProtocol)
17 .with_fetch(
18 RouterAssetFetch::default()
19 .route(
20 |_| true,
21 ContainerAssetFetch::new(ZipContainerPartialFetch::new(ZipArchive::new(
22 File::open("./resources/main.zip")?,
23 )?)),
24 0,
25 )
26 .route(
27 |path| path.path().starts_with("dlc/"),
28 RewriteAssetFetch::new(
29 ContainerAssetFetch::new(ZipContainerPartialFetch::new(ZipArchive::new(
30 File::open("./resources/dlc.zip")?,
31 )?)),
32 |path| {
33 Ok(format!(
34 "{}://{}",
35 path.protocol(),
36 path.path_with_meta().strip_prefix("dlc/").unwrap()
37 )
38 .into())
39 },
40 ),
41 1,
42 ),
43 );
44
45 let lorem = database.ensure("text://lorem.txt")?;
46 println!("Lorem Ipsum: {}", lorem.access::<&String>(&database));
47
48 let trash = database.ensure("bytes://dlc/trash.bin")?;
49 println!("Bytes: {:?}", trash.access::<&Vec<u8>>(&database));
50
51 Ok(())
52}Sourcepub fn schedule(
&self,
database: &mut AssetDatabase,
) -> Result<AssetHandle, Box<dyn Error>>
pub fn schedule( &self, database: &mut AssetDatabase, ) -> Result<AssetHandle, Box<dyn Error>>
Schedules the asset in the given AssetDatabase.
Sourcepub fn ensure(
&self,
database: &mut AssetDatabase,
) -> Result<AssetHandle, Box<dyn Error>>
pub fn ensure( &self, database: &mut AssetDatabase, ) -> Result<AssetHandle, Box<dyn Error>>
Ensures the asset is loaded in the given AssetDatabase.
Sourcepub fn find(&self, database: &AssetDatabase) -> Option<AssetHandle>
pub fn find(&self, database: &AssetDatabase) -> Option<AssetHandle>
Searches for the asset in the given AssetDatabase.
Examples found in repository?
45 fn contents(&self, database: &AssetDatabase) -> String {
46 let mut result = String::new();
47 let mut current = Some(self);
48 while let Some(asset) = current {
49 result.push_str(asset.content.as_str());
50 current = current
51 .as_ref()
52 .and_then(|asset| asset.next.as_ref())
53 .and_then(|path| path.find(database))
54 .and_then(|handle| handle.access_checked::<&Self>(database));
55 if current.is_some() {
56 result.push(' ');
57 }
58 }
59 result
60 }More examples
44 fn contents(&self, database: &AssetDatabase) -> String {
45 // Read this and it's siblings content to output.
46 let mut result = String::new();
47 let mut current = Some(self);
48 while let Some(asset) = current {
49 result.push_str(asset.content.as_str());
50 current = current
51 .as_ref()
52 .and_then(|asset| asset.next.as_ref())
53 .and_then(|path| path.find(database))
54 .and_then(|handle| handle.access_checked::<&Self>(database));
55 if current.is_some() {
56 result.push(' ');
57 }
58 }
59 result
60 }Trait Implementations§
Source§impl<'de, 'a> Deserialize<'de> for AssetPath<'a>
impl<'de, 'a> Deserialize<'de> for AssetPath<'a>
Source§fn deserialize<__D>(__deserializer: __D) -> Result<Self, __D::Error>where
__D: Deserializer<'de>,
fn deserialize<__D>(__deserializer: __D) -> Result<Self, __D::Error>where
__D: Deserializer<'de>,
Source§impl From<AssetPath<'static>> for AssetRef
impl From<AssetPath<'static>> for AssetRef
Source§fn from(path: AssetPathStatic) -> Self
fn from(path: AssetPathStatic) -> Self
impl<'a> Eq for AssetPath<'a>
impl<'a> StructuralPartialEq for AssetPath<'a>
Auto Trait Implementations§
impl<'a> Freeze for AssetPath<'a>
impl<'a> RefUnwindSafe for AssetPath<'a>
impl<'a> Send for AssetPath<'a>
impl<'a> Sync for AssetPath<'a>
impl<'a> Unpin for AssetPath<'a>
impl<'a> UnwindSafe for AssetPath<'a>
Blanket Implementations§
Source§impl<T> BorrowMut<T> for Twhere
T: ?Sized,
impl<T> BorrowMut<T> for Twhere
T: ?Sized,
Source§fn borrow_mut(&mut self) -> &mut T
fn borrow_mut(&mut self) -> &mut T
Source§impl<T> CloneToUninit for Twhere
T: Clone,
impl<T> CloneToUninit for Twhere
T: Clone,
Source§impl<Q, K> Equivalent<K> for Q
impl<Q, K> Equivalent<K> for Q
Source§fn equivalent(&self, key: &K) -> bool
fn equivalent(&self, key: &K) -> bool
key and return true if they are equal.