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>
impl<T> SysnoMap<T>
sourcepub const fn contains_key(&self, sysno: Sysno) -> bool
pub const fn contains_key(&self, sysno: Sysno) -> bool
Returns true if the map contains the given syscall.
sourcepub fn is_empty(&self) -> bool
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.
sourcepub fn count(&self) -> usize
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.
sourcepub fn insert(&mut self, sysno: Sysno, value: T) -> Option<T>
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.
sourcepub fn remove(&mut self, sysno: Sysno) -> Option<T>
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.
sourcepub fn get(&self, sysno: Sysno) -> Option<&T>
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.
sourcepub fn get_mut(&mut self, sysno: Sysno) -> Option<&mut T>
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.
sourcepub fn iter(&self) -> SysnoMapIter<'_, T> ⓘ
pub fn iter(&self) -> SysnoMapIter<'_, T> ⓘ
Returns an iterator that iterates over the syscalls contained in the map.
sourcepub fn values(&self) -> SysnoMapValues<'_, T> ⓘ
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>
impl<T: Copy> SysnoMap<T>
sourcepub const fn from_slice(slice: &[(Sysno, T)]) -> Self
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");
Trait Implementations§
source§impl<T> Extend<(Sysno, T)> for SysnoMap<T>
impl<T> Extend<(Sysno, T)> for SysnoMap<T>
source§fn extend<I: IntoIterator<Item = (Sysno, T)>>(&mut self, iter: I)
fn extend<I: IntoIterator<Item = (Sysno, T)>>(&mut self, iter: I)
source§fn extend_one(&mut self, item: A)
fn extend_one(&mut self, item: A)
extend_one
)source§fn extend_reserve(&mut self, additional: usize)
fn extend_reserve(&mut self, additional: usize)
extend_one
)