Skip to content

Commit

Permalink
refactor(core)!: change Assets::iter item to use Cow (#10907)
Browse files Browse the repository at this point in the history
* 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
  • Loading branch information
lucasfernog authored Sep 5, 2024
1 parent d9c8d3c commit faa259b
Show file tree
Hide file tree
Showing 7 changed files with 108 additions and 44 deletions.
6 changes: 6 additions & 0 deletions .changes/assets-iter-cow.md
Original file line number Diff line number Diff line change
@@ -0,0 +1,6 @@
---
"tauri-utils": patch:breaking
"tauri": patch:breaking
---

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`.
12 changes: 10 additions & 2 deletions crates/tauri-utils/src/assets.rs
Original file line number Diff line number Diff line change
Expand Up @@ -12,6 +12,9 @@ use std::{
path::{Component, Path},
};

/// Assets iterator.
pub type AssetsIter<'a> = dyn Iterator<Item = (Cow<'a, str>, Cow<'a, [u8]>)> + 'a;

/// Represent an asset file path in a normalized way.
///
/// The following rules are enforced and added if needed:
Expand Down Expand Up @@ -155,8 +158,13 @@ impl EmbeddedAssets {
}

/// Iterate on the assets.
pub fn iter(&self) -> Box<dyn Iterator<Item = (&str, &[u8])> + '_> {
Box::new(self.assets.into_iter().map(|(k, b)| (*k, *b)))
pub fn iter(&self) -> Box<AssetsIter<'_>> {
Box::new(
self
.assets
.into_iter()
.map(|(k, b)| (Cow::Borrowed(*k), Cow::Borrowed(*b))),
)
}

/// CSP hashes for the given asset.
Expand Down
4 changes: 2 additions & 2 deletions crates/tauri/src/app.rs
Original file line number Diff line number Diff line change
Expand Up @@ -37,7 +37,7 @@ use tauri_runtime::{
window::DragDropEvent,
RuntimeInitArgs,
};
use tauri_utils::PackageInfo;
use tauri_utils::{assets::AssetsIter, PackageInfo};

use serde::Serialize;
use std::{
Expand Down Expand Up @@ -309,7 +309,7 @@ impl<R: Runtime> AssetResolver<R> {
}

/// Iterate on all assets.
pub fn iter(&self) -> Box<dyn Iterator<Item = (&str, &[u8])> + '_> {
pub fn iter(&self) -> Box<AssetsIter<'_>> {
self.manager.assets.iter()
}
}
Expand Down
4 changes: 2 additions & 2 deletions crates/tauri/src/ipc/protocol.rs
Original file line number Diff line number Diff line change
Expand Up @@ -17,7 +17,6 @@ use http::{
},
HeaderValue, Method, Request, StatusCode,
};
use mime::APPLICATION_OCTET_STREAM;
use url::Url;

use super::{CallbackFn, InvokeResponse};
Expand Down Expand Up @@ -279,7 +278,8 @@ fn handle_ipc_message<R: Runtime>(request: Request<String>, manager: &AppManager
serde_json::from_str::<IsolationMessage<'_>>(request.body())
.map_err(Into::into)
.and_then(|message| {
let is_raw = message.payload.content_type() == &APPLICATION_OCTET_STREAM.to_string();
let is_raw =
message.payload.content_type() == &mime::APPLICATION_OCTET_STREAM.to_string();
let payload = crypto_keys.decrypt(message.payload)?;
Ok(Message {
cmd: message.cmd,
Expand Down
7 changes: 4 additions & 3 deletions crates/tauri/src/lib.rs
Original file line number Diff line number Diff line change
Expand Up @@ -79,6 +79,7 @@ pub use tauri_macros::include_image;
pub use tauri_macros::mobile_entry_point;
pub use tauri_macros::{command, generate_handler};

use tauri_utils::assets::AssetsIter;
pub use url::Url;

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

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

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

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

Expand Down Expand Up @@ -1084,7 +1085,7 @@ mod test_utils {
fn check_spawn_task(task in "[a-z]+") {
// create dummy task function
let dummy_task = async move {
format!("{task}-run-dummy-task");
let _ = format!("{task}-run-dummy-task");
};
// call spawn
crate::async_runtime::spawn(dummy_task);
Expand Down
11 changes: 8 additions & 3 deletions crates/tauri/src/test/mod.rs
Original file line number Diff line number Diff line change
Expand Up @@ -64,7 +64,7 @@ use crate::{
};
use tauri_utils::{
acl::resolved::Resolved,
assets::{AssetKey, CspHash},
assets::{AssetKey, AssetsIter, CspHash},
config::{AppConfig, Config},
};

Expand All @@ -82,8 +82,13 @@ impl<R: Runtime> Assets<R> for NoopAsset {
None
}

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

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

0 comments on commit faa259b

Please sign in to comment.