Logo Questions Linux Laravel Mysql Ubuntu Git Menu
 

How can I write binding to a C function that expects an open file handle in Rust?

Tags:

rust

ffi

I've toyed with writing library bindings in Rust before, and it wasn't difficult. Now, however, I'm stuck: I'm trying to write a binding for librsync, and some of its functions expect you to pass an open file handle (a FILE* in C).

For primitive types, there's a straightforward way to pass them into C, (the same for pointers to primitive types). And, to be clear, I'm aware that the libc crate implements fopen, which in turn gives me a mut FILE* (which would eventually do the job). However, I was wondering if there is something in the Rust standard library that gives me a FILE* to pass to librsync — maybe something analog to std::ffi::CString.

like image 930
dodecaphonic Avatar asked Sep 09 '15 16:09

dodecaphonic


1 Answers

You could of course use a RawFd, transmute and call libc::funcs::posix88::stdio::fdopen(_, mode) with it. That would be highly unportable though.

like image 147
llogiq Avatar answered Oct 06 '22 08:10

llogiq