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

Add UnsafeRichArray#at syntax extension #2888

Merged
merged 2 commits into from Oct 10, 2022

Conversation

armanbilge
Copy link
Member

This PR exposes an unsafe syntax extension for retrieving the Ptr backing an array, making official the widely-used trick of casting to a runtime.Array and accessing it like that.

The new syntax is then adopted within the codebase.

Comment on lines +127 to +130
/** Scala Native unsafe extensions to Arrays */
implicit class UnsafeRichArray[T](val value: Array[T]) extends AnyVal {
@inline def at(i: Int): Ptr[T] = value.asInstanceOf[runtime.Array[T]].at(i)
}
Copy link
Member Author

Choose a reason for hiding this comment

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

This is the new syntax, all other modifications are applying it.

Copy link
Contributor

@LeeTibbert LeeTibbert left a comment

Choose a reason for hiding this comment

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

LGTM Makes the code much clearer and reduces the hackery.

A HUGE thank you for getting this done.

@LeeTibbert
Copy link
Contributor

LeeTibbert commented Oct 7, 2022

I suspect that my pending PR #2877 has quite a few places which will
benefit from this PR. If it is agreeable to you, Arman, I will change them after this
merges.

This PR makes it much easier to get the idiom right. Should make maintenance
much easier all down the line.

@david-bouyssie
Copy link
Contributor

david-bouyssie commented Oct 8, 2022

Do you think the "at" method name is specific enough?
Why not "ptrAt"?

@WojciechMazur WojciechMazur merged commit d350595 into scala-native:0.4.x Oct 10, 2022
WojciechMazur pushed a commit to WojciechMazur/scala-native that referenced this pull request Oct 27, 2022
* Add `UnsafeRichArray#at` syntax extension

* Use it more
WojciechMazur pushed a commit that referenced this pull request Oct 27, 2022
* Add `UnsafeRichArray#at` syntax extension

* Use it more
ekrich pushed a commit to ekrich/scala-native that referenced this pull request Oct 27, 2022
* Add `UnsafeRichArray#at` syntax extension

* Use it more
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

4 participants