forked from aldanor/hdf5-rust
-
Notifications
You must be signed in to change notification settings - Fork 16
Object Reference Types #36
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
Merged
Merged
Changes from 18 commits
Commits
Show all changes
20 commits
Select commit
Hold shift + click to select a range
80563d7
checkpoint
mulimoen 6b38cff
Working?
mulimoen eaa782b
checkpoint
mulimoen 65a5417
Add Integration Tests for Current Reference Implementations
JamesWiresmith b7f696b
Factor references out to their own module
JamesWiresmith c9568ee
Add version flags to HDF5-Types References
JamesWiresmith bf80ad4
refactor: ObjectReference Trait
JamesWiresmith ca95204
fix: Have types work against pre-1.12
JamesWiresmith 68c82ce
wip: Support for Object1 and Object2 References
JamesWiresmith 000b902
fix: Mark DynValue variable as unused
JamesWiresmith 87c0362
fix: Remove StdReference try_clone
JamesWiresmith 88e2ff1
refactor: some renames
JamesWiresmith 43351b9
refactor: Move References to Module in H5Types
JamesWiresmith 840dbe8
fix: Gate ObjectReference2 on v1.12.1
JamesWiresmith 5c819b3
backout VarArray non-copy changes
JamesWiresmith 141eccf
fix: Fix API compatibility for ObjectReference1
JamesWiresmith 348defc
fix object references for new metno fork
JamesWiresmith ef23e6d
fix: Add cargo feature checks to hdf5-types
JamesWiresmith eb70091
rename references module
JamesWiresmith c461ec1
seal object reference trait.
JamesWiresmith File filter
Filter by extension
Conversations
Failed to load comments.
Loading
Jump to
Jump to file
Failed to load files.
Loading
Diff view
Diff view
There are no files selected for viewing
Submodule hdf5
updated
4731 files
This file contains hidden or bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
This file contains hidden or bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
This file contains hidden or bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
This file contains hidden or bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
This file contains hidden or bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
| Original file line number | Diff line number | Diff line change |
|---|---|---|
| @@ -0,0 +1,22 @@ | ||
| //! Types for references. | ||
|
|
||
| use std::mem; | ||
|
|
||
| #[derive(Copy, Clone, Debug, PartialEq, Eq)] | ||
| pub enum Reference { | ||
| Object, | ||
| Region, | ||
| #[cfg(feature = "1.12.0")] | ||
| Std, | ||
| } | ||
|
|
||
| impl Reference { | ||
| pub fn size(self) -> usize { | ||
| match self { | ||
| Self::Object => mem::size_of::<hdf5_sys::h5r::hobj_ref_t>(), | ||
| Self::Region => mem::size_of::<hdf5_sys::h5r::hdset_reg_ref_t>(), | ||
| #[cfg(feature = "1.12.0")] | ||
| Self::Std => mem::size_of::<hdf5_sys::h5r::H5R_ref_t>(), | ||
| } | ||
| } | ||
| } |
This file contains hidden or bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
This file contains hidden or bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
This file contains hidden or bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
This file contains hidden or bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
This file contains hidden or bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
| Original file line number | Diff line number | Diff line change |
|---|---|---|
| @@ -0,0 +1,69 @@ | ||
| //! The pre v1.12.0 reference types. | ||
|
|
||
| use crate::internal_prelude::*; | ||
|
|
||
| use hdf5_sys::{ | ||
| h5o::H5O_type_t, | ||
| h5p::H5P_DEFAULT, | ||
| h5r::{hobj_ref_t, H5Rcreate, H5Rdereference, H5Rget_obj_type2}, | ||
| }; | ||
| use hdf5_types::H5Type; | ||
|
|
||
| #[cfg(not(feature = "1.12.0"))] | ||
| use hdf5_sys::h5r::H5R_OBJECT as H5R_OBJECT1; | ||
| #[cfg(feature = "1.12.0")] | ||
| use hdf5_sys::h5r::H5R_OBJECT1; | ||
|
|
||
| use crate::{Location, ObjectReference}; | ||
|
|
||
| #[repr(transparent)] | ||
| #[derive(Debug, Copy, Clone)] | ||
| pub struct ObjectReference1 { | ||
| inner: hobj_ref_t, | ||
| } | ||
|
|
||
| unsafe impl H5Type for ObjectReference1 { | ||
| fn type_descriptor() -> hdf5_types::TypeDescriptor { | ||
| hdf5_types::TypeDescriptor::Reference(hdf5_types::Reference::Object) | ||
| } | ||
| } | ||
|
|
||
| impl ObjectReference for ObjectReference1 { | ||
| const REF_TYPE: hdf5_sys::h5r::H5R_type_t = H5R_OBJECT1; | ||
|
|
||
| fn ptr(&self) -> *const c_void { | ||
| let pointer = std::ptr::addr_of!(self.inner); | ||
| pointer.cast() | ||
| } | ||
|
|
||
| fn create(location: &Location, name: &str) -> Result<Self> { | ||
| let mut ref_out: std::mem::MaybeUninit<hobj_ref_t> = std::mem::MaybeUninit::uninit(); | ||
| let name = to_cstring(name)?; | ||
| h5call!(H5Rcreate( | ||
| ref_out.as_mut_ptr().cast(), | ||
| location.id(), | ||
| name.as_ptr(), | ||
| Self::REF_TYPE, | ||
| -1 | ||
| ))?; | ||
| let reference = unsafe { ref_out.assume_init() }; | ||
| Ok(Self { inner: reference }) | ||
| } | ||
|
|
||
| fn get_object_type(&self, location: &Location) -> Result<hdf5_sys::h5o::H5O_type_t> { | ||
| let mut objtype = std::mem::MaybeUninit::<H5O_type_t>::uninit(); | ||
| h5call!(H5Rget_obj_type2(location.id(), H5R_OBJECT1, self.ptr(), objtype.as_mut_ptr()))?; | ||
| let objtype = unsafe { objtype.assume_init() }; | ||
| Ok(objtype) | ||
| } | ||
|
|
||
| fn dereference(&self, location: &Location) -> Result<ReferencedObject> { | ||
| let object_type = self.get_object_type(location)?; | ||
| #[cfg(feature = "1.10.0")] | ||
| let object_id = | ||
| h5call!(H5Rdereference(location.id(), H5P_DEFAULT, H5R_OBJECT1, self.ptr()))?; | ||
| #[cfg(not(feature = "1.10.0"))] | ||
| let object_id = h5call!(H5Rdereference(location.id(), H5R_OBJECT1, self.ptr()))?; | ||
| ReferencedObject::from_type_and_id(object_type, object_id) | ||
| } | ||
| } |
This file contains hidden or bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
| Original file line number | Diff line number | Diff line change |
|---|---|---|
| @@ -0,0 +1,53 @@ | ||
| use crate::internal_prelude::*; | ||
| use crate::Location; | ||
|
|
||
| mod legacy; | ||
|
|
||
| #[cfg(feature = "1.12.1")] | ||
| mod standard; | ||
|
|
||
| use hdf5_sys::h5o::H5O_type_t; | ||
| use hdf5_sys::h5r::H5R_type_t; | ||
|
|
||
| pub use legacy::ObjectReference1; | ||
| #[cfg(feature = "1.12.1")] | ||
| pub use standard::ObjectReference2; | ||
|
|
||
| pub trait ObjectReference: Sized + H5Type { | ||
JamesWiresmith marked this conversation as resolved.
Outdated
Show resolved
Hide resolved
|
||
| const REF_TYPE: H5R_type_t; | ||
| fn ptr(&self) -> *const c_void; | ||
|
|
||
| /// Get the type of the object that the reference points in the same space as the provided location. | ||
| fn get_object_type(&self, location: &Location) -> Result<H5O_type_t>; | ||
|
|
||
| /// Create a new reference in the same structure as the location provided. | ||
| fn create(location: &Location, name: &str) -> Result<Self>; | ||
|
|
||
| /// Dereference the object reference in the space provided. | ||
| fn dereference(&self, location: &Location) -> Result<ReferencedObject>; | ||
| } | ||
| /// The result of dereferencing an [object reference](ObjectReference). | ||
| /// | ||
| /// Each variant represents a different type of object that can be referenced by a [ObjectReference]. | ||
| #[derive(Clone, Debug)] | ||
| pub enum ReferencedObject { | ||
| Group(Group), | ||
| Dataset(Dataset), | ||
| Datatype(Datatype), | ||
| } | ||
|
|
||
| impl ReferencedObject { | ||
| pub fn from_type_and_id(object_type: H5O_type_t, object_id: hid_t) -> Result<Self> { | ||
| use hdf5_sys::h5o::H5O_type_t::*; | ||
| let referenced_object = match object_type { | ||
| H5O_TYPE_GROUP => ReferencedObject::Group(Group::from_id(object_id)?), | ||
| H5O_TYPE_DATASET => ReferencedObject::Dataset(Dataset::from_id(object_id)?), | ||
| H5O_TYPE_NAMED_DATATYPE => ReferencedObject::Datatype(Datatype::from_id(object_id)?), | ||
| #[cfg(feature = "1.12.0")] | ||
| H5O_TYPE_MAP => fail!("Can not create object from a map"), | ||
| H5O_TYPE_UNKNOWN => fail!("Unknown datatype"), | ||
| H5O_TYPE_NTYPES => fail!("hdf5 should not produce this type"), | ||
| }; | ||
| Ok(referenced_object) | ||
| } | ||
| } | ||
This file contains hidden or bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
| Original file line number | Diff line number | Diff line change |
|---|---|---|
| @@ -0,0 +1,104 @@ | ||
| //! New standard reference types introduced in v1.12.0. | ||
| //! | ||
| //! These are gated on v1.12.1 since there appear to be multiple bugs in v1.12.0. | ||
| //! | ||
| use hdf5_sys::h5o::H5O_type_t; | ||
| use hdf5_sys::h5r::H5R_type_t::H5R_OBJECT2; | ||
| use hdf5_sys::h5r::{H5R_ref_t, H5Rcreate_object, H5Rdestroy, H5Rget_obj_type3, H5Ropen_object}; | ||
|
|
||
| use super::ObjectReference; | ||
| use crate::internal_prelude::*; | ||
| use crate::Location; | ||
|
|
||
| /// A reference to a HDF5 item that can be stored in attributes or datasets. | ||
| #[repr(transparent)] | ||
| pub struct StdReference(H5R_ref_t); | ||
|
|
||
| impl StdReference { | ||
| fn ptr(&self) -> *const H5R_ref_t { | ||
| std::ptr::addr_of!(self.0) | ||
| } | ||
| } | ||
|
|
||
| //todo: could we query some actual object parameters to make this more useful? | ||
| impl std::fmt::Debug for StdReference { | ||
| fn fmt(&self, f: &mut std::fmt::Formatter<'_>) -> std::fmt::Result { | ||
| f.write_str("StdReference") | ||
| } | ||
| } | ||
|
|
||
| unsafe impl H5Type for StdReference { | ||
| fn type_descriptor() -> hdf5_types::TypeDescriptor { | ||
| hdf5_types::TypeDescriptor::Reference(hdf5_types::Reference::Std) | ||
| } | ||
| } | ||
|
|
||
| impl Drop for StdReference { | ||
| fn drop(&mut self) { | ||
| let _e = h5call!(H5Rdestroy(&mut self.0)); | ||
| } | ||
| } | ||
|
|
||
| #[repr(transparent)] | ||
| #[derive(Debug)] | ||
| pub struct ObjectReference2(StdReference); | ||
|
|
||
| impl ObjectReference for ObjectReference2 { | ||
| const REF_TYPE: hdf5_sys::h5r::H5R_type_t = H5R_OBJECT2; | ||
|
|
||
| fn ptr(&self) -> *const c_void { | ||
| self.0.ptr().cast() | ||
| } | ||
|
|
||
| fn create(location: &Location, name: &str) -> Result<Self> { | ||
| let reference: H5R_ref_t = create_object_reference(location, name)?; | ||
| Ok(Self(StdReference(reference))) | ||
| } | ||
|
|
||
| fn get_object_type(&self, _location: &Location) -> Result<hdf5_sys::h5o::H5O_type_t> { | ||
| let mut objtype = std::mem::MaybeUninit::<H5O_type_t>::uninit(); | ||
| h5call!(H5Rget_obj_type3(self.0.ptr(), H5P_DEFAULT, objtype.as_mut_ptr()))?; | ||
| let objtype = unsafe { objtype.assume_init() }; | ||
| Ok(objtype) | ||
| } | ||
|
|
||
| fn dereference(&self, location: &Location) -> Result<ReferencedObject> { | ||
| let object_type = self.get_object_type(location)?; | ||
| let object_id = h5call!(H5Ropen_object(self.0.ptr(), H5P_DEFAULT, H5P_DEFAULT))?; | ||
| ReferencedObject::from_type_and_id(object_type, object_id) | ||
| } | ||
| } | ||
|
|
||
| unsafe impl H5Type for ObjectReference2 { | ||
| fn type_descriptor() -> hdf5_types::TypeDescriptor { | ||
| hdf5_types::TypeDescriptor::Reference(hdf5_types::Reference::Std) | ||
| } | ||
| } | ||
|
|
||
| fn create_object_reference(dataset: &Location, name: &str) -> Result<H5R_ref_t> { | ||
| let mut out: std::mem::MaybeUninit<H5R_ref_t> = std::mem::MaybeUninit::uninit(); | ||
| let name = to_cstring(name)?; | ||
| h5call!(H5Rcreate_object(dataset.id(), name.as_ptr(), H5P_DEFAULT, out.as_mut_ptr().cast(),))?; | ||
| unsafe { Ok(out.assume_init()) } | ||
| } | ||
|
|
||
| #[cfg(test)] | ||
| mod tests { | ||
| use super::*; | ||
|
|
||
| #[test] | ||
| pub fn test_references() { | ||
| use super::ReferencedObject; | ||
| with_tmp_file(|file| { | ||
| file.create_group("g").unwrap(); | ||
| let gref = file.reference::<ObjectReference2>("g").unwrap(); | ||
| let group = file.dereference(&gref).unwrap(); | ||
| assert!(matches!(group, ReferencedObject::Group(_))); | ||
|
|
||
| file.new_dataset::<i32>().create("ds").unwrap(); | ||
| let dsref = file.reference::<ObjectReference2>("ds").unwrap(); | ||
| let ds = file.dereference(&dsref).unwrap(); | ||
| assert!(matches!(ds, ReferencedObject::Dataset(_))); | ||
| }) | ||
| } | ||
| } |
Oops, something went wrong.
Oops, something went wrong.
Add this suggestion to a batch that can be applied as a single commit.
This suggestion is invalid because no changes were made to the code.
Suggestions cannot be applied while the pull request is closed.
Suggestions cannot be applied while viewing a subset of changes.
Only one suggestion per line can be applied in a batch.
Add this suggestion to a batch that can be applied as a single commit.
Applying suggestions on deleted lines is not supported.
You must change the existing code in this line in order to create a valid suggestion.
Outdated suggestions cannot be applied.
This suggestion has been applied or marked resolved.
Suggestions cannot be applied from pending reviews.
Suggestions cannot be applied on multi-line comments.
Suggestions cannot be applied while the pull request is queued to merge.
Suggestion cannot be applied right now. Please check back later.
Uh oh!
There was an error while loading. Please reload this page.