Skip to content

Commit 0a7f3f7

Browse files
overload Base.rest (#844)
* overload Base.rest * Update test/abstractarray.jl Co-authored-by: Mateusz Baran <mateuszbaran89@gmail.com> * address review comments * Update src/abstractarray.jl Co-authored-by: Mateusz Baran <mateuszbaran89@gmail.com> Co-authored-by: Mateusz Baran <mateuszbaran89@gmail.com>
1 parent e5abc2f commit 0a7f3f7

File tree

2 files changed

+45
-0
lines changed

2 files changed

+45
-0
lines changed

src/abstractarray.jl

Lines changed: 9 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -286,3 +286,12 @@ end
286286
@inbounds return similar_type(a, promote_type(eltype(a), eltype(b)), Size($Snew))(tuple($(exprs...)))
287287
end
288288
end
289+
290+
if VERSION >= v"1.6.0-DEV.1334"
291+
# FIXME: This always assumes one-based linear indexing and that subtypes of StaticArray
292+
# don't overload iterate
293+
@inline function Base.rest(a::StaticArray{S}, (_, i) = (nothing, 0)) where {S}
294+
newlen = tuple_prod(S) - i
295+
return similar_type(typeof(a), Size(newlen))(Base.rest(Tuple(a), i + 1))
296+
end
297+
end

test/abstractarray.jl

Lines changed: 36 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -273,3 +273,39 @@ end
273273
@test @inferred(hcat(SA[1,2,3])) === SMatrix{3,1}(1,2,3)
274274
@test @inferred(hcat(SA[1 2 3])) === SA[1 2 3]
275275
end
276+
277+
@static if VERSION >= v"1.6.0-DEV.1334"
278+
@testset "Base.rest" begin
279+
x = SA[1, 2, 3]
280+
@test Base.rest(x) == x
281+
a, b... = x
282+
@test b == SA[2, 3]
283+
284+
x = SA[1 2; 3 4]
285+
@test Base.rest(x) == vec(x)
286+
a, b... = x
287+
@test b == SA[3, 2, 4]
288+
289+
a, b... = SA[1]
290+
@test b == []
291+
@test b isa SVector{0}
292+
293+
for (Vec, Mat) in [(MVector, MMatrix), (SizedVector, SizedMatrix)]
294+
x = Vec(1, 2, 3)
295+
@test Base.rest(x) == x
296+
@test pointer(Base.rest(x)) != pointer(x)
297+
a, b... = x
298+
@test b == Vec(2, 3)
299+
300+
x = Mat{2,2}(1, 2, 3, 4)
301+
@test Base.rest(x) == vec(x)
302+
@test pointer(Base.rest(x)) != pointer(x)
303+
a, b... = x
304+
@test b == Vec(2, 3, 4)
305+
306+
a, b... = Vec(1)
307+
@test b == []
308+
@test b isa Vec{0}
309+
end
310+
end
311+
end

0 commit comments

Comments
 (0)