Struct syscalls::SysnoMap

source ·
pub struct SysnoMap<T> { /* private fields */ }
Expand description

A map of syscalls to a type T.

This provides constant-time lookup of syscalls within a static array.

§Examples

struct Point { x: i32, y: i32 }

let mut map = SysnoMap::new();
map.insert(Sysno::openat, Point { x: 1, y: 2 });
assert!(map.get(Sysno::openat).is_some());

Use function callbacks:

let mut map = SysnoMap::<fn() -> i32>::new();
map.insert(Sysno::openat, || 1);
map.insert(Sysno::close, || -1);
assert_eq!(map.get(Sysno::openat).unwrap()(), 1);
assert_eq!(map.get(Sysno::close).unwrap()(), -1);
let mut syscalls = SysnoMap::from_iter([
    (Sysno::openat, 0),
    (Sysno::close, 42),
]);

assert!(!syscalls.is_empty());
assert_eq!(syscalls.remove(Sysno::openat), Some(0));
assert_eq!(syscalls.insert(Sysno::close, 4), Some(42));
assert!(syscalls.contains_key(Sysno::close));
assert_eq!(syscalls.get(Sysno::close), Some(&4));
assert_eq!(syscalls.insert(Sysno::close, 11), Some(4));
assert_eq!(syscalls.count(), 1);
assert_eq!(syscalls.remove(Sysno::close), Some(11));
assert!(syscalls.is_empty());

Implementations§

source§

impl<T> SysnoMap<T>

source

pub const fn new() -> Self

Initializes an empty syscall map.

source

pub const fn contains_key(&self, sysno: Sysno) -> bool

Returns true if the map contains the given syscall.

source

pub fn clear(&mut self)

Clears the map, removing all syscalls.

source

pub fn is_empty(&self) -> bool

Returns true if the map is empty. Athough this is an O(1) operation (because the total number of syscalls is always constant), it must always iterate over the whole map to determine if it is empty or not. Thus, this may have a large, constant overhead.

source

pub fn count(&self) -> usize

Returns the number of syscalls in the map. Although This is an O(1) operation (because the total number of syscalls is always constant), it must always iterate over the whole map to determine how many items it has. Thus, this may have a large, constant overhead.

source

pub fn insert(&mut self, sysno: Sysno, value: T) -> Option<T>

Inserts the given syscall into the map. Returns true if the syscall was not already in the map.

source

pub fn remove(&mut self, sysno: Sysno) -> Option<T>

Removes the given syscall from the map. Returns old value if the syscall was in the map.

source

pub fn get(&self, sysno: Sysno) -> Option<&T>

Returns a reference to the value corresponding to sysno. Returns None if the syscall is not in the map.

source

pub fn get_mut(&mut self, sysno: Sysno) -> Option<&mut T>

Returns a mutable reference to the value corresponding to sysno. Returns None if the syscall is not in the map.

source

pub fn iter(&self) -> SysnoMapIter<'_, T>

Returns an iterator that iterates over the syscalls contained in the map.

source

pub fn values(&self) -> SysnoMapValues<'_, T>

Returns an iterator that iterates over all enabled values contained in the map.

source§

impl<T: Copy> SysnoMap<T>

source

pub const fn from_slice(slice: &[(Sysno, T)]) -> Self

Initialize a syscall map from the given slice. Note that T must be Copy due to const fn limitations.

This is useful for constructing a static callback table.

§Example
use syscalls::{Sysno, SysnoMap};

static CALLBACKS: SysnoMap<fn() -> i32> = SysnoMap::from_slice(&[
    (Sysno::openat, || 42),
    (Sysno::close, || 43),
]);

static DESCRIPTIONS: SysnoMap<&'static str> = SysnoMap::from_slice(&[
    (Sysno::openat, "open and possibly create a file"),
    (Sysno::close, "close a file descriptor"),
]);

assert_eq!(CALLBACKS[Sysno::openat](), 42);
assert_eq!(DESCRIPTIONS[Sysno::close], "close a file descriptor");
source§

impl<T: Clone> SysnoMap<T>

source

pub fn init_all(default: &T) -> Self

Initializes all possible syscalls in the map with the given default value.

Trait Implementations§

source§

impl<T: Debug> Debug for SysnoMap<T>

source§

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

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

impl<T> Default for SysnoMap<T>

source§

fn default() -> Self

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

impl<T> Drop for SysnoMap<T>

source§

fn drop(&mut self)

Executes the destructor for this type. Read more
source§

impl<T> Extend<(Sysno, T)> for SysnoMap<T>

source§

fn extend<I: IntoIterator<Item = (Sysno, T)>>(&mut self, iter: I)

Extends a collection with the contents of an iterator. Read more
source§

fn extend_one(&mut self, item: A)

🔬This is a nightly-only experimental API. (extend_one)
Extends a collection with exactly one element.
source§

fn extend_reserve(&mut self, additional: usize)

🔬This is a nightly-only experimental API. (extend_one)
Reserves capacity in a collection for the given number of additional elements. Read more
source§

impl<T> FromIterator<(Sysno, T)> for SysnoMap<T>

source§

fn from_iter<I>(iter: I) -> Self
where I: IntoIterator<Item = (Sysno, T)>,

Creates a value from an iterator. Read more
source§

impl<T> Index<Sysno> for SysnoMap<T>

§

type Output = T

The returned type after indexing.
source§

fn index(&self, sysno: Sysno) -> &T

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

impl<T> IndexMut<Sysno> for SysnoMap<T>

source§

fn index_mut(&mut self, sysno: Sysno) -> &mut T

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

impl<'a, T> IntoIterator for &'a SysnoMap<T>

§

type Item = (Sysno, &'a T)

The type of the elements being iterated over.
§

type IntoIter = SysnoMapIter<'a, T>

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

fn into_iter(self) -> Self::IntoIter

Creates an iterator from a value. Read more

Auto Trait Implementations§

§

impl<T> Freeze for SysnoMap<T>
where T: Freeze,

§

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

§

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

§

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

§

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

§

impl<T> UnwindSafe for SysnoMap<T>
where T: 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>,

§

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>,

§

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.