Skip to content

RFC: setindex! for non-isbits MArrays #749

New issue

Have a question about this project? Sign up for a free GitHub account to open an issue and contact its maintainers and the community.

By clicking “Sign up for GitHub”, you agree to our terms of service and privacy statement. We’ll occasionally send you account related emails.

Already on GitHub? Sign in to your account

Open
wants to merge 3 commits into
base: master
Choose a base branch
from
Open
Show file tree
Hide file tree
Changes from all commits
Commits
File filter

Filter by extension

Filter by extension

Conversations
Failed to load comments.
Loading
Jump to
Jump to file
Failed to load files.
Loading
Diff view
Diff view
9 changes: 6 additions & 3 deletions src/MArray.jl
Original file line number Diff line number Diff line change
Expand Up @@ -94,9 +94,12 @@ end
if isbitstype(T)
GC.@preserve v unsafe_store!(Base.unsafe_convert(Ptr{T}, pointer_from_objref(v)), convert(T, val), i)
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.")
# This is inherently slow because we've got to rebuild a new Tuple to
# reassign to data.v.
#
# However it seems not possible to make fast, at least in julia 1.4
# TODO: Revisit if https://github.com/JuliaLang/julia/pull/34126 is merged
@inbounds v.data = setindex(v.data, val, i)
end

return val
Expand Down
21 changes: 18 additions & 3 deletions test/MArray.jl
Original file line number Diff line number Diff line change
Expand Up @@ -137,14 +137,29 @@
m[4] = 14
@test m.data === (11, 12, 13, 14)

m = @MMatrix [0 0; 0 0]
m[1] = Int8(11)
m[2] = Int8(12)
m[3] = Int8(13)
m[4] = Int8(14)
@test m.data === (11, 12, 13, 14)

v = @MVector [1.,2.,3.]
v[1] = Float16(11)
@test v.data === (11., 2., 3.)

v = @MArray [1,2,3]
@test_throws BoundsError setindex!(v, 4, -1)
mm = @MArray zeros(3,3,3,3)
@test_throws BoundsError setindex!(mm, 4, -1)
@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)
# setindex with non-bits eltype
m = fill("a", MMatrix{2,2, String})
m[1,1] = "b"
m[1,2] = "c"
@test m == ["b" "c";
"a" "a"]
end

@testset "promotion" begin
Expand Down
20 changes: 0 additions & 20 deletions test/MMatrix.jl
Original file line number Diff line number Diff line change
Expand Up @@ -89,24 +89,4 @@

@test length(m) === 4
end

@testset "setindex!" begin
m = @MMatrix [0 0; 0 0]
m[1] = 11
m[2] = 12
m[3] = 13
m[4] = 14
@test m.data === (11, 12, 13, 14)

m = @MMatrix [0 0; 0 0]
m[1] = Int8(11)
m[2] = Int8(12)
m[3] = Int8(13)
m[4] = Int8(14)
@test m.data === (11, 12, 13, 14)

# setindex with non-elbits type
m = MMatrix{2,2,String}(undef)
@test_throws ErrorException setindex!(m, "a", 1, 1)
end
end
19 changes: 0 additions & 19 deletions test/MVector.jl
Original file line number Diff line number Diff line change
Expand Up @@ -69,23 +69,4 @@

@test length(v) === 3
end

@testset "setindex!" begin
v = @MVector [1,2,3]
v[1] = 11
v[2] = 12
v[3] = 13
@test v.data === (11, 12, 13)

v = @MVector [1.,2.,3.]
v[1] = Float16(11)
@test v.data === (11., 2., 3.)

@test_throws BoundsError setindex!(v, 4., -1)
@test_throws BoundsError setindex!(v, 4., 4)

# setindex with non-elbits type
v = MVector{2,String}(undef)
@test_throws ErrorException setindex!(v, "a", 1)
end
end