Oid

Struct Oid 

Source
pub struct Oid { /* private fields */ }
Expand description

Object Identifier.

Stored as a sequence of arc values (u32). Uses SmallVec to avoid heap allocation for OIDs with 16 or fewer arcs.

Implementations§

Source§

impl Oid

Source

pub fn empty() -> Self

Create an empty OID.

Source

pub fn new(arcs: impl IntoIterator<Item = u32>) -> Self

Create an OID from arc values.

Accepts any iterator of u32 values.

§Examples
use async_snmp::oid::Oid;

// From a Vec
let oid = Oid::new(vec![1, 3, 6, 1, 2, 1]);
assert_eq!(oid.arcs(), &[1, 3, 6, 1, 2, 1]);

// From an array
let oid = Oid::new([1, 3, 6, 1]);
assert_eq!(oid.len(), 4);

// From a range
let oid = Oid::new(0..5);
assert_eq!(oid.arcs(), &[0, 1, 2, 3, 4]);
Source

pub fn from_slice(arcs: &[u32]) -> Self

Create an OID from a slice of arcs.

§Examples
use async_snmp::oid::Oid;

let arcs = [1, 3, 6, 1, 2, 1, 1, 1, 0];
let oid = Oid::from_slice(&arcs);
assert_eq!(oid.to_string(), "1.3.6.1.2.1.1.1.0");

// Empty slice creates an empty OID
let empty = Oid::from_slice(&[]);
assert!(empty.is_empty());
Source

pub fn parse(s: &str) -> Result<Self>

Parse an OID from dotted string notation (e.g., “1.3.6.1.2.1.1.1.0”).

§Validation

This method parses the string format but does not validate arc constraints per X.690 Section 8.19.4. Invalid OIDs like "3.0" (arc1 must be 0, 1, or 2) or "0.40" (arc2 must be ≤39 when arc1 < 2) will parse successfully.

To validate arc constraints, call validate() after parsing, or use to_ber_checked() which validates before encoding.

§Examples
use async_snmp::oid::Oid;

// Valid OID
let oid = Oid::parse("1.3.6.1.2.1.1.1.0").unwrap();
assert!(oid.validate().is_ok());

// Invalid arc1 parses but fails validation
let invalid = Oid::parse("3.0").unwrap();
assert!(invalid.validate().is_err());
Source

pub fn arcs(&self) -> &[u32]

Get the arc values.

Source

pub fn len(&self) -> usize

Get the number of arcs.

Source

pub fn is_empty(&self) -> bool

Check if the OID is empty.

Source

pub fn starts_with(&self, other: &Oid) -> bool

Check if this OID starts with another OID.

Returns true if self begins with the same arcs as other. An OID always starts with itself, and any OID starts with an empty OID.

§Examples
use async_snmp::oid::Oid;

let sys_descr = Oid::parse("1.3.6.1.2.1.1.1.0").unwrap();
let system = Oid::parse("1.3.6.1.2.1.1").unwrap();
let interfaces = Oid::parse("1.3.6.1.2.1.2").unwrap();

// sysDescr is under the system subtree
assert!(sys_descr.starts_with(&system));

// sysDescr is not under the interfaces subtree
assert!(!sys_descr.starts_with(&interfaces));

// Every OID starts with itself
assert!(sys_descr.starts_with(&sys_descr));

// Every OID starts with the empty OID
assert!(sys_descr.starts_with(&Oid::empty()));
Source

pub fn parent(&self) -> Option<Oid>

Get the parent OID (all arcs except the last).

Returns None if the OID is empty.

§Examples
use async_snmp::oid::Oid;

let sys_descr = Oid::parse("1.3.6.1.2.1.1.1.0").unwrap();
let parent = sys_descr.parent().unwrap();
assert_eq!(parent.to_string(), "1.3.6.1.2.1.1.1");

// Can chain parent() calls
let grandparent = parent.parent().unwrap();
assert_eq!(grandparent.to_string(), "1.3.6.1.2.1.1");

// Empty OID has no parent
assert!(Oid::empty().parent().is_none());
Source

pub fn child(&self, arc: u32) -> Oid

Create a child OID by appending an arc.

§Examples
use async_snmp::oid::Oid;

let system = Oid::parse("1.3.6.1.2.1.1").unwrap();

// sysDescr is system.1
let sys_descr = system.child(1);
assert_eq!(sys_descr.to_string(), "1.3.6.1.2.1.1.1");

