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.0"
version = "4.5.1"

[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)
names = fieldnames(Core.eval(__module__, T))
quote
function Adapt.adapt_structure(to, obj::$(esc(T)))
$(esc(T))($([:(Adapt.adapt_structure(to, obj.$name)) for name in names]...))
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...)
end
end
end)
end
7 changes: 7 additions & 0 deletions test/runtests.jl
Original file line number Diff line number Diff line change
Expand Up @@ -214,6 +214,13 @@ end

@test_adapt CustomArray MyStruct(u,v) MyStruct(CustomArray(u), CustomArray(v))
@test_adapt CustomArray MyStruct(u,1.0) MyStruct(CustomArray(u), 1.0)

# Bug 102; type defined in the same block as @adapt_structure
struct LocalStruct
x::Int
end
Adapt.@adapt_structure LocalStruct
@test adapt(CustomArray, LocalStruct(1)) === LocalStruct(1)
end


Expand Down
Loading