Skip to content
Open
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
11 changes: 11 additions & 0 deletions docs/src/APIs/datalayouts_api.md
Original file line number Diff line number Diff line change
Expand Up @@ -19,3 +19,14 @@ DataLayouts.IJHF
DataLayouts.VIHF
DataLayouts.VIJHF
```

## Internals

```@docs
check_basetype
is_valid_basetype
first_invalid_primitive_type
replace_basetype
parent_array_type
promote_parent_array_type
```
23 changes: 21 additions & 2 deletions src/DataLayouts/struct.jl
Original file line number Diff line number Diff line change
Expand Up @@ -24,6 +24,21 @@ is_valid_basetype(::Type{T}, ::Type{<:T}) where {T} = true
is_valid_basetype(::Type{T}, ::Type{S}, Ss...) where {T, S} =
is_valid_basetype(T, S) && is_valid_basetype(T, Ss...)

"""
first_invalid_primitive_type(::Type{T}, ::Type{S})

Returns the first primitive subtype of `S` that cannot be represented using
base type `T`, or `nothing` if `T` is a valid base type for `S`.
"""
first_invalid_primitive_type(::Type{T}, ::Type{S}) where {T, S} =
is_valid_basetype(T, S) ? nothing :
Base.isprimitivetype(S) ? S :
first_invalid_primitive_type(T, fieldtypes(S)...)
first_invalid_primitive_type(::Type{T}, ::Type{S}, Ss...) where {T, S} =
isnothing(first_invalid_primitive_type(T, S)) ?
first_invalid_primitive_type(T, Ss...) :
first_invalid_primitive_type(T, S)

"""
check_basetype(::Type{T}, ::Type{S})

Expand All @@ -39,16 +54,20 @@ function check_basetype(::Type{T}, ::Type{S}) where {T, S}
estr = "Struct type $S has indeterminate size"
:(error($estr))
elseif !is_valid_basetype(T, S)
estr = "Struct type $S cannot be represented using base type $T"
P = first_invalid_primitive_type(T, S)
estr = "Struct type $S contains subtype $P, \
which cannot be represented using base type $T"
:(error($estr))
else
:(return nothing)
end
else
isbitstype(T) || error("Base type $T has indeterminate size")
isbitstype(S) || error("Struct type $S has indeterminate size")
P = first_invalid_primitive_type(T, S)
is_valid_basetype(T, S) ||
error("Struct type $S cannot be represented using base type $T")
error("Struct type $S contains subtype $P, \
which cannot be represented using base type $T")
return nothing
end
end
Expand Down
Loading