-
Notifications
You must be signed in to change notification settings - Fork 137
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
Tuple support #1060
Tuple support #1060
Conversation
src/typeconversions.jl
Outdated
@@ -64,14 +64,15 @@ end | |||
|
|||
# These will use finalizers. Close them eagerly to avoid issues. | |||
datatype(::T) where {T} = Datatype(hdf5_type_id(T), true) | |||
datatype(T::Type) = Datatype(hdf5_type_id(T), true) |
There was a problem hiding this comment.
Choose a reason for hiding this comment
The reason will be displayed to describe this comment to others. Learn more.
I believe we actually want this.
datatype(T::Type) = Datatype(hdf5_type_id(T), true) | |
datatype(::Type{T}) where T = Datatype(hdf5_type_id(T), true) |
There was a problem hiding this comment.
Choose a reason for hiding this comment
The reason will be displayed to describe this comment to others. Learn more.
Yes, I agree that your change fixes several issues
datatype(x::AbstractArray{T}) where {T} = Datatype(hdf5_type_id(T), true) | ||
|
||
hdf5_type_id(::Type{T}) where {T} = hdf5_type_id(T, Val(isstructtype(T))) | ||
function hdf5_type_id(::Type{T}, isstruct::Val{true}) where {T} | ||
dtype = API.h5t_create(API.H5T_COMPOUND, sizeof(T)) | ||
for (idx, fn) in enumerate(fieldnames(T)) | ||
ftype = fieldtype(T, idx) | ||
API.h5t_insert(dtype, fn, fieldoffset(T, idx), hdf5_type_id(ftype)) | ||
API.h5t_insert(dtype, Symbol(fn), fieldoffset(T, idx), hdf5_type_id(ftype)) |
There was a problem hiding this comment.
Choose a reason for hiding this comment
The reason will be displayed to describe this comment to others. Learn more.
What does this change do?
There was a problem hiding this comment.
Choose a reason for hiding this comment
The reason will be displayed to describe this comment to others. Learn more.
This is the line that adds support for tuples to be saved as hdf5 compound data types. The field names of tuples are integers whereas other structs have field names that are symbols, which is why this conversion is necessary.
I'm going to try to get this formatted and merged soon to do a release. |
Since there is already support for reading/writing compound types, I wanted to try adding support for
Tuple
s as well, since sometimes saving tuple data is convenient, or compound types have tuple data (e.g.SArray
). This pr writes tuples to compound types with member names corresponding to the indices of each element, i.e.var"1", var"2", var"3",...
. I understand that there is no native HDF5 type corresponding to a tuple, however this pr is non-breaking and since tuples will be read back as named tuples, which inherit the majority of behavior from tuples, this only provides additional features. Please advise.I also made the following changes:
dset[i] = e
fore
a compound type by loosening method signaturesdatatype(::Type{T})
so thatcreate_dataset(parent, path, T, dataspace)
works for compound typesT
I also prepared a follow-up pr showing that we can save StaticArrays as compound types (https://github.com/lxvm/HDF5.jl/tree/pr_sarray), although one method would change in a breaking way.