From 4eb3d879debe5ba4676f5e3f6a36613eeea1ff87 Mon Sep 17 00:00:00 2001 From: =?UTF-8?q?Sebastian=20Dr=C3=B6ge?= Date: Wed, 10 Aug 2016 18:27:38 +0200 Subject: [PATCH] Move FFI usage out of actual Sink/Source implementations --- src/lib.rs | 12 +++++------- src/rsfilesink.rs | 7 +++---- src/rsfilesrc.rs | 7 +++---- src/rshttpsrc.rs | 7 +++---- src/rssink.rs | 5 ++--- src/rssource.rs | 5 ++--- 6 files changed, 18 insertions(+), 25 deletions(-) diff --git a/src/lib.rs b/src/lib.rs index 236fa08a..da28f41b 100644 --- a/src/lib.rs +++ b/src/lib.rs @@ -49,8 +49,7 @@ extern "C" { classification: *const c_char, author: *const c_char, rank: i32, - create_instance: extern "C" fn(controller: SourceController) - -> *mut Box, + create_instance: fn(controller: SourceController) -> Box, protocols: *const c_char, push_only: GBoolean) -> GBoolean; @@ -64,8 +63,7 @@ extern "C" { classification: *const c_char, author: *const c_char, rank: i32, - create_instance: extern "C" fn(controller: SinkController) - -> *mut Box, + create_instance: fn(controller: SinkController) -> Box, protocols: *const c_char) -> GBoolean; } @@ -83,7 +81,7 @@ pub extern "C" fn sources_register(plugin: *const c_void) -> GBoolean { .unwrap() .as_ptr(), 256 + 100, - FileSrc::new_ptr, + FileSrc::new_boxed, CString::new("file").unwrap().as_ptr(), GBoolean::False); @@ -96,7 +94,7 @@ pub extern "C" fn sources_register(plugin: *const c_void) -> GBoolean { .unwrap() .as_ptr(), 256 + 100, - HttpSrc::new_ptr, + HttpSrc::new_boxed, CString::new("http:https").unwrap().as_ptr(), GBoolean::True); } @@ -117,7 +115,7 @@ pub extern "C" fn sinks_register(plugin: *const c_void) -> GBoolean { .unwrap() .as_ptr(), 256 + 100, - FileSink::new_ptr, + FileSink::new_boxed, CString::new("file").unwrap().as_ptr()); } return GBoolean::True; diff --git a/src/rsfilesink.rs b/src/rsfilesink.rs index a6d2c4b2..161c74cf 100644 --- a/src/rsfilesink.rs +++ b/src/rsfilesink.rs @@ -38,7 +38,7 @@ unsafe impl Sync for FileSink {} unsafe impl Send for FileSink {} impl FileSink { - fn new(controller: SinkController) -> FileSink { + pub fn new(controller: SinkController) -> FileSink { FileSink { controller: controller, location: Mutex::new(None), @@ -47,9 +47,8 @@ impl FileSink { } } - pub extern "C" fn new_ptr(controller: SinkController) -> *mut Box { - let instance: Box> = Box::new(Box::new(FileSink::new(controller))); - return Box::into_raw(instance); + pub fn new_boxed(controller: SinkController) -> Box { + Box::new(FileSink::new(controller)) } } diff --git a/src/rsfilesrc.rs b/src/rsfilesrc.rs index fc859b28..6de2b800 100644 --- a/src/rsfilesrc.rs +++ b/src/rsfilesrc.rs @@ -39,7 +39,7 @@ unsafe impl Sync for FileSrc {} unsafe impl Send for FileSrc {} impl FileSrc { - fn new(controller: SourceController) -> FileSrc { + pub fn new(controller: SourceController) -> FileSrc { FileSrc { controller: controller, location: Mutex::new(None), @@ -48,9 +48,8 @@ impl FileSrc { } } - pub extern "C" fn new_ptr(controller: SourceController) -> *mut Box { - let instance: Box> = Box::new(Box::new(FileSrc::new(controller))); - return Box::into_raw(instance); + pub fn new_boxed(controller: SourceController) -> Box { + Box::new(FileSrc::new(controller)) } } diff --git a/src/rshttpsrc.rs b/src/rshttpsrc.rs index cf7cffb0..df02d9bb 100644 --- a/src/rshttpsrc.rs +++ b/src/rshttpsrc.rs @@ -47,7 +47,7 @@ unsafe impl Sync for HttpSrc {} unsafe impl Send for HttpSrc {} impl HttpSrc { - fn new(controller: SourceController) -> HttpSrc { + pub fn new(controller: SourceController) -> HttpSrc { HttpSrc { controller: controller, url: Mutex::new(None), @@ -61,9 +61,8 @@ impl HttpSrc { } } - pub extern "C" fn new_ptr(controller: SourceController) -> *mut Box { - let instance: Box> = Box::new(Box::new(HttpSrc::new(controller))); - return Box::into_raw(instance); + pub fn new_boxed(controller: SourceController) -> Box { + Box::new(HttpSrc::new(controller)) } pub fn do_request(&mut self, start: u64, stop: u64) -> bool { diff --git a/src/rssink.rs b/src/rssink.rs index 615a917d..3cb401a9 100644 --- a/src/rssink.rs +++ b/src/rssink.rs @@ -51,10 +51,9 @@ pub trait Sink: Sync + Send { #[no_mangle] pub extern "C" fn sink_new(sink: *mut c_void, - create_instance: extern "C" fn(controller: SinkController) - -> *mut Box) + create_instance: fn(controller: SinkController) -> Box) -> *mut Box { - create_instance(SinkController::new(sink)) + Box::into_raw(Box::new(create_instance(SinkController::new(sink)))) } #[no_mangle] diff --git a/src/rssource.rs b/src/rssource.rs index b6ac6c7b..8cb5d466 100644 --- a/src/rssource.rs +++ b/src/rssource.rs @@ -55,10 +55,9 @@ pub trait Source: Sync + Send { #[no_mangle] pub extern "C" fn source_new(source: *mut c_void, - create_instance: extern "C" fn(controller: SourceController) - -> *mut Box) + create_instance: fn(controller: SourceController) -> Box) -> *mut Box { - create_instance(SourceController::new(source)) + Box::into_raw(Box::new(create_instance(SourceController::new(source)))) } #[no_mangle]