Type Alias KVVec

Source
pub type KVVec<T> = Vec<T, KVmalloc>;
Expand description

Type alias for Vec with a KVmalloc allocator.

§Examples

let mut v = KVVec::new();
v.push(1, GFP_KERNEL)?;
assert_eq!(&v, &[1]);

Aliased Type§

struct KVVec<T> { /* private fields */ }

Implementations

Source§

impl<T, A> Vec<T, A>
where A: Allocator,

Source

pub fn capacity(&self) -> usize

Returns the number of elements that can be stored within the vector without allocating additional memory.

Source

pub fn len(&self) -> usize

Returns the number of elements stored within the vector.

Source

pub unsafe fn inc_len(&mut self, additional: usize)

Increments self.len by additional.

§Safety
  • additional must be less than or equal to self.capacity - self.len.
  • All elements within the interval [self.len,self.len + additional) must be initialized.
Source

pub fn as_slice(&self) -> &[T]

Returns a slice of the entire vector.

Source

pub fn as_mut_slice(&mut self) -> &mut [T]

Returns a mutable slice of the entire vector.

Source

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.

Source

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.

Source

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());
Source

pub const fn new() -> Self

Creates a new, empty Vec<T, A>.

This method does not allocate by itself.

Source

pub fn spare_capacity_mut(&mut self) -> &mut [MaybeUninit<T>]

Returns a slice of MaybeUninit<T> for the remaining spare capacity of the vector.

Source

pub fn push(&mut self, v: T, flags: Flags) -> Result<(), AllocError>

Appends an element to the back of the Vec instance.

§Examples
let mut v = KVec::new();
v.push(1, GFP_KERNEL)?;
assert_eq!(&v, &[1]);

v.push(2, GFP_KERNEL)?;
assert_eq!(&v, &[1, 2]);
Source

pub fn push_within_capacity(&mut self, v: T) -> Result<(), PushError<T>>

Appends an element to the back of the Vec instance without reallocating.

Fails if the vector does not have capacity for the new element.

§Examples
let mut v = KVec::with_capacity(10, GFP_KERNEL)?;
for i in 0..10 {
    v.push_within_capacity(i)?;
}

assert!(v.push_within_capacity(10).is_err());
Source

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]);
Source

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);
Source

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]);
Source

pub fn with_capacity(capacity: usize, flags: Flags) -> Result<Self, AllocError>

Creates a new Vec instance with at least the given capacity.

§Examples
let v = KVec::<u32>::with_capacity(20, GFP_KERNEL)?;

assert!(v.capacity() >= 20);
Source

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 allocator A.
  • ptr must satisfy or exceed the alignment requirements of T.
  • ptr must point to memory with a size of at least size_of::<T>() * capacity bytes.
  • The allocated size in bytes must not be larger than isize::MAX.
  • length must be less than or equal to capacity.
  • The first length elements must be initialized values of type T.

It is also valid to create an empty Vec passing a dangling pointer for ptr and zero for cap and len.

Source

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.

Source

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());
Source

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

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

pub fn drain_all(&mut self) -> DrainAll<'_, T>

Takes ownership of all items in this vector without consuming the allocation.

§Examples
let mut v = kernel::kvec![0, 1, 2, 3]?;

for (i, j) in v.drain_all().enumerate() {
    assert_eq!(i, j);
}

assert!(v.capacity() >= 4);
Source

pub fn retain(&mut self, f: impl FnMut(&mut T) -> bool)

Removes all elements that don’t match the provided closure.

§Examples
let mut v = kernel::kvec![1, 2, 3, 4]?;
v.retain(|i| *i % 2 == 0);
assert_eq!(v, [2, 4]);
Source§

impl<T: Clone, A: Allocator> Vec<T, A>

Source

pub fn extend_with( &mut self, n: usize, value: T, flags: Flags, ) -> Result<(), AllocError>

Extend the vector by n clones of value.

Source

pub fn extend_from_slice( &mut self, other: &[T], flags: Flags, ) -> Result<(), AllocError>

Pushes clones of the elements of slice into the Vec instance.

§Examples
let mut v = KVec::new();
v.push(1, GFP_KERNEL)?;

v.extend_from_slice(&[20, 30, 40], GFP_KERNEL)?;
assert_eq!(&v, &[1, 20, 30, 40]);

v.extend_from_slice(&[50, 60], GFP_KERNEL)?;
assert_eq!(&v, &[1, 20, 30, 40, 50, 60]);
Source

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.

Source

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::truncated. 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: Debug, A: Allocator> Debug for Vec<T, A>

