Skip to content

Commit

Permalink
Preserve ranges.
Browse files Browse the repository at this point in the history
  • Loading branch information
maleadt committed Oct 22, 2024
1 parent f33930a commit feb845c
Show file tree
Hide file tree
Showing 3 changed files with 33 additions and 1 deletion.
2 changes: 1 addition & 1 deletion Project.toml
Original file line number Diff line number Diff line change
@@ -1,6 +1,6 @@
name = "Adapt"
uuid = "79e6a3ab-5dfb-504d-930d-738a2a938a0e"
version = "4.0.4"
version = "4.1.0"

[deps]
LinearAlgebra = "37e2e46d-f89d-539d-b4ee-838fcccc9c8e"
Expand Down
20 changes: 20 additions & 0 deletions src/base.jl
Original file line number Diff line number Diff line change
Expand Up @@ -16,6 +16,7 @@ _adapt_tuple_structure(to, xs::Tuple) =
_adapt_tuple_structure(to, xs::Tuple{}) = ()
_adapt_tuple_structure(to, xs::Tuple{<:Any}) = (adapt(to, first(xs)), )


## Closures

# two things can be captured: static parameters, and actual values (fields)
Expand Down Expand Up @@ -63,3 +64,22 @@ adapt_structure(to, bc::Broadcasted{Style}) where Style =

adapt_structure(to, ex::Extruded) =
Extruded(adapt(to, ex.x), ex.keeps, ex.defaults)


## Ranges

adapt_structure(to, r::UnitRange) =
UnitRange(adapt(to, r.start), adapt(to, r.stop))

adapt_structure(to, r::Base.OneTo) = Base.OneTo(adapt(to, r.stop))

adapt_structure(to, r::StepRange) =
StepRange(adapt(to, r.start), adapt(to, r.step), adapt(to, r.stop))

adapt_structure(to, r::StepRangeLen) =
StepRangeLen(adapt(to, r.ref), adapt(to, r.step), r.len, r.offset)

adapt_structure(to, r::Base.Slice) = Base.Slice(adapt(to, r.indices))

adapt_structure(to, r::LinRange) =
LinRange(adapt(to, r.start), adapt(to, r.stop), r.len)
12 changes: 12 additions & 0 deletions test/runtests.jl
Original file line number Diff line number Diff line change
Expand Up @@ -232,3 +232,15 @@ end
#@test_adapt SArray [1,2,3] SArray{Tuple{3}}([1,2,3])
@test adapt(SArray, [1,2,3]) === SArray{Tuple{3}}([1,2,3])
end

@testset "Ranges" begin
# normally these fall back to `convert(Array, r)`, so we only need to test
# that the type matches

@test adapt(Array, 1:10) === 1:10
@test adapt(Array, Base.OneTo(10)) === Base.OneTo(10)
@test adapt(Array, 1:2:10) === 1:2:10
@test adapt(Array, 1.:2.:10.) === 1.:2.:10.
@test adapt(Array, Base.Slice(1:10)) === Base.Slice(1:10)
@test adapt(Array, LinRange(1,2,10)) === LinRange(1,2,10)
end

0 comments on commit feb845c

Please sign in to comment.