Skip to content

Commit

Permalink
implement Base.elsize for the strided arrays interface
Browse files Browse the repository at this point in the history
Makes `pointer(::FixedSizeArray, ::Int)` work.

Fixes #25
  • Loading branch information
nsajko committed Apr 24, 2024
1 parent 07af05e commit 419adae
Show file tree
Hide file tree
Showing 2 changed files with 20 additions and 0 deletions.
6 changes: 6 additions & 0 deletions src/FixedSizeArrays.jl
Original file line number Diff line number Diff line change
Expand Up @@ -76,6 +76,8 @@ end

# helper functions

parent_type(::Type{<:FixedSizeArray{T}}) where {T} = Memory{T}

memory_of(m::Memory) = m
memory_of(f::FixedSizeArray) = f.mem

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

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

# `elsize`: part of the strided arrays interface, used for `pointer`

Base.elsize(::Type{A}) where {A<:FixedSizeArray} = Base.elsize(parent_type(A))

end # module FixedSizeArrays
14 changes: 14 additions & 0 deletions test/runtests.jl
Original file line number Diff line number Diff line change
Expand Up @@ -237,4 +237,18 @@ end
@test M_mul M * M
end
end

@testset "`pointer`" begin
for elem_type (Int8, Int16, Int32, Int64)
for dim_count 0:4
siz = ntuple(Returns(2), dim_count)
arr = FixedSizeArray{elem_type, dim_count}(undef, siz)
@test pointer(arr) === pointer(arr, 1) === pointer(arr, 2) - sizeof(elem_type)
@test (@inferred pointer(arr)) isa Ptr{elem_type}
@test (@inferred pointer(arr, 1)) isa Ptr{elem_type}
@test iszero(allocated(pointer, arr))
@test iszero(allocated(pointer, arr, 1))
end
end
end
end

0 comments on commit 419adae

Please sign in to comment.