Skip to content

Commit

Permalink
hide array implementation details
Browse files Browse the repository at this point in the history
  • Loading branch information
rbran committed May 3, 2024
1 parent 92a468d commit bfe4044
Show file tree
Hide file tree
Showing 18 changed files with 99 additions and 194 deletions.
13 changes: 3 additions & 10 deletions rust/src/backgroundtask.rs
Original file line number Diff line number Diff line change
Expand Up @@ -104,21 +104,14 @@ unsafe impl RefCountable for BackgroundTask {
impl CoreArrayProvider for BackgroundTask {
type Raw = *mut BNBackgroundTask;
type Context = ();
type Wrapped<'a> = Guard<'a, BackgroundTask>;
}

unsafe impl CoreOwnedArrayProvider for BackgroundTask {
unsafe impl CoreArrayProviderInner for BackgroundTask {
unsafe fn free(raw: *mut *mut BNBackgroundTask, count: usize, _context: &()) {
BNFreeBackgroundTaskList(raw, count);
}
}

unsafe impl CoreArrayWrapper for BackgroundTask {
type Wrapped<'a> = Guard<'a, BackgroundTask>;

unsafe fn wrap_raw<'a>(
raw: &'a *mut BNBackgroundTask,
context: &'a (),
) -> Self::Wrapped<'a> {
unsafe fn wrap_raw<'a>(raw: &'a *mut BNBackgroundTask, context: &'a ()) -> Self::Wrapped<'a> {
Guard::new(BackgroundTask::from_raw(*raw), context)
}
}
Expand Down
16 changes: 4 additions & 12 deletions rust/src/basicblock.rs
Original file line number Diff line number Diff line change
Expand Up @@ -68,17 +68,13 @@ pub struct EdgeContext<'a, C: 'a + BlockContext> {
impl<'a, C: 'a + BlockContext> CoreArrayProvider for Edge<'a, C> {
type Raw = BNBasicBlockEdge;
type Context = EdgeContext<'a, C>;
type Wrapped<'b> = Edge<'b, C> where 'a: 'b;
}

unsafe impl<'a, C: 'a + BlockContext> CoreOwnedArrayProvider for Edge<'a, C> {
unsafe impl<'a, C: 'a + BlockContext> CoreArrayProviderInner for Edge<'a, C> {
unsafe fn free(raw: *mut Self::Raw, count: usize, _context: &Self::Context) {
BNFreeBasicBlockEdgeList(raw, count);
}
}

