Skip to content

Commit

Permalink
Add SplitBorrow trait to split borrow tuple_list elements (#1624)
Browse files Browse the repository at this point in the history
* Add SplitBorrow trait to split borrow tuple_list elements

* clippy
  • Loading branch information
andreafioraldi authored Oct 12, 2023
1 parent 4c17da0 commit f6ba9de
Showing 1 changed file with 39 additions and 0 deletions.
39 changes: 39 additions & 0 deletions libafl_bolts/src/tuples.rs
Original file line number Diff line number Diff line change
Expand Up @@ -209,6 +209,45 @@ where
}
}

/// Borrow each member of the tuple
pub trait SplitBorrow<'a> {
/// The Resulting [`TupleList`], of an [`SplitBorrow::split_borrow()`] call
type SplitBorrowResult;
/// The Resulting [`TupleList`], of an [`SplitBorrow::split_borrow_mut()`] call
type SplitBorrowMutResult;

/// Return a tuple of borrowed references
fn split_borrow(&'a self) -> Self::SplitBorrowResult;
/// Return a tuple of borrowed mutable references
fn split_borrow_mut(&'a mut self) -> Self::SplitBorrowMutResult;
}

impl<'a> SplitBorrow<'a> for () {
type SplitBorrowResult = ();
type SplitBorrowMutResult = ();

fn split_borrow(&'a self) -> Self::SplitBorrowResult {}

fn split_borrow_mut(&'a mut self) -> Self::SplitBorrowMutResult {}
}

impl<'a, Head, Tail> SplitBorrow<'a> for (Head, Tail)
where
Head: 'a,
Tail: SplitBorrow<'a>,
{
type SplitBorrowResult = (&'a Head, Tail::SplitBorrowResult);
type SplitBorrowMutResult = (&'a mut Head, Tail::SplitBorrowMutResult);

fn split_borrow(&'a self) -> Self::SplitBorrowResult {
(&self.0, self.1.split_borrow())
}

fn split_borrow_mut(&'a mut self) -> Self::SplitBorrowMutResult {
(&mut self.0, self.1.split_borrow_mut())
}
}

/// A named tuple
pub trait NamedTuple: HasConstLen {
/// Gets the name of this tuple
Expand Down

0 comments on commit f6ba9de

Please sign in to comment.