pub struct OidTable<V> { /* private fields */ }Expand description
Helper for implementing GETNEXT with lexicographic OID ordering.
This struct simplifies implementing the get_next method of MibHandler
by maintaining a sorted list of OID-value pairs and providing efficient
lookup for the next OID.
§Example
use async_snmp::handler::{MibHandler, RequestContext, GetResult, GetNextResult, OidTable, BoxFuture};
use async_snmp::{Oid, Value, VarBind, oid};
struct MyHandler {
table: OidTable<Value>,
}
impl MyHandler {
fn new() -> Self {
let mut table = OidTable::new();
table.insert(oid!(1, 3, 6, 1, 4, 1, 99999, 1, 0), Value::Integer(42));
table.insert(oid!(1, 3, 6, 1, 4, 1, 99999, 2, 0), Value::OctetString("test".into()));
Self { table }
}
}
impl MibHandler for MyHandler {
fn get<'a>(&'a self, _ctx: &'a RequestContext, oid: &'a Oid) -> BoxFuture<'a, GetResult> {
Box::pin(async move {
self.table.get(oid)
.cloned()
.map(GetResult::Value)
.unwrap_or(GetResult::NoSuchObject)
})
}
fn get_next<'a>(&'a self, _ctx: &'a RequestContext, oid: &'a Oid) -> BoxFuture<'a, GetNextResult> {
Box::pin(async move {
self.table.get_next(oid)
.map(|(next_oid, value)| GetNextResult::Value(VarBind::new(next_oid.clone(), value.clone())))
.unwrap_or(GetNextResult::EndOfMibView)
})
}
}Implementations§
Source§impl<V> OidTable<V>
impl<V> OidTable<V>
Sourcepub fn with_capacity(capacity: usize) -> Self
pub fn with_capacity(capacity: usize) -> Self
Create an OID table with pre-allocated capacity.
Sourcepub fn insert(&mut self, oid: Oid, value: V)
pub fn insert(&mut self, oid: Oid, value: V)
Insert an OID-value pair, maintaining sorted order.
If the OID already exists, its value is replaced.
Sourcepub fn remove(&mut self, oid: &Oid) -> Option<V>
pub fn remove(&mut self, oid: &Oid) -> Option<V>
Remove an OID from the table.
Returns the removed value if the OID was present.
Trait Implementations§
Auto Trait Implementations§
impl<V> Freeze for OidTable<V>
impl<V> RefUnwindSafe for OidTable<V>where
V: RefUnwindSafe,
impl<V> Send for OidTable<V>where
V: Send,
impl<V> Sync for OidTable<V>where
V: Sync,
impl<V> Unpin for OidTable<V>where
V: Unpin,
impl<V> UnwindSafe for OidTable<V>where
V: UnwindSafe,
Blanket Implementations§
Source§impl<T> BorrowMut<T> for Twhere
T: ?Sized,
impl<T> BorrowMut<T> for Twhere
T: ?Sized,
Source§fn borrow_mut(&mut self) -> &mut T
fn borrow_mut(&mut self) -> &mut T
Mutably borrows from an owned value. Read more