Implement std::io::Read on Adapter

Patch based on the one from Thibault Saunier for gst-plugin-rs's Adapter
This commit is contained in:
Sebastian Dröge 2017-11-06 14:48:59 +02:00
parent 95204c2294
commit 2f7ee30aa1

View file

@ -10,6 +10,7 @@ use ffi;
use glib::translate::*;
use gst;
use Adapter;
use std::io;
impl Adapter {
pub fn copy(&self, offset: usize, dest: &mut [u8]) {
@ -30,3 +31,28 @@ impl Adapter {
}
}
}
impl io::Read for Adapter {
fn read(&mut self, buf: &mut [u8]) -> Result<usize, io::Error> {
let mut len = self.available();
if len == 0 {
return Err(io::Error::new(
io::ErrorKind::WouldBlock,
format!(
"Missing data: requesting {} but only got {}.",
buf.len(),
len
),
));
}
if buf.len() < len {
len = buf.len();
}
self.copy(0, &mut buf[0..len]);
self.flush(len);
Ok(len)
}
}