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
Added FFI bindings to lib.rs #1
Conversation
@@ -13,5 +13,6 @@ fn main() { | |||
.unwrap() | |||
.success()); | |||
println!("cargo:rustc-flags=-L native={}", env::var("OUT_DIR").unwrap()); | |||
println!("cargo:rustc-flags=-l tinyfiledialogs -L target/debug/build/tinyfiledialogs-4949874712ebf6d8/out"); |
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.
I believe this should be equivalent to println!("cargo:rustc-flags=-l tinyfiledialogs -L {}", env::var("OUT_DIR").unwrap())
, which is also future-proof.
Apart from those notes, this looks great! We should include the Rust example in an |
I have added the rust example I am not sure if that is the best way to call the C functions. |
|
||
|
||
fn message_box(title: &CStr, message: &CStr, dialog_type: &CStr, icon: &CStr, button: c_int) { | ||
|
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.
nit: remove the extra newline after each fn
line.
Looks good apart from the mostly stylistic comments! I've also made a pull request to your branch to turn on automated tests using TravisCI, so please merge that :) |
Integrate with TravisCI
@@ -124,14 +113,12 @@ fn main() | |||
|
|||
let color_title = CString::new("Color Choose").unwrap(); | |||
let color_default_hex = CString::new("Color Choose").unwrap(); | |||
let color_default_RGB : [c_uchar ; 3] = [250,0,0]; | |||
let color_result_RGB : [c_uchar ; 3] = [100,0,0]; | |||
let color_default_rgb = CString::new("FFF").unwrap(); |
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.
This doesn't seem to match the documentation at https://github.com/jdm/tinyfiledialogs/blob/master/libtinyfiledialogs/tinyfiledialogs.h#L128 . Why this change?
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.
We should really be passing &[100, 0, 0]
and using color_default_rgb.as_ptr()
when calling the FFI function.
Looking good apart from the most recent comments! |
title.as_ptr(), | ||
path.as_ptr(), | ||
num_patterns, | ||
filter_patterns.as_ptr(), | ||
filter_patterns.iter().map(|c| c.as_ptr()).collect::<Vec<*const c_char>>().as_ptr(), |
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.
This needs to be in a local variable in order to be safe; otherwise the pointer is pointing at memory that is freed as soon as the collect() expression goes out of scope.
Fix those comments and I'll merge this :) |
jdm I have moved the map as_ptr into a local variable |
@@ -33,12 +33,13 @@ fn save_file_dialog(title: &CStr, path: &CStr, | |||
num_patterns: c_int, | |||
filter_pattern_one: &CStr, des: &CStr) -> *const c_char { | |||
let filter_patterns : [&CStr; 1] = [filter_pattern_one]; | |||
let ptr_filter_patterns = filter_patterns.iter().map(|c| c.as_ptr()).collect::<Vec<*const c_char>>().as_ptr(); |
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.
Sorry, this is still unsafe. The as_ptr() is what makes the Vec go out of scope; it should be moved to the actual argument to the function call instead.
moved vector as_ptr method to function call argument |
Thanks! |
No description provided.