File tree Expand file tree Collapse file tree 4 files changed +30
-0
lines changed Expand file tree Collapse file tree 4 files changed +30
-0
lines changed Original file line number Diff line number Diff line change @@ -65,6 +65,27 @@ IndexStyle(::Type{<:GenericMemory}) = IndexLinear()
6565
6666parent (ref:: GenericMemoryRef ) = ref. mem
6767
68+ """
69+ memoryindex(ref::GenericMemoryRef)::Int
70+
71+ Get the 1-based index of `ref` in its `GenericMemory`.
72+
73+ # Examples
74+ ```jldoctest
75+ julia> mem = Memory{String}(undef, 10);
76+
77+ julia> ref = Base.memoryindex(memoryref(mem, 3))
78+ 3
79+
80+ julia> Base.memoryindex(memoryref(Memory{Nothing}(undef, 10), 8))
81+ 8
82+ ```
83+
84+ !!! compat "Julia 1.13"
85+ This function requires at least Julia 1.13.
86+ """
87+ memoryindex (ref:: GenericMemoryRef ) = memoryrefoffset (ref)
88+
6889pointer (mem:: GenericMemoryRef ) = unsafe_convert (Ptr{Cvoid}, mem) # no bounds check, even for empty array
6990
7091_unsetindex! (A:: Memory , i:: Int ) = (@_propagate_inbounds_meta ; _unsetindex! (memoryref (A, i)); A)
Original file line number Diff line number Diff line change 3434# arrays
3535 has_offset_axes,
3636 require_one_based_indexing,
37+ memoryindex,
3738
3839# collections
3940 IteratorEltype,
Original file line number Diff line number Diff line change @@ -34,6 +34,7 @@ Base.GenericMemory
3434Base.Memory
3535Base.Memory(::UndefInitializer, ::Int)
3636Base.memoryref
37+ Base.memoryindex
3738Base.Slices
3839Base.RowSlices
3940Base.ColumnSlices
Original file line number Diff line number Diff line change @@ -3403,4 +3403,11 @@ end
34033403 mem = Memory {Float32} (undef, 3 )
34043404 ref = memoryref (mem, 2 )
34053405 @test parent (ref) === mem
3406+ @test Base. memoryindex (ref) === 2
3407+
3408+ # Test for zero-sized structs
3409+ mem = Memory {Nothing} (undef, 10 )
3410+ ref = memoryref (mem, 8 )
3411+ @test parent (ref) === mem
3412+ @test Base. memoryindex (ref) === 8
34063413end
You can’t perform that action at this time.
0 commit comments