Skip to content

Commit

Permalink
Add into_samples method and iterator
Browse files Browse the repository at this point in the history
  • Loading branch information
linclelinkpart5 committed Jan 11, 2021
1 parent 2f05385 commit 66bf343
Showing 1 changed file with 40 additions and 1 deletion.
41 changes: 40 additions & 1 deletion src/lib.rs
Original file line number Diff line number Diff line change
Expand Up @@ -177,7 +177,11 @@ pub struct FlacSamples<R: ReadBytes> {
has_failed: bool,
}

// TODO: Add a `FlacIntoSamples`.
/// An iterator that yields samples read from a `FlacReader`.
pub struct FlacIntoSamples<R: ReadBytes> {
// This works because `ReadBytes` is implemented for both `&mut R` and `R`.
inner: FlacSamples<R>,
}

fn read_stream_header<R: ReadBytes>(input: &mut R) -> Result<()> {
// A FLAC stream starts with a 32-bit header 'fLaC' (big endian).
Expand Down Expand Up @@ -407,6 +411,29 @@ impl<R: io::Read> FlacReader<R> {
}
}

/// Same as `samples`, but takes ownership of the `FlacReader`.
///
/// See `samples()` for more info.
pub fn into_samples(self) -> FlacIntoSamples<BufferedReader<R>> {
match self.input {
FlacReaderState::Full(inp) => {
FlacIntoSamples {
inner: FlacSamples {
frame_reader: frame::FrameReader::new(inp),
block: Block::empty(),
sample: 0,
channel: 0,
has_failed: false,
}
}
}
FlacReaderState::MetadataOnly(..) => {
panic!("FlacReaderOptions::metadata_only must be false \
to be able to use FlacReader::into_samples()")
}
}
}

/// Destroys the FLAC reader and returns the underlying reader.
///
/// Because the reader employs buffering internally, anything in the buffer
Expand Down Expand Up @@ -491,3 +518,15 @@ impl<R: ReadBytes> Iterator for FlacSamples<R> {
Some(Ok(self.block.sample(self.channel, self.sample)))
}
}

impl<R: ReadBytes> Iterator for FlacIntoSamples<R> {
type Item = Result<i32>;

fn next(&mut self) -> Option<Self::Item> {
self.inner.next()
}

fn size_hint(&self) -> (usize, Option<usize>) {
self.inner.size_hint()
}
}

0 comments on commit 66bf343

Please sign in to comment.