Struct HashMap

Source
pub struct HashMap<K: Copy + Default, V: Copy + Default> { /* private fields */ }
Expand description

A hashmap that exposes an idiomatic Rust interface to an underlying BPF hashmap.

Implementations§

Source§

impl<K: Copy + Default, V: Copy + Default> HashMap<K, V>

Source

pub fn with_capacity(entries: u32) -> Result<Self, Error>

Creates a new BPF hashmap with entries elements.

§Arguments
  • entries - The number of elements in the hashmap.
§Example
use bpf_api::collections::HashMap;

let hashmap = HashMap::<u32, u32>::with_capacity(10).expect("Failed to create hashmap");
Source

pub fn get(&self, key: K) -> Result<V, Error>

Retrieves the value for a given key.

§Arguments
  • key - The key associated with the value to be retrieved.
§Example
use bpf_api::collections::HashMap;

let hashmap = HashMap::<u32, u32>::with_capacity(10).expect("Failed to create hashmap");
assert!(matches!(hashmap.get(1000), Err(_)));
Source

pub fn insert(&self, key: K, val: V) -> Result<(), Error>

Sets the value for a given key.

§Arguments
  • key - The key associated with the value to be set.
  • value - The new value.
§Example
use bpf_api::collections::HashMap;

let hashmap = HashMap::<u32, u32>::with_capacity(10).expect("Failed to create hashmap");
assert!(matches!(hashmap.get(1000), Err(_)));
assert!(matches!(hashmap.insert(1000, 0xdeadbeef), Ok(_)));
assert!(matches!(hashmap.get(1000), Ok(0xdeadbeef)));
Source

pub fn remove(&self, key: K) -> Result<(), Error>

Deletes an entry from the hash map given a key.

§Arguments
  • key - The key of the entry to be deleted.
§Example
use bpf_api::collections::HashMap;

let hashmap = HashMap::<u32, u32>::with_capacity(10).expect("Failed to create hashmap");
assert!(matches!(hashmap.get(1000), Err(_)));
assert!(matches!(hashmap.insert(1000, 0xdeadbeef), Ok(_)));
assert!(matches!(hashmap.get(1000), Ok(0xdeadbeef)));
assert!(matches!(hashmap.remove(1000), Ok(_)));
assert!(matches!(hashmap.remove(1000), Err(_)));
Source

pub fn get_identifier(&self) -> u32

Retrieve the BPF identifier for this map. This is the underlying file descriptor that’s used in eBPF programs.

§Example
use bpf_api::collections::HashMap;

let hashmap = HashMap::<u32, u32>::with_capacity(10).expect("Failed to create hashmap");
hashmap.get_identifier();

Auto Trait Implementations§

§

impl<K, V> !Freeze for HashMap<K, V>

§

impl<K, V> RefUnwindSafe for HashMap<K, V>

§

impl<K, V> Send for HashMap<K, V>
where K: Send, V: Send,

§

impl<K, V> Sync for HashMap<K, V>
where K: Sync, V: Sync,

§

impl<K, V> Unpin for HashMap<K, V>
where K: Unpin, V: Unpin,

§

impl<K, V> UnwindSafe for HashMap<K, V>
where K: UnwindSafe, V: UnwindSafe,

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.