use crate::{Buf, BufMut, Channel, ChannelMut, ExactSizeBuf, ReadBuf};
pub struct Limit<B> {
buf: B,
limit: usize,
}
impl<B> Limit<B> {
pub(crate) fn new(buf: B, limit: usize) -> Self {
Self { buf, limit }
}
}
impl<B> Buf for Limit<B>
where
B: Buf,
{
type Sample = B::Sample;
type Channel<'this> = B::Channel<'this>
where
Self: 'this;
type IterChannels<'this> = IterChannels<B::IterChannels<'this>>
where
Self: 'this;
fn frames_hint(&self) -> Option<usize> {
let frames = self.buf.frames_hint()?;
Some(usize::min(frames, self.limit))
}
fn channels(&self) -> usize {
self.buf.channels()
}
fn get_channel(&self, channel: usize) -> Option<Self::Channel<'_>> {
Some(self.buf.get_channel(channel)?.limit(self.limit))
}
fn iter_channels(&self) -> Self::IterChannels<'_> {
IterChannels {
iter: self.buf.iter_channels(),
limit: self.limit,
}
}
}
impl<B> BufMut for Limit<B>
where
B: BufMut,
{
type ChannelMut<'this> = B::ChannelMut<'this>
where
Self: 'this;
type IterChannelsMut<'this> = IterChannelsMut<B::IterChannelsMut<'this>>
where
Self: 'this;
fn get_channel_mut(&mut self, channel: usize) -> Option<Self::ChannelMut<'_>> {
Some(self.buf.get_channel_mut(channel)?.limit(self.limit))
}
fn copy_channel(&mut self, from: usize, to: usize)
where
Self::Sample: Copy,
{
self.buf.copy_channel(from, to);
}
fn iter_channels_mut(&mut self) -> Self::IterChannelsMut<'_> {
IterChannelsMut {
iter: self.buf.iter_channels_mut(),
limit: self.limit,
}
}
}
impl<B> ExactSizeBuf for Limit<B>
where
B: ExactSizeBuf,
{
fn frames(&self) -> usize {
usize::min(self.buf.frames(), self.limit)
}
}
impl<B> ReadBuf for Limit<B>
where
B: ReadBuf,
{
fn remaining(&self) -> usize {
usize::min(self.buf.remaining(), self.limit)
}
fn advance(&mut self, n: usize) {
self.buf.advance(usize::min(n, self.limit));
}
}
iterators!(limit: usize => self.limit(limit));