[vlc-devel] [RFC: PATCH 1/2] libvlc: Add Rust API for writing modules in rust

Romain Vimont rom1v at videolabs.io
Thu Sep 10 19:44:31 CEST 2020


Hi Kartik,

Thank you for your work.

(comment below)

On Thu, Sep 10, 2020 at 10:17:29PM +0530, Kartik Ohri wrote:
> diff --git a/src/vlccore-rs/src/stream.rs b/src/vlccore-rs/src/stream.rs
> new file mode 100644
> index 0000000000..ea2ab4fc52
> --- /dev/null
> +++ b/src/vlccore-rs/src/stream.rs
> @@ -0,0 +1,119 @@
> +use vlccore_sys::stream as ffi;
> +use libc::{c_char, c_void};
> +use std::ffi::{CStr, CString};
> +use std::io::{Error, ErrorKind, Read, Result, Seek, SeekFrom};
> +use std::ptr::null_mut;
> +
> +pub struct Stream {
> +    stream: *mut ffi::stream_t,
> +    is_owned: bool

The ownership should be tracked by the Rust syntax.

To avoid the need for a flag here, I suggest the following changes to
your patch 2. The idea is to pass the "&mut Stream" to the real Rust
implementation, and mem::forget() the stream in the wrapper function.

--------8<-------------------------------------------------------------
diff --git a/modules/demux/playlist/cuesheet/src/capi.rs b/modules/demux/playlist/cuesheet/src/capi.rs
index b2e3e9e683..11eb92562c 100644
--- a/modules/demux/playlist/cuesheet/src/capi.rs
+++ b/modules/demux/playlist/cuesheet/src/capi.rs
@@ -14,14 +14,21 @@ pub struct CCuesheet {
 
 #[no_mangle]
 pub extern fn cuesheet_from_demux(s: *mut stream_t) -> *mut CCuesheet {
-    let stream = stream::Stream::from(s);
+    let mut stream = stream::Stream::from(s);
+    let cuesheet = rust_cuesheet_from_demux(&mut stream);
+    std::mem::forget(stream);
+
+    let c_cuesheet = Box::new(CCuesheet {cuesheet});
+    Box::into_raw(c_cuesheet)
+}
+
+fn rust_cuesheet_from_demux(stream: &mut stream::Stream) -> Cuesheet {
     let mut cuesheet = Cuesheet::default();
     let reader = BufReader::new(stream);
     reader.lines().for_each(|line| {
         line.map(|l| cuesheet.process_line(l.as_str()));
     });
-    let c_cuesheet = Box::new(CCuesheet {cuesheet});
-    Box::into_raw(c_cuesheet)
+    cuesheet
 }
 
 #[no_mangle]
--------8<-------------------------------------------------------------

Regards


More information about the vlc-devel mailing list