-
-
Notifications
You must be signed in to change notification settings - Fork 1.6k
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
Promoting a writable Slice
to a read-only one
#12133
Comments
I love the freeze method, but my only concern here is the deprecation of the parameter, because I can imagine future internal optimizations with the parameter form that I don't think can be done in the method form. x.map(read_only: true, &.itself) # Here you know within the map call that the result will be frozen
x.map(&.itself).freeze # Here you _don't_ know within the map call that the result will be frozen |
I think there is actually a somewhat dangerous aspect to it. Not necessarily unsafe, but you need to be aware of when using this. When a slice points to memory that is writable through some other means, the slice itself is read-only, but the data may still change. When Currently, you can only create a read-only If we allow arbitrarily creating a read-only slice as a read-only window to writable memory, we're weakening the assumptions you can make about I think there are good reasons for each use case - read-only memory as well as a read-only window to writable memory in order to protect it from write access where it's not meant to be writable. |
I thought about this a bit more, and my conclusion is that to create a read-only window one could do it generically: module Indexable(T)
record View(I, T), indexable : I do
include Indexable(T)
delegate unsafe_fetch, size, to: @indexable
def view
self
end
end
# it is tempting to erase the `I` type above, but the following would produce a
# recursive struct because `@indexable`'s type includes `View(T)` itself
# it also forces method dispatch on `@indexable` where often unnecessary
# record View(T), indexable : Indexable(T)
def view
View(self, T).new(self)
end
end The above works also on types whose elements aren't laid out contiguously in memory, like By that reasoning, there is actually no need to use owning |
There is surprisingly no safe way to produce a read-only
Slice
from a writablex : Slice
that refers to the same elements:There is no reason this would be considered unsafe. I propose wrapping the first expression into a method:
If this sounds familiar, the name
freeze
comes from Ruby'sObject#freeze
.With
#freeze
we can simplify a few other methods that take aread_only
argument:So I suggest we deprecate the
read_only
parameter in the above methods too. The ones inSlice
's constructors can probably stay, although strictly speaking, only#initialize
needs this parameter and the other.new
overloads don't.The text was updated successfully, but these errors were encountered: