#libc #stream #ffi #object

fopencookie

interface between std::io and libc::FILE

2 releases

0.1.1 May 10, 2024
0.1.0 May 10, 2024

#387 in Filesystem

Download history 178/week @ 2024-05-05 21/week @ 2024-05-12 9/week @ 2024-05-19

208 downloads per month

MIT/Apache

49KB
959 lines

Convert an io::Write/io::Read/io::Seek to a libc::FILE stream using the fopencookie syscall.

Great for passing rust traits across FFI.

let mut v = vec![];
let stream = fopencookie::IoCStream::writer(&mut v);

// Use the libc stream functions
assert_eq!(
    unsafe {
        libc::fprintf(stream.as_ptr(), c"hello, world!".as_ptr())
    },
    13 // all bytes written
);

// It's reflected in our rust type!
assert_eq!(v, b"hello, world!");

Trait objects are supported!

let mut reader: Box<dyn io::Read>;
let stream = fopencookie::IoCStream::reader(reader);

You can use the Builder for more flexibility.

let mut file: File;
let stream = fopencookie::Builder::new()
    .read()
    .write()
    .seek()
    .build(file);

lib.rs:

Convert an io::Write/io::Read/io::Seek to a libc::FILE stream using the fopencookie syscall.

Great for passing rust traits across FFI.

let mut v = vec![];
let stream = fopencookie::IoCStream::writer(&mut v);

// Use the libc stream functions
assert_eq!(
    unsafe {
        libc::fprintf(stream.as_ptr(), c"hello, world!".as_ptr())
    },
    13 // all bytes written
);

// It's reflected in our rust type!
assert_eq!(v, b"hello, world!");

Trait objects are supported!

let mut reader: Box<dyn io::Read>;
let stream = fopencookie::IoCStream::reader(reader);

You can use the Builder for more flexibility.

let mut file: File;
let stream = fopencookie::Builder::new()
    .read()
    .write()
    .seek()
    .build(file);

Dependencies

~0–2MB
~41K SLoC