diff options
| author | Glenn Smith <glenn@vector35.com> | 2022-07-19 17:02:09 -0400 |
|---|---|---|
| committer | Glenn Smith <glenn@vector35.com> | 2022-09-29 21:02:21 -0400 |
| commit | 2e18783a2f6e29c225d177e8a0fb866a9c9d85df (patch) | |
| tree | 4921f16f509eaad788fafbda6ead42d79b05b82f /rust/src | |
| parent | 83eddd3eddc54b6362b5db37da0d1a28a115e7db (diff) | |
[Rust API] General type fixes and additions
Diffstat (limited to 'rust/src')
| -rw-r--r-- | rust/src/binaryview.rs | 18 | ||||
| -rw-r--r-- | rust/src/string.rs | 15 |
2 files changed, 27 insertions, 6 deletions
diff --git a/rust/src/binaryview.rs b/rust/src/binaryview.rs index e64dbb61..43eb1dfc 100644 --- a/rust/src/binaryview.rs +++ b/rust/src/binaryview.rs @@ -18,7 +18,7 @@ //! TODO : Mirror the Python docs for this use binaryninjacore_sys::*; -use std::convert::TryFrom; +use std::convert::{TryFrom, TryInto}; pub use binaryninjacore_sys::BNModificationStatus as ModificationStatus; @@ -144,6 +144,11 @@ pub trait BinaryViewExt: BinaryViewBase { } } + fn type_name(&self) -> BnString { + let ptr: *mut c_char = unsafe { BNGetViewType(self.as_ref().handle) }; + unsafe { BnString::from_raw(ptr) } + } + fn parent_view(&self) -> Result<Ref<BinaryView>> { let handle = unsafe { BNGetParentView(self.as_ref().handle) }; @@ -847,11 +852,12 @@ pub trait BinaryViewExt: BinaryViewBase { } } - fn get_metadata<T: TryFrom<&Metadata>, S: BnStrCompatible>( - &self, - key: S, - ) -> Option<std::result::Result<T, T::Error>> { - self.query_metadata(key).map(|md| T::try_from(md)) + fn get_metadata<T, S: BnStrCompatible>(&self, key: S) -> Option<Result<T>> + where + T: for<'a> TryFrom<&'a Metadata>, + { + self.query_metadata(key) + .map(|md| T::try_from(md.as_ref()).map_err(|_| ())) } fn remove_metadata<S: BnStrCompatible>(&self, key: S) { diff --git a/rust/src/string.rs b/rust/src/string.rs index e6de576b..42ff4fdc 100644 --- a/rust/src/string.rs +++ b/rust/src/string.rs @@ -17,6 +17,7 @@ use std::borrow::{Borrow, Cow}; use std::ffi::{CStr, CString}; use std::fmt; +use std::hash::{Hash, Hasher}; use std::mem; use std::ops::Deref; use std::os::raw; @@ -163,6 +164,20 @@ impl AsRef<[u8]> for BnString { } } +impl Hash for BnString { + fn hash<H: Hasher>(&self, state: &mut H) { + self.raw.hash(state) + } +} + +impl PartialEq for BnString { + fn eq(&self, other: &Self) -> bool { + self.deref() == other.deref() + } +} + +impl Eq for BnString {} + impl fmt::Display for BnString { fn fmt(&self, f: &mut fmt::Formatter) -> fmt::Result { write!(f, "{}", self.as_cstr().to_string_lossy()) |
