pub struct Assets { /* private fields */ }Expand description
A collection of EmbeddedFiles plus the Route table that maps served URLs to them.
You normally get one via the generated ASSETS static (see embed!), not by calling
Assets::new yourself.
Implementations§
Source§impl Assets
impl Assets
Sourcepub const fn new(
files: &'static [EmbeddedFile],
routes: &'static [Route],
) -> Self
pub const fn new( files: &'static [EmbeddedFile], routes: &'static [Route], ) -> Self
Construct an asset set from its files and route table. routes must be sorted by
Route::url, and each Route::file must index into files; the build script
guarantees both for the generated ASSETS static.
Sourcepub fn resolve(&self, url: &str) -> Option<Resolved>
pub fn resolve(&self, url: &str) -> Option<Resolved>
Resolve a served URL — a hashed_url like
/assets/css/style.9f3a1c2b.css, or the plain url of an
immutable_dir asset — to the file that answers it and the Cache-Control to send. This is
the lookup ServeEmbedded uses; hand-rolled (non-tower) integrations call it to build a
response themselves.
Sourcepub fn get_logical(&self, logical: &str) -> Option<&'static EmbeddedFile>
pub fn get_logical(&self, logical: &str) -> Option<&'static EmbeddedFile>
Look up a file by its crate-root-relative logical_path,
e.g. assets/css/style.css.
Sourcepub fn url(&self, logical: &str) -> Option<&'static str>
pub fn url(&self, logical: &str) -> Option<&'static str>
The URL to reference an asset by, given its crate-root-relative path, e.g.
url("assets/css/style.css") → Some("/assets/css/style.9f3a1c2b.css"). Returns the
cache-busted hashed_url when there is one, otherwise the plain
url (for immutable_dir assets).
This is the runtime equivalent of the asset! macro, for cases where the asset name isn’t
known at compile time. Prefer asset! when you can — it’s checked at compile time.
Sourcepub fn iter(&self) -> impl Iterator<Item = &'static EmbeddedFile>
pub fn iter(&self) -> impl Iterator<Item = &'static EmbeddedFile>
Iterate over every embedded file.
Sourcepub fn service(&'static self) -> ServeEmbedded
pub fn service(&'static self) -> ServeEmbedded
A tower::Service that serves these assets.
Generated asset URLs are already full paths, so mount the service as a fallback (no nesting or prefix stripping):
Router::new().fallback_service(ASSETS.service())