Struct 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§

Source§

impl<T> RawVec<T>

Source

pub fn new() -> Self

Creates a new RawVec of capacity 0 and a dangling pointer. No allocation is performed.

Source

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.

Source

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.

Source

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.

Source

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.

Source

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.

Source

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.

Source

pub fn raw(&self) -> NonNull<T>

The raw non-null pointer to the first element.

Source

pub fn raw_slice(&self) -> NonNull<[T]>

The raw non-null pointer to the slice with length equal to the RawVec’s capacity.

Source

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.

Source

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.

Source

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.

Source

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.

Source

pub fn resize(&mut self, new_cap: usize)

Resizes the 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.

Source

pub fn try_resize(&mut self, new_cap: usize) -> Result<(), RawVecErr>

Resizes the RawVec with a given capacity. In case of allocation error or overflow calculating the total size, Err is returned. In case of failure, the original allocation is untouched.

Trait Implementations§

Source§

impl<T> Debug for RawVec<T>

Source§

fn fmt(&self, fmtr: &mut Formatter<'_>) -> Result

Formats the value using the given formatter. Read more
Source§

impl<T> Default for RawVec<T>

Source§

fn default() -> Self

Returns the “default value” for a type. Read more
Source§

impl<T> Drop for RawVec<T>

Source§

fn drop(&mut self)

Executes the destructor for this type. Read more
Source§

impl<T> From<RawVec<T>> for UninitAlloc<[T]>

Source§

fn from(alloc: RawVec<T>) -> Self

Converts to this type from the input type.
Source§

impl<T> From<UninitAlloc<T>> for RawVec<T>

Source§

fn from(alloc: UninitAlloc<T>) -> Self

Converts to this type from the input type.
Source§

impl<T> Send for RawVec<T>
where T: Send,

Source§

impl<T> Sync for RawVec<T>
where T: Sync,

Auto Trait Implementations§

§

impl<T> Freeze for RawVec<T>

§

impl<T> RefUnwindSafe for RawVec<T>
where T: RefUnwindSafe,

§

impl<T> Unpin for RawVec<T>
where T: Unpin,

§

impl<T> UnwindSafe for RawVec<T>

Blanket Implementations§

Source§

impl<T> Any for T
where T: 'static + ?Sized,

Source§

fn type_id(&self) -> TypeId

Gets the TypeId of self. Read more
Source§

impl<T> Borrow<T> for T
where T: ?Sized,

Source§

fn borrow(&self) -> &T

Immutably borrows from an owned value. Read more
Source§

impl<T> BorrowMut<T> for T
where T: ?Sized,

Source§

fn borrow_mut(&mut self) -> &mut T

Mutably borrows from an owned value. Read more
Source§

impl<T> From<T> for T

Source§

fn from(t: T) -> T

Returns the argument unchanged.

Source§

impl<T, U> Into<U> for T
where U: From<T>,

Source§

fn into(self) -> U

Calls U::from(self).

That is, this conversion is whatever the implementation of From<T> for U chooses to do.

Source§

impl<T, U> TryFrom<U> for T
where U: Into<T>,

Source§

type Error = Infallible

The type returned in the event of a conversion error.
Source§

fn try_from(value: U) -> Result<T, <T as TryFrom<U>>::Error>

Performs the conversion.
Source§

impl<T, U> TryInto<U> for T
where U: TryFrom<T>,

Source§

type Error = <U as TryFrom<T>>::Error

The type returned in the event of a conversion error.
Source§

fn try_into(self) -> Result<U, <U as TryFrom<T>>::Error>

Performs the conversion.