Files
firezone/rust/etherparse-ext/src/slice_utils.rs
Thomas Eizinger d1d0874699 refactor(rust): introduce etherparse-ext crate (#8500)
Within Firezone's Rust codebase, we use the `etherparse` crate
extensively to parse network packets. To provide a more ergonomic API,
this is all encapsulated in our `ip-packet` crate.

For #7518, we need to write an eBPF kernel that parses and manipulates
network packets. Etherparse itself doesn't provide any facilities to
manipulate network packets. That is an open feature request:
https://github.com/JulianSchmid/etherparse/issues/9. For the packet
manipulation that we are doing in `connlib`, we already wrote certain
extensions to the `etherparse` crate but today, those are all within the
`ip-packet` crate.

In order to reuse that within the eBPF kernel, we cannot just depend on
`ip-packet` directly because eBPF is a no-std and no-alloc environment,
thus no crate in the dependency tree is allowed to depend on Rust's
std-lib. `etherparse` itself actually has an `std` feature flag that we
can turn off. Introducing the same in `ip-packet` would require a lot of
conditional-compilation gates using `#[cfg]`. it is much easier to just
introduce a new crate that houses all our in-house extensions to
`etherparse`. Eventually, we can hopefully upstream those which is
another motivator to separate this out.
2025-03-25 22:33:14 +00:00

18 lines
495 B
Rust

/// Writes the given byte-array to the slice at the specified index.
///
/// # Safety
///
/// `offset` + `bytes.len()` must be within the slice.
pub unsafe fn write_to_offset_unchecked<const N: usize>(
slice: &mut [u8],
offset: usize,
bytes: [u8; N],
) {
debug_assert!(offset + N <= slice.len());
let (_front, rest) = unsafe { slice.split_at_mut_unchecked(offset) };
let (target, _rest) = unsafe { rest.split_at_mut_unchecked(N) };
target.copy_from_slice(&bytes)
}