unsafe impl<'a, C: BlockContext> CoreArrayWrapper for Edge<'a, C> {
type Wrapped<'b> = Edge<'b, C> where 'a: 'b;

unsafe fn wrap_raw<'b>(raw: &'b Self::Raw, context: &'b Self::Context) -> Self::Wrapped<'b> {
let edge_target = Guard::new(
BasicBlock::from_raw(raw.target, context.orig_block.context.clone()),
Expand Down Expand Up @@ -301,17 +297,13 @@ unsafe impl<C: BlockContext> RefCountable for BasicBlock<C> {
impl<C: BlockContext> CoreArrayProvider for BasicBlock<C> {
type Raw = *mut BNBasicBlock;
type Context = C;
type Wrapped<'a> = Guard<'a, BasicBlock<C>> where C: 'a;
}

unsafe impl<C: BlockContext> CoreOwnedArrayProvider for BasicBlock<C> {
unsafe impl<C: BlockContext> CoreArrayProviderInner for BasicBlock<C> {
unsafe fn free(raw: *mut Self::Raw, count: usize, _context: &Self::Context) {
BNFreeBasicBlockList(raw, count);
}
}

unsafe impl<C: BlockContext> CoreArrayWrapper for BasicBlock<C> {
type Wrapped<'a> = Guard<'a, BasicBlock<C>> where C: 'a;

unsafe fn wrap_raw<'a>(raw: &'a Self::Raw, context: &'a Self::Context) -> Self::Wrapped<'a> {
Guard::new(BasicBlock::from_raw(*raw, context.clone()), context)
}
Expand Down
10 changes: 3 additions & 7 deletions rust/src/callingconvention.rs
Original file line number Diff line number Diff line change
Expand Up @@ -26,7 +26,7 @@ use binaryninjacore_sys::*;

use crate::architecture::{Architecture, ArchitectureExt, CoreArchitecture, Register};
use crate::rc::{
CoreArrayProvider, CoreArrayWrapper, CoreOwnedArrayProvider, Guard, Ref, RefCountable,
CoreArrayProvider, CoreArrayProviderInner, Guard, Ref, RefCountable,
};
use crate::string::*;

Expand Down Expand Up @@ -686,17 +686,13 @@ unsafe impl<A: Architecture> RefCountable for CallingConvention<A> {
impl<A: Architecture> CoreArrayProvider for CallingConvention<A> {
type Raw = *mut BNCallingConvention;
type Context = A::Handle;
type Wrapped<'a> = Guard<'a, CallingConvention<A>>;
}

unsafe impl<A: Architecture> CoreOwnedArrayProvider for CallingConvention<A> {
unsafe impl<A: Architecture> CoreArrayProviderInner for CallingConvention<A> {
unsafe fn free(raw: *mut *mut BNCallingConvention, count: usize, _content: &Self::Context) {
BNFreeCallingConventionList(raw, count);
}
}

unsafe impl<A: Architecture> CoreArrayWrapper for CallingConvention<A> {
type Wrapped<'a> = Guard<'a, CallingConvention<A>>;

unsafe fn wrap_raw<'a>(raw: &'a Self::Raw, context: &'a Self::Context) -> Self::Wrapped<'a> {
Guard::new(
CallingConvention {
Expand Down
8 changes: 2 additions & 6 deletions rust/src/custombinaryview.rs
Original file line number Diff line number Diff line change
Expand Up @@ -290,17 +290,13 @@ impl BinaryViewTypeBase for BinaryViewType {
impl CoreArrayProvider for BinaryViewType {
type Raw = *mut BNBinaryViewType;
type Context = ();
type Wrapped<'a> = Guard<'a, BinaryViewType>;
}

unsafe impl CoreOwnedArrayProvider for BinaryViewType {
unsafe impl CoreArrayProviderInner for BinaryViewType {
unsafe fn free(raw: *mut Self::Raw, _count: usize, _context: &Self::Context) {
BNFreeBinaryViewTypeList(raw);
}
}

unsafe impl CoreArrayWrapper for BinaryViewType {
type Wrapped<'a> = Guard<'a, BinaryViewType>;

unsafe fn wrap_raw<'a>(raw: &'a Self::Raw, _context: &'a Self::Context) -> Self::Wrapped<'a> {
Guard::new(BinaryViewType(*raw), &())
}
Expand Down
6 changes: 5 additions & 1 deletion rust/src/debuginfo.rs
Original file line number Diff line number Diff line change
Expand Up @@ -269,12 +269,16 @@ impl ToOwned for DebugInfoParser {
impl CoreArrayProvider for DebugInfoParser {
type Raw = *mut BNDebugInfoParser;
type Context = ();
type Wrapped<'a> = Guard<'a, DebugInfoParser>;
}

unsafe impl CoreOwnedArrayProvider for DebugInfoParser {
unsafe impl CoreArrayProviderInner for DebugInfoParser {
unsafe fn free(raw: *mut Self::Raw, count: usize, _: &Self::Context) {
BNFreeDebugInfoParserList(raw, count);
}
unsafe fn wrap_raw<'a>(raw: &'a Self::Raw, context: &'a Self::Context) -> Self::Wrapped<'a> {
Guard::new(Self { handle: *raw }, context)
}
}

///////////////////////
Expand Down
12 changes: 3 additions & 9 deletions rust/src/downloadprovider.rs
Original file line number Diff line number Diff line change
@@ -1,6 +1,4 @@
use crate::rc::{
Array, CoreArrayProvider, CoreArrayWrapper, CoreOwnedArrayProvider, Guard, Ref, RefCountable,
};
use crate::rc::{Array, CoreArrayProvider, Guard, CoreArrayProviderInner, Ref, RefCountable};
use crate::settings::Settings;
use crate::string::{BnStrCompatible, BnString};
use binaryninjacore_sys::*;
Expand Down Expand Up @@ -63,17 +61,13 @@ impl DownloadProvider {
impl CoreArrayProvider for DownloadProvider {
type Raw = *mut BNDownloadProvider;
type Context = ();
type Wrapped<'a> = Guard<'a, DownloadProvider>;
}

unsafe impl CoreOwnedArrayProvider for DownloadProvider {
unsafe impl CoreArrayProviderInner for DownloadProvider {
unsafe fn free(raw: *mut Self::Raw, _count: usize, _context: &Self::Context) {
BNFreeDownloadProviderList(raw);
}
}

unsafe impl CoreArrayWrapper for DownloadProvider {
type Wrapped<'a> = Guard<'a, DownloadProvider>;

unsafe fn wrap_raw<'a>(raw: &'a Self::Raw, _context: &'a Self::Context) -> Self::Wrapped<'a> {
Guard::new(DownloadProvider::from_raw(*raw), &())
}
Expand Down
16 changes: 4 additions & 12 deletions rust/src/function.rs
Original file line number Diff line number Diff line change
Expand Up @@ -414,17 +414,13 @@ unsafe impl RefCountable for Function {
impl CoreArrayProvider for Function {
type Raw = *mut BNFunction;
type Context = ();
type Wrapped<'a> = Guard<'a, Function>;
}

unsafe impl CoreOwnedArrayProvider for Function {
unsafe impl CoreArrayProviderInner for Function {
unsafe fn free(raw: *mut *mut BNFunction, count: usize, _context: &()) {
BNFreeFunctionList(raw, count);
}
}

unsafe impl CoreArrayWrapper for Function {
type Wrapped<'a> = Guard<'a, Function>;

unsafe fn wrap_raw<'a>(raw: &'a *mut BNFunction, context: &'a ()) -> Self::Wrapped<'a> {
Guard::new(Function { handle: *raw }, context)
}
Expand Down Expand Up @@ -469,16 +465,12 @@ impl AddressRange {
impl CoreArrayProvider for AddressRange {
type Raw = BNAddressRange;
type Context = ();
type Wrapped<'a> = &'a AddressRange;
}
unsafe impl CoreOwnedArrayProvider for AddressRange {
unsafe impl CoreArrayProviderInner for AddressRange {
unsafe fn free(raw: *mut Self::Raw, _count: usize, _context: &Self::Context) {
BNFreeAddressRanges(raw);
}
}

unsafe impl CoreArrayWrapper for AddressRange {
type Wrapped<'a> = &'a AddressRange;

unsafe fn wrap_raw<'a>(raw: &'a Self::Raw, _context: &'a Self::Context) -> Self::Wrapped<'a> {
mem::transmute(raw)
}
Expand Down
8 changes: 2 additions & 6 deletions rust/src/linearview.rs
Original file line number Diff line number Diff line change
Expand Up @@ -415,17 +415,13 @@ impl std::fmt::Display for LinearDisassemblyLine {
impl CoreArrayProvider for LinearDisassemblyLine {
type Raw = BNLinearDisassemblyLine;
type Context = ();
type Wrapped<'a> = Guard<'a, LinearDisassemblyLine>;
}

unsafe impl CoreOwnedArrayProvider for LinearDisassemblyLine {
unsafe impl CoreArrayProviderInner for LinearDisassemblyLine {
unsafe fn free(raw: *mut BNLinearDisassemblyLine, count: usize, _context: &()) {
BNFreeLinearDisassemblyLines(raw, count);
}
}

unsafe impl CoreArrayWrapper for LinearDisassemblyLine {
type Wrapped<'a> = Guard<'a, LinearDisassemblyLine>;

unsafe fn wrap_raw<'a>(raw: &'a Self::Raw, _context: &'a Self::Context) -> Self::Wrapped<'a> {
Guard::new(LinearDisassemblyLine::from_raw(raw), _context)
}
Expand Down
12 changes: 3 additions & 9 deletions rust/src/metadata.rs
Original file line number Diff line number Diff line change
@@ -1,6 +1,4 @@
use crate::rc::{
Array, CoreArrayProvider, CoreArrayWrapper, CoreOwnedArrayProvider, Guard, Ref, RefCountable,
};
use crate::rc::{Array, CoreArrayProvider, Guard, CoreArrayProviderInner, Ref, RefCountable};
use crate::string::{BnStrCompatible, BnString};
use binaryninjacore_sys::*;
use std::collections::HashMap;
Expand Down Expand Up @@ -335,17 +333,13 @@ unsafe impl RefCountable for Metadata {
impl CoreArrayProvider for Metadata {
type Raw = *mut BNMetadata;
type Context = ();
type Wrapped<'a> = Guard<'a, Metadata>;
}

unsafe impl CoreOwnedArrayProvider for Metadata {
unsafe impl CoreArrayProviderInner for Metadata {
unsafe fn free(raw: *mut *mut BNMetadata, _count: usize, _context: &()) {
BNFreeMetadataArray(raw);
}
}

unsafe impl CoreArrayWrapper for Metadata {
type Wrapped<'a> = Guard<'a, Metadata>;

unsafe fn wrap_raw<'a>(raw: &'a *mut BNMetadata, context: &'a ()) -> Self::Wrapped<'a> {
Guard::new(Metadata::from_raw(*raw), context)
}
Expand Down
8 changes: 2 additions & 6 deletions rust/src/platform.rs
Original file line number Diff line number Diff line change
Expand Up @@ -365,17 +365,13 @@ unsafe impl RefCountable for Platform {
impl CoreArrayProvider for Platform {
type Raw = *mut BNPlatform;
type Context = ();
type Wrapped<'a> = Guard<'a, Platform>;
}

unsafe impl CoreOwnedArrayProvider for Platform {
unsafe impl CoreArrayProviderInner for Platform {
unsafe fn free(raw: *mut *mut BNPlatform, count: usize, _context: &()) {
BNFreePlatformList(raw, count);
}
}

unsafe impl CoreArrayWrapper for Platform {
type Wrapped<'a> = Guard<'a, Platform>;

unsafe fn wrap_raw<'a>(raw: &'a *mut BNPlatform, context: &'a ()) -> Self::Wrapped<'a> {
debug_assert!(!raw.is_null());
Guard::new(Platform { handle: *raw }, context)
Expand Down
Loading

0 comments on commit bfe4044

Please sign in to comment.