tendril/
util.rs

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
7use std::mem;
8use std::{ptr, slice};
9
10#[inline(always)]
11pub 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(buf.as_ptr().offset(start as isize), new_len)
15}
16
17#[inline(always)]
18pub 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(buf.as_mut_ptr().offset(start as isize), new_len)
26}
27
28#[inline(always)]
29pub unsafe fn copy_and_advance(dest: &mut *mut u8, src: &[u8]) {
30    ptr::copy_nonoverlapping(src.as_ptr(), *dest, src.len());
31    *dest = dest.offset(src.len() as isize)
32}
33
34#[inline(always)]
35pub 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(ptr)
40}
41
42#[inline(always)]
43pub unsafe fn copy_lifetime<'a, S: ?Sized, T: ?Sized + 'a>(_ptr: &'a S, ptr: &T) -> &'a T {
44    mem::transmute(ptr)
45}