pub struct SysnoMap<T> { /* private fields */ }Expand description
A map of syscalls to a type T.
Backed by a static array indexed by the syscall table offset, this provides
O(1) lookups with minimal overhead. Membership is tracked by a companion
SysnoSet so that iteration visits only initialized entries.
Intended uses
- Fast dispatch tables (e.g., mapping
Sysnoto handlers). - Statically initialized description tables via
from_slice.
Complexity
get/insert/remove: O(1)iter/values: O(k) where k is the number of set entries
§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 rawsys_linux::{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)