Xeiron / sndfile.rs

A safe rust wrapper of libsndfile
MIT License
10 stars 7 forks source link

Update SndFileError #6

Open jazzyeagle opened 1 year ago

jazzyeagle commented 1 year ago

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!