From 2f7ee30aa118589f4ae8e0bae772e7e10ea825ea Mon Sep 17 00:00:00 2001 From: =?UTF-8?q?Sebastian=20Dr=C3=B6ge?= Date: Mon, 6 Nov 2017 14:48:59 +0200 Subject: [PATCH] Implement std::io::Read on Adapter Patch based on the one from Thibault Saunier for gst-plugin-rs's Adapter --- gstreamer-base/src/adapter.rs | 26 ++++++++++++++++++++++++++ 1 file changed, 26 insertions(+) diff --git a/gstreamer-base/src/adapter.rs b/gstreamer-base/src/adapter.rs index 9bf6d6290..687ccc984 100644 --- a/gstreamer-base/src/adapter.rs +++ b/gstreamer-base/src/adapter.rs @@ -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 { + 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) + } +}