Skip to content
New issue

Have a question about this project? Sign up for a free GitHub account to open an issue and contact its maintainers and the community.

By clicking “Sign up for GitHub”, you agree to our terms of service and privacy statement. We’ll occasionally send you account related emails.

Already on GitHub? Sign in to your account

feat(core): add AssetManager::iter #8288

Merged
merged 1 commit into from
Nov 23, 2023

Conversation

lucasfernog-crabnebula
Copy link
Contributor

What kind of change does this PR introduce?

  • Bugfix
  • Feature
  • Docs
  • New Binding issue #___
  • Code style update
  • Refactor
  • Build-related changes
  • Other, please describe:

Does this PR introduce a breaking change?

  • Yes, and the changes were approved in issue #___
  • No

Checklist

  • When resolving issues, they are referenced in the PR's title (e.g fix: remove a typo, closes #___, #___)
  • A change file is added if any packages will require a version bump due to this PR per the instructions in the readme.
  • I have added a convincing reason for adding this feature, if necessary

Other information

This new function allows users to iterate on all embedded assets, important if you want to AssetManager::get an asset you are not sure exists.

Sorry, something went wrong.

Verified

This commit was created on GitHub.com and signed with GitHub’s verified signature. The key has expired.
This new function allows users to iterate on all embedded assets, important if you want to AssetManager::get an asset you are not sure exists.
@lucasfernog-crabnebula lucasfernog-crabnebula requested a review from a team as a code owner November 22, 2023 15:27
@@ -163,6 +166,10 @@ impl Assets for EmbeddedAssets {
.map(|a| Cow::Owned(a.to_vec()))
}

fn iter(&self) -> Box<dyn Iterator<Item = (&&str, &&[u8])> + '_> {
Box::new(self.assets.into_iter())
Copy link
Member

@amrbashir amrbashir Nov 22, 2023

Choose a reason for hiding this comment

The reason will be displayed to describe this comment to others. Learn more.

Do we really need the box here? can't we implement InotIterator directly on the type and it construct a new type AssetEntries that is an iterator?

Copy link
Contributor Author

Choose a reason for hiding this comment

The reason will be displayed to describe this comment to others. Learn more.

it needs to be dyn because we're in the context of a trait.. so it needs Box too to be sized. Writing another type doesn't help this. It's also the solution used for csp_hashes.

Copy link
Member

@amrbashir amrbashir Nov 23, 2023

Choose a reason for hiding this comment

The reason will be displayed to describe this comment to others. Learn more.

If we return an AssetsEntries struct, we don't need to deal with dyn and can just implement Iterator on AssetsEntries

      fn iter<'a>(&'a self) -> AssetsEntries<'a, &'_ str, &'_ [u8]>;

or we can return phf::map::Entries directly

      fn iter<'a>(&'a self) -> phf::map::Entries<'a, &'_ str, &'_ [u8]>;

This should also be done for csp_hashes as well but to avoid breaking changes, we can delay that change to v2

Copy link
Contributor Author

Choose a reason for hiding this comment

The reason will be displayed to describe this comment to others. Learn more.

but then the AssetEntries need to hold the iterator anyway, unless we force the type to be a HashMap which isn't a good idea really for a trait

Copy link
Member

@amrbashir amrbashir Nov 23, 2023

Choose a reason for hiding this comment

The reason will be displayed to describe this comment to others. Learn more.

The only downside I see with this, is that AssetsEntries will need to use phf::map::Entries as a concrete type under the hood and now the trait Assets is not so generic and that makes me wonder, do we need this trait? seems like our usage of it, could be just replaced with the concrete EmbeddedAssets type

edit: GitHub didn't show me your previous message about the HashMap

Copy link
Contributor Author

Choose a reason for hiding this comment

The reason will be displayed to describe this comment to others. Learn more.

for tests it's nice to have this trait

but we can also put the author @chippers on the line

Copy link
Member

Choose a reason for hiding this comment

The reason will be displayed to describe this comment to others. Learn more.

this is not a blocker btw, just would be a nice improvement for v2 (unless the trait is really required)

Copy link
Member

Choose a reason for hiding this comment

The reason will be displayed to describe this comment to others. Learn more.

Late on seeing this @amrbashir @lucasfernog-crabnebula , the asset trait allows people to implement their own asset handling, such as if they want to fetch them from disk or network. it's one of the original reasons it became possible to change the items in a Context

@amrbashir amrbashir merged commit b3e53e7 into tauri-apps:1.x Nov 23, 2023
Sign up for free to join this conversation on GitHub. Already have an account? Sign in to comment
Labels
None yet
Projects
None yet
Development

Successfully merging this pull request may close these issues.

None yet

3 participants