// sysDescr.0 is the scalar instance
let sys_descr_instance = sys_descr.child(0);
assert_eq!(sys_descr_instance.to_string(), "1.3.6.1.2.1.1.1.0");
Source

pub fn strip_prefix(&self, prefix: &Oid) -> Option<Oid>

Strip a prefix OID, returning the remaining arcs as a new Oid.

Returns None if self doesn’t start with the given prefix. Follows str::strip_prefix semantics - stripping an equal OID returns an empty OID.

This is useful for extracting table indexes from walked OIDs.

§Examples
use async_snmp::{oid, Oid};

let if_descr_5 = oid!(1, 3, 6, 1, 2, 1, 2, 2, 1, 2, 5);
let if_descr = oid!(1, 3, 6, 1, 2, 1, 2, 2, 1, 2);

// Extract the index
let index = if_descr_5.strip_prefix(&if_descr).unwrap();
assert_eq!(index.arcs(), &[5]);

// Non-matching prefix returns None
let sys_descr = oid!(1, 3, 6, 1, 2, 1, 1, 1);
assert!(if_descr_5.strip_prefix(&sys_descr).is_none());

// Equal OIDs return empty
let same = oid!(1, 3, 6);
assert!(same.strip_prefix(&same).unwrap().is_empty());

// Empty prefix returns self
let any = oid!(1, 2, 3);
assert_eq!(any.strip_prefix(&Oid::empty()).unwrap(), any);
Source

pub fn suffix(&self, n: usize) -> Option<&[u32]>

Get the last N arcs as a slice (for multi-level table indexes).

Returns None if n exceeds the OID length.

This is useful for grouping SNMP table walk results by composite indexes.

§Examples
use async_snmp::oid;

// ipNetToMediaPhysAddress has index (ifIndex, IpAddress) = 5 arcs
let oid = oid!(1, 3, 6, 1, 2, 1, 4, 22, 1, 2, 1, 192, 168, 1, 100);

// Get the 5-arc index (ifIndex=1, IP=192.168.1.100)
let index = oid.suffix(5).unwrap();
assert_eq!(index, &[1, 192, 168, 1, 100]);

// Get just the last arc
assert_eq!(oid.suffix(1), Some(&[100][..]));

// suffix(0) returns empty slice
assert_eq!(oid.suffix(0), Some(&[][..]));

// Too large returns None
assert!(oid.suffix(100).is_none());
Source

pub fn validate(&self) -> Result<()>

Validate OID arcs per X.690 Section 8.19.4.

  • arc1 must be 0, 1, or 2
  • arc2 must be <= 39 when arc1 is 0 or 1
  • arc2 must not cause overflow when computing first subidentifier (arc1*40 + arc2)
§Examples
use async_snmp::oid::Oid;

// Standard SNMP OIDs are valid
let oid = Oid::parse("1.3.6.1.2.1.1.1.0").unwrap();
assert!(oid.validate().is_ok());

// arc1 must be 0, 1, or 2
let invalid = Oid::from_slice(&[3, 0]);
assert!(invalid.validate().is_err());

// arc2 must be <= 39 when arc1 is 0 or 1
let invalid = Oid::from_slice(&[0, 40]);
assert!(invalid.validate().is_err());

// arc2 can be large when arc1 is 2, but must not overflow
let valid = Oid::from_slice(&[2, 999]);
assert!(valid.validate().is_ok());
Source

pub fn validate_length(&self) -> Result<()>

Validate that the OID doesn’t exceed the maximum arc count.

SNMP implementations commonly limit OIDs to 128 subidentifiers. This check provides protection against DoS attacks from maliciously long OIDs.

§Examples
use async_snmp::oid::{Oid, MAX_OID_LEN};

let oid = Oid::parse("1.3.6.1.2.1.1.1.0").unwrap();
assert!(oid.validate_length().is_ok());

// Create an OID with too many arcs
let too_long: Vec<u32> = (0..150).collect();
let long_oid = Oid::new(too_long);
assert!(long_oid.validate_length().is_err());
Source

pub fn validate_all(&self) -> Result<()>

Validate both arc constraints and length.

Combines validate() and validate_length().

Source

pub fn to_ber_smallvec(&self) -> SmallVec<[u8; 64]>

Encode to BER format, returning bytes in a stack-allocated buffer.

Uses SmallVec to avoid heap allocation for OIDs with up to ~20 arcs. This is the optimized version used internally by encoding routines.

