Skip to content
Merged
Show file tree
Hide file tree
Changes from all commits
Commits
File filter

Filter by extension

Filter by extension

Conversations
Failed to load comments.
Loading
Jump to
Jump to file
Failed to load files.
Loading
Diff view
Diff view
2 changes: 0 additions & 2 deletions Project.toml
Original file line number Diff line number Diff line change
Expand Up @@ -4,7 +4,6 @@ version = "4.5.2"

[deps]
LinearAlgebra = "37e2e46d-f89d-539d-b4ee-838fcccc9c8e"
Requires = "ae029012-a4dd-5104-9daa-d747884805df"

[weakdeps]
SparseArrays = "2f01184e-e22b-5df5-ae63-d93ebab69eaf"
Expand All @@ -15,7 +14,6 @@ AdaptSparseArraysExt = "SparseArrays"
AdaptStaticArraysExt = "StaticArrays"

[compat]
Requires = "1"
StaticArrays = "1"
julia = "1.10"
LinearAlgebra = "1"
Expand Down
2 changes: 1 addition & 1 deletion ext/AdaptSparseArraysExt.jl
Original file line number Diff line number Diff line change
@@ -1,7 +1,7 @@
module AdaptSparseArraysExt

using Adapt
isdefined(Base, :get_extension) ? (using SparseArrays) : (using ..SparseArrays)
using SparseArrays

Adapt.adapt_storage(::Type{Array}, xs::SparseVector) = xs
Adapt.adapt_storage(::Type{Array}, xs::SparseMatrixCSC) = xs
Expand Down
2 changes: 1 addition & 1 deletion ext/AdaptStaticArraysExt.jl
Original file line number Diff line number Diff line change
@@ -1,7 +1,7 @@
module AdaptStaticArraysExt

using Adapt
isdefined(Base, :get_extension) ? (using StaticArrays) : (using ..StaticArrays)
using StaticArrays

