Skip to content

Instantly share code, notes, and snippets.

@vi
Last active December 19, 2015 18:49
Show Gist options
  • Select an option

  • Save vi/6001905 to your computer and use it in GitHub Desktop.

Select an option

Save vi/6001905 to your computer and use it in GitHub Desktop.
Sluggish attempt to test how Rust binds to C code
/* automatically generated by rust-bindgen */
// sources: https://raw.github.com/vi/forsnapshotfs/master/simplecow.h https://raw.github.com/vi/forsnapshotfs/master/simplecow.c
use std::libc::*;
//pub type backing_read_t = *u8;
pub type Struct_simplecow = c_void;
#[link_args = "-lsimplecow"]
pub extern "C" {
fn simplecow_write(cow: *mut Struct_simplecow, offset: c_longlong,
size: c_int, buf: *c_schar) -> c_int;
fn simplecow_read(cow: *mut Struct_simplecow, offset: c_longlong,
size: c_int, buf: *mut c_schar) -> c_int;
fn simplecow_create(br: c_void /* what to write here ? */, usr: *mut c_void) ->
*mut Struct_simplecow;
fn simplecow_destroy(cow: *mut Struct_simplecow);
}
// stub reading function that just fills the buffer with \x38.
fn myread(usr:c_void, offset: c_longlong, size:c_int, buf:c_schar) -> c_int {
unsafe {
std::ptr::set_memory(buf as (*mut u8), 0x38, size as uint);
}
size
}
//typedef int (*backing_read_t)(void* usr, long long int offset, int size, char* buf);
fn main() {
//let mut q : Struct_simplecow =
// simplecow_create(myread as c_void /* what to write here ?*/, std::ptr::mut_null());
//let mut q : Struct_simplecow = std::ptr::mut_null(); // also fails
let mut q : (*mut Struct_simplecow);
let mut buf : [i8, .. 3] = [4,5,6];
//simplecow_read(q, 111, 3, buf as (*mut i8)); // also fails...
println(fmt!("%02x %02x %02x\n", buf[0] as uint, buf[1] as uint, buf[2] as uint));
}
Sign up for free to join this conversation on GitHub. Already have an account? Sign in to comment