-
Notifications
You must be signed in to change notification settings - Fork 157
New issue
Have a question about this project? Sign up for a free GitHub account to open an issue and contact its maintainers and the community.
By clicking “Sign up for GitHub”, you agree to our terms of service and privacy statement. We’ll occasionally send you account related emails.
Already on GitHub? Sign in to your account
use bindgen to auto generate ffi rust code #398
base: master
Are you sure you want to change the base?
Conversation
Please fix the conflicts |
@Connor1996 I have fixed the conficts. |
librocksdb_sys/crocksdb/crocksdb/c.h
Outdated
@@ -940,10 +943,10 @@ extern C_ROCKSDB_LIBRARY_API void crocksdb_options_enable_statistics( | |||
crocksdb_options_t*, unsigned char); | |||
extern C_ROCKSDB_LIBRARY_API void crocksdb_options_reset_statistics( | |||
crocksdb_options_t*); | |||
extern C_ROCKSDB_LIBRARY_API bool crocksdb_load_latest_options( | |||
extern C_ROCKSDB_LIBRARY_API unsigned char crocksdb_load_latest_options( |
There was a problem hiding this comment.
Choose a reason for hiding this comment
The reason will be displayed to describe this comment to others. Learn more.
bindgen
generates FFI based on c.h
, but why we modify c.h
?
There was a problem hiding this comment.
Choose a reason for hiding this comment
The reason will be displayed to describe this comment to others. Learn more.
@Connor1996 this function was added by us, so not conform to c's style.
There was a problem hiding this comment.
Choose a reason for hiding this comment
The reason will be displayed to describe this comment to others. Learn more.
rocksdb's c.h has convention:
Bools have the type unsigned char (0 == false; rest == true)
There was a problem hiding this comment.
Choose a reason for hiding this comment
The reason will be displayed to describe this comment to others. Learn more.
got it
There was a problem hiding this comment.
Choose a reason for hiding this comment
The reason will be displayed to describe this comment to others. Learn more.
can you extract them to a separate PR?
There was a problem hiding this comment.
Choose a reason for hiding this comment
The reason will be displayed to describe this comment to others. Learn more.
OK
step 1 finished. Signed-off-by: fredchenbj <[email protected]>
Signed-off-by: fredchenbj <[email protected]>
cc31d36
to
7f4b70e
Compare
@fredchenbj awesome changes! Some general questions:
Thanks. |
It introduces clang dependency, which can be a pain to fix. You can learn how to avoid it from tikv/grpc-rs. |
@BusyJay You mean pre-generate binding? Yeah, that's what we plan to do. For easy reviewing, we plan to do it step by step. |
@@ -26,7 +28,7 @@ use std::slice; | |||
/// TablePropertiesCollector object per table and then call it sequentially | |||
pub trait TablePropertiesCollector { | |||
/// Will be called when a new key/value pair is inserted into the table. | |||
fn add(&mut self, key: &[u8], value: &[u8], entry_type: DBEntryType, seq: u64, file_size: u64); | |||
fn add(&mut self, key: &[i8], value: &[i8], entry_type: DBEntryType, seq: u64, file_size: u64); |
There was a problem hiding this comment.
Choose a reason for hiding this comment
The reason will be displayed to describe this comment to others. Learn more.
why changing u8
to i8
?
There was a problem hiding this comment.
Choose a reason for hiding this comment
The reason will be displayed to describe this comment to others. Learn more.
for const char*
in c.h is transferred to *const libc::c_char
by bindgen, which requires i8
. All transfer like this is for that. @Connor1996
There was a problem hiding this comment.
Choose a reason for hiding this comment
The reason will be displayed to describe this comment to others. Learn more.
see references:
- rust std implementation https://doc.rust-lang.org/src/std/ffi/c_str.rs.html#975,
- https://doc.rust-lang.org/std/os/raw/type.c_char.html,
- https://stackoverflow.com/questions/47684111/make-string-type-compatible-with-arm/47684200.
i8
or u8
is only due to how C char
represents in different platforms. But the data is always one-byte long. As str
in Rust is represented as &[u8]
, so we must convert it to u8
.
There was a problem hiding this comment.
Choose a reason for hiding this comment
The reason will be displayed to describe this comment to others. Learn more.
got it. I would fix it.
|
||
impl WriteStallInfo { | ||
pub fn cf_name(&self) -> &str { | ||
unsafe { fetch_str!(crocksdb_writestallinfo_cf_name(&self.0)) } | ||
} | ||
pub fn cur(&self) -> WriteStallCondition { | ||
pub fn cur(&self) -> crocksdb_writestallcondition_t { |
There was a problem hiding this comment.
Choose a reason for hiding this comment
The reason will be displayed to describe this comment to others. Learn more.
All public functions/traits of src/xxx.rs
should be unchanged, otherwise TiKV
or other users may change code dramatically. Consider rename type crocksdb_writestallcondition_t
to WriteStallCondition
. Same for other enums like DBEntryType
.
There was a problem hiding this comment.
Choose a reason for hiding this comment
The reason will be displayed to describe this comment to others. Learn more.
OK
@yiwu-arbug Thanks for your questions. for 3, the migration plan has three steps: 1) use the old crocksdb's c.h and bindgen to generate ffi rust code, and make it work for all dependency code; 2) replace with rocksdb's c.h, and make all code in c.h conform to rocksdb's style, here we would only include rocksdb's c.h and not change it at all; 3) try to use rocksdb cpp api to generate ffi rust code. This pr is only step 1. for 1, this pr has mainly three changes: 1) change crocksdb's c.h and c.cc to conform to bindgen's requirement; 2) use bindgen to generate ffi rust code; 3) change code in rust-rocksdb, including many type changes, to make those code could call the code geneated by bindgen. for 2, the generated code is in |
@fredchenbj for 3, can you give more details for step 1)? Does the generated code provide difference interface than the existing rust API? If so, how much work is it to update tikv? If that work is huge, is there way to split the work into small bits? Also for 3, if we will trying generating code from c++ directly, how useful step 2) would be? Currently the wrapper doesn't handle |
refer to #381.
just use old c.h and bindgen to gen ffi's rust code. and fix to pass
cargo build.
this is step 1, later replace with rocksdb's c.h to gen code.
Signed-off-by: fredchenbj [email protected]