gstreamer: add MemoryRef::dump()

This commit is contained in:
Guillaume Desmottes 2019-12-23 15:14:11 +05:30
parent a418dcbe05
commit c298577139
3 changed files with 58 additions and 0 deletions

View file

@ -28,6 +28,7 @@ serde = { version = "1.0", optional = true }
serde_bytes = { version = "0.11", optional = true }
serde_derive = { version = "1.0", optional = true }
paste = { version = "0.1" }
pretty-hex = "0.1"
[build-dependencies]
rustdoc-stripper = { version = "0.1", optional = true }

View file

@ -36,6 +36,8 @@ extern crate futures_core;
extern crate muldiv;
extern crate pretty_hex;
#[cfg(feature = "ser_de")]
extern crate serde;
#[cfg(feature = "ser_de")]

View file

@ -308,6 +308,10 @@ impl MemoryRef {
assert!(offset + (size as isize) < (self.get_maxsize() as isize));
unsafe { gst_sys::gst_memory_resize(self.as_mut_ptr(), offset, size) }
}
pub fn dump(&self, size: Option<usize>) -> Dump {
Dump { memory: self, size }
}
}
impl<'a, T> MemoryMap<'a, T> {
@ -466,3 +470,54 @@ impl<T> Eq for MappedMemory<T> {}
unsafe impl<T> Send for MappedMemory<T> {}
unsafe impl<T> Sync for MappedMemory<T> {}
pub struct Dump<'a> {
memory: &'a MemoryRef,
size: Option<usize>,
}
impl<'a> Dump<'a> {
fn fmt(&self, f: &mut fmt::Formatter, debug: bool) -> fmt::Result {
use pretty_hex::*;
let map = self.memory.map_readable().expect("Failed to map memory");
let data = map.as_slice();
let size = self.size.unwrap_or_else(|| self.memory.get_size());
let data = &data[0..size];
if debug {
write!(f, "{:?}", data.hex_dump())
} else {
write!(f, "{}", data.hex_dump())
}
}
}
impl<'a> fmt::Display for Dump<'a> {
fn fmt(&self, f: &mut fmt::Formatter<'_>) -> fmt::Result {
self.fmt(f, false)
}
}
impl<'a> fmt::Debug for Dump<'a> {
fn fmt(&self, f: &mut fmt::Formatter) -> fmt::Result {
self.fmt(f, true)
}
}
#[cfg(test)]
mod tests {
#[test]
fn test_dump() {
::init().unwrap();
let mem = ::Memory::from_slice(vec![1, 2, 3, 4]);
println!("{}", mem.dump(Some(mem.get_size())));
let mem = ::Memory::from_slice(vec![1, 2, 3, 4]);
println!("{:?}", mem.dump(Some(2)));
let mem = ::Memory::from_slice(vec![0; 64]);
dbg!(mem.dump(None));
}
}