From 55c1af1a632602d8d5a4115cd301cdb49b6d5e02 Mon Sep 17 00:00:00 2001 From: MasonProtter Date: Sun, 2 Apr 2023 21:49:34 -0600 Subject: [PATCH 1/3] allow mutation of non-isbits eltypes --- src/MArray.jl | 4 ++-- test/MArray.jl | 6 ++++-- 2 files changed, 6 insertions(+), 4 deletions(-) diff --git a/src/MArray.jl b/src/MArray.jl index 9d8e87f3..3195025e 100644 --- a/src/MArray.jl +++ b/src/MArray.jl @@ -36,9 +36,9 @@ end else # This one is unsafe (#27) # unsafe_store!(Base.unsafe_convert(Ptr{Ptr{Nothing}}, pointer_from_objref(v.data)), pointer_from_objref(val), i) - error("setindex!() with non-isbitstype eltype is not supported by StaticArrays. Consider using SizedArray.") + # error("setindex!() with non-isbitstype eltype is not supported by StaticArrays. Consider using SizedArray.") + setfield!(v, :data, Base.setindex(getfield(v, :data), val, i)) end - return v end diff --git a/test/MArray.jl b/test/MArray.jl index 79022dd9..e8a865cc 100644 --- a/test/MArray.jl +++ b/test/MArray.jl @@ -209,8 +209,10 @@ @test_throws BoundsError setindex!(mm, 4, 82) # setindex with non-elbits type - m = MArray{Tuple{2,2,2}, String}(undef) - @test_throws ErrorException setindex!(m, "a", 1, 1, 1) + m = MArray{Tuple{2,2,2}, String}(("b" for _ ∈ 1:2^3)) + @test setindex!(m, "a", 1, 1, 1) == MArray{Tuple{2,2,2}, String}(("a", ("b" for _ ∈ 1:2^3-1)...,)) + @test m[1,1,1] == "a" + @test m[1,1,2] == "b" end @testset "promotion" begin From 97b1c08561ca82699127d1e50761f4ed25d9b063 Mon Sep 17 00:00:00 2001 From: MasonProtter Date: Sun, 2 Apr 2023 21:52:38 -0600 Subject: [PATCH 2/3] add in a `convert` for good measure --- src/MArray.jl | 4 ++-- 1 file changed, 2 insertions(+), 2 deletions(-) diff --git a/src/MArray.jl b/src/MArray.jl index 3195025e..8cd2b21a 100644 --- a/src/MArray.jl +++ b/src/MArray.jl @@ -27,7 +27,7 @@ end getfield(v,:data)[i] end -@propagate_inbounds function setindex!(v::MArray, val, i::Int) +@propagate_inbounds function setindex!(v::MArray, val, i::Int) @boundscheck checkbounds(v,i) T = eltype(v) @@ -37,7 +37,7 @@ end # This one is unsafe (#27) # unsafe_store!(Base.unsafe_convert(Ptr{Ptr{Nothing}}, pointer_from_objref(v.data)), pointer_from_objref(val), i) # error("setindex!() with non-isbitstype eltype is not supported by StaticArrays. Consider using SizedArray.") - setfield!(v, :data, Base.setindex(getfield(v, :data), val, i)) + setfield!(v, :data, Base.setindex(getfield(v, :data), convert(eltype(v), val), i)) end return v end From 1bfb0d0bcb4beb515cde1b0a84613595cb693281 Mon Sep 17 00:00:00 2001 From: MasonProtter Date: Mon, 3 Apr 2023 10:04:29 -0600 Subject: [PATCH 3/3] try to fix test error --- src/MArray.jl | 2 +- test/MArray.jl | 4 ++-- 2 files changed, 3 insertions(+), 3 deletions(-) diff --git a/src/MArray.jl b/src/MArray.jl index 8cd2b21a..9f56a92a 100644 --- a/src/MArray.jl +++ b/src/MArray.jl @@ -27,7 +27,7 @@ end getfield(v,:data)[i] end -@propagate_inbounds function setindex!(v::MArray, val, i::Int) +@propagate_inbounds function setindex!(v::MArray, val, i::Int) @boundscheck checkbounds(v,i) T = eltype(v) diff --git a/test/MArray.jl b/test/MArray.jl index e8a865cc..bc08ddf4 100644 --- a/test/MArray.jl +++ b/test/MArray.jl @@ -209,8 +209,8 @@ @test_throws BoundsError setindex!(mm, 4, 82) # setindex with non-elbits type - m = MArray{Tuple{2,2,2}, String}(("b" for _ ∈ 1:2^3)) - @test setindex!(m, "a", 1, 1, 1) == MArray{Tuple{2,2,2}, String}(("a", ("b" for _ ∈ 1:2^3-1)...,)) + m = MArray{Tuple{2,2,2}, String}(ntuple(_ -> "b", 2^3)) + @test setindex!(m, "a", 1, 1, 1) == MArray{Tuple{2,2,2}, String}(ntuple(i -> i == 1 ? "a" : "b", 2^3)) @test m[1,1,1] == "a" @test m[1,1,2] == "b" end