Skip to content

Commit df1976d

Browse files
authored
Define parent(::GenericMemoryRef) (#55325)
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.
1 parent 2179f14 commit df1976d

File tree

2 files changed

+8
-0
lines changed

2 files changed

+8
-0
lines changed

base/genericmemory.jl

Lines changed: 2 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -71,6 +71,8 @@ size(a::GenericMemory) = (length(a),)
7171

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

74+
parent(ref::GenericMemoryRef) = ref.mem
75+
7476
pointer(mem::GenericMemoryRef) = unsafe_convert(Ptr{Cvoid}, mem) # no bounds check, even for empty array
7577

7678
_unsetindex!(A::Memory, i::Int) = (@_propagate_inbounds_meta; _unsetindex!(memoryref(A, i)); A)

test/arrayops.jl

Lines changed: 6 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -3246,3 +3246,9 @@ end
32463246
@test size(mem, 2) == 1
32473247
@test size(mem, 0x2) == 1
32483248
end
3249+
3250+
@testset "MemoryRef" begin
3251+
mem = Memory{Float32}(undef, 3)
3252+
ref = memoryref(mem, 2)
3253+
@test parent(ref) === mem
3254+
end

0 commit comments

Comments
 (0)