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: 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.5.1"
version = "4.5.2"

[deps]
LinearAlgebra = "37e2e46d-f89d-539d-b4ee-838fcccc9c8e"
Expand Down
10 changes: 5 additions & 5 deletions src/macro.jl
Original file line number Diff line number Diff line change
Expand Up @@ -5,10 +5,10 @@ Define a method `adapt_structure(to, obj::T)` which calls `adapt_structure` on e
of `obj` and constructs a new instance of `T` using the default constuctor `T(...)`.
"""
macro adapt_structure(T)
esc(quote
@generated function Adapt.adapt_structure(to, obj::$T)
assignments = Any[:(Adapt.adapt_structure(to, obj.$name)) for name in fieldnames(obj)]
return Expr(:call, $T, assignments...)
quote
@generated function $Adapt.adapt_structure($(esc(:to)), $(esc(:obj))::$(esc(T)))
assignments = Any[:($$Adapt.adapt_structure(to, obj.$name)) for name in fieldnames($(esc(:obj)))]
return Expr(:call, $(esc(T)), assignments...)
end
end)
end
end
11 changes: 11 additions & 0 deletions test/runtests.jl
Original file line number Diff line number Diff line change
Expand Up @@ -221,6 +221,17 @@ end
end
Adapt.@adapt_structure LocalStruct
@test adapt(CustomArray, LocalStruct(1)) === LocalStruct(1)

# The macro must work in modules that only import @adapt_structure,
# not the Adapt module itself (e.g. `using Adapt: @adapt_structure`).
@eval module ModuleOnlyImportingMacro
using Adapt: @adapt_structure
struct S
x::Int
end
@adapt_structure S
end
@test adapt(CustomArray, ModuleOnlyImportingMacro.S(1)) === ModuleOnlyImportingMacro.S(1)
end


Expand Down
Loading