Skip to content

Commit

Permalink
remove unnecessary transmutions
Browse files Browse the repository at this point in the history
  • Loading branch information
rbran committed Apr 17, 2024
1 parent 58673b2 commit 562fbfa
Show file tree
Hide file tree
Showing 3 changed files with 13 additions and 18 deletions.
4 changes: 2 additions & 2 deletions rust/src/binaryview.rs
Original file line number Diff line number Diff line change
Expand Up @@ -574,7 +574,7 @@ pub trait BinaryViewExt: BinaryViewBase {
}
}

fn define_auto_data_var(&self, dv: Ref<DataVariable>) {
fn define_auto_data_var(&self, dv: &DataVariable) {
unsafe {
BNDefineDataVariable(
self.as_ref().handle,
Expand All @@ -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<DataVariable>) {
fn define_user_data_var(&self, dv: &DataVariable) {
unsafe {
BNDefineUserDataVariable(
self.as_ref().handle,
Expand Down
1 change: 0 additions & 1 deletion rust/src/rc.rs
Original file line number Diff line number Diff line change
Expand Up @@ -43,7 +43,6 @@ pub unsafe trait RefCountable: ToOwned<Owned = Ref<Self>> + 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<T: RefCountable> {
contents: T,
}
Expand Down
26 changes: 11 additions & 15 deletions rust/src/types.rs
Original file line number Diff line number Diff line change
Expand Up @@ -57,7 +57,6 @@ pub type MemberScope = BNMemberScope;
// Confidence

/// Compatible with the `BNType*WithConfidence` types
#[repr(C)]
pub struct Conf<T> {
pub contents: T,
pub confidence: u8,
Expand Down Expand Up @@ -2487,9 +2486,8 @@ impl NameAndType {
unsafe { mem::transmute::<_, &Type>(&self.0.type_) }
}

pub fn type_with_confidence(&self) -> &Conf<Type> {
// the struct BNNameAndType contains a Conf inside of it, so this is safe
unsafe { mem::transmute::<_, &Conf<Type>>(&self.0.type_) }
pub fn type_with_confidence(&self) -> Conf<&Type> {
Conf::new(self.t(), self.0.typeConfidence)
}
}

Expand Down Expand Up @@ -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
// })
// }
// }

Expand All @@ -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<Ref<Type>> {
// 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<Ref<Symbol>> {
Expand Down

0 comments on commit 562fbfa

Please sign in to comment.