From ac94730a927db93810a1b17d92ce92a98113e0ab Mon Sep 17 00:00:00 2001 From: Josh Ferrell Date: Mon, 8 Jul 2024 16:04:24 -0400 Subject: ExternalLibrary rust api fixes --- rust/src/externallibrary.rs | 37 +++++++++++++++++++++---------------- 1 file changed, 21 insertions(+), 16 deletions(-) (limited to 'rust/src') diff --git a/rust/src/externallibrary.rs b/rust/src/externallibrary.rs index c7edbbcb..a5dcefc8 100644 --- a/rust/src/externallibrary.rs +++ b/rust/src/externallibrary.rs @@ -51,14 +51,18 @@ impl ExternalLibrary { } /// Get the file backing this external library - pub fn backing_file(&self) -> ProjectFile { + pub fn backing_file(&self) -> Option { let result = unsafe { BNExternalLibraryGetBackingFile(self.as_raw()) }; - unsafe { ProjectFile::from_raw(ptr::NonNull::new(result).unwrap()) } + let handle = ptr::NonNull::new(result)?; + Some(unsafe { ProjectFile::from_raw(handle) }) } /// Set the file backing this external library - pub fn set_backing_file(&self, file: &ProjectFile) { - unsafe { BNExternalLibrarySetBackingFile(self.as_raw(), file.as_raw()) } + pub fn set_backing_file(&self, file: Option<&ProjectFile>) { + let file_handle = file + .map(|x| unsafe {x.as_raw() as *mut _}) + .unwrap_or(ptr::null_mut()); + unsafe { BNExternalLibrarySetBackingFile(self.as_raw(), file_handle) } } } @@ -124,14 +128,18 @@ impl ExternalLocation { } /// Get the ExternalLibrary that this ExternalLocation targets - pub fn library(&self) -> ExternalLibrary { + pub fn library(&self) -> Option { let result = unsafe { BNExternalLocationGetExternalLibrary(self.as_raw()) }; - unsafe { ExternalLibrary::from_raw(ptr::NonNull::new(result).unwrap()) } + let handle = ptr::NonNull::new(result)?; + Some(unsafe { ExternalLibrary::from_raw(handle) }) } /// Set the ExternalLibrary that this ExternalLocation targets - pub fn set_external_library(&self, lib: &ExternalLibrary) { - unsafe { BNExternalLocationSetExternalLibrary(self.as_raw(), lib.as_raw()) } + pub fn set_external_library(&self, lib: Option<&ExternalLibrary>) { + let lib_handle = lib + .map(|x| unsafe {x.as_raw() as *mut _}) + .unwrap_or(ptr::null_mut()); + unsafe { BNExternalLocationSetExternalLibrary(self.as_raw(), lib_handle) } } /// Check if this ExternalLocation has a target address @@ -169,19 +177,16 @@ impl ExternalLocation { }) } - /// Remove the symbol pointed to by this ExternalLocation. - pub fn remove_target_symbol(&self) -> bool { - unsafe { BNExternalLocationSetTargetSymbol(self.as_raw(), ptr::null_mut()) } - } - /// Set the symbol pointed to by this ExternalLocation. /// ExternalLocations must have a valid target address and/or symbol set. - pub fn set_target_symbol(&self, symbol: S) -> bool { - let symbol = symbol.into_bytes_with_nul(); + pub fn set_target_symbol(&self, symbol: Option) -> bool { + let symbol = symbol + .map(|x| x.into_bytes_with_nul().as_ref().as_ptr() as *const ffi::c_char) + .unwrap_or(ptr::null_mut()); unsafe { BNExternalLocationSetTargetSymbol( self.as_raw(), - symbol.as_ref().as_ptr() as *const ffi::c_char, + symbol, ) } } -- cgit v1.3.1