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

Check mutation of elements from readonly map iterator #410

Conversation

fxamacker
Copy link
Member

@fxamacker fxamacker commented May 21, 2024

Updates #409

Projects using atree might unintentionally mutate elements returned by readonly iterators.

As always, mutation of elements from readonly iterators are not guaranteed to persist.

This PR returns error from mutation functions, and supports callback functions that allow projects using atree to log or debug such mutations with extra context, panic, etc.

If elements from readonly iterators are mutated:

  • those changes are not guaranteed to persist.
  • mutation functions of child containers return ReadOnlyIteratorElementMutationError.
  • ReadOnlyMapIteratorMutationCallback are called (if provided and mutation occurs).

Two callback functions can be provided by using ReadOnlyIteratorWithMutationCallback.

func (m *OrderedMap) ReadOnlyIteratorWithMutationCallback(
	keyMutatinCallback ReadOnlyMapIteratorMutationCallback,
	valueMutationCallback ReadOnlyMapIteratorMutationCallback,
) (MapIterator, error) 

  • Targeted PR against main branch
  • Linked to Github issue with discussion and accepted design OR link to spec that describes this work
  • Code follows the standards mentioned here
  • Updated relevant documentation
  • Re-reviewed Files changed in the Github PR explorer
  • Added appropriate labels

This commit returns ReadOnlyIteratorElementMutationError when
elements of readonly map iterator are mutated. Also, a
callback can be provided by the caller to log or debug
such mutations with more context.

As always, mutation of elements from readonly iterators are
not guaranteed to persist.

Instead of relying on other projects not to mutate readonly elements,
this commit returns ReadOnlyIteratorElementMutationError when
elements from readonly iterators are mutated.

This commit also adds readonly iterator functions that receive
mutation callbacks.  Callbacks are useful for logging, etc. with
more context when mutation occurs.  Mutation handling is the same
with or without callbacks.  If needed, other projects using atree
can choose to panic in the callback when mutation is detected.

If elements from readonly iterators are mutated:
- those changes are not guaranteed to persist.
- mutation functions of child containers return
  ReadOnlyIteratorElementMutationError.
- ReadOnlyMapIteratorMutationCallback are called if provided
@fxamacker fxamacker added the enhancement New feature or request label May 21, 2024
@fxamacker fxamacker self-assigned this May 21, 2024
Copy link
Member

@turbolent turbolent left a comment

Choose a reason for hiding this comment

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

Great work, thanks for adding this! I'll make the usage in Cadence more robust 👍

@turbolent turbolent requested review from a team May 21, 2024 16:54
@fxamacker fxamacker merged commit bbd27a3 into feature/array-map-inlining May 21, 2024
7 checks passed
Sign up for free to join this conversation on GitHub. Already have an account? Sign in to comment
Labels
enhancement New feature or request
Projects
None yet
Development

Successfully merging this pull request may close these issues.

2 participants