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

allow Create/Open database with progress to use FnMut #5631

Open
wants to merge 1 commit into
base: dev
Choose a base branch
from
Open
Changes from all commits
Commits
File filter

Filter by extension

Filter by extension

Conversations
Failed to load comments.
Loading
Jump to
Jump to file
Failed to load files.
Loading
Diff view
Diff view
83 changes: 52 additions & 31 deletions rust/src/filemetadata.rs
Original file line number Diff line number Diff line change
Expand Up @@ -205,28 +205,35 @@ impl FileMetadata {
}
}

pub fn create_database<S: BnStrCompatible>(
&self,
filename: S,
progress_func: Option<fn(usize, usize) -> bool>,
) -> bool {
pub fn create_database<S: BnStrCompatible>(&self, filename: S) -> bool {
let filename = filename.into_bytes_with_nul();
let filename_ptr = filename.as_ref().as_ptr() as *mut _;
let raw = "Raw".into_bytes_with_nul();
let raw_ptr = raw.as_ptr() as *mut _;

let handle = unsafe { BNGetFileViewOfType(self.handle, raw_ptr) };
match progress_func {
None => unsafe { BNCreateDatabase(handle, filename_ptr, ptr::null_mut()) },
Some(func) => unsafe {
BNCreateDatabaseWithProgress(
handle,
filename_ptr,
func as *mut libc::c_void,
Some(cb_progress_func),
ptr::null_mut(),
)
},
unsafe { BNCreateDatabase(handle, filename_ptr, ptr::null_mut()) }
}

pub fn create_database_with_progress<S, F>(&self, filename: S, mut progress_func: F) -> bool
where
S: BnStrCompatible,
F: FnMut(usize, usize) -> bool,
{
let filename = filename.into_bytes_with_nul();
let filename_ptr = filename.as_ref().as_ptr() as *mut _;
let raw = "Raw".into_bytes_with_nul();
let raw_ptr = raw.as_ptr() as *mut _;

let handle = unsafe { BNGetFileViewOfType(self.handle, raw_ptr) };
unsafe {
BNCreateDatabaseWithProgress(
handle,
filename_ptr,
&mut progress_func as *mut F as *mut libc::c_void,
Some(cb_progress_func::<F>),
ptr::null_mut(),
)
}
}

Expand Down Expand Up @@ -257,24 +264,38 @@ impl FileMetadata {
}
}

pub fn open_database<S: BnStrCompatible>(
pub fn open_database<S: BnStrCompatible>(&self, filename: S) -> Result<Ref<BinaryView>, ()> {
let filename = filename.into_bytes_with_nul();
let filename_ptr = filename.as_ref().as_ptr() as *mut _;

let view = unsafe { BNOpenExistingDatabase(self.handle, filename_ptr) };

if view.is_null() {
Err(())
} else {
Ok(unsafe { BinaryView::from_raw(view) })
}
}

pub fn open_database_with_progress<S, F>(
&self,
filename: S,
progress_func: Option<fn(usize, usize) -> bool>,
) -> Result<Ref<BinaryView>, ()> {
mut progress_func: F,
) -> Result<Ref<BinaryView>, ()>
where
S: BnStrCompatible,
F: FnMut(usize, usize) -> bool,
{
let filename = filename.into_bytes_with_nul();
let filename_ptr = filename.as_ref().as_ptr() as *mut _;

let view = match progress_func {
None => unsafe { BNOpenExistingDatabase(self.handle, filename_ptr) },
Some(func) => unsafe {
BNOpenExistingDatabaseWithProgress(
self.handle,
filename_ptr,
func as *mut libc::c_void,
Some(cb_progress_func),
)
},
let view = unsafe {
BNOpenExistingDatabaseWithProgress(
self.handle,
filename_ptr,
&mut progress_func as *mut F as *mut libc::c_void,
Some(cb_progress_func::<F>),
)
};

if view.is_null() {
Expand Down Expand Up @@ -305,11 +326,11 @@ unsafe impl RefCountable for FileMetadata {
}
}

unsafe extern "C" fn cb_progress_func(
unsafe extern "C" fn cb_progress_func<F: FnMut(usize, usize) -> bool>(
ctxt: *mut ::std::os::raw::c_void,
progress: usize,
total: usize,
) -> bool {
let func: fn(usize, usize) -> bool = core::mem::transmute(ctxt);
let func: &mut F = &mut *(ctxt as *mut F);
func(progress, total)
}
Loading