forked from unum-cloud/ustore
-
Notifications
You must be signed in to change notification settings - Fork 0
Commit
This commit does not belong to any branch on this repository, and may belong to a fork outside of the repository.
Merge pull request #1 from michaelgrigoryan25/dev
- Loading branch information
Showing
6 changed files
with
51 additions
and
1,256 deletions.
There are no files selected for viewing
This file contains 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
This file contains 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
Original file line number | Diff line number | Diff line change |
---|---|---|
@@ -1,23 +1,20 @@ | ||
extern crate bindgen; | ||
|
||
use std::env::var; | ||
use std::path::PathBuf; | ||
|
||
fn main() { | ||
// Tell cargo to invalidate the built crate whenever the wrapper changes | ||
fn main() -> Result<(), Box<dyn std::error::Error>> { | ||
println!("cargo:rerun-if-changed=wrapper.h"); | ||
// Tell cargo to look for shared libraries in the specified directory | ||
println!("cargo:rustc-link-search=../include/ukv"); | ||
// println!("cargo:rustc-link-search=../include"); | ||
|
||
let bindings = bindgen::Builder::default() | ||
// .header("../include/ukv/db.h") | ||
// .header("../include/ukv/blobs.h") | ||
.header("./wrapper.h") | ||
.clang_args(&["-I../include", "-I../include/ukv"]) | ||
.detect_include_paths(true) | ||
.parse_callbacks(Box::new(bindgen::CargoCallbacks)) | ||
.generate() | ||
.expect("Unable to generate bindings"); | ||
.generate()?; | ||
|
||
// Write the bindings to the $OUT_DIR/bindings.rs file. | ||
let out_path = PathBuf::from("./src/ukv"); | ||
bindings.write_to_file(out_path.join("bindings.rs")).expect("Couldn't write bindings!"); | ||
let out_path = PathBuf::from(var("OUT_DIR")?); | ||
bindings.write_to_file(out_path.join("bindings.rs"))?; | ||
Ok(()) | ||
} |
This file contains 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
Original file line number | Diff line number | Diff line change |
---|---|---|
@@ -1,47 +1,62 @@ | ||
mod error; | ||
mod ukv; | ||
#![allow(non_upper_case_globals)] | ||
#![allow(non_camel_case_types)] | ||
#![allow(non_snake_case)] | ||
|
||
use std::ffi::CString; | ||
mod error; | ||
|
||
include!("../src/ukv/bindings.rs"); | ||
pub mod bindings { | ||
include!(concat!(env!("OUT_DIR"), "/bindings.rs")); | ||
} | ||
|
||
type UkvDatabaseInitType = ukv_database_init_t; | ||
type UkvDatabaseInitType = bindings::ukv_database_init_t; | ||
|
||
pub struct Database { | ||
db: UkvDatabaseInitType, | ||
pub db: UkvDatabaseInitType, | ||
} | ||
|
||
impl Default for Database { | ||
fn default() -> Self { | ||
let config: *const _ = std::ffi::CString::default().as_ptr(); | ||
let error: *mut _ = &mut std::ffi::CString::default().as_ptr(); | ||
let void_fn = &mut () as *mut _ as *mut std::ffi::c_void; | ||
let mut db = UkvDatabaseInitType { | ||
error, | ||
config, | ||
db: void_fn as _, | ||
}; | ||
|
||
unsafe { bindings::ukv_database_init(&mut db) }; | ||
Self { | ||
db, | ||
} | ||
} | ||
} | ||
|
||
impl Database { | ||
/// Open a new database using ukv_database_init() | ||
pub fn new() -> Result<Self, error::DataStoreError> { | ||
let c_str = CString::default(); | ||
let config: *const std::ffi::c_char = c_str.as_ptr(); | ||
let error: *mut *const std::ffi::c_char = &mut c_str.as_ptr(); | ||
let mut void_fn = &mut () as *mut _ as *mut std::ffi::c_void; | ||
let db_ptr = &mut void_fn; | ||
let mut database = UkvDatabaseInitType { | ||
config, | ||
db: db_ptr, | ||
pub fn new() -> Self { | ||
let config: *const _ = std::ffi::CString::default().as_ptr(); | ||
let error: *mut _ = &mut std::ffi::CString::default().as_ptr(); | ||
let void_fn = &mut () as *mut _ as *mut std::ffi::c_void; | ||
let mut db = UkvDatabaseInitType { | ||
error, | ||
}; | ||
// Calling C++ function generated from bindgen | ||
unsafe { | ||
ukv_database_init(&mut database); | ||
config, | ||
db: void_fn as _, | ||
}; | ||
|
||
Ok(Self { | ||
db: database, | ||
}) | ||
unsafe { bindings::ukv_database_init(&mut db) }; | ||
Self { | ||
db, | ||
} | ||
} | ||
|
||
pub fn contains_key() {} | ||
|
||
// Close a database using ukv_database_free() | ||
pub fn close() -> Result<(), error::DataStoreError> { | ||
let void_fn = &mut () as *mut _ as *mut std::ffi::c_void; | ||
unsafe { | ||
ukv_database_free(void_fn); | ||
}; | ||
unsafe { bindings::ukv_database_free(void_fn) }; | ||
|
||
Ok(()) | ||
} | ||
} |
Oops, something went wrong.