1 | // Licensed under the Apache License, Version 2.0 <LICENSE-APACHE or |
2 | // https://www.apache.org/licenses/LICENSE-2.0> or the MIT license |
3 | // <LICENSE-MIT or https://opensource.org/licenses/MIT>, at your |
4 | // option. This file may not be copied, modified, or distributed |
5 | // except according to those terms. |
6 | |
7 | use std::mem; |
8 | use std::{ptr, slice}; |
9 | |
10 | #[inline (always)] |
11 | pub unsafe fn unsafe_slice<'a>(buf: &'a [u8], start: usize, new_len: usize) -> &'a [u8] { |
12 | debug_assert!(start <= buf.len()); |
13 | debug_assert!(new_len <= (buf.len() - start)); |
14 | slice::from_raw_parts(data:buf.as_ptr().offset(count:start as isize), new_len) |
15 | } |
16 | |
17 | #[inline (always)] |
18 | pub unsafe fn unsafe_slice_mut<'a>( |
19 | buf: &'a mut [u8], |
20 | start: usize, |
21 | new_len: usize, |
22 | ) -> &'a mut [u8] { |
23 | debug_assert!(start <= buf.len()); |
24 | debug_assert!(new_len <= (buf.len() - start)); |
25 | slice::from_raw_parts_mut(data:buf.as_mut_ptr().offset(count:start as isize), new_len) |
26 | } |
27 | |
28 | #[inline (always)] |
29 | pub unsafe fn copy_and_advance(dest: &mut *mut u8, src: &[u8]) { |
30 | ptr::copy_nonoverlapping(src:src.as_ptr(), *dest, count:src.len()); |
31 | *dest = dest.offset(count:src.len() as isize) |
32 | } |
33 | |
34 | #[inline (always)] |
35 | pub unsafe fn copy_lifetime_mut<'a, S: ?Sized, T: ?Sized + 'a>( |
36 | _ptr: &'a mut S, |
37 | ptr: &mut T, |
38 | ) -> &'a mut T { |
39 | mem::transmute(src:ptr) |
40 | } |
41 | |
42 | #[inline (always)] |
43 | pub unsafe fn copy_lifetime<'a, S: ?Sized, T: ?Sized + 'a>(_ptr: &'a S, ptr: &T) -> &'a T { |
44 | mem::transmute(src:ptr) |
45 | } |
46 | |