/rust/registry/src/index.crates.io-1949cf8c6b5b557f/zlib-rs-0.6.8/src/deflate/window.rs
Line | Count | Source |
1 | | use crate::weak_slice::WeakSliceMut; |
2 | | |
3 | | #[derive(Debug)] |
4 | | pub struct Window<'a> { |
5 | | // the full window allocation. This is longer than w_size so that operations don't need to |
6 | | // perform bounds checks. |
7 | | buf: WeakSliceMut<'a, u8>, |
8 | | |
9 | | window_bits: usize, |
10 | | } |
11 | | |
12 | | impl<'a> Window<'a> { |
13 | 0 | pub unsafe fn from_raw_parts(ptr: *mut u8, window_bits: usize) -> Self { |
14 | 0 | let len = (1 << window_bits) * 2; |
15 | 0 | let buf = unsafe { WeakSliceMut::from_raw_parts_mut(ptr, len) }; |
16 | | |
17 | 0 | Self { buf, window_bits } |
18 | 0 | } |
19 | | |
20 | 0 | pub fn as_ptr(&self) -> *const u8 { |
21 | 0 | self.buf.as_ptr() |
22 | 0 | } |
23 | | |
24 | 0 | pub fn capacity(&self) -> usize { |
25 | 0 | 2 * (1 << self.window_bits) |
26 | 0 | } |
27 | | |
28 | | /// Returns a shared reference to the filled portion of the buffer. |
29 | | #[inline] |
30 | 0 | pub fn filled(&self) -> &[u8] { |
31 | | // SAFETY: `self.buf` has been initialized for at least `filled` elements |
32 | 0 | unsafe { core::slice::from_raw_parts(self.buf.as_ptr().cast(), self.buf.len()) } |
33 | 0 | } |
34 | | |
35 | | /// Returns a mutable reference to the filled portion of the buffer. |
36 | | #[inline] |
37 | 0 | pub fn filled_mut(&mut self) -> &mut [u8] { |
38 | | // SAFETY: `self.buf` has been initialized for at least `filled` elements |
39 | 0 | unsafe { core::slice::from_raw_parts_mut(self.buf.as_mut_ptr().cast(), self.buf.len()) } |
40 | 0 | } |
41 | | |
42 | | /// # Safety |
43 | | /// |
44 | | /// `src` must point to `range.end - range.start` valid (initialized!) bytes |
45 | 0 | pub unsafe fn copy_and_initialize(&mut self, range: core::ops::Range<usize>, src: *const u8) { |
46 | 0 | let (start, end) = (range.start, range.end); |
47 | | |
48 | 0 | let dst = self.buf.as_mut_slice()[range].as_mut_ptr(); |
49 | 0 | unsafe { core::ptr::copy_nonoverlapping(src, dst, end - start) }; |
50 | 0 | } |
51 | | } |