Skip to content
New issue

Have a question about this project? Sign up for a free GitHub account to open an issue and contact its maintainers and the community.

By clicking “Sign up for GitHub”, you agree to our terms of service and privacy statement. We’ll occasionally send you account related emails.

Already on GitHub? Sign in to your account

Improve typestability. #53

Merged
merged 2 commits into from
Jul 25, 2022
Merged
Show file tree
Hide file tree
Changes from 1 commit
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
19 changes: 13 additions & 6 deletions src/base.jl
Original file line number Diff line number Diff line change
@@ -1,21 +1,28 @@
# help struct to keep internal stability
struct AdaptTo{T} <: Function
to::T
AdaptTo(to) = new{Core.Typeof(to)}(to)
end
(f::AdaptTo)(x) = adapt(f.to, x)

# predefined adaptors for working with types from the Julia standard library

adapt_structure(to, xs::Union{Tuple,NamedTuple}) = map(x->adapt(to,x), xs)
adapt_structure(to, xs::Union{Tuple,NamedTuple}) = map(AdaptTo(to), xs)
maleadt marked this conversation as resolved.
Show resolved Hide resolved


## Closures

# two things can be captured: static parameters, and actual values (fields)

@eval function adapt_structure(to, f::F) where {F<:Function}
isempty(F.parameters) && return f
nsparams = length(F.parameters) - nfields(f)
npar = length(F.parameters)
npar <= 0 && return f
nsparams = npar - nfields(f)

# TODO: we should adapt the static parameters too
# (but adapt currently only works with values)
sparams = ntuple(i->F.parameters[i], nsparams)
fields = ntuple(i->adapt(to, getfield(f, i)), nfields(f))

fields = adapt(to, ntuple(i->getfield(f, i), nfields(f)))
# TODO: this assumes the typevars of the closure matches the sparams + fields.
# that may not always be true, and definitely isn't for arbitrary callable objects.
ftyp = F.name.wrapper{sparams..., map(Core.Typeof, fields)...}
Expand All @@ -28,7 +35,7 @@ end
import Base.Broadcast: Broadcasted, Extruded

adapt_structure(to, bc::Broadcasted{Style}) where Style =
Broadcasted{Style}(adapt(to, bc.f), map(arg->adapt(to, arg), bc.args), bc.axes)
Broadcasted{Style}(adapt(to, bc.f), adapt(to, bc.args), bc.axes)

adapt_structure(to, ex::Extruded) =
Extruded(adapt(to, ex.x), ex.keeps, ex.defaults)
8 changes: 6 additions & 2 deletions src/wrappers.jl
Original file line number Diff line number Diff line change
Expand Up @@ -9,8 +9,12 @@ export WrappedArray

adapt_structure(to, A::SubArray) =
SubArray(adapt(to, Base.parent(A)), adapt(to, parentindices(A)))
adapt_structure(to, A::PermutedDimsArray) =
PermutedDimsArray(adapt(to, Base.parent(A)), permutation(A))
function adapt_structure(to, A::PermutedDimsArray)
perm = permutation(A)
iperm = invperm(perm)
A′ = adapt(to, Base.parent(A))
PermutedDimsArray{Base.eltype(A′),Base.ndims(A′),perm,iperm,typeof(A′)}(A′)
end
adapt_structure(to, A::Base.ReshapedArray) =
Base.reshape(adapt(to, Base.parent(A)), size(A))
@static if isdefined(Base, :NonReshapedReinterpretArray)
Expand Down
4 changes: 2 additions & 2 deletions test/runtests.jl
Original file line number Diff line number Diff line change
Expand Up @@ -5,7 +5,7 @@ using Test
# custom array type

struct CustomArray{T,N} <: AbstractArray{T,N}
arr::Array
arr::Array{T,N}
end

CustomArray(x::Array{T,N}) where {T,N} = CustomArray{T,N}(x)
Expand All @@ -31,7 +31,7 @@ macro test_adapt(to, src_expr, dst_expr, typ=nothing)
src = $(esc(src_expr))
dst = $(esc(dst_expr))

res = adapt($(esc(to)), src)
res = @inferred(adapt($(esc(to)), src))
@test res == dst
@test typeof(res) == typeof(dst)

Expand Down