OidTable

Struct OidTable 

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

Source

pub fn new() -> Self

Create a new empty OID table.

Source

pub fn with_capacity(capacity: usize) -> Self

Create an OID table with pre-allocated capacity.

Source

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.

Source

pub fn remove(&mut self, oid: &Oid) -> Option<V>

Remove an OID from the table.

Returns the removed value if the OID was present.

Source

pub fn get(&self, oid: &Oid) -> Option<&V>

Get the value for an exact OID match.

Source

pub fn get_next(&self, oid: &Oid) -> Option<(&Oid, &V)>

Get the lexicographically next OID and value after the given OID.

Returns None if there are no OIDs greater than the given one.

Source

pub fn len(&self) -> usize

Get the number of entries in the table.

Source

pub fn is_empty(&self) -> bool

Check if the table is empty.

Source

pub fn clear(&mut self)

Clear all entries from the table.

Source

pub fn iter(&self) -> impl Iterator<Item = (&Oid, &V)>

Iterate over all OID-value pairs in lexicographic order.

Trait Implementations§

Source§

impl<V: Clone> Clone for OidTable<V>

Source§

fn clone(&self) -> OidTable<V>

Returns a duplicate of the value. Read more
1.0.0 · Source§

fn clone_from(&mut self, source: &Self)

Performs copy-assignment from source. Read more
Source§

impl<V: Debug> Debug for OidTable<V>

Source§

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

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

impl<V> Default for OidTable<V>

Source§

fn default() -> Self

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

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> 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> CloneToUninit for T
where T: Clone,

Source§

unsafe fn clone_to_uninit(&self, dest: *mut u8)

🔬This is a nightly-only experimental API. (clone_to_uninit)
Performs copy-assignment from self to dest. Read more
Source§

impl<T> From<T> for T

Source§

fn from(t: T) -> T

Returns the argument unchanged.

Source§

impl<T> Instrument for T

Source§

fn instrument(self, span: Span) -> Instrumented<Self>

Instruments this type with the provided Span, returning an Instrumented wrapper. Read more
Source§

fn in_current_span(self) -> Instrumented<Self>

Instruments this type with the current Span, returning an Instrumented wrapper. Read more
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> Same for T

Source§

type Output = T

Should always be Self
Source§

impl<T> ToOwned for T
where T: Clone,

Source§

type Owned = T

The resulting type after obtaining ownership.
Source§

fn to_owned(&self) -> T

Creates owned data from borrowed data, usually by cloning. Read more
Source§

fn clone_into(&self, target: &mut T)

Uses borrowed data to replace owned data, usually by cloning. Read more
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.
Source§

impl<T> WithSubscriber for T

Source§

fn with_subscriber<S>(self, subscriber: S) -> WithDispatch<Self>
where S: Into<Dispatch>,

Attaches the provided Subscriber to this type, returning a WithDispatch wrapper. Read more
Source§

fn with_current_subscriber(self) -> WithDispatch<Self>

Attaches the current default Subscriber to this type, returning a WithDispatch wrapper. Read more