Skip to content

Commit

Permalink
Define parent(::GenericMemoryRef) (JuliaLang#55325)
Browse files Browse the repository at this point in the history
It would be nice to have function to extract the `GenericMemory`
underlying the `GenericMemoryRef`, without accessing its undocumented
field `.mem`. I'm not sure `parent` is the right function for this, but
it's the best I could think of.
  • Loading branch information
jakobnissen authored and lazarusA committed Aug 17, 2024
1 parent 177437b commit 22a517c
Show file tree
Hide file tree
Showing 2 changed files with 8 additions and 0 deletions.
2 changes: 2 additions & 0 deletions base/genericmemory.jl
Original file line number Diff line number Diff line change
Expand Up @@ -71,6 +71,8 @@ size(a::GenericMemory) = (length(a),)

IndexStyle(::Type{<:GenericMemory}) = IndexLinear()

parent(ref::GenericMemoryRef) = ref.mem

pointer(mem::GenericMemoryRef) = unsafe_convert(Ptr{Cvoid}, mem) # no bounds check, even for empty array

_unsetindex!(A::Memory, i::Int) = (@_propagate_inbounds_meta; _unsetindex!(memoryref(A, i)); A)
Expand Down
6 changes: 6 additions & 0 deletions test/arrayops.jl
Original file line number Diff line number Diff line change
Expand Up @@ -3246,3 +3246,9 @@ end
@test size(mem, 2) == 1
@test size(mem, 0x2) == 1
end

@testset "MemoryRef" begin
mem = Memory{Float32}(undef, 3)
ref = memoryref(mem, 2)
@test parent(ref) === mem
end

0 comments on commit 22a517c

Please sign in to comment.