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

Kartik Ohri kartikohri13 at gmail.com
Thu Sep 10 22:17:30 CEST 2020


On Thu, Sep 10, 2020 at 11:14 PM Romain Vimont <rom1v at videolabs.io> wrote:

> 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.
>
> Finally, I understood what you meant the first time :D. I have applied the
changes and used ManuallyDrop.  I am inclined towards doing this on the API
level because this should be the right behaviour when we receive a stream
pointer from C. I am unsure how to do so as using the From trait we cannot
return ManuallyDrop.

> --------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
> _______________________________________________
> vlc-devel mailing list
> To unsubscribe or modify your subscription options:
> https://mailman.videolan.org/listinfo/vlc-devel
-------------- next part --------------
An HTML attachment was scrubbed...
URL: <http://mailman.videolan.org/pipermail/vlc-devel/attachments/20200911/7295068d/attachment.html>


More information about the vlc-devel mailing list