pub type KVec<T> = Vec<T, Kmalloc>;
Expand description
Aliased Type§
struct KVec<T> { /* private fields */ }
Implementations
Source§impl<T, A> Vec<T, A>where
A: Allocator,
impl<T, A> Vec<T, A>where
A: Allocator,
Sourcepub fn capacity(&self) -> usize
pub fn capacity(&self) -> usize
Returns the number of elements that can be stored within the vector without allocating additional memory.
Sourcepub unsafe fn set_len(&mut self, new_len: usize)
pub unsafe fn set_len(&mut self, new_len: usize)
Forcefully sets self.len
to new_len
.
§Safety
new_len
must be less than or equal toSelf::capacity
.- If
new_len
is greater thanself.len
, all elements within the interval [self.len
,new_len
) must be initialized.
Sourcepub fn as_mut_slice(&mut self) -> &mut [T]
pub fn as_mut_slice(&mut self) -> &mut [T]
Returns a mutable slice of the entire vector.
Sourcepub fn as_mut_ptr(&mut self) -> *mut T
pub fn as_mut_ptr(&mut self) -> *mut T
Returns a mutable raw pointer to the vector’s backing buffer, or, if T
is a ZST, a
dangling raw pointer.
Sourcepub fn as_ptr(&self) -> *const T
pub fn as_ptr(&self) -> *const T
Returns a raw pointer to the vector’s backing buffer, or, if T
is a ZST, a dangling raw
pointer.
Sourcepub fn is_empty(&self) -> bool
pub fn is_empty(&self) -> bool
Returns true
if the vector contains no elements, false
otherwise.
§Examples
let mut v = KVec::new();
assert!(v.is_empty());
v.push(1, GFP_KERNEL);
assert!(!v.is_empty());
Sourcepub const fn new() -> Self
pub const fn new() -> Self
Creates a new, empty Vec<T, A>
.
This method does not allocate by itself.
Sourcepub fn spare_capacity_mut(&mut self) -> &mut [MaybeUninit<T>]
pub fn spare_capacity_mut(&mut self) -> &mut [MaybeUninit<T>]
Returns a slice of MaybeUninit<T>
for the remaining spare capacity of the vector.
Sourcepub fn with_capacity(capacity: usize, flags: Flags) -> Result<Self, AllocError>
pub fn with_capacity(capacity: usize, flags: Flags) -> Result<Self, AllocError>
Sourcepub unsafe fn from_raw_parts(
ptr: *mut T,
length: usize,
capacity: usize,
) -> Self
pub unsafe fn from_raw_parts( ptr: *mut T, length: usize, capacity: usize, ) -> Self
Creates a Vec<T, A>
from a pointer, a length and a capacity using the allocator A
.
§Examples
let mut v = kernel::kvec![1, 2, 3]?;
v.reserve(1, GFP_KERNEL)?;
let (mut ptr, mut len, cap) = v.into_raw_parts();
// SAFETY: We've just reserved memory for another element.
unsafe { ptr.add(len).write(4) };
len += 1;
// SAFETY: We only wrote an additional element at the end of the `KVec`'s buffer and
// correspondingly increased the length of the `KVec` by one. Otherwise, we construct it
// from the exact same raw parts.
let v = unsafe { KVec::from_raw_parts(ptr, len, cap) };
assert_eq!(v, [1, 2, 3, 4]);
§Safety
If T
is a ZST:
ptr
must be a dangling, well aligned pointer.
Otherwise:
ptr
must have been allocated with the allocatorA
.ptr
must satisfy or exceed the alignment requirements ofT
.ptr
must point to memory with a size of at leastsize_of::<T>() * capacity
bytes.- The allocated size in bytes must not be larger than
isize::MAX
. length
must be less than or equal tocapacity
.- The first
length
elements must be initialized values of typeT
.
It is also valid to create an empty Vec
passing a dangling pointer for ptr
and zero for
cap
and len
.
Sourcepub fn into_raw_parts(self) -> (*mut T, usize, usize)
pub fn into_raw_parts(self) -> (*mut T, usize, usize)
Consumes the Vec<T, A>
and returns its raw components pointer
, length
and capacity
.
This will not run the destructor of the contained elements and for non-ZSTs the allocation
will stay alive indefinitely. Use Vec::from_raw_parts
to recover the Vec
, drop the
elements and free the allocation, if any.
Sourcepub fn reserve(
&mut self,
additional: usize,
flags: Flags,
) -> Result<(), AllocError>
pub fn reserve( &mut self, additional: usize, flags: Flags, ) -> Result<(), AllocError>
Ensures that the capacity exceeds the length by at least additional
elements.
§Examples
let mut v = KVec::new();
v.push(1, GFP_KERNEL)?;
v.reserve(10, GFP_KERNEL)?;
let cap = v.capacity();
assert!(cap >= 10);
v.reserve(10, GFP_KERNEL)?;
let new_cap = v.capacity();
assert_eq!(new_cap, cap);
Source§impl<T: Clone, A: Allocator> Vec<T, A>
impl<T: Clone, A: Allocator> Vec<T, A>
Sourcepub fn extend_with(
&mut self,
n: usize,
value: T,
flags: Flags,
) -> Result<(), AllocError>
pub fn extend_with( &mut self, n: usize, value: T, flags: Flags, ) -> Result<(), AllocError>
Extend the vector by n
clones of value
.
Sourcepub fn extend_from_slice(
&mut self,
other: &[T],
flags: Flags,
) -> Result<(), AllocError>
pub fn extend_from_slice( &mut self, other: &[T], flags: Flags, ) -> Result<(), AllocError>
Trait Implementations§
Source§impl<T, A> IntoIterator for Vec<T, A>where
A: Allocator,
impl<T, A> IntoIterator for Vec<T, A>where
A: Allocator,
Source§fn into_iter(self) -> Self::IntoIter
fn into_iter(self) -> Self::IntoIter
Consumes the Vec<T, A>
and creates an Iterator
, which moves each value out of the
vector (from start to end).
§Examples
let v = kernel::kvec![1, 2]?;
let mut v_iter = v.into_iter();
let first_element: Option<u32> = v_iter.next();
assert_eq!(first_element, Some(1));
assert_eq!(v_iter.next(), Some(2));
assert_eq!(v_iter.next(), None);
let v = kernel::kvec![];
let mut v_iter = v.into_iter();
let first_element: Option<u32> = v_iter.next();
assert_eq!(first_element, None);