Function bitvec::slice::bits_from_raw_parts[][src]

pub unsafe fn bits_from_raw_parts<'a, O, T>(
    data: *const T,
    head: BitIdx<T>,
    bits: usize
) -> &'a BitSlice<O, T> where
    O: BitOrder,
    T: 'a + BitStore

Forms a BitSlice from a pointer, starting position, and length.

The head argument is the starting index, not the starting bit position. The bits argumnent is the number of bits, not the number of elements T.

This function is the semantic equivalent to [T]::from_raw_parts, in contrast to from_raw_parts which is the API equivalent.

Safety

This function is unsafe as there is no guarantee that the given pointer is valid for the elements required to hold head + bits bits, nor whether the lifetime inferred is a suitable lifetime for the returned slice.

data must be non-null and aligned, even for zero-length slices. This is due to requirements in the bitvec data structure operations. You can obtain a pointer that is usable as data for zero-length slices from NonNull::dangling().

The total size of the bit slice must be no larger than BitPtr::<T>::MAX_BITS bits in memory. See the safety documentation of BitPtr (if available).

Caveat

The lifetime for the returned slice is inferred from its usage. To prevent accidental misuse, it’s suggested to tie the lifetime to whichever source lifetime is safe in the context, such as by providing a helper function taking the lifetime of a host value for the slice, or by explicit annotation.

Examples

use bitvec::{
    indices::BitIdx,
    order::Local,
    slice,
    slice::BitSlice,
};

// manifest a slice for a single element
let x = 42u8;
let ptr = &x as *const u8;
let bits: &BitSlice<Local, u8> = unsafe { slice::bits_from_raw_parts(
    ptr,
    BitIdx::new(2).unwrap(),
    5
) };
assert_eq!(bits.len(), 5);