lzfse_rust/types/
wide_bytes.rs1use crate::kit::{CopyType, CopyTypeLong, W00, WIDE};
2use crate::ops::{CopyLong, CopyShort, Len, ShortLimit, Skip};
3
4use std::ops::Deref;
5use std::slice;
6
7pub struct WideBytes<'a>(&'a [u8]);
9
10impl<'a> WideBytes<'a> {
11 #[allow(dead_code)]
12 pub fn from_bytes(bytes: &[u8], len: usize) -> Self {
13 assert!(len + WIDE <= bytes.len());
14 unsafe { Self::from_bytes_unchecked(bytes, len) }
15 }
16
17 #[inline(always)]
18 pub unsafe fn from_bytes_unchecked(bytes: &[u8], len: usize) -> Self {
19 debug_assert!(len + WIDE <= bytes.len());
20 Self::from_raw_parts(bytes.as_ptr(), len)
21 }
22
23 #[inline(always)]
27 pub unsafe fn from_raw_parts(ptr: *const u8, len: usize) -> Self {
28 Self(slice::from_raw_parts(ptr, len))
29 }
30}
31
32impl<'a> CopyLong for WideBytes<'a> {
33 #[inline(always)]
34 unsafe fn copy_long_raw(&self, dst: *mut u8, len: usize) {
35 debug_assert!(len <= self.len());
36 CopyTypeLong::wide_copy::<W00>(self.0.as_ptr(), dst, len);
37 }
38}
39
40impl<'a> CopyShort for WideBytes<'a> {
41 #[inline(always)]
42 unsafe fn copy_short_raw<V: CopyType>(&self, dst: *mut u8, len: usize) {
43 debug_assert!(len <= self.len());
44 V::wide_copy::<W00>(self.0.as_ptr(), dst, len);
45 }
46}
47
48impl<'a> Len for WideBytes<'a> {
49 #[inline(always)]
50 fn len(&self) -> usize {
51 self.0.len()
52 }
53}
54
55unsafe impl<'a> ShortLimit for WideBytes<'a> {
56 const SHORT_LIMIT: u32 = i32::MAX as u32;
57}
58
59impl<'a> Skip for WideBytes<'a> {
60 #[inline(always)]
61 unsafe fn skip_unchecked(&mut self, len: usize) {
62 debug_assert!(len <= self.len());
63 self.0 = self.0.get_unchecked(len..);
64 }
65}
66
67impl<'a> Deref for WideBytes<'a> {
68 type Target = [u8];
69
70 #[inline(always)]
71 fn deref(&self) -> &Self::Target {
72 self.0
73 }
74}