Trait std::io::Read is not implemented for &NpzArchive<BufReader<File>>

I'm using npyz crate to load a *.npz file and access the data. Here is my code:

use npyz::npz::NpzArchive;

fn main() -> Result<(), Box<dyn std::error::Error>> {
    // Open an npz file
    let mut bytes = NpzArchive::open("file.npz")?;

    let data = NpzArchive::new(&bytes)?;
    Ok(())
}

I'm running into this error:

error[E0277]: the trait bound `&NpzArchive<BufReader<File>>: std::io::Read` is not satisfied
  --> src/main.rs:8:32
   |
8  |     let data = NpzArchive::new(&bytes)?;
   |                --------------- ^^^^^^ the trait `std::io::Read` is not implemented for `&NpzArchive<BufReader<File>>`
   |                |
   |                required by a bound introduced by this call
   |
note: required by a bound in `NpzArchive::<R>::new`
  --> /Users/avataar/.cargo/registry/src/github.com-1ecc6299db9ec823/npyz-0.7.2/src/npz_feature.rs:28:9
   |
28 | impl<R: io::Read + io::Seek> NpzArchive<R> {
   |         ^^^^^^^^ required by this bound in `NpzArchive::<R>::new`

error[E0277]: the trait bound `&NpzArchive<BufReader<File>>: Seek` is not satisfied
  --> src/main.rs:8:32
   |
8  |     let data = NpzArchive::new(&bytes)?;
   |                --------------- ^^^^^^ the trait `Seek` is not implemented for `&NpzArchive<BufReader<File>>`
   |                |
   |                required by a bound introduced by this call
   |
   = help: the following other types implement trait `Seek`:
             &File
             &mut S
             Box<S>
             BufReader<R>
             BufWriter<W>
             File
             std::io::Cursor<T>
             std::io::Empty
note: required by a bound in `NpzArchive::<R>::new`
  --> /Users/avataar/.cargo/registry/src/github.com-1ecc6299db9ec823/npyz-0.7.2/src/npz_feature.rs:28:20
   |
28 | impl<R: io::Read + io::Seek> NpzArchive<R> {
   |                    ^^^^^^^^ required by this bound in `NpzArchive::<R>::new`

My Cargo.toml files dependencies looks like this:

[dependencies]
npyz = {version = "0.7.2", features = ["npz"] }

Calling NpzArchive::open followed by NpzArchive::new looks wrong. You should be able to use the return value of NpzArchive::open directly.

2 Likes

Thanks @alice for replying. What if I want to see the data inside the *.npz file. The NpzArchive doesn't have a debug trait. How could I see the data inside the file?

You would then read the contents of the archive into an array.

To inspect an NpzArchive, you're supposed to call bytes.array_names() to see a list of arrays in the archive, and to use by_name to get at an individual array.

You can then use methods on NpyFile to examine the contents of an array, and to deserialize the data into a Rust data type (e.g. by calling into_vec to get a Vec<_> from the array).

This topic was automatically closed 90 days after the last reply. We invite you to open a new topic if you have further questions or comments.