Skip to content

Commit 90fdf9d

Browse files
committed
move Adapt to ext
And use curried adapter to avoid possible instability
1 parent b0d509b commit 90fdf9d

File tree

3 files changed

+20
-6
lines changed

3 files changed

+20
-6
lines changed

Project.toml

+3-2
Original file line numberDiff line numberDiff line change
@@ -3,21 +3,22 @@ uuid = "09ab397b-f2b6-538f-b94a-2f83cf4a842a"
33
version = "0.6.16"
44

55
[deps]
6-
Adapt = "79e6a3ab-5dfb-504d-930d-738a2a938a0e"
76
ConstructionBase = "187b0558-2788-49d3-abe0-74a17ed4e7c9"
87
DataAPI = "9a962f9c-6df0-11e9-0e5d-c546b8b5ee8a"
98
Tables = "bd369af6-aec1-5ad0-b16a-f7cc5008161c"
109

1110
[weakdeps]
11+
Adapt = "79e6a3ab-5dfb-504d-930d-738a2a938a0e"
1212
GPUArraysCore = "46192b85-c4d5-4398-a991-12ede77f4527"
1313
StaticArrays = "90137ffa-7385-5640-81b9-e52037218182"
1414

1515
[extensions]
16+
StructArraysAdaptExt = "Adapt"
1617
StructArraysGPUArraysCoreExt = "GPUArraysCore"
1718
StructArraysStaticArraysExt = "StaticArrays"
1819

1920
[compat]
20-
Adapt = "1, 2, 3"
21+
Adapt = "2, 3"
2122
ConstructionBase = "1"
2223
DataAPI = "1"
2324
GPUArraysCore = "0.1.2"

ext/StructArraysAdaptExt.jl

+16
Original file line numberDiff line numberDiff line change
@@ -0,0 +1,16 @@
1+
module StructArraysAdaptExt
2+
# Use Adapt allows for automatic conversion of CPU to GPU StructArrays
3+
using Adapt, StructArrays
4+
@static if !applicable(Adapt.adapt, Int)
5+
# Adapt.jl has curried support, implement it ourself
6+
adpat(to) = Base.Fix1(Adapt.adapt, to)
7+
if VERSION < v"1.9.0-DEV.857"
8+
@eval function adapt(to::Type{T}) where {T}
9+
(@isdefined T) || return Base.Fix1(Adapt.adapt, to)
10+
AT = Base.Fix1{typeof(Adapt.adapt),Type{T}}
11+
return $(Expr(:new, :AT, :(Adapt.adapt), :to))
12+
end
13+
end
14+
end
15+
Adapt.adapt_structure(to, s::StructArray) = replace_storage(adapt(to), s)
16+
end

src/StructArrays.jl

+1-4
Original file line numberDiff line numberDiff line change
@@ -25,11 +25,8 @@ function refvalue(s::StructArray{T}, v::Tup) where {T}
2525
createinstance(T, map(refvalue, components(s), v)...)
2626
end
2727

28-
# Use Adapt allows for automatic conversion of CPU to GPU StructArrays
29-
import Adapt
30-
Adapt.adapt_structure(to, s::StructArray) = replace_storage(x->Adapt.adapt(to, x), s)
31-
3228
@static if !isdefined(Base, :get_extension)
29+
include("../ext/StructArraysAdaptExt.jl")
3330
include("../ext/StructArraysGPUArraysCoreExt.jl")
3431
include("../ext/StructArraysStaticArraysExt.jl")
3532
end

0 commit comments

Comments
 (0)