Expand description
š§° Resource API
This API is useful for embedding and accessing static resources in a module.
Example usage
In Cargo.toml
for the module, define which static file resource the module wants to be able to access.
[package.metadata.ark]
include-files = [
"assets/coin.wav",
]
This will associate the file āassets/coin.wavā as a dependent static resource of the Ark module and Ark will load it and make it available for the module before it is loaded.
Inside the module one can then use ark::resource::get("assets/coin.wav")
to get a slice to the
contents of the resource. This data is always loaded in memory and as such always available for the module.
File roots
If one has a shared workspace with multiple modules and want to share resources between them, or have a more
complex path for them, one can set one or multiple file roots for the included files in Cargo.toml
.
In the below example we have an assets
folder that is located in a shared workspace directory relative to the
Cargo.toml
file and where we want to include files from. Note that in this example the resource would be referenced
in the module with the ark::resource::get("assets/coin.wav")
[package.metadata.ark]
include-file-roots = [
"../../assets",
include-files = [
"coin.wav",
]
It is also possible to have multiple roots specified. The file will then be loaded from the first root it is found in
Alternatives
This mechanism is meant for resources that are not trivially small (say large than 1 KB) and enables Ark to compress, de-duplicate and cache the resources efficiently for distribution, storage, and loading.
But if one has very small file resources, where compression and caching is not as beneficial, then it can be more optimal and easier
to embed the file directly into the module binary instead with the Rust standard include_bytes!
macro
Structs
Represents a handle to unique resource
Functions
Retrieves named static resource
Retrieves named static resource in a unsafe manner