Struct owned_alloc::RawVec
source · pub struct RawVec<T> { /* private fields */ }
Expand description
Raw Vector allocation. This allocation, instead of holding a pointer to a
single T
, holds a pointer to as many T
are required. The allocation is
resizable and is freed on drop
. No initialization or deinitialization of
the elements is performed. This type may be useful for Vec
-like types. If
the size of the allocation is zero, no allocation is performed and a
dangling pointer is used (just like in std
). For the drop checker, the
type acts as if it contains a T
due to usage of PhantomData<T>
.
extern crate owned_alloc;
use owned_alloc::RawVec;
let mut vec = RawVec::<usize>::with_capacity(200);
assert_eq!(200, vec.cap());
assert_eq!(200, unsafe { vec.as_slice().len() });
vec.resize(354);
assert_eq!(354, vec.cap());
assert_eq!(354, unsafe { vec.as_slice().len() });
Implementations
sourceimpl<T> RawVec<T>
impl<T> RawVec<T>
sourcepub fn new() -> Self
pub fn new() -> Self
Creates a new RawVec
of capacity 0
and a dangling pointer. No
allocation is performed.
sourcepub fn with_capacity(cap: usize) -> Self
pub fn with_capacity(cap: usize) -> Self
Creates a new RawVec
with a given capacity. In case of allocation
error, the handler registered via stdlib is called. In case of overflow
calculating the total size, the function panics.
sourcepub fn try_with_capacity(cap: usize) -> Result<Self, RawVecErr>
pub fn try_with_capacity(cap: usize) -> Result<Self, RawVecErr>
Creates a new RawVec
with a given capacity. In case of allocation
error or overflow calculating the total size, Err
is returned.
sourcepub unsafe fn from_vec(vec: Vec<T>) -> Self
pub unsafe fn from_vec(vec: Vec<T>) -> Self
Creates a RawVec
from a plain old standard library Vec
. Beware, only
the pointer and the capacity are saved. The length is discarded. If you
want to keep track of the length, you will have to store it for
yourself. Note also that no element is dropped (ever) by the
RawVec
.
Safety
This function is unsafe
because there are no guarantees that Vec
and
RawVec
allocate in the same way. They probably do in the Rust version
you are using, but there are no future guarantees.
sourcepub unsafe fn from_raw_parts(nnptr: NonNull<T>, cap: usize) -> Self
pub unsafe fn from_raw_parts(nnptr: NonNull<T>, cap: usize) -> Self
Recreate the RawVec
from a raw non-null pointer and a capacity.
Safety
This functions is unsafe
because passing the wrong pointer leads to
undefined behaviour. Passing wrong capacity also leads to undefined
behaviour.
sourcepub unsafe fn from_raw_slice(raw: NonNull<[T]>) -> Self
pub unsafe fn from_raw_slice(raw: NonNull<[T]>) -> Self
Recreate the RawVec
from a raw non-null pointer to a slice with length
equal to the RawVec
’s capacity.
Safety
This functions is unsafe
because passing the wrong pointer leads to
undefined behaviour, including passing a pointer with the wrong length.
sourcepub fn cap(&self) -> usize
pub fn cap(&self) -> usize
The requested allocation capacity. It is guaranteed to be the capacity
passed to the last capacity-modifier method. Those are
with_capacity
, try_with_capacity
and resize
. The methods new
and try_new
initialize the capacity to 0
.
sourcepub fn raw_slice(&self) -> NonNull<[T]>
pub fn raw_slice(&self) -> NonNull<[T]>
The raw non-null pointer to the slice with length equal to the
RawVec
’s capacity.
sourcepub fn into_raw_slice(self) -> NonNull<[T]>
pub fn into_raw_slice(self) -> NonNull<[T]>
“Forgets” dropping the allocation and returns a raw non-null pointer to
the slice with length equal to the RawVec
’s capacity.
sourcepub unsafe fn as_slice(&self) -> &[T] ⓘ
pub unsafe fn as_slice(&self) -> &[T] ⓘ
Encodes the RawVec
as an immutable reference to a slice with length
equal to the capacity.
Safety
This function is unsafe
because if the index of an uninitialized
element is accessed incorrectly, undefined behavior occurs.
sourcepub unsafe fn as_mut_slice(&mut self) -> &mut [T] ⓘ
pub unsafe fn as_mut_slice(&mut self) -> &mut [T] ⓘ
Encodes the RawVec
as an mutable reference to a slice with length
equal to the capacity.
Safety
This function is unsafe
because if the index of an uninitialized
element is accessed incorrectly, undefined behavior occurs.
sourcepub unsafe fn into_vec(self, len: usize) -> Vec<T>
pub unsafe fn into_vec(self, len: usize) -> Vec<T>
Creates a plain old standard library Vec
from the RawVec
and a given
length.
Safety
This function is unsafe
because there are no guarantees that Vec
and
RawVec
allocate in the same way. They probably do in the Rust version
you are using, but there are no future guarantees. Also, the length
argument must be passed correctly, since the elements until the given
length will be considered correctly, but the RawVec
initialize no
element.