OID encoding (X.690 Section 8.19):

  • First two arcs encoded as (arc1 * 40) + arc2 using base-128
  • Remaining arcs encoded as base-128 variable length
Source

pub fn to_ber(&self) -> Vec<u8>

Encode to BER format.

OID encoding (X.690 Section 8.19):

  • First two arcs encoded as (arc1 * 40) + arc2 using base-128
  • Remaining arcs encoded as base-128 variable length
§Empty OID Encoding

Empty OIDs are encoded as zero bytes (empty content). Note that net-snmp encodes empty OIDs as [0x00] (single zero byte). This difference is unlikely to matter in practice since empty OIDs are rarely used in SNMP.

§Validation

This method does not validate arc constraints. Use to_ber_checked() for validation, or call validate() first.

Source

pub fn to_ber_checked(&self) -> Result<Vec<u8>>

Encode to BER format with validation.

Returns an error if the OID has invalid arcs per X.690 Section 8.19.4.

Source

pub fn from_ber(data: &[u8]) -> Result<Self>

Decode from BER format.

Enforces MAX_OID_LEN limit per RFC 2578 Section 3.5.

Trait Implementations§

Source§

impl Clone for Oid

Source§

fn clone(&self) -> Oid

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 Debug for Oid

Source§

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

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

impl Display for Oid

Source§

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

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

impl From<&[u32]> for Oid

Source§

fn from(arcs: &[u32]) -> Self

Converts to this type from the input type.
Source§

impl<const N: usize> From<[u32; N]> for Oid

Source§

fn from(arcs: [u32; N]) -> Self

Converts to this type from the input type.
Source§

impl From<Oid> for Value

Source§

fn from(oid: Oid) -> Self

Converts to this type from the input type.
Source§

impl FromStr for Oid

Source§

type Err = Box<Error>

The associated error which can be returned from parsing.
Source§

fn from_str(s: &str) -> Result<Self, Self::Err>

Parses a string s to return a value of this type. Read more
Source§

impl Hash for Oid

Source§

fn hash<__H: Hasher>(&self, state: &mut __H)

Feeds this value into the given Hasher. Read more
1.3.0 · Source§

fn hash_slice<H>(data: &[Self], state: &mut H)
where H: Hasher, Self: Sized,

Feeds a slice of this type into the given Hasher. Read more
Source§

impl Ord for Oid

Source§

fn cmp(&self, other: &Self) -> Ordering

This method returns an Ordering between self and other. Read more
1.21.0 · Source§

fn max(self, other: Self) -> Self
where Self: Sized,

Compares and returns the maximum of two values. Read more
1.21.0 · Source§

fn min(self, other: Self) -> Self
where Self: Sized,

Compares and returns the minimum of two values. Read more
1.50.0 · Source§

fn clamp(self, min: Self, max: Self) -> Self
where Self: Sized,

Restrict a value to a certain interval. Read more
Source§

impl PartialEq for Oid

Source§

fn eq(&self, other: &Oid) -> bool

Tests for self and other values to be equal, and is used by ==.
1.0.0 · Source§

fn ne(&self, other: &Rhs) -> bool

Tests for !=. The default implementation is almost always sufficient, and should not be overridden without very good reason.
Source§

impl PartialOrd for Oid

Source§

fn partial_cmp(&self, other: &Self) -> Option<Ordering>

This method returns an ordering between self and other values if one exists. Read more
1.0.0 · Source§

fn lt(&self, other: &Rhs) -> bool

Tests less than (for self and other) and is used by the < operator. Read more
1.0.0 · Source§

fn le(&self, other: &Rhs) -> bool

Tests less than or equal to (for self and other) and is used by the <= operator. Read more
1.0.0 · Source§

fn gt(&self, other: &Rhs) -> bool

Tests greater than (for self and other) and is used by the > operator. Read more
1.0.0 · Source§

fn ge(&self, other: &Rhs) -> bool

Tests greater than or equal to (for self and other) and is used by the >= operator. Read more
Source§

impl Eq for Oid

Source§

impl StructuralPartialEq for Oid

Auto Trait Implementations§

§

impl Freeze for Oid

§

impl RefUnwindSafe for Oid

§

impl Send for Oid

§

impl Sync for Oid

§

impl Unpin for Oid

§

impl UnwindSafe for Oid

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> ToString for T
where T: Display + ?Sized,

Source§

fn to_string(&self) -> String

Converts the given value to a String. 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