mirror of
https://github.com/tauri-apps/tauri.git
synced 2026-04-05 10:13:00 +02:00
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:
@@ -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.
|
||||
|
||||
Reference in New Issue
Block a user