Skip to content

Commit

Permalink
optimize Array::concat (#111)
Browse files Browse the repository at this point in the history
The resulting assembly is much nicer. As a bonus, `concat` no longer has
panicking branches.

---------

Signed-off-by: Eric Lagergren <[email protected]>
  • Loading branch information
ericlagergren authored Jan 10, 2025
1 parent ca1240b commit 03b3c79
Showing 1 changed file with 13 additions and 1 deletion.
14 changes: 13 additions & 1 deletion src/lib.rs
Original file line number Diff line number Diff line change
Expand Up @@ -226,13 +226,25 @@ where

/// Concatenates `self` with `other`.
#[inline]
#[allow(clippy::arithmetic_side_effects, reason = "`i` will never overflow")]
pub fn concat<N>(self, other: Array<T, N>) -> Array<T, Sum<U, N>>
where
N: ArraySize,
U: Add<N>,
Sum<U, N>: ArraySize,
{
self.into_iter().chain(other).collect()
let mut c = Array::uninit();
let mut i = 0;
for v in self {
c[i].write(v);
i += 1;
}
for v in other {
c[i].write(v);
i += 1;
}
// SAFETY: We wrote to every element of `c`.
unsafe { c.assume_init() }
}

/// Splits `self` at index `N` in two arrays.
Expand Down

0 comments on commit 03b3c79

Please sign in to comment.