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>

Notable traits for &'a BitSlice<O, T>

impl<'a, O, T> Read for &'a BitSlice<O, T> where
    O: BitOrder,
    T: BitStore,
    BitSlice<O, T>: BitField
impl<'a, O, T> Write for &'a mut BitSlice<O, T> where
    O: BitOrder,
    T: BitStore,
    BitSlice<O, T::Alias>: BitField
where
    O: BitOrder,
    T: 'a + BitStore + BitMemory

Forms a bitslice from a pointer and a length.

The len argument is the number of elements, not the number of bits.

Original

slice::from_raw_parts

Safety

Behavior is undefined if any of the following conditions are violated:

  • data must be valid for len * 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 as data for zero-length slices using NonNull::dangling().
  • The memory referenced by the returned bitslice must not be mutated for the duration of the lifetime 'a, except inside an UnsafeCell.
  • The total size len * T::Mem::BITS of the slice must be no larger than BitSlice::<_, 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);