aldanor / hdf5-rust

HDF5 for Rust
https://docs.rs/hdf5
Apache License 2.0
310 stars 85 forks source link

Example code appears to be broken and I can't figure out how to interact with ndarray #148

Closed samuelbryant closed 3 years ago

samuelbryant commented 3 years ago

When I tried to run the code in example.rs using the exact same dependencies as the master project itself (namely hdf5 = "0.7.1" and ndarray = "0.15"), it spits out the errors:

the trait bound `ArrayBase<OwnedRepr<Pixel>, Dim<[usize; 2]>>: From<&ArrayBase<OwnedRepr<Pixel>, Dim<[usize; 2]>>>` is not satisfied

I checked whether this would work because I am getting the same sort of error in my own code. In my own code I am getting something that seems non-sensical. Here are two snippets of code which seem to contradict each other:

Code Snippet 1

use ndarray::{ArrayBase, OwnedRepr, Dim, ViewRepr, ArrayView};

let dataset = hdf5::File::create("test.hdf5").unwrap()
  .new_dataset::<i32>()
  .create("data", (5, )).unwrap();

let data: Vec<i32> = vec![1,2,3,4,5];
let ab: ArrayBase<OwnedRepr<i32>, Dim<[usize; 1]>> = ArrayBase::from(data);
dataset.write(&ab).unwrap();
let ab2: ArrayBase<OwnedRepr<i32>, Dim<[usize; 1]>> = (&ab).into();

Both the dataset.write line and let ab2: ... trigger the same error:

the trait bound `ArrayBase<OwnedRepr<i32>, Dim<[usize; 1]>>: From<&ArrayBase<OwnedRepr<i32>, Dim<[usize; 1]>>>` is not satisfied

Which is fine... I guess they can't convert from a reference to owned data to the owned data itself without copying so that makes sense. But then when you try passing the object directly, it works in the first case but not the second case:

Code Snippet 2

let dataset = hdf5::File::create("test.hdf5").unwrap()
  .new_dataset::<i32>()
  .create("data", (5, )).unwrap();

let data: Vec<i32> = vec![1,2,3,4,5];
let ab: ArrayBase<OwnedRepr<i32>, Dim<[usize; 1]>> = ArrayBase::from(data);
let ab2: ArrayBase<OwnedRepr<i32>, Dim<[usize; 1]>> = (ab).into();
dataset.write(ab).unwrap();

Now the first line "let ab2: ArrayBase...." runs fine. But now the dataset.write line is complaining that

the trait bound `ndarray::ArrayBase<ndarray::ViewRepr<&_>, _>: From<ArrayBase<OwnedRepr<i32>, Dim<[usize; 1]>>>` is not satisfied

Huh? This is extremely confusing. It seems like there are two trait bounds which appear mututally exclusive? Maybe I'm just being thick. I really don't understand what this write function is looking for.

mulimoen commented 3 years ago

Is this using hdf5 from the master branch? 0.7.1 is still only ndarray 0.14: https://crates.io/crates/hdf5/0.7.1/dependencies

samuelbryant commented 3 years ago

I tried using 0.7.1 and 0.7.0 with ndarray 0.13, 0.14, and 0.15.

samuelbryant commented 3 years ago

Wait I'm sorry. I tried using 0.15 and 0.13. Apparently 0.14 does in fact work. Thank you for the help.