Skip to content

Commit 6215ede

Browse files
Merge pull request #57 from chriselrod/sizedrangeinterfacestaticarrays
Add known_(first/last) methods for StaticArrays.SOneTo.
2 parents 61a3f73 + 7261297 commit 6215ede

File tree

4 files changed

+10
-2
lines changed

4 files changed

+10
-2
lines changed

Project.toml

Lines changed: 1 addition & 1 deletion
Original file line numberDiff line numberDiff line change
@@ -1,6 +1,6 @@
11
name = "ArrayInterface"
22
uuid = "4fba245c-0d91-5ea0-9b3e-6abc04ee57a9"
3-
version = "2.10.0"
3+
version = "2.11.0"
44

55
[deps]
66
LinearAlgebra = "37e2e46d-f89d-539d-b4ee-838fcccc9c8e"

README.md

Lines changed: 2 additions & 1 deletion
Original file line numberDiff line numberDiff line change
@@ -103,7 +103,8 @@ returns `one(Int)`.
103103
## known_last(::Type{T})
104104

105105
If `last` of instances of type `T` are known at compile time, return that
106-
last element. Otherwise, return `nothing`.
106+
last element. Otherwise, return `nothing`. For example,
107+
`known_last(StaticArrays.SOneTo{4})` returns 4.
107108

108109
## known_step(::Type{T})
109110

src/ArrayInterface.jl

Lines changed: 5 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -509,6 +509,8 @@ If `last` of an instance of type `T` is known at compile time, return it.
509509
Otherwise, return `nothing`.
510510
511511
@test isnothing(known_last(typeof(1:4)))
512+
using StaticArrays
513+
@test known_last(typeof(SOneTo(4))) == 4
512514
"""
513515
known_last(::Any) = nothing
514516
"""
@@ -554,6 +556,9 @@ function __init__()
554556
StaticArrays.SArray{S}(y)
555557
end
556558

559+
known_first(::Type{<:StaticArrays.SOneTo}) = 1
560+
known_last(::Type{StaticArrays.SOneTo{N}}) where {N} = N
561+
557562
@require Adapt="79e6a3ab-5dfb-504d-930d-738a2a938a0e" begin
558563
function Adapt.adapt_storage(::Type{<:StaticArrays.SArray{S}},xs::Array) where S
559564
StaticArrays.SArray{S}(xs)

test/runtests.jl

Lines changed: 2 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -8,6 +8,8 @@ using StaticArrays
88
@test ArrayInterface.ismutable(@MVector [1,2,3]) == true
99
@test ArrayInterface.ismutable(1:10) == false
1010
@test ArrayInterface.ismutable((0.1,1.0)) == false
11+
@test isone(ArrayInterface.known_first(typeof(StaticArrays.SOneTo(7))))
12+
@test ArrayInterface.known_last(typeof(StaticArrays.SOneTo(7))) == 7
1113

1214
using LinearAlgebra, SparseArrays
1315

0 commit comments

Comments
 (0)