Function bitvec::slice::from_raw_parts [−][src]
pub unsafe fn from_raw_parts<'a, O, T>(
data: *const T,
len: usize
) -> &'a BitSlice<O, T>ⓘ where
O: BitOrder,
T: 'a + BitStore + BitMemory,
Expand description
Forms a bitslice from a pointer and a length.
The len
argument is the number of elements, not the number of bits.
Original
Safety
Behavior is undefined if any of the following conditions are violated:
data
must be valid forlen * mem::size_of::<T>()
many bytes, and it must be properly aligned. This means in particular:- The entire memory range of this slice must be contained within a single allocated object! Slices can never span across multiple allocated objects.
data
must be non-null and aligned even for zero-length slices. The&BitSlice
pointer encoding requires this porperty to hold. You can obtain a pointer that is usable asdata
for zero-length slices usingNonNull::dangling()
.
- The memory referenced by the returned bitslice must not be mutated for the
duration of the lifetime
'a
, except inside anUnsafeCell
. - The total size
len * T::Mem::BITS
of the slice must be no larger thanBitSlice::<_, T>::MAX_BITS
.
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::prelude::*;
use bitvec::slice as bv_slice;
let x = 42u8;
let ptr = &x as *const _;
let bits = unsafe {
bv_slice::from_raw_parts::<LocalBits, u8>(ptr, 1)
};
assert_eq!(bits.count_ones(), 3);