Skip to content

Commit

Permalink
pull master
Browse files Browse the repository at this point in the history
  • Loading branch information
Dhairya Gandhi committed Jan 16, 2019
2 parents 7dc162d + 2f5b711 commit e2fdd66
Show file tree
Hide file tree
Showing 2 changed files with 15 additions and 1 deletion.
13 changes: 12 additions & 1 deletion src/base.jl
Original file line number Diff line number Diff line change
Expand Up @@ -2,7 +2,18 @@

## Base

adapt_structure(to, xs::Tuple) = Tuple(adapt(to, x) for x in xs)
#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))...)

Expand Down
3 changes: 3 additions & 0 deletions test/runtests.jl
Original file line number Diff line number Diff line change
Expand Up @@ -38,6 +38,9 @@ Adapt.adapt_structure(to, xs::Wrapper) = Wrapper(adapt(to, xs.arr))
## base wrappers

@test adapt(CustomArray, (val.arr,)) == (val,)
@test @allocated(adapt(nothing, ())) == 0
@test @allocated(adapt(nothing, (1,))) == 0
@test @allocated(adapt(nothing, (1,2,3,4,5,6,7,8,9,10))) == 0

@test adapt(CustomArray, (a=val.arr,)) == (a=val,)

Expand Down

0 comments on commit e2fdd66

Please sign in to comment.