Skip to content

Commit

Permalink
Merge pull request #20 from marius311/master
Browse files Browse the repository at this point in the history
simplify code for adapting (Named)Tuples and fix adapting NamedTuple()
  • Loading branch information
maleadt authored Dec 2, 2019
2 parents bfeda4c + ba8dff6 commit 7f04253
Show file tree
Hide file tree
Showing 2 changed files with 2 additions and 14 deletions.
15 changes: 1 addition & 14 deletions src/base.jl
Original file line number Diff line number Diff line change
Expand Up @@ -2,20 +2,7 @@

## Base

#adapt_structure(to, xs::Tuple) = Tuple(adapt(to, x) for x in xs)
#
# non-allocating version
#@generated adapt_structure(to, x::Tuple) =
# Expr(:tuple, (:(adapt(to, x[$i])) for i in 1:fieldcount(x))...)
#
# non-allocating, non-@generated version
adapt_structure(to, xs::Tuple) = _adapt_structure(to, xs)
_adapt_structure(to, xs::Tuple{}) = ()
_adapt_structure(to, xs::Tuple) =
tuple(adapt(to, xs[1]), _adapt_structure(to, Base.tail(xs))...)

@generated adapt_structure(to, x::NamedTuple) =
Expr(:tuple, (:($f=adapt(to, x.$f)) for f in fieldnames(x))...)
adapt_structure(to, xs::Union{Tuple,NamedTuple}) = map(x->adapt(to,x), xs)


## Array wrappers
Expand Down
1 change: 1 addition & 0 deletions test/runtests.jl
Original file line number Diff line number Diff line change
Expand Up @@ -37,6 +37,7 @@ Adapt.adapt_structure(to, xs::Wrapper) = Wrapper(adapt(to, xs.arr))

## base wrappers

@test @inferred(adapt(nothing, NamedTuple())) == NamedTuple()
@test adapt(CustomArray, (val.arr,)) == (val,)
@test @allocated(adapt(nothing, ())) == 0
@test @allocated(adapt(nothing, (1,))) == 0
Expand Down

0 comments on commit 7f04253

Please sign in to comment.