Skip to content

Commit 53a19c3

Browse files
authored
Merge pull request #554 from JuliaArrays/teh/soneto
Support construction/conversion of UnitRange->SOneTo
2 parents d14604c + abc5564 commit 53a19c3

File tree

2 files changed

+12
-1
lines changed

2 files changed

+12
-1
lines changed

src/SOneTo.jl

Lines changed: 7 additions & 1 deletion
Original file line numberDiff line numberDiff line change
@@ -8,12 +8,18 @@ struct SOneTo{n} <: AbstractUnitRange{Int}
88
end
99

1010
SOneTo(n::Int) = SOneTo{n}()
11+
function SOneTo{n}(r::AbstractUnitRange) where n
12+
((first(r) == 1) & (last(r) == n)) && return SOneTo{n}()
13+
14+
errmsg(r) = throw(DimensionMismatch("$r is inconsistent with SOneTo{$n}")) # avoid GC frame
15+
errmsg(r)
16+
end
1117

1218
Base.axes(s::SOneTo) = (s,)
1319
Base.size(s::SOneTo) = (length(s),)
1420
Base.length(s::SOneTo{n}) where {n} = n
1521

16-
function Base.getindex(s::SOneTo, i::Int)
22+
function Base.getindex(s::SOneTo, i::Int)
1723
@boundscheck checkbounds(s, i)
1824
return i
1925
end

test/core.jl

Lines changed: 5 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -189,5 +189,10 @@
189189
b = StaticArrays.SOneTo{2}()
190190
@test @inferred(promote(a, b)) === (a, Base.OneTo(2))
191191
@test @inferred(promote(b, a)) === (Base.OneTo(2), a)
192+
193+
@test StaticArrays.SOneTo{2}(1:2) === StaticArrays.SOneTo{2}()
194+
@test convert(StaticArrays.SOneTo{2}, 1:2) === StaticArrays.SOneTo{2}()
195+
@test_throws DimensionMismatch StaticArrays.SOneTo{2}(1:3)
196+
@test_throws DimensionMismatch StaticArrays.SOneTo{2}(1:1)
192197
end
193198
end

0 commit comments

Comments
 (0)