Adapt.adapt_storage(::Type{<:SArray{S}}, xs::Array) where {S} = SArray{S}(xs)
Adapt.adapt_storage(::Type{SArray}, xs::Array) = SArray{Tuple{size(xs)...}}(xs)
Expand Down
22 changes: 0 additions & 22 deletions src/Adapt.jl
Original file line number Diff line number Diff line change
Expand Up @@ -46,13 +46,6 @@ Create a function that adapts its argument according to `to`.
If no specific adaptions have been registered for `to`, the returned function will be equivalent to `identity`.
"""
adapt(to) = Base.Fix1(adapt, to)
if VERSION < v"1.9.0-DEV.857"
@eval function adapt(to::Type{T}) where {T}
(@isdefined T) || return Base.Fix1(adapt, to)
AT = Base.Fix1{typeof(adapt),Type{T}}
return $(Expr(:new, :AT, :adapt, :to))
end
end

adapt_structure(to, x) = adapt_storage(to, x)
adapt_storage(to, x) = x
Expand All @@ -67,19 +60,4 @@ include("arrays.jl")
# helpers
include("macro.jl")

if !isdefined(Base, :get_extension)
using Requires
end

@static if !isdefined(Base, :get_extension)
function __init__()
@require SparseArrays = "2f01184e-e22b-5df5-ae63-d93ebab69eaf" begin
include("../ext/AdaptSparseArraysExt.jl")
end
@require StaticArrays = "90137ffa-7385-5640-81b9-e52037218182" begin
include("../ext/AdaptStaticArraysExt.jl")
end
end
end

end # module
6 changes: 2 additions & 4 deletions src/base.jl
Original file line number Diff line number Diff line change
Expand Up @@ -49,10 +49,8 @@ end

adapt_structure(to, x::Core.Box) = Core.Box(adapt(to, x.contents))

if VERSION >= v"1.7"
# we can't rewrite opaque closures
adapt_structure(to, oc::Core.OpaqueClosure) = oc
end
# we can't rewrite opaque closures
adapt_structure(to, oc::Core.OpaqueClosure) = oc


## Broadcast
Expand Down
13 changes: 4 additions & 9 deletions src/wrappers.jl
Original file line number Diff line number Diff line change
Expand Up @@ -17,15 +17,10 @@ function adapt_structure(to, A::PermutedDimsArray)
end
adapt_structure(to, A::Base.ReshapedArray) =
Base.reshape(adapt(to, parent(A)), size(A))
@static if isdefined(Base, :NonReshapedReinterpretArray)
adapt_structure(to, A::Base.NonReshapedReinterpretArray) =
Base.reinterpret(eltype(A), adapt(to, parent(A)))
adapt_structure(to, A::Base.ReshapedReinterpretArray) =
Base.reinterpret(reshape, eltype(A), adapt(to, parent(A)))
else
adapt_structure(to, A::Base.ReinterpretArray) =
Base.reinterpret(eltype(A), adapt(to, parent(A)))
end
adapt_structure(to, A::Base.NonReshapedReinterpretArray) =
Base.reinterpret(eltype(A), adapt(to, parent(A)))
adapt_structure(to, A::Base.ReshapedReinterpretArray) =
Base.reinterpret(reshape, eltype(A), adapt(to, parent(A)))
@eval function adapt_structure(to, A::Base.LogicalIndex{T}) where T
# prevent re-calculating the count of booleans during LogicalIndex construction
mask = adapt(to, A.mask)
Expand Down
12 changes: 4 additions & 8 deletions test/runtests.jl
Original file line number Diff line number Diff line change
Expand Up @@ -140,9 +140,7 @@ inds = CustomArray{Int,1}([1,2])

@test_adapt CustomArray reinterpret(Int64,mat.arr) reinterpret(Int64,mat) AnyCustomArray

@static if isdefined(Base, :NonReshapedReinterpretArray)
@test_adapt CustomArray reinterpret(reshape,Int64,mat.arr) reinterpret(reshape,Int64,mat) AnyCustomArray
end
@test_adapt CustomArray reinterpret(reshape,Int64,mat.arr) reinterpret(reshape,Int64,mat) AnyCustomArray


## doubly-wrapped
Expand All @@ -160,11 +158,9 @@ end
@test_adapt CustomArray view(reshape(reinterpret(Int64,mat.arr), (2,2)), :, :) view(reshape(reinterpret(Int64,mat), (2,2)), :, :) AnyCustomArray
@test_adapt CustomArray view(reshape(reinterpret(Int64,view(mat.arr,:,:)), (2,2)), :, :) view(reshape(reinterpret(Int64,view(mat,:,:)), (2,2)), :, :) AnyCustomArray

@static if isdefined(Base, :NonReshapedReinterpretArray)
@test_adapt CustomArray reinterpret(reshape,Int64,view(mat.arr,:,:)) reinterpret(reshape,Int64,view(mat,:,:)) AnyCustomArray
@test_adapt CustomArray view(reinterpret(reshape,Int64,mat.arr), :, :) view(reinterpret(reshape,Int64,mat), :, :) AnyCustomArray
@test_adapt CustomArray view(reinterpret(reshape,Int64,view(mat.arr,:,:)), :, :) view(reinterpret(reshape,Int64,view(mat,:,:)), :, :) AnyCustomArray
end
@test_adapt CustomArray reinterpret(reshape,Int64,view(mat.arr,:,:)) reinterpret(reshape,Int64,view(mat,:,:)) AnyCustomArray
@test_adapt CustomArray view(reinterpret(reshape,Int64,mat.arr), :, :) view(reinterpret(reshape,Int64,mat), :, :) AnyCustomArray
@test_adapt CustomArray view(reinterpret(reshape,Int64,view(mat.arr,:,:)), :, :) view(reinterpret(reshape,Int64,view(mat,:,:)), :, :) AnyCustomArray


using LinearAlgebra
Expand Down
Loading