From 5ab65b7a3172c3f985a402fd8f7bad2183838792 Mon Sep 17 00:00:00 2001 From: Pierre Krieger Date: Fri, 16 Oct 2015 13:42:57 +0200 Subject: [PATCH] Hide the implementation of the decoder --- src/decoder/mod.rs | 42 ++++++++++++++++++++++-------------------- 1 file changed, 22 insertions(+), 20 deletions(-) diff --git a/src/decoder/mod.rs b/src/decoder/mod.rs index 6dddc33..81b84a1 100644 --- a/src/decoder/mod.rs +++ b/src/decoder/mod.rs @@ -3,11 +3,13 @@ use std::io::{Read, Seek}; use Sample; use Source; -pub mod vorbis; -pub mod wav; +mod vorbis; +mod wav; /// Source of audio samples from decoding a file. -pub enum Decoder where R: Read + Seek { +pub struct Decoder(DecoderImpl) where R: Read + Seek; + +enum DecoderImpl where R: Read + Seek { Wav(wav::WavDecoder), Vorbis(vorbis::VorbisDecoder), } @@ -17,12 +19,12 @@ impl Decoder where R: Read + Seek + Send + 'static { let data = match wav::WavDecoder::new(data) { Err(data) => data, Ok(decoder) => { - return Decoder::Wav(decoder); + return Decoder(DecoderImpl::Wav(decoder)); } }; if let Ok(decoder) = vorbis::VorbisDecoder::new(data) { - return Decoder::Vorbis(decoder); + return Decoder(DecoderImpl::Vorbis(decoder)); } panic!("Invalid format"); @@ -34,17 +36,17 @@ impl Iterator for Decoder where R: Read + Seek { #[inline] fn next(&mut self) -> Option { - match self { - &mut Decoder::Wav(ref mut source) => source.next().map(|s| s.to_f32()), - &mut Decoder::Vorbis(ref mut source) => source.next(), + match self.0 { + DecoderImpl::Wav(ref mut source) => source.next().map(|s| s.to_f32()), + DecoderImpl::Vorbis(ref mut source) => source.next(), } } #[inline] fn size_hint(&self) -> (usize, Option) { - match self { - &Decoder::Wav(ref source) => source.size_hint(), - &Decoder::Vorbis(ref source) => source.size_hint(), + match self.0 { + DecoderImpl::Wav(ref source) => source.size_hint(), + DecoderImpl::Vorbis(ref source) => source.size_hint(), } } } @@ -54,25 +56,25 @@ impl Iterator for Decoder where R: Read + Seek { impl Source for Decoder where R: Read + Seek { #[inline] fn get_current_frame_len(&self) -> usize { - match self { - &Decoder::Wav(ref source) => source.get_current_frame_len(), - &Decoder::Vorbis(ref source) => source.get_current_frame_len(), + match self.0 { + DecoderImpl::Wav(ref source) => source.get_current_frame_len(), + DecoderImpl::Vorbis(ref source) => source.get_current_frame_len(), } } #[inline] fn get_channels(&self) -> u16 { - match self { - &Decoder::Wav(ref source) => source.get_channels(), - &Decoder::Vorbis(ref source) => source.get_channels(), + match self.0 { + DecoderImpl::Wav(ref source) => source.get_channels(), + DecoderImpl::Vorbis(ref source) => source.get_channels(), } } #[inline] fn get_samples_rate(&self) -> u32 { - match self { - &Decoder::Wav(ref source) => source.get_samples_rate(), - &Decoder::Vorbis(ref source) => source.get_samples_rate(), + match self.0 { + DecoderImpl::Wav(ref source) => source.get_samples_rate(), + DecoderImpl::Vorbis(ref source) => source.get_samples_rate(), } } }