chore: reduce cloning in EmbeddedAssets::get (#15112)

* reduce cloning in `EmbeddedAssets::get`

* Apply suggestion from @Legend-Master

* Fix github suggestion

---------

Co-authored-by: Tony <68118705+Legend-Master@users.noreply.github.com>
Co-authored-by: Tony <legendmastertony@gmail.com>
This commit is contained in:
llogiq
2026-03-17 05:10:51 +01:00
committed by GitHub
parent 8db451c791
commit 1fa1db5cd9

View File

@@ -168,27 +168,18 @@ impl EmbeddedAssets {
/// Get an asset by key.
#[cfg(feature = "compression")]
pub fn get(&self, key: &AssetKey) -> Option<Cow<'_, [u8]>> {
self
.assets
.get(key.as_ref())
.map(|&(mut asdf)| {
// with the exception of extremely small files, output should usually be
// at least as large as the compressed version.
let mut buf = Vec::with_capacity(asdf.len());
brotli::BrotliDecompress(&mut asdf, &mut buf).map(|()| buf)
})
.and_then(Result::ok)
.map(Cow::Owned)
let &(mut asdf) = self.assets.get(key.as_ref())?;
// with the exception of extremely small files, output should usually be
// at least as large as the compressed version.
let mut buf = Vec::with_capacity(asdf.len());
brotli::BrotliDecompress(&mut asdf, &mut buf).ok()?;
Some(Cow::Owned(buf))
}
/// Get an asset by key.
#[cfg(not(feature = "compression"))]
pub fn get(&self, key: &AssetKey) -> Option<Cow<'_, [u8]>> {
self
.assets
.get(key.as_ref())
.copied()
.map(|a| Cow::Owned(a.to_vec()))
Some(Cow::Borrowed(self.assets.get(key.as_ref())?))
}
/// Iterate on the assets.