Skip to content
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

Merged
merged 13 commits into from Mar 23, 2016
Merged

Added FFI bindings to lib.rs #1

merged 13 commits into from Mar 23, 2016

Conversation

ddefisher
Copy link
Contributor

No description provided.

@@ -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");
Copy link
Owner

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.

@jdm
Copy link
Owner

jdm commented Mar 18, 2016

Apart from those notes, this looks great! We should include the Rust example in an examples directory, as well.

@ddefisher
Copy link
Contributor Author

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) {

Copy link
Owner

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.

@jdm
Copy link
Owner

jdm commented Mar 21, 2016

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 :)

@@ -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();
Copy link
Owner

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?

Copy link
Owner

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.

@jdm
Copy link
Owner

jdm commented Mar 22, 2016

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(),
Copy link
Owner

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.

@jdm
Copy link
Owner

jdm commented Mar 23, 2016

Fix those comments and I'll merge this :)

@ddefisher
Copy link
Contributor Author

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();
Copy link
Owner

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.

@ddefisher
Copy link
Contributor Author

moved vector as_ptr method to function call argument

@jdm jdm merged commit 29311bc into jdm:master Mar 23, 2016
@jdm
Copy link
Owner

jdm commented Mar 23, 2016

Thanks!

Sign up for free to join this conversation on GitHub. Already have an account? Sign in to comment
Labels
None yet
Projects
None yet
Development

Successfully merging this pull request may close these issues.

None yet

2 participants