Skip to content

Commit 4392680

Browse files
authored
implement Base.elsize for the strided arrays interface (#34)
Makes `pointer(::FixedSizeArray, ::Int)` work. Fixes #25
1 parent 07af05e commit 4392680

File tree

2 files changed

+20
-0
lines changed

2 files changed

+20
-0
lines changed

src/FixedSizeArrays.jl

Lines changed: 6 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -76,6 +76,8 @@ end
7676

7777
# helper functions
7878

79+
parent_type(::Type{<:FixedSizeArray{T}}) where {T} = Memory{T}
80+
7981
memory_of(m::Memory) = m
8082
memory_of(f::FixedSizeArray) = f.mem
8183

@@ -143,4 +145,8 @@ Base.@propagate_inbounds Base.copyto!(dst::Memory , src::FixedSizeArray)
143145

144146
Base.unsafe_convert(::Type{Ptr{T}}, a::FixedSizeArray{T}) where {T} = Base.unsafe_convert(Ptr{T}, a.mem)
145147

148+
# `elsize`: part of the strided arrays interface, used for `pointer`
149+
150+
Base.elsize(::Type{A}) where {A<:FixedSizeArray} = Base.elsize(parent_type(A))
151+
146152
end # module FixedSizeArrays

test/runtests.jl

Lines changed: 14 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -237,4 +237,18 @@ end
237237
@test M_mul M * M
238238
end
239239
end
240+
241+
@testset "`pointer`" begin
242+
for elem_type (Int8, Int16, Int32, Int64)
243+
for dim_count 0:4
244+
siz = ntuple(Returns(2), dim_count)
245+
arr = FixedSizeArray{elem_type, dim_count}(undef, siz)
246+
@test pointer(arr) === pointer(arr, 1) === pointer(arr, 2) - sizeof(elem_type)
247+
@test (@inferred pointer(arr)) isa Ptr{elem_type}
248+
@test (@inferred pointer(arr, 1)) isa Ptr{elem_type}
249+
@test iszero(allocated(pointer, arr))
250+
@test iszero(allocated(pointer, arr, 1))
251+
end
252+
end
253+
end
240254
end

0 commit comments

Comments
 (0)