internal: remove unneeded boxes (#12)

This commit is contained in:
Tom Milligan 2021-02-15 12:38:39 +00:00 committed by GitHub
parent 0341515380
commit 6fac8af4d0
No known key found for this signature in database
GPG key ID: 4AEE18F83AFDEB23
2 changed files with 26 additions and 22 deletions

View file

@ -437,7 +437,7 @@ impl<'old, 'new, 'bufs, T: DiffableStr + ?Sized + 'old + 'new> TextDiff<'old, 'n
pub fn iter_changes<'x, 'slf>( pub fn iter_changes<'x, 'slf>(
&'slf self, &'slf self,
op: &DiffOp, op: &DiffOp,
) -> impl Iterator<Item = Change<'x, T>> + 'slf ) -> impl Iterator<Item = Change<'x, T>> + '_
where where
'x: 'slf, 'x: 'slf,
'old: 'x, 'old: 'x,
@ -462,16 +462,13 @@ impl<'old, 'new, 'bufs, T: DiffableStr + ?Sized + 'old + 'new> TextDiff<'old, 'n
/// ///
/// This is a shortcut for combining [`TextDiff::ops`] with /// This is a shortcut for combining [`TextDiff::ops`] with
/// [`TextDiff::iter_changes`]. /// [`TextDiff::iter_changes`].
pub fn iter_all_changes<'x, 'slf>(&'slf self) -> impl Iterator<Item = Change<'x, T>> + 'slf pub fn iter_all_changes<'x, 'slf>(&'slf self) -> impl Iterator<Item = Change<'x, T>> + '_
where where
'x: 'slf, 'x: 'slf + 'old + 'new,
'old: 'x, 'old: 'x,
'new: 'x, 'new: 'x,
{ {
// unclear why this needs Box::new here. It seems to infer some really self.ops().iter().flat_map(move |op| self.iter_changes(&op))
// odd lifetimes I can't figure out how to work with.
Box::new(self.ops().iter().flat_map(move |op| self.iter_changes(&op)))
as Box<dyn Iterator<Item = _>>
} }
/// Utility to return a unified diff formatter. /// Utility to return a unified diff formatter.
@ -492,14 +489,12 @@ impl<'old, 'new, 'bufs, T: DiffableStr + ?Sized + 'old + 'new> TextDiff<'old, 'n
/// ///
/// Requires the `inline` feature. /// Requires the `inline` feature.
#[cfg(feature = "inline")] #[cfg(feature = "inline")]
pub fn iter_inline_changes<'x, 'slf>( pub fn iter_inline_changes<'slf>(
&'slf self, &'slf self,
op: &DiffOp, op: &DiffOp,
) -> impl Iterator<Item = InlineChange<'x, T>> + 'slf ) -> impl Iterator<Item = InlineChange<'slf, T>> + '_
where where
'x: 'slf, 'slf: 'old + 'new,
'old: 'x,
'new: 'x,
{ {
inline::iter_inline_changes(self, op) inline::iter_inline_changes(self, op)
} }

View file

@ -176,7 +176,10 @@ impl<'diff, 'old, 'new, 'bufs, T: DiffableStr + ?Sized> UnifiedDiff<'diff, 'old,
} }
/// Write the unified diff as bytes to the output stream. /// Write the unified diff as bytes to the output stream.
pub fn to_writer<W: io::Write>(&self, mut w: W) -> Result<(), io::Error> { pub fn to_writer<W: io::Write>(&self, mut w: W) -> Result<(), io::Error>
where
'diff: 'old + 'new + 'bufs,
{
let mut header = self.header.as_ref(); let mut header = self.header.as_ref();
for hunk in self.iter_hunks() { for hunk in self.iter_hunks() {
if let Some((old_file, new_file)) = header.take() { if let Some((old_file, new_file)) = header.take() {
@ -237,18 +240,20 @@ impl<'diff, 'old, 'new, 'bufs, T: DiffableStr + ?Sized>
} }
/// Iterates over all changes in a hunk. /// Iterates over all changes in a hunk.
pub fn iter_changes(&self) -> impl Iterator<Item = Change<'_, T>> + '_ { pub fn iter_changes(&self) -> impl Iterator<Item = Change<'diff, T>> + '_
// unclear why this needs Box::new here. It seems to infer some really where
// odd lifetimes I can't figure out how to work with. 'diff: 'old + 'new + 'bufs,
Box::new( {
self.ops() self.ops()
.iter() .iter()
.flat_map(move |op| self.diff.iter_changes(op)), .flat_map(move |op| self.diff.iter_changes(op))
) as Box<dyn Iterator<Item = _>>
} }
/// Write the hunk as bytes to the output stream. /// Write the hunk as bytes to the output stream.
pub fn to_writer<W: io::Write>(&self, mut w: W) -> Result<(), io::Error> { pub fn to_writer<W: io::Write>(&self, mut w: W) -> Result<(), io::Error>
where
'diff: 'old + 'new + 'bufs,
{
for (idx, change) in self.iter_changes().enumerate() { for (idx, change) in self.iter_changes().enumerate() {
if idx == 0 { if idx == 0 {
writeln!(w, "{}", self.header())?; writeln!(w, "{}", self.header())?;
@ -268,6 +273,8 @@ impl<'diff, 'old, 'new, 'bufs, T: DiffableStr + ?Sized>
impl<'diff, 'old, 'new, 'bufs, T: DiffableStr + ?Sized> fmt::Display impl<'diff, 'old, 'new, 'bufs, T: DiffableStr + ?Sized> fmt::Display
for UnifiedDiffHunk<'diff, 'old, 'new, 'bufs, T> for UnifiedDiffHunk<'diff, 'old, 'new, 'bufs, T>
where
'diff: 'old + 'new + 'bufs,
{ {
fn fmt(&self, f: &mut fmt::Formatter) -> fmt::Result { fn fmt(&self, f: &mut fmt::Formatter) -> fmt::Result {
for (idx, change) in self.iter_changes().enumerate() { for (idx, change) in self.iter_changes().enumerate() {
@ -288,6 +295,8 @@ impl<'diff, 'old, 'new, 'bufs, T: DiffableStr + ?Sized> fmt::Display
impl<'diff, 'old, 'new, 'bufs, T: DiffableStr + ?Sized> fmt::Display impl<'diff, 'old, 'new, 'bufs, T: DiffableStr + ?Sized> fmt::Display
for UnifiedDiff<'diff, 'old, 'new, 'bufs, T> for UnifiedDiff<'diff, 'old, 'new, 'bufs, T>
where
'diff: 'old + 'new + 'bufs,
{ {
fn fmt(&self, f: &mut fmt::Formatter) -> fmt::Result { fn fmt(&self, f: &mut fmt::Formatter) -> fmt::Result {
let mut header = self.header.as_ref(); let mut header = self.header.as_ref();