pub type KVVec<T> = Vec<T, KVmalloc>;
Expand description
Aliased Type§
struct KVVec<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 inc_len(&mut self, additional: usize)
pub unsafe fn inc_len(&mut self, additional: usize)
Increments self.len
by additional
.
§Safety
additional
must be less than or equal toself.capacity - self.len
.- All elements within the interval [
self.len
,self.len + additional
) 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 push_within_capacity(&mut self, v: T) -> Result<(), PushError<T>>
pub fn push_within_capacity(&mut self, v: T) -> Result<(), PushError<T>>
Sourcepub fn insert_within_capacity(
&mut self,
index: usize,
element: T,
) -> Result<(), InsertError<T>>
pub fn insert_within_capacity( &mut self, index: usize, element: T, ) -> Result<(), InsertError<T>>
Inserts an element at the given index in the Vec
instance.
Fails if the vector does not have capacity for the new element. Panics if the index is out of bounds.
§Examples
use kernel::alloc::kvec::InsertError;
let mut v = KVec::with_capacity(5, GFP_KERNEL)?;
for i in 0..5 {
v.insert_within_capacity(0, i)?;
}
assert!(matches!(v.insert_within_capacity(0, 5), Err(InsertError::OutOfCapacity(_))));
assert!(matches!(v.insert_within_capacity(1000, 5), Err(InsertError::IndexOutOfBounds(_))));
assert_eq!(v, [4, 3, 2, 1, 0]);
Sourcepub fn pop(&mut self) -> Option<T>
pub fn pop(&mut self) -> Option<T>
Removes the last element from a vector and returns it, or None
if it is empty.
§Examples
let mut v = KVec::new();
v.push(1, GFP_KERNEL)?;
v.push(2, GFP_KERNEL)?;
assert_eq!(&v, &[1, 2]);
assert_eq!(v.pop(), Some(2));
assert_eq!(v.pop(), Some(1));
assert_eq!(v.pop(), None);
Sourcepub fn remove(&mut self, i: usize) -> Result<T, RemoveError>
pub fn remove(&mut self, i: usize) -> Result<T, RemoveError>
Removes the element at the given index.
§Examples
let mut v = kernel::kvec![1, 2, 3]?;
assert_eq!(v.remove(1)?, 2);
assert_eq!(v, [1, 3]);
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 clear(&mut self)
pub fn clear(&mut self)
Clears the vector, removing all values.
Note that this method has no effect on the allocated capacity of the vector.
§Examples
let mut v = kernel::kvec![1, 2, 3]?;
v.clear();
assert!(v.is_empty());
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);
Sourcepub fn truncate(&mut self, len: usize)
pub fn truncate(&mut self, len: usize)
Shortens the vector, setting the length to len
and drops the removed values.
If len
is greater than or equal to the current length, this does nothing.
This has no effect on the capacity and will not allocate.
§Examples
let mut v = kernel::kvec![1, 2, 3]?;
v.truncate(1);
assert_eq!(v.len(), 1);
assert_eq!(&v, &[1]);
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>
Sourcepub fn from_elem(value: T, n: usize, flags: Flags) -> Result<Self, AllocError>
pub fn from_elem(value: T, n: usize, flags: Flags) -> Result<Self, AllocError>
Create a new Vec<T, A>
and extend it by n
clones of value
.
Sourcepub fn resize(
&mut self,
new_len: usize,
value: T,
flags: Flags,
) -> Result<(), AllocError>
pub fn resize( &mut self, new_len: usize, value: T, flags: Flags, ) -> Result<(), AllocError>
Resizes the Vec
so that len
is equal to new_len
.
If new_len
is smaller than len
, the Vec
is Vec::truncate
d.
If new_len
is larger, each new slot is filled with clones of value
.
§Examples
let mut v = kernel::kvec![1, 2, 3]?;
v.resize(1, 42, GFP_KERNEL)?;
assert_eq!(&v, &[1]);
v.resize(3, 42, GFP_KERNEL)?;
assert_eq!(&v, &[1, 42, 42]);
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);