Make ScopedBuffer a bit safer and explicit

This commit is contained in:
Sebastian Dröge 2016-09-12 17:30:51 +02:00
parent 61b5420102
commit 1ef938b6ec
3 changed files with 11 additions and 8 deletions

View file

@ -440,15 +440,19 @@ impl<'a> Drop for ReadWriteBufferMap<'a> {
}
}
#[repr(C)]
pub struct ScopedBufferPtr(*mut c_void);
pub struct ScopedBuffer<'a> {
buffer: Buffer,
phantom: PhantomData<&'a c_void>,
#[allow(dead_code)]
phantom: PhantomData<&'a ScopedBufferPtr>,
}
impl<'a> ScopedBuffer<'a> {
pub unsafe fn new(ptr: *mut c_void) -> ScopedBuffer<'a> {
pub unsafe fn new(ptr: &'a ScopedBufferPtr) -> ScopedBuffer<'a> {
ScopedBuffer {
buffer: Buffer::new_from_ptr_scoped(ptr),
buffer: Buffer::new_from_ptr_scoped(ptr.0),
phantom: PhantomData,
}
}
@ -467,4 +471,3 @@ impl<'a> DerefMut for ScopedBuffer<'a> {
&mut self.buffer
}
}

View file

@ -205,12 +205,12 @@ pub unsafe extern "C" fn sink_stop(ptr: *const SinkWrapper) -> GBoolean {
#[no_mangle]
pub unsafe extern "C" fn sink_render(ptr: *const SinkWrapper,
buffer: *mut c_void)
buffer: ScopedBufferPtr)
-> GstFlowReturn {
let wrap: &SinkWrapper = &*ptr;
panic_to_error!(wrap, GstFlowReturn::Error, {
let sink = &mut wrap.sink.lock().unwrap();
let buffer = ScopedBuffer::new(buffer);
let buffer = ScopedBuffer::new(&buffer);
match sink.render(&buffer) {
Ok(..) => GstFlowReturn::Ok,

View file

@ -235,13 +235,13 @@ pub unsafe extern "C" fn source_stop(ptr: *const SourceWrapper) -> GBoolean {
pub unsafe extern "C" fn source_fill(ptr: *const SourceWrapper,
offset: u64,
length: u32,
buffer: *mut c_void)
buffer: ScopedBufferPtr)
-> GstFlowReturn {
let wrap: &SourceWrapper = &*ptr;
panic_to_error!(wrap, GstFlowReturn::Error, {
let source = &mut wrap.source.lock().unwrap();
let mut buffer = ScopedBuffer::new(buffer);
let mut buffer = ScopedBuffer::new(&buffer);
match source.fill(offset, length, &mut buffer) {
Ok(()) => GstFlowReturn::Ok,