Implementing an MP3 decoder

We're now ready to create this new module. Create a new mp3.rs file with the following content:

use std::io::{Read, Seek, SeekFrom};
use std::time::Duration;

use simplemad;

We start this module with some import statements as usual. The important one is simplemad, which will be used to decode the frames of an MP3 file:

pub struct Mp3Decoder<R> where R: Read {
    reader: simplemad::Decoder<R>,
    current_frame: simplemad::Frame,
    current_frame_channel: usize,
    current_frame_sample_pos: usize,
    current_time: u64,
}

We saw in Chapter 1, Basics of Rust, that we can add trait bounds to generic parameters in a function. We can also add them to the generic parameters of a type. Here we see an alternative syntax using a where clause. ...

Get Rust Programming By Example now with O’Reilly online learning.

O’Reilly members experience live online training, plus books, videos, and digital content from 200+ publishers.