Skip to content

Update SndFileError #6

@jazzyeagle

Description

@jazzyeagle

I'm trying to work with loading the data into a vector in a struct I'm using. I'm attempting to use the following code for reading in the data:

pub fn load(&self, filename: &str) -> Result<(), sndfile::SndFileError> {
     let snd_file      = sndfile::OpenOptions::ReadOnly(sndfile::ReadOptions::Auto).from_path(filename)?;
     self.audioData    = snd_file.read_all_to_vec()?;
     self.sampleRate   = snd_file.get_samplerate();
     self.channelCount = snd_file.get_channels();
     self.format       = snd_file.get_major_format();

      Ok(())
}

The error comes with those "?" methods:

error[E0277]: `?` couldn't convert the error to `SndFileError`
  --> src/data/lup.rs:17:55
   |
17 |         self.audioData    = snd_file.read_all_to_vec()?;
   |                                                       ^ the trait `From<()>` is not implemented for `SndFileError`
   |
   = note: the question mark operation (`?`) implicitly performs a conversion on the error value using the `From` trait
   = help: the following other types implement trait `FromResidual<R>`:
             <Result<T, F> as FromResidual<Result<Infallible, E>>>
             <Result<T, F> as FromResidual<Yeet<E>>>
   = note: required for `Result<(), SndFileError>` to implement `FromResidual<Result<Infallible, ()>>`

For more information about this error, try `rustc --explain E0277`.

SndFileError needs a few additional impl's to use the ? methods, notably std::fmt::Display, std::error::Error, and From<>. Since these ? methods are a nice way to write cleaner code, I thought it could be beneficial to have in your library.

At that point, I may even be able to refactor some of the other code for you as well. I'm working on a PR right now, which I may even have for you later today, but I wanted to make sure I logged the issue so you had an opportunity to review and respond, see if you agree with my proposed changes.

Thanks!

Metadata

Metadata

Assignees

No one assigned

    Labels

    No labels
    No labels

    Type

    No type

    Projects

    No projects

    Milestone

    No milestone

    Relationships

    None yet

    Development

    No branches or pull requests

    Issue actions