Skip to content

Commit 11ec063

Browse files
authored
Merge pull request #336 from JuliaArrays/cjf/fix-copying-constructor
Ensure static array constructors create copies
2 parents 0e9ce69 + 489b9c7 commit 11ec063

File tree

8 files changed

+21
-3
lines changed

8 files changed

+21
-3
lines changed

src/MArray.jl

Lines changed: 1 addition & 1 deletion
Original file line numberDiff line numberDiff line change
@@ -71,7 +71,7 @@ end
7171
end
7272
end
7373

74-
@inline MArray(a::StaticArray) = MArray{size_tuple(typeof(a))}(Tuple(a))
74+
@inline MArray(a::StaticArray) = MArray{size_tuple(Size(a))}(Tuple(a))
7575

7676
# Simplified show for the type
7777
show(io::IO, ::Type{MArray{S, T, N}}) where {S, T, N} = print(io, "MArray{$S,$T,$N}")

src/SArray.jl

Lines changed: 1 addition & 1 deletion
Original file line numberDiff line numberDiff line change
@@ -50,7 +50,7 @@ end
5050
end
5151
end
5252

53-
@inline SArray(a::StaticArray) = SArray{size_tuple(a)}(Tuple(a)) # TODO fixme
53+
@inline SArray(a::StaticArray) = SArray{size_tuple(Size(a))}(Tuple(a))
5454

5555
# Simplified show for the type
5656
show(io::IO, ::Type{SArray{S, T, N}}) where {S, T, N} = print(io, "SArray{$S,$T,$N}")

src/convert.jl

Lines changed: 2 additions & 1 deletion
Original file line numberDiff line numberDiff line change
@@ -1,7 +1,8 @@
11
(::Type{SA})(x::Tuple{Tuple{Tuple{<:Tuple}}}) where {SA <: StaticArray} = error("No precise constructor for $SA found. Length of input was $(length(x[1][1][1])).")
22

33
@inline (::Type{SA})(x...) where {SA <: StaticArray} = SA(x)
4-
@inline (::Type{SA})(a::AbstractArray) where {SA <: StaticArray} = convert(SA, a) # Is this a good idea?
4+
@inline (::Type{SA})(a::StaticArray) where {SA<:StaticArray} = SA(Tuple(a))
5+
@inline (::Type{SA})(a::AbstractArray) where {SA <: StaticArray} = convert(SA, a)
56

67
# this covers most conversions and "statically-sized reshapes"
78
@inline convert(::Type{SA}, sa::StaticArray) where {SA<:StaticArray} = SA(Tuple(sa))

src/traits.jl

Lines changed: 2 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -86,6 +86,8 @@ Length(::Type{SA}) where {SA <: StaticArray} = Length(Size(SA))
8686

8787
@pure @inline Base.sub2ind(::Size{S}, x::Int...) where {S} = sub2ind(S, x...)
8888

89+
@pure size_tuple(::Size{S}) where {S} = Tuple{S...}
90+
8991
# Some @pure convenience functions for `Length`
9092
@pure get(::Length{L}) where {L} = L
9193

test/MArray.jl

Lines changed: 5 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -27,6 +27,11 @@
2727
@test MArray{Tuple{2,2},Int}((1,2,3,4)).data === (1,2,3,4)
2828
@test MArray{Tuple{2,2}}((1,2,3,4)).data === (1,2,3,4)
2929

30+
@test MArray(SVector(1,2)) isa MArray{Tuple{2}}
31+
# Constructors should create a copy (#335)
32+
v = MArray{Tuple{2}}(1,2)
33+
@test MArray(v) !== v && MArray(v) == v
34+
3035
@test ((@MArray [1])::MArray{Tuple{1}}).data === (1,)
3136
@test ((@MArray [1,2])::MArray{Tuple{2}}).data === (1,2)
3237
@test ((@MArray Float64[1,2,3])::MArray{Tuple{3}}).data === (1.0, 2.0, 3.0)

test/MVector.jl

Lines changed: 4 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -16,6 +16,10 @@
1616
@test MVector((1,)).data === (1,)
1717
@test MVector((1.0,)).data === (1.0,)
1818

19+
# Constructors should create a copy (#335)
20+
v = MVector(1,2)
21+
@test MVector(v) !== v && MVector(v) == v
22+
1923
@test ((@MVector [1.0])::MVector{1}).data === (1.0,)
2024
@test ((@MVector [1, 2, 3])::MVector{3}).data === (1, 2, 3)
2125
@test ((@MVector Float64[1,2,3])::MVector{3}).data === (1.0, 2.0, 3.0)

test/SArray.jl

Lines changed: 2 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -25,6 +25,8 @@
2525
@test SArray{Tuple{2,2},Int}((1,2,3,4)).data === (1,2,3,4)
2626
@test SArray{Tuple{2,2}}((1,2,3,4)).data === (1,2,3,4)
2727

28+
@test SArray(SArray{Tuple{2}}(1,2)) === SArray{Tuple{2}}(1,2)
29+
2830
@test ((@SArray [1])::SArray{Tuple{1}}).data === (1,)
2931
@test ((@SArray [1,2])::SArray{Tuple{2}}).data === (1,2)
3032
@test ((@SArray Float64[1,2,3])::SArray{Tuple{3}}).data === (1.0, 2.0, 3.0)

test/core.jl

Lines changed: 4 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -105,6 +105,10 @@
105105
@test @inferred(convert(SArray{Tuple{1},Float64}, ma_int)) === sa_float
106106
@test @inferred(convert(SArray{Tuple{1},Float64,1}, ma_int)) === sa_float
107107
@test @inferred(convert(SArray{Tuple{1},Float64,1,1}, ma_int)) === sa_float
108+
109+
# Self-conversion returns the matrix itself
110+
@test convert(MArray, ma_int) === ma_int
111+
@test convert(MArray, ma_float) === ma_float
108112
end
109113

110114
@testset "AbstractArray conversion" begin

0 commit comments

Comments
 (0)