Skip to content

Commit 28ac7e2

Browse files
committed
specialize axes(S, dim) to return SOneTo
1 parent 5d0802e commit 28ac7e2

File tree

2 files changed

+5
-0
lines changed

2 files changed

+5
-0
lines changed

src/abstractarray.jl

Lines changed: 1 addition & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -12,6 +12,7 @@ Base.axes(s::StaticArray) = _axes(Size(s))
1212
@pure function _axes(::Size{sizes}) where {sizes}
1313
map(SOneTo, sizes)
1414
end
15+
Base.axes(s::StaticArray, d) = d <= ndims(s) ? _axes(Size(s))[d] : SOneTo{1}()
1516
Base.axes(rv::Adjoint{<:Any,<:StaticVector}) = (SOneTo(1), axes(rv.parent)...)
1617
Base.axes(rv::Transpose{<:Any,<:StaticVector}) = (SOneTo(1), axes(rv.parent)...)
1718

test/abstractarray.jl

Lines changed: 4 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -119,6 +119,10 @@ using StaticArrays, Test, LinearAlgebra
119119
# IndexLinear
120120
@test reshape(view(ones(4, 4), 1, 1:4), Size(4, 1)) == SMatrix{4,1}(ones(4, 1))
121121
@test_throws DimensionMismatch reshape(view(ones(4,4), 1:4, 1:2), Size(5, 2))
122+
123+
s = SA[1,2];
124+
s2 = @inferred reshape(s, axes(s,1), axes(s,2))
125+
@test s2 isa StaticArray
122126
end
123127

124128
@testset "copy" begin

0 commit comments

Comments
 (0)