Skip to content

Commit 6bcc120

Browse files
committed
Change types of filter_list and default_path to Option
Passing None (and hence std::ptr::nul()) is better than allocating an empty string. Update example.rs and README.md to reflect this.
1 parent 8085b51 commit 6bcc120

File tree

3 files changed

+27
-13
lines changed

3 files changed

+27
-13
lines changed

README.md

Lines changed: 3 additions & 3 deletions
Original file line numberDiff line numberDiff line change
@@ -21,15 +21,15 @@ Currently, only single file and save dialogs are supported, and the crate has be
2121
use nfd::*;
2222

2323
fn main() {
24-
let filter_str = "";
25-
let default_path = "~";
26-
let result = open_file_dialog(filter_str, default_path);
24+
25+
let result = open_file_dialog(None, None);
2726

2827
match result {
2928
NFDResult::Okay(file_path) => println!("File path = {:?}", file_path),
3029
NFDResult::Cancel => println!("User canceled"),
3130
NFDResult::Error(error) => println!("Error: {}", error),
3231
}
32+
3333
}
3434
```
3535

examples/example.rs

Lines changed: 1 addition & 3 deletions
Original file line numberDiff line numberDiff line change
@@ -3,9 +3,7 @@ extern crate nfd;
33
use nfd::*;
44

55
fn main() {
6-
let filter_str = "";
7-
let default_path = "~";
8-
let result = open_file_dialog(filter_str, default_path);
6+
let result = open_file_dialog(None, None);
97

108
match result {
119
NFDResult::Okay(file_path) => println!("File path = {:?}", file_path),

src/lib.rs

Lines changed: 23 additions & 7 deletions
Original file line numberDiff line numberDiff line change
@@ -23,32 +23,48 @@ enum DialogType {
2323

2424
/// Open single file dialog
2525
#[inline(always)]
26-
pub fn open_file_dialog(filter_list: &str, default_path: &str) -> NFDResult {
26+
pub fn open_file_dialog(filter_list: Option<&str>, default_path: Option<&str>) -> NFDResult {
2727
open_dialog(filter_list, default_path, &DialogType::SingleFile)
2828
}
2929

3030
/// Open save dialog
3131
#[inline(always)]
32-
pub fn open_save_dialog(filter_list: &str, default_path: &str) -> NFDResult {
32+
pub fn open_save_dialog(filter_list: Option<&str>, default_path: Option<&str>) -> NFDResult {
3333
open_dialog(filter_list, default_path, &DialogType::SaveFile)
3434
}
3535

36-
fn open_dialog(filter_list: &str, default_path: &str, dialog_type: &DialogType) -> NFDResult {
36+
fn open_dialog(filter_list: Option<&str>, default_path: Option<&str>, dialog_type: &DialogType) -> NFDResult {
3737
let result: nfdresult_t;
3838
let result_cstring;
3939

40-
let filter_list_cstring = CString::new(filter_list).unwrap();
41-
let default_path_cstring = CString::new(default_path).unwrap();
40+
let filter_list_cstring;
41+
let filter_list_ptr = match filter_list {
42+
Some(fl_str) => {
43+
filter_list_cstring = CString::new(fl_str).unwrap();
44+
filter_list_cstring.as_ptr()
45+
}
46+
None => std::ptr::null()
47+
};
48+
49+
let default_path_cstring;
50+
let default_path_ptr = match default_path {
51+
Some(dp_str) => {
52+
default_path_cstring = CString::new(dp_str).unwrap();
53+
default_path_cstring.as_ptr()
54+
}
55+
None => std::ptr::null()
56+
};
57+
4258
let out_path = CString::new("").unwrap().into_raw() as *mut *mut c_char;
4359

4460
unsafe {
4561
result = match dialog_type {
4662
&DialogType::SingleFile => {
47-
NFD_OpenDialog(filter_list_cstring.as_ptr(), default_path_cstring.as_ptr(), out_path)
63+
NFD_OpenDialog(filter_list_ptr, default_path_ptr, out_path)
4864
},
4965

5066
&DialogType::SaveFile => {
51-
NFD_SaveDialog(filter_list_cstring.as_ptr(), default_path_cstring.as_ptr(), out_path)
67+
NFD_SaveDialog(filter_list_ptr, default_path_ptr, out_path)
5268
},
5369
};
5470

0 commit comments

Comments
 (0)