Skip to content

Commit

Permalink
fix the array implementation using GAT
Browse files Browse the repository at this point in the history
  • Loading branch information
rbran committed Apr 10, 2024
1 parent 16fc310 commit 37b5146
Show file tree
Hide file tree
Showing 17 changed files with 120 additions and 113 deletions.
8 changes: 4 additions & 4 deletions rust/src/backgroundtask.rs
Original file line number Diff line number Diff line change
Expand Up @@ -112,13 +112,13 @@ unsafe impl CoreOwnedArrayProvider for BackgroundTask {
}
}

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

unsafe fn wrap_raw(
unsafe fn wrap_raw<'a>(
raw: &'a *mut BNBackgroundTask,
context: &'a (),
) -> Guard<'a, BackgroundTask> {
) -> Self::Wrapped<'a> {
Guard::new(BackgroundTask::from_raw(*raw), context)
}
}
Expand Down
12 changes: 6 additions & 6 deletions rust/src/basicblock.rs
Original file line number Diff line number Diff line change
Expand Up @@ -76,10 +76,10 @@ unsafe impl<'a, C: 'a + BlockContext> CoreOwnedArrayProvider for Edge<'a, C> {
}
}

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

unsafe fn wrap_raw(raw: &'a Self::Raw, context: &'a Self::Context) -> Edge<'a, C> {
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()),
raw,
Expand Down Expand Up @@ -309,10 +309,10 @@ unsafe impl<C: BlockContext> CoreOwnedArrayProvider for BasicBlock<C> {
}
}

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

unsafe fn wrap_raw(raw: &'a Self::Raw, context: &'a Self::Context) -> Self::Wrapped {
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)
}
}
6 changes: 3 additions & 3 deletions rust/src/callingconvention.rs
Original file line number Diff line number Diff line change
Expand Up @@ -662,10 +662,10 @@ unsafe impl<A: Architecture> CoreOwnedArrayProvider for CallingConvention<A> {
}
}

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

unsafe fn wrap_raw(raw: &'a Self::Raw, context: &'a Self::Context) -> Self::Wrapped {
unsafe fn wrap_raw<'a>(raw: &'a Self::Raw, context: &'a Self::Context) -> Self::Wrapped<'a> {
Guard::new(
CallingConvention {
handle: *raw,
Expand Down
8 changes: 5 additions & 3 deletions rust/src/custombinaryview.rs
Original file line number Diff line number Diff line change
Expand Up @@ -297,10 +297,12 @@ unsafe impl CoreOwnedArrayProvider for BinaryViewType {
}
}

unsafe impl<'a> CoreArrayWrapper<'a> for BinaryViewType {
type Wrapped = BinaryViewType;
unsafe impl CoreArrayWrapper for BinaryViewType {
// TODO there is nothing blocking the returned value from out-living the
// array, change it to &_ or Guard?
type Wrapped<'a> = BinaryViewType;

unsafe fn wrap_raw(raw: &'a Self::Raw, _context: &'a Self::Context) -> Self::Wrapped {
unsafe fn wrap_raw<'a>(raw: &'a Self::Raw, _context: &'a Self::Context) -> Self::Wrapped<'a> {
BinaryViewType(*raw)
}
}
Expand Down
8 changes: 5 additions & 3 deletions rust/src/downloadprovider.rs
Original file line number Diff line number Diff line change
Expand Up @@ -71,10 +71,12 @@ unsafe impl CoreOwnedArrayProvider for DownloadProvider {
}
}

unsafe impl<'a> CoreArrayWrapper<'a> for DownloadProvider {
type Wrapped = DownloadProvider;
unsafe impl CoreArrayWrapper for DownloadProvider {
// TODO there is nothing blocking the returned value from out-living the
// array, change it to &_ or Guard?
type Wrapped<'a> = DownloadProvider;

unsafe fn wrap_raw(raw: &'a Self::Raw, _context: &'a Self::Context) -> Self::Wrapped {
unsafe fn wrap_raw<'a>(raw: &'a Self::Raw, _context: &'a Self::Context) -> Self::Wrapped<'a> {
DownloadProvider::from_raw(*raw)
}
}
Expand Down
13 changes: 6 additions & 7 deletions rust/src/function.rs
Original file line number Diff line number Diff line change
Expand Up @@ -30,7 +30,6 @@ pub use binaryninjacore_sys::BNAnalysisSkipReason as AnalysisSkipReason;
pub use binaryninjacore_sys::BNFunctionAnalysisSkipOverride as FunctionAnalysisSkipOverride;
pub use binaryninjacore_sys::BNFunctionUpdateType as FunctionUpdateType;


use std::hash::Hash;
use std::{fmt, mem};

Expand Down Expand Up @@ -407,10 +406,10 @@ unsafe impl CoreOwnedArrayProvider for Function {
}
}

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

unsafe fn wrap_raw(raw: &'a *mut BNFunction, context: &'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 @@ -461,10 +460,10 @@ unsafe impl CoreOwnedArrayProvider for AddressRange {
}
}

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

unsafe fn wrap_raw(raw: &'a Self::Raw, _context: &'a Self::Context) -> Self::Wrapped {
unsafe fn wrap_raw<'a>(raw: &'a Self::Raw, _context: &'a Self::Context) -> Self::Wrapped<'a> {
mem::transmute(raw)
}
}
6 changes: 3 additions & 3 deletions rust/src/linearview.rs
Original file line number Diff line number Diff line change
Expand Up @@ -423,10 +423,10 @@ unsafe impl CoreOwnedArrayProvider for LinearDisassemblyLine {
}
}

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

unsafe fn wrap_raw(raw: &'a Self::Raw, _context: &'a Self::Context) -> Self::Wrapped {
unsafe fn wrap_raw<'a>(raw: &'a Self::Raw, _context: &'a Self::Context) -> Self::Wrapped<'a> {
Guard::new(LinearDisassemblyLine::from_raw(raw), _context)
}
}
6 changes: 3 additions & 3 deletions rust/src/metadata.rs
Original file line number Diff line number Diff line change
Expand Up @@ -343,10 +343,10 @@ unsafe impl CoreOwnedArrayProvider for Metadata {
}
}

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

unsafe fn wrap_raw(raw: &'a *mut BNMetadata, context: &'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
6 changes: 3 additions & 3 deletions rust/src/platform.rs
Original file line number Diff line number Diff line change
Expand Up @@ -373,10 +373,10 @@ unsafe impl CoreOwnedArrayProvider for Platform {
}
}

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

unsafe fn wrap_raw(raw: &'a *mut BNPlatform, context: &'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
76 changes: 37 additions & 39 deletions rust/src/rc.rs
Original file line number Diff line number Diff line change
Expand Up @@ -196,14 +196,12 @@ pub unsafe trait CoreOwnedArrayProvider: CoreArrayProvider {
unsafe fn free(raw: *mut Self::Raw, count: usize, context: &Self::Context);
}

pub unsafe trait CoreArrayWrapper<'a>: CoreArrayProvider
where
Self::Raw: 'a,
Self::Context: 'a,
{
type Wrapped: 'a;
pub unsafe trait CoreArrayWrapper: CoreArrayProvider {
type Wrapped<'a>
where
Self: 'a;

unsafe fn wrap_raw(raw: &'a Self::Raw, context: &'a Self::Context) -> Self::Wrapped;
unsafe fn wrap_raw<'a>(raw: &'a Self::Raw, context: &'a Self::Context) -> Self::Wrapped<'a>;
}

pub struct Array<P: CoreOwnedArrayProvider> {
Expand Down Expand Up @@ -250,25 +248,25 @@ impl<P: CoreOwnedArrayProvider> Array<P> {
}
}

impl<'a, P: 'a + CoreArrayWrapper<'a> + CoreOwnedArrayProvider> Array<P> {
impl<P: CoreArrayWrapper + CoreOwnedArrayProvider> Array<P> {
#[inline]
pub fn get(&'a self, index: usize) -> P::Wrapped {
pub fn get(&self, index: usize) -> P::Wrapped<'_> {
unsafe {
let backing = slice::from_raw_parts(self.contents, self.count);
P::wrap_raw(&backing[index], &self.context)
}
}

pub fn iter(&'a self) -> ArrayIter<'a, P> {
pub fn iter(&self) -> ArrayIter<P> {
ArrayIter {
it: unsafe { slice::from_raw_parts(self.contents, self.count).iter() },
context: &self.context,
}
}
}

impl<'a, P: 'a + CoreArrayWrapper<'a> + CoreOwnedArrayProvider> IntoIterator for &'a Array<P> {
type Item = P::Wrapped;
impl<'a, P: CoreArrayWrapper + CoreOwnedArrayProvider> IntoIterator for &'a Array<P> {
type Item = P::Wrapped<'a>;
type IntoIter = ArrayIter<'a, P>;

fn into_iter(self) -> Self::IntoIter {
Expand Down Expand Up @@ -323,25 +321,25 @@ impl<P: CoreArrayProvider> ArrayGuard<P> {
}
}

impl<'a, P: 'a + CoreArrayWrapper<'a> + CoreArrayProvider> ArrayGuard<P> {
impl<P: CoreArrayWrapper + CoreArrayProvider> ArrayGuard<P> {
#[inline]
pub fn get(&'a self, index: usize) -> P::Wrapped {
pub fn get(&self, index: usize) -> P::Wrapped<'_> {
unsafe {
let backing = slice::from_raw_parts(self.contents, self.count);
P::wrap_raw(&backing[index], &self.context)
}
}

pub fn iter(&'a self) -> ArrayIter<'a, P> {
pub fn iter(&self) -> ArrayIter<P> {
ArrayIter {
it: unsafe { slice::from_raw_parts(self.contents, self.count).iter() },
context: &self.context,
}
}
}

impl<'a, P: 'a + CoreArrayWrapper<'a> + CoreArrayProvider> IntoIterator for &'a ArrayGuard<P> {
type Item = P::Wrapped;
impl<'a, P: CoreArrayWrapper + CoreArrayProvider> IntoIterator for &'a ArrayGuard<P> {
type Item = P::Wrapped<'a>;
type IntoIter = ArrayIter<'a, P>;

fn into_iter(self) -> Self::IntoIter {
Expand All @@ -351,27 +349,27 @@ impl<'a, P: 'a + CoreArrayWrapper<'a> + CoreArrayProvider> IntoIterator for &'a

pub struct ArrayIter<'a, P>
where
P: 'a + CoreArrayWrapper<'a>,
P: CoreArrayWrapper,
{
it: slice::Iter<'a, P::Raw>,
context: &'a P::Context,
}

unsafe impl<'a, P> Send for ArrayIter<'a, P>
unsafe impl<P> Send for ArrayIter<'_, P>
where
P: CoreArrayWrapper<'a>,
P: CoreArrayWrapper,
P::Context: Sync,
{
}

impl<'a, P> Iterator for ArrayIter<'a, P>
where
P: 'a + CoreArrayWrapper<'a>,
P: 'a + CoreArrayWrapper,
{
type Item = P::Wrapped;
type Item = P::Wrapped<'a>;

#[inline]
fn next(&mut self) -> Option<P::Wrapped> {
fn next(&mut self) -> Option<Self::Item> {
self.it
.next()
.map(|r| unsafe { P::wrap_raw(r, self.context) })
Expand All @@ -385,7 +383,7 @@ where

impl<'a, P> ExactSizeIterator for ArrayIter<'a, P>
where
P: 'a + CoreArrayWrapper<'a>,
P: 'a + CoreArrayWrapper,
{
#[inline]
fn len(&self) -> usize {
Expand All @@ -395,10 +393,10 @@ where

impl<'a, P> DoubleEndedIterator for ArrayIter<'a, P>
where
P: 'a + CoreArrayWrapper<'a>,
P: 'a + CoreArrayWrapper,
{
#[inline]
fn next_back(&mut self) -> Option<P::Wrapped> {
fn next_back(&mut self) -> Option<P::Wrapped<'a>> {
self.it
.next_back()
.map(|r| unsafe { P::wrap_raw(r, self.context) })
Expand All @@ -412,20 +410,20 @@ use rayon::prelude::*;
use rayon::iter::plumbing::*;

#[cfg(feature = "rayon")]
impl<'a, P> Array<P>
impl<P> Array<P>
where
P: 'a + CoreArrayWrapper<'a> + CoreOwnedArrayProvider,
P: CoreArrayWrapper + CoreOwnedArrayProvider,
P::Context: Sync,
P::Wrapped: Send,
for<'a> P::Wrapped<'a>: Send,
{
pub fn par_iter(&'a self) -> ParArrayIter<'a, P> {
pub fn par_iter(&self) -> ParArrayIter<'_, P> {
ParArrayIter { it: self.iter() }
}
}
#[cfg(feature = "rayon")]
pub struct ParArrayIter<'a, P>
where
P: 'a + CoreArrayWrapper<'a>,
P: 'a + CoreArrayWrapper,
ArrayIter<'a, P>: Send,
{
it: ArrayIter<'a, P>,
Expand All @@ -434,11 +432,11 @@ where
#[cfg(feature = "rayon")]
impl<'a, P> ParallelIterator for ParArrayIter<'a, P>
where
P: 'a + CoreArrayWrapper<'a>,
P::Wrapped: Send,
P: 'a + CoreArrayWrapper,
P::Wrapped<'a>: Send,
ArrayIter<'a, P>: Send,
{
type Item = P::Wrapped;
type Item = P::Wrapped<'a>;

fn drive_unindexed<C>(self, consumer: C) -> C::Result
where
Expand All @@ -455,8 +453,8 @@ where
#[cfg(feature = "rayon")]
impl<'a, P> IndexedParallelIterator for ParArrayIter<'a, P>
where
P: 'a + CoreArrayWrapper<'a>,
P::Wrapped: Send,
P: 'a + CoreArrayWrapper,
P::Wrapped<'a>: Send,
ArrayIter<'a, P>: Send,
{
fn drive<C>(self, consumer: C) -> C::Result
Expand All @@ -481,7 +479,7 @@ where
#[cfg(feature = "rayon")]
struct ArrayIterProducer<'a, P>
where
P: 'a + CoreArrayWrapper<'a>,
P: 'a + CoreArrayWrapper,
ArrayIter<'a, P>: Send,
{
it: ArrayIter<'a, P>,
Expand All @@ -490,10 +488,10 @@ where
#[cfg(feature = "rayon")]
impl<'a, P> Producer for ArrayIterProducer<'a, P>
where
P: 'a + CoreArrayWrapper<'a>,
P: 'a + CoreArrayWrapper,
ArrayIter<'a, P>: Send,
{
type Item = P::Wrapped;
type Item = P::Wrapped<'a>;
type IntoIter = ArrayIter<'a, P>;

fn into_iter(self) -> ArrayIter<'a, P> {
Expand Down
Loading

0 comments on commit 37b5146

Please sign in to comment.