From 562fbfae8aa17a2f3758710b23ec25e828d16cd6 Mon Sep 17 00:00:00 2001 From: Rubens Brandao Date: Wed, 17 Apr 2024 12:49:43 -0300 Subject: remove unnecessary transmutions --- rust/src/binaryview.rs | 4 ++-- rust/src/rc.rs | 1 - rust/src/types.rs | 26 +++++++++++--------------- 3 files changed, 13 insertions(+), 18 deletions(-) (limited to 'rust/src') diff --git a/rust/src/binaryview.rs b/rust/src/binaryview.rs index ecf75384..33fd9881 100644 --- a/rust/src/binaryview.rs +++ b/rust/src/binaryview.rs @@ -574,7 +574,7 @@ pub trait BinaryViewExt: BinaryViewBase { } } - fn define_auto_data_var(&self, dv: Ref) { + fn define_auto_data_var(&self, dv: &DataVariable) { unsafe { BNDefineDataVariable( self.as_ref().handle, @@ -585,7 +585,7 @@ pub trait BinaryViewExt: BinaryViewBase { } /// You likely would also like to call [`Self::define_user_symbol`] to bind this data variable with a name - fn define_user_data_var(&self, dv: Ref) { + fn define_user_data_var(&self, dv: &DataVariable) { unsafe { BNDefineUserDataVariable( self.as_ref().handle, diff --git a/rust/src/rc.rs b/rust/src/rc.rs index 91e51565..cdcae179 100644 --- a/rust/src/rc.rs +++ b/rust/src/rc.rs @@ -43,7 +43,6 @@ pub unsafe trait RefCountable: ToOwned> + Sized { // Represents an 'owned' reference tracked by the core // that we are responsible for cleaning up once we're // done with the encapsulated value. -#[repr(transparent)] pub struct Ref { contents: T, } diff --git a/rust/src/types.rs b/rust/src/types.rs index 05cad7e6..9fcd9d45 100644 --- a/rust/src/types.rs +++ b/rust/src/types.rs @@ -57,7 +57,6 @@ pub type MemberScope = BNMemberScope; // Confidence /// Compatible with the `BNType*WithConfidence` types -#[repr(C)] pub struct Conf { pub contents: T, pub confidence: u8, @@ -2487,9 +2486,8 @@ impl NameAndType { unsafe { mem::transmute::<_, &Type>(&self.0.type_) } } - pub fn type_with_confidence(&self) -> &Conf { - // the struct BNNameAndType contains a Conf inside of it, so this is safe - unsafe { mem::transmute::<_, &Conf>(&self.0.type_) } + pub fn type_with_confidence(&self) -> Conf<&Type> { + Conf::new(self.t(), self.0.typeConfidence) } } @@ -2545,11 +2543,11 @@ pub struct DataVariable(pub(crate) BNDataVariable); // impl DataVariable { // pub(crate) fn from_raw(var: &BNDataVariable) -> Self { -// Self { -// address: var.address, -// t: Conf::new(unsafe { Type::ref_from_raw(var.type_) }, var.typeConfidence), -// auto_discovered: var.autoDiscovered, -// } +// let var = DataVariable(*var); +// Self(BNDataVariable { +// type_: unsafe { Ref::into_raw(var.t().to_owned()).handle }, +// ..var.0 +// }) // } // } @@ -2558,18 +2556,16 @@ impl DataVariable { self.0.address } - pub fn auto_discovered(&self) -> &bool { - unsafe { mem::transmute(&self.0.autoDiscovered) } + pub fn auto_discovered(&self) -> bool { + self.0.autoDiscovered } pub fn t(&self) -> &Type { unsafe { mem::transmute(&self.0.type_) } } - pub fn type_with_confidence(&self) -> Conf> { - // if it was not for the `autoDiscovered: bool` between `type_` and - // `typeConfidence` this could have being a reference, like NameAndType - Conf::new(self.t().to_owned(), self.0.typeConfidence) + pub fn type_with_confidence(&self) -> Conf<&Type> { + Conf::new(self.t(), self.0.typeConfidence) } pub fn symbol(&self, bv: &BinaryView) -> Option> { -- cgit v1.3.1