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 array iterator #411

Conversation

fxamacker
Copy link
Member

@fxamacker fxamacker commented May 21, 2024

Closes #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.
  • ReadOnlyArrayIteratorMutationCallback are called (if provided and mutation occurs).

Callback function can be provided by using ReadOnlyIteratorWithMutationCallback.

func (a *Array) ReadOnlyIteratorWithMutationCallback(
	valueMutationCallback ReadOnlyArrayIteratorMutationCallback,
) (ArrayIterator, error)

Equivalent PR for readonly map iterator is:


  • 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 array 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.

Nice!

@fxamacker fxamacker changed the base branch from fxamacker/add-mutation-callback-to-readonly-map-iterator to feature/array-map-inlining May 21, 2024 20:47
@fxamacker fxamacker merged commit 688791c into feature/array-map-inlining May 21, 2024
3 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