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 the O’Reilly learning platform.

O’Reilly members experience books, live events, courses curated by job role, and more from O’Reilly and nearly 200 top publishers.