Source§

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

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

impl<T, A> Deref for Vec<T, A>
where A: Allocator,

Source§

type Target = [T]

The resulting type after dereferencing.
Source§

fn deref(&self) -> &[T]

Dereferences the value.
Source§

impl<T, A> DerefMut for Vec<T, A>
where A: Allocator,

Source§

fn deref_mut(&mut self) -> &mut [T]

Mutably dereferences the value.
Source§

impl<T, A> Drop for Vec<T, A>
where A: Allocator,

Source§

fn drop(&mut self)

Executes the destructor for this type. Read more
Source§

impl<T, A, const N: usize> From<Box<[T; N], A>> for Vec<T, A>
where A: Allocator,

Source§

fn from(b: Box<[T; N], A>) -> Vec<T, A>

Converts to this type from the input type.
Source§

impl<T, I: SliceIndex<[T]>, A> Index<I> for Vec<T, A>
where A: Allocator,

Source§

type Output = <I as SliceIndex<[T]>>::Output

The returned type after indexing.
Source§

fn index(&self, index: I) -> &Self::Output

Performs the indexing (container[index]) operation. Read more
Source§

impl<T, I: SliceIndex<[T]>, A> IndexMut<I> for Vec<T, A>
where A: Allocator,

Source§

fn index_mut(&mut self, index: I) -> &mut Self::Output

Performs the mutable indexing (container[index]) operation. Read more
Source§

impl<T, A> IntoIterator for Vec<T, A>
where A: Allocator,

Source§

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);
Source§

type Item = T

The type of the elements being iterated over.
Source§

type IntoIter = IntoIter<T, A>

Which kind of iterator are we turning this into?
Source§

impl<T, U, A: Allocator> PartialEq<&[U]> for Vec<T, A>
where T: PartialEq<U>,

Source§

fn eq(&self, other: &&[U]) -> bool

Tests for self and other values to be equal, and is used by ==.
1.0.0 · Source§

fn ne(&self, other: &Rhs) -> bool

Tests for !=. The default implementation is almost always sufficient, and should not be overridden without very good reason.
Source§

impl<T, U, A: Allocator, const N: usize> PartialEq<&[U; N]> for Vec<T, A>
where T: PartialEq<U>,

Source§

fn eq(&self, other: &&[U; N]) -> bool

Tests for self and other values to be equal, and is used by ==.
1.0.0 · Source§

fn ne(&self, other: &Rhs) -> bool

Tests for !=. The default implementation is almost always sufficient, and should not be overridden without very good reason.
Source§

impl<T, U, A: Allocator> PartialEq<&mut [U]> for Vec<T, A>
where T: PartialEq<U>,

Source§

fn eq(&self, other: &&mut [U]) -> bool

Tests for self and other values to be equal, and is used by ==.
1.0.0 · Source§

fn ne(&self, other: &Rhs) -> bool

Tests for !=. The default implementation is almost always sufficient, and should not be overridden without very good reason.
Source§

impl<T, U, A: Allocator> PartialEq<[U]> for Vec<T, A>
where T: PartialEq<U>,

Source§

fn eq(&self, other: &[U]) -> bool

Tests for self and other values to be equal, and is used by ==.
1.0.0 · Source§

fn ne(&self, other: &Rhs) -> bool

Tests for !=. The default implementation is almost always sufficient, and should not be overridden without very good reason.
Source§

impl<T, U, A: Allocator, const N: usize> PartialEq<[U; N]> for Vec<T, A>
where T: PartialEq<U>,

Source§

fn eq(&self, other: &[U; N]) -> bool

Tests for self and other values to be equal, and is used by ==.
1.0.0 · Source§

fn ne(&self, other: &Rhs) -> bool

Tests for !=. The default implementation is almost always sufficient, and should not be overridden without very good reason.
Source§

impl<T, U, A1: Allocator, A2: Allocator> PartialEq<Vec<U, A2>> for Vec<T, A1>
where T: PartialEq<U>,

Source§

fn eq(&self, other: &Vec<U, A2>) -> bool

Tests for self and other values to be equal, and is used by ==.
1.0.0 · Source§

fn ne(&self, other: &Rhs) -> bool

Tests for !=. The default implementation is almost always sufficient, and should not be overridden without very good reason.
Source§

impl<T: Eq, A> Eq for Vec<T, A>
where A: Allocator,

Source§

impl<T, A> Send for Vec<T, A>
where T: Send, A: Allocator,

Source§

impl<T, A> Sync for Vec<T, A>
where T: Sync, A: Allocator,