Skip to content

Allow transposing stores with non-Copy lenses - #6004

Open
RaphaelFakhri wants to merge 1 commit into
DioxusLabs:mainfrom
RaphaelFakhri:fix-store-transpose-non-copy-lens
Open

RaphaelFakhri wants to merge 1 commit into
DioxusLabs:mainfrom
RaphaelFakhri:fix-store-transpose-non-copy-lens

Conversation

@RaphaelFakhri

Copy link
Copy Markdown

Fixes #6001.

Problem

The transpose() method that #[derive(Store)] generates requires Self: Copy, for both structs and enums. A store of a HashMap or BTreeMap value uses a GetWrite<K, _> lens that holds the key. That lens is Clone but not Copy when the key is not Copy (for example, String), so calling transpose() on a map value store fails to compile:

the trait bound `GetWrite<String, MappedMutSignal<..>>: Copy` is not satisfied

The same type works when it lives in a Vec, because the index lens is Copy.

Solution

  • Relax the bound on the generated transpose() from Self: Copy to Self: Clone. Copy implies Clone, so every call that compiles today still compiles.
  • In the generated bodies, clone the store explicitly where it was copied implicitly: once per field for structs, and once per field selector inside each enum match arm (the arm runs while the store is borrowed for the variant check).
  • The single-field as_variant() accessors keep using self directly, so they gain no new bound.

Testing

  • Adds derive_transpose_non_copy_lens to packages/stores/tests/macro.rs. It transposes struct and enum values from HashMap<String, _> store fields.
  • Without the fix, cargo test -p dioxus-stores --test macro fails with two E0277 errors (struct and enum transpose). With the fix, it passes.
  • cargo test -p dioxus-stores -p dioxus-stores-macro passes (including the trybuild UI tests).
  • cargo fmt --check and cargo clippy --all-targets for both crates are clean.

This branch has not been deployed

No deployments
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.

Enum values in HashMap Store field cannot be transposed

1 participant