From 22a517cd1280de80ebe5f2799788a609c7ef9766 Mon Sep 17 00:00:00 2001 From: Jakob Nybo Nissen Date: Thu, 1 Aug 2024 11:28:30 +0200 Subject: [PATCH] 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. --- base/genericmemory.jl | 2 ++ test/arrayops.jl | 6 ++++++ 2 files changed, 8 insertions(+) diff --git a/base/genericmemory.jl b/base/genericmemory.jl index 6df6c880f74a8..6537839320206 100644 --- a/base/genericmemory.jl +++ b/base/genericmemory.jl @@ -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) diff --git a/test/arrayops.jl b/test/arrayops.jl index e407ad7233bc2..c904a52e8cad9 100644 --- a/test/arrayops.jl +++ b/test/arrayops.jl @@ -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