pub struct H5Attribute { /* private fields */ }Expand description
A handle to an HDF5 attribute.
After creating an attribute via AttrBuilder::create, use
write_scalar or write_string
to set its value.
In read mode, use read_string to read string attributes.
Implementations§
Source§impl H5Attribute
impl H5Attribute
Sourcepub fn write_scalar(&self, value: &VarLenUnicode) -> Result<()>
pub fn write_scalar(&self, value: &VarLenUnicode) -> Result<()>
Write a scalar value to the attribute.
For VarLenUnicode, this writes a fixed-length string attribute
whose size is determined by the string value.
Sourcepub fn write_string(&self, value: &str) -> Result<()>
pub fn write_string(&self, value: &str) -> Result<()>
Write a string value to the attribute (convenience method).
Sourcepub fn write_numeric<T: H5Type>(&self, value: &T) -> Result<()>
pub fn write_numeric<T: H5Type>(&self, value: &T) -> Result<()>
Write a numeric scalar attribute.
let file = H5File::create("num_attr.h5").unwrap();
let ds = file.new_dataset::<f32>().shape(&[10]).create("data").unwrap();
ds.write_raw(&[0.0f32; 10]).unwrap();
let attr = ds.new_attr::<f64>().shape(()).create("scale").unwrap();
attr.write_numeric(&3.14f64).unwrap();Sourcepub fn read_numeric<T: H5Type>(&self) -> Result<T>
pub fn read_numeric<T: H5Type>(&self) -> Result<T>
Read a numeric scalar attribute.
let file = H5File::open("num_attr.h5").unwrap();
let ds = file.dataset("data").unwrap();
let attr = ds.attr("scale").unwrap();
let val: f64 = attr.read_numeric().unwrap();Sourcepub fn read_string(&self) -> Result<String>
pub fn read_string(&self) -> Result<String>
Read the attribute value as a string.
Handles both fixed-length string attributes and variable-length string attributes (h5py’s default), resolving a vlen value through the global heap.
Sourcepub fn datatype(&self) -> Result<DatatypeMessage>
pub fn datatype(&self) -> Result<DatatypeMessage>
Return the attribute datatype as parsed from the file (read mode only).
Mirrors H5Dataset::datatype:
it exposes the full datatype — class (integer vs floating-point vs
string vs compound …), signedness, byte order and bit precision — so
callers mapping an attribute to a NumPy / Arrow dtype need not infer a
type from the byte width, which cannot distinguish u8 from i8 (both
1 byte) or i32 from f32 (both 4 bytes).
§Errors
Returns an error for a write-mode handle, which carries no decoded attribute message.
let file = H5File::open("data.h5").unwrap();
let ds = file.dataset("image").unwrap();
let attr = ds.attr("scale").unwrap();
match attr.datatype().unwrap() {
DatatypeMessage::FloatingPoint { size, .. } => println!("float: {size} bytes"),
other => println!("other type: {other}"),
}