lzfse_rust/ops/
patch_into.rs1use crate::types::Idx;
2
3use super::pos::Pos;
4
5pub trait PatchInto: Pos {
6 #[must_use]
12 fn patch_into(&mut self, pos: Idx, len: usize) -> &mut [u8];
13
14 #[inline(always)]
15 fn patch_bytes(&mut self, pos: Idx, bytes: &[u8]) {
16 self.patch_into(pos, bytes.len()).copy_from_slice(bytes);
17 }
18}
19
20impl PatchInto for Vec<u8> {
21 #[inline(always)]
22 fn patch_into(&mut self, pos: Idx, len: usize) -> &mut [u8] {
23 let delta = self.pos() - pos;
24 let position = (self.len() as isize - delta as isize) as usize;
25 &mut self[position..position + len]
26 }
27}
28
29impl<T: PatchInto + ?Sized> PatchInto for &mut T {
30 #[inline(always)]
31 fn patch_into(&mut self, pos: Idx, len: usize) -> &mut [u8] {
32 (**self).patch_into(pos, len)
33 }
34
35 #[inline(always)]
36 fn patch_bytes(&mut self, pos: Idx, bytes: &[u8]) {
37 (**self).patch_bytes(pos, bytes)
38 }
39}