Skip to content

Commit 8e7a264

Browse files
committed
Fix bug converting ptr to Option
1 parent 17dfe27 commit 8e7a264

File tree

1 file changed

+9
-8
lines changed

1 file changed

+9
-8
lines changed

src/file/ffi.rs

Lines changed: 9 additions & 8 deletions
Original file line numberDiff line numberDiff line change
@@ -38,20 +38,21 @@ impl convert::From<Ffi__File> for File {
3838
}
3939

4040
#[repr(C)]
41+
#[derive(Debug)]
4142
pub struct Ffi__FileOptions {
42-
backup_existing: Option<*const c_char>,
43-
chunk_size: Option<uint64_t>,
43+
backup_existing: *const c_char,
44+
chunk_size: *const uint64_t,
4445
}
4546

4647
impl convert::From<Ffi__FileOptions> for Vec<FileOptions> {
4748
fn from(ffi_opts: Ffi__FileOptions) -> Vec<FileOptions> {
4849
let mut opts = vec![];
49-
if let Some(c_suffix) = ffi_opts.backup_existing {
50-
let suffix = unsafe { str::from_utf8(CStr::from_ptr(c_suffix).to_bytes()).unwrap().to_string() };
50+
if ffi_opts.backup_existing != ptr::null() {
51+
let suffix = unsafe { str::from_utf8(CStr::from_ptr(ffi_opts.backup_existing).to_bytes()).unwrap().to_string() };
5152
opts.push(FileOptions::BackupExisting(suffix));
5253
}
53-
if let Some(size) = ffi_opts.chunk_size {
54-
opts.push(FileOptions::ChunkSize(size));
54+
if ffi_opts.chunk_size != ptr::null() {
55+
opts.push(FileOptions::ChunkSize(unsafe { ptr::read(ffi_opts.chunk_size) }));
5556
}
5657
opts
5758
}
@@ -284,8 +285,8 @@ mod tests {
284285
#[test]
285286
fn test_convert_ffi_file_options() {
286287
let ffi_file_options = Ffi__FileOptions {
287-
backup_existing: Some(CString::new("_bak").unwrap().into_raw()),
288-
chunk_size: Some(123),
288+
backup_existing: CString::new("_bak").unwrap().into_raw(),
289+
chunk_size: &123,
289290
};
290291
let file_options = Vec::<FileOptions>::from(ffi_file_options);
291292

0 commit comments

Comments
 (0)