diff --git a/Project.toml b/Project.toml index da2ed6be..0575ed08 100644 --- a/Project.toml +++ b/Project.toml @@ -1,6 +1,6 @@ name = "StaticArrays" uuid = "90137ffa-7385-5640-81b9-e52037218182" -version = "1.1.3" +version = "1.2.0" [deps] LinearAlgebra = "37e2e46d-f89d-539d-b4ee-838fcccc9c8e" diff --git a/src/abstractarray.jl b/src/abstractarray.jl index 40221f8e..ee890a6e 100644 --- a/src/abstractarray.jl +++ b/src/abstractarray.jl @@ -295,3 +295,13 @@ if VERSION >= v"1.6.0-DEV.1334" return similar_type(typeof(a), Size(newlen))(Base.rest(Tuple(a), i + 1)) end end + +# SArrays may avoid the SubArray wrapper and consequently an additional level of indirection +# The output may use the broadcasting machinery defined for StaticArrays (see issue #892) +# wrap elements in Scalar to be consistent with 0D views +_maybewrapscalar(S::SArray{<:Any,T}, r::T) where {T} = Scalar{T}(r) +_maybewrapscalar(S, r) = r +function Base.view(S::SArray, I::Union{Colon, Integer, SOneTo, StaticArray{<:Tuple, Int}, CartesianIndex}...) + V = getindex(S, I...) + _maybewrapscalar(S, V) +end diff --git a/test/SArray.jl b/test/SArray.jl index f2d19fc9..641a5786 100644 --- a/test/SArray.jl +++ b/test/SArray.jl @@ -139,6 +139,16 @@ @test_throws Exception m[1] = 1 @test Base.dataids(m) === () + + @test (@inferred view(m, :, :)) === m + @test (@inferred view(m, :, 1)) === @SArray [11, 12] + @test (@inferred view(m, SVector{2,Int}(1,2), 1)) === @SArray [11, 12] + @test (@inferred view(m, SMatrix{2,2,Int}(1,2,3,4))) === m + @test (@inferred view(m, SOneTo(2), 1)) === @SArray [11, 12] + @test (@inferred view(m, 1, 1)) === Scalar(m[1, 1]) + @test (@inferred view(m, CartesianIndex(1, 1))) === Scalar(m[1, 1]) + @test (@inferred view(m, CartesianIndex(1, 1, 1))) === Scalar(m[1, 1]) + @test (@inferred view(m, 1, 1, CartesianIndex(1))) === Scalar(m[1, 1]) end @testset "promotion" begin diff --git a/test/SVector.jl b/test/SVector.jl index 33a04a53..5e9310d7 100644 --- a/test/SVector.jl +++ b/test/SVector.jl @@ -82,6 +82,14 @@ @test length(v) === 3 @test_throws Exception v[1] = 1 + + @test (@inferred view(v, :)) === v + @test (@inferred view(v, 1)) === Scalar(v[1]) + @test (@inferred view(v, CartesianIndex(1))) === Scalar(v[1]) + @test (@inferred view(v, CartesianIndex(1,1))) === Scalar(v[1]) + @test (@inferred view(v, 1, CartesianIndex(1))) === Scalar(v[1]) + @test (@inferred view(v, SVector{2,Int}(1,2))) === @SArray [11, 12] + @test (@inferred view(v, SOneTo(2))) === @SArray [11, 12] end @testset "CartesianIndex" begin