Last active
May 19, 2016 22:03
-
-
Save jbaiter/8e95cd0e682b17e46af2e3b7de048de9 to your computer and use it in GitHub Desktop.
This file contains hidden or bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
extern crate libc; | |
extern crate fst; | |
use std::ffi::{CStr,CString}; | |
use std::fs::File; | |
use std::io; | |
use std::mem; | |
use fst::{IntoStreamer, Streamer, Levenshtein, Set, SetBuilder}; | |
fn cstr_to_str<'a>(s: *mut libc::c_char) -> &'a str { | |
let cstr = unsafe { CStr::from_ptr(s) }; | |
cstr.to_str().unwrap() | |
} | |
#[no_mangle] | |
pub extern fn cleanup_str(s: *mut libc::c_char) { | |
unsafe { CString::from_raw(s) }; | |
} | |
pub type FileSetBuilder = SetBuilder<&'static mut io::BufWriter<File>>; | |
#[no_mangle] | |
pub extern fn bufwriter_new(s: *mut libc::c_char) -> *mut io::BufWriter<File> { | |
let path = cstr_to_str(s); | |
Box::into_raw(Box::new(io::BufWriter::new(File::create(path).unwrap()))) | |
} | |
#[no_mangle] | |
pub extern fn fst_setbuilder_new(wtr_ptr: *mut io::BufWriter<File>) -> *mut FileSetBuilder { | |
let wtr = unsafe { | |
assert!(!wtr_ptr.is_null()); | |
&mut *wtr_ptr | |
}; | |
let build = SetBuilder::new(wtr).unwrap(); | |
Box::into_raw(Box::new(build)) | |
} | |
#[no_mangle] | |
pub extern fn fst_setbuilder_insert(ptr: *mut FileSetBuilder, s: *mut libc::c_char) { | |
let build = unsafe { | |
assert!(!ptr.is_null()); | |
&mut *ptr | |
}; | |
build.insert(cstr_to_str(s)).unwrap(); | |
} | |
#[no_mangle] | |
pub extern fn fst_setbuilder_finish(ptr: *mut FileSetBuilder) { | |
let build = unsafe { | |
assert!(!ptr.is_null()); | |
&mut *ptr | |
}; | |
/* This call fails with "cannot move out of borrowed content". | |
The problem seems to be that build is a reference, whereas finish() expects | |
an actual value. How can I solve this? */ | |
build.finish().unwrap() | |
} |
Sign up for free
to join this conversation on GitHub.
Already have an account?
Sign in to comment