Skip to content

Commit faa259b

Browse files
authored
refactor(core)!: change Assets::iter item to use Cow (#10907)
* refactor(core): change `Assets::iter` item to use `Cow` make the iterator more flexible to support Assets implementations that do not rely on static assets * fix test? * lint * lint * clippy again
1 parent d9c8d3c commit faa259b

File tree

7 files changed

+108
-44
lines changed

7 files changed

+108
-44
lines changed

.changes/assets-iter-cow.md

Lines changed: 6 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -0,0 +1,6 @@
1+
---
2+
"tauri-utils": patch:breaking
3+
"tauri": patch:breaking
4+
---
5+
6+
The `Assets::iter` function now must return a iterator with `Item = (Cow<'_, str>, Cow<'_, [u8]>)` to be more flexible on contexts where the assets are not `'static`.

crates/tauri-utils/src/assets.rs

Lines changed: 10 additions & 2 deletions
Original file line numberDiff line numberDiff line change
@@ -12,6 +12,9 @@ use std::{
1212
path::{Component, Path},
1313
};
1414

15+
/// Assets iterator.
16+
pub type AssetsIter<'a> = dyn Iterator<Item = (Cow<'a, str>, Cow<'a, [u8]>)> + 'a;
17+
1518
/// Represent an asset file path in a normalized way.
1619
///
1720
/// The following rules are enforced and added if needed:
@@ -155,8 +158,13 @@ impl EmbeddedAssets {
155158
}
156159

157160
/// Iterate on the assets.
158-
pub fn iter(&self) -> Box<dyn Iterator<Item = (&str, &[u8])> + '_> {
159-
Box::new(self.assets.into_iter().map(|(k, b)| (*k, *b)))
161+
pub fn iter(&self) -> Box<AssetsIter<'_>> {
162+
Box::new(
163+
self
164+
.assets
165+
.into_iter()
166+
.map(|(k, b)| (Cow::Borrowed(*k), Cow::Borrowed(*b))),
167+
)
160168
}
161169

162170
/// CSP hashes for the given asset.

crates/tauri/src/app.rs

Lines changed: 2 additions & 2 deletions
Original file line numberDiff line numberDiff line change
@@ -37,7 +37,7 @@ use tauri_runtime::{
3737
window::DragDropEvent,
3838
RuntimeInitArgs,
3939
};
40-
use tauri_utils::PackageInfo;
40+
use tauri_utils::{assets::AssetsIter, PackageInfo};
4141

4242
use serde::Serialize;
4343
use std::{
@@ -309,7 +309,7 @@ impl<R: Runtime> AssetResolver<R> {
309309
}
310310

311311
/// Iterate on all assets.
312-
pub fn iter(&self) -> Box<dyn Iterator<Item = (&str, &[u8])> + '_> {
312+
pub fn iter(&self) -> Box<AssetsIter<'_>> {
313313
self.manager.assets.iter()
314314
}
315315
}

crates/tauri/src/ipc/protocol.rs

Lines changed: 2 additions & 2 deletions
Original file line numberDiff line numberDiff line change
@@ -17,7 +17,6 @@ use http::{
1717
},
1818
HeaderValue, Method, Request, StatusCode,
1919
};
20-
use mime::APPLICATION_OCTET_STREAM;
2120
use url::Url;
2221

2322
use super::{CallbackFn, InvokeResponse};
@@ -279,7 +278,8 @@ fn handle_ipc_message<R: Runtime>(request: Request<String>, manager: &AppManager
279278
serde_json::from_str::<IsolationMessage<'_>>(request.body())
280279
.map_err(Into::into)
281280
.and_then(|message| {
282-
let is_raw = message.payload.content_type() == &APPLICATION_OCTET_STREAM.to_string();
281+
let is_raw =
282+
message.payload.content_type() == &mime::APPLICATION_OCTET_STREAM.to_string();
283283
let payload = crypto_keys.decrypt(message.payload)?;
284284
Ok(Message {
285285
cmd: message.cmd,

crates/tauri/src/lib.rs

Lines changed: 4 additions & 3 deletions
Original file line numberDiff line numberDiff line change
@@ -79,6 +79,7 @@ pub use tauri_macros::include_image;
7979
pub use tauri_macros::mobile_entry_point;
8080
pub use tauri_macros::{command, generate_handler};
8181

82+
use tauri_utils::assets::AssetsIter;
8283
pub use url::Url;
8384

8485
pub(crate) mod app;
@@ -351,7 +352,7 @@ pub trait Assets<R: Runtime>: Send + Sync + 'static {
351352
fn get(&self, key: &AssetKey) -> Option<Cow<'_, [u8]>>;
352353

353354
/// Iterator for the assets.
354-
fn iter(&self) -> Box<dyn Iterator<Item = (&str, &[u8])> + '_>;
355+
fn iter(&self) -> Box<tauri_utils::assets::AssetsIter<'_>>;
355356

356357
/// Gets the hashes for the CSP tag of the HTML on the given path.
357358
fn csp_hashes(&self, html_path: &AssetKey) -> Box<dyn Iterator<Item = CspHash<'_>> + '_>;
@@ -362,7 +363,7 @@ impl<R: Runtime> Assets<R> for EmbeddedAssets {
362363
EmbeddedAssets::get(self, key)
363364
}
364365

365-
fn iter(&self) -> Box<dyn Iterator<Item = (&str, &[u8])> + '_> {
366+
fn iter(&self) -> Box<AssetsIter<'_>> {
366367
EmbeddedAssets::iter(self)
367368
}
368369

@@ -1084,7 +1085,7 @@ mod test_utils {
10841085
fn check_spawn_task(task in "[a-z]+") {
10851086
// create dummy task function
10861087
let dummy_task = async move {
1087-
format!("{task}-run-dummy-task");
1088+
let _ = format!("{task}-run-dummy-task");
10881089
};
10891090
// call spawn
10901091
crate::async_runtime::spawn(dummy_task);

crates/tauri/src/test/mod.rs

Lines changed: 8 additions & 3 deletions
Original file line numberDiff line numberDiff line change
@@ -64,7 +64,7 @@ use crate::{
6464
};
6565
use tauri_utils::{
6666
acl::resolved::Resolved,
67-
assets::{AssetKey, CspHash},
67+
assets::{AssetKey, AssetsIter, CspHash},
6868
config::{AppConfig, Config},
6969
};
7070

@@ -82,8 +82,13 @@ impl<R: Runtime> Assets<R> for NoopAsset {
8282
None
8383
}
8484

85-
fn iter(&self) -> Box<dyn Iterator<Item = (&str, &[u8])> + '_> {
86-
Box::new(self.assets.iter().map(|(k, b)| (k.as_str(), b.as_slice())))
85+
fn iter(&self) -> Box<AssetsIter<'_>> {
86+
Box::new(
87+
self
88+
.assets
89+
.iter()
90+
.map(|(k, b)| (Cow::Borrowed(k.as_str()), Cow::Borrowed(b.as_slice()))),
91+
)
8792
}
8893

8994
fn csp_hashes(&self, html_path: &AssetKey) -> Box<dyn Iterator<Item = CspHash<'_>> + '_> {

0 commit comments

Comments
 (0)