Value

Enum Value 

Source
#[non_exhaustive]
pub enum Value {
Show 14 variants Integer(i32), OctetString(Bytes), Null, ObjectIdentifier(Oid), IpAddress([u8; 4]), Counter32(u32), Gauge32(u32), TimeTicks(u32), Opaque(Bytes), Counter64(u64), NoSuchObject, NoSuchInstance, EndOfMibView, Unknown { tag: u8, data: Bytes, },
}
Expand description

SNMP value.

Represents all SNMP data types including SMIv2 types and exception values.

Variants (Non-exhaustive)§

This enum is marked as non-exhaustive
Non-exhaustive enums could have additional variants added in future. Therefore, when matching against variants of non-exhaustive enums, an extra wildcard arm must be added to account for any future variants.
§

Integer(i32)

INTEGER (ASN.1 primitive, signed 32-bit)

§

OctetString(Bytes)

OCTET STRING (arbitrary bytes).

Per RFC 2578 (SMIv2), OCTET STRING values have a maximum size of 65535 octets. This limit is not enforced during decoding to maintain permissive parsing behavior. Applications that require strict compliance should validate size after decoding.

§

Null

NULL

§

ObjectIdentifier(Oid)

OBJECT IDENTIFIER

§

IpAddress([u8; 4])

IpAddress (4 bytes, big-endian)

§

Counter32(u32)

Counter32 (unsigned 32-bit, wrapping)

§

Gauge32(u32)

Gauge32 / Unsigned32 (unsigned 32-bit, non-wrapping)

§

TimeTicks(u32)

TimeTicks (hundredths of seconds since epoch)

§

Opaque(Bytes)

Opaque (legacy, arbitrary bytes)

§

Counter64(u64)

Counter64 (unsigned 64-bit, wrapping).

SNMPv2c/v3 only. Counter64 was introduced in SNMPv2 (RFC 2578) and is not supported in SNMPv1. When sending Counter64 values to an SNMPv1 agent, the value will be silently ignored or cause an error depending on the agent implementation.

If your application needs to support SNMPv1, avoid using Counter64 or fall back to Counter32 (with potential overflow for high-bandwidth counters).

§

NoSuchObject

noSuchObject exception - the requested OID exists in the MIB but has no value.

This exception indicates that the agent recognizes the OID (it’s a valid MIB object), but there is no instance available. This commonly occurs when requesting a table column OID without an index.

§Example

use async_snmp::Value;

let response = Value::NoSuchObject;
assert!(response.is_exception());

// When handling responses, check for exceptions:
match response {
    Value::NoSuchObject => println!("OID exists but has no value"),
    _ => {}
}
§

NoSuchInstance

noSuchInstance exception - the specific instance does not exist.

This exception indicates that while the MIB object exists, the specific instance (index) requested does not. This commonly occurs when querying a table row that doesn’t exist.

§Example

use async_snmp::Value;

let response = Value::NoSuchInstance;
assert!(response.is_exception());
§

EndOfMibView

endOfMibView exception - end of the MIB has been reached.

This exception is returned during GETNEXT/GETBULK operations when there are no more OIDs lexicographically greater than the requested OID. This is the normal termination condition for SNMP walks.

§Example

use async_snmp::Value;

let response = Value::EndOfMibView;
assert!(response.is_exception());

// Commonly used to detect end of walk
if matches!(response, Value::EndOfMibView) {
    println!("Walk complete - reached end of MIB");
}
§

Unknown

Unknown/unrecognized value type (for forward compatibility)

Fields

§tag: u8
§data: Bytes

Implementations§

Source§

impl Value

Source

pub fn as_i32(&self) -> Option<i32>

Try to get as i32.

Returns Some(i32) for Value::Integer, None otherwise.

§Examples
use async_snmp::Value;

let v = Value::Integer(42);
assert_eq!(v.as_i32(), Some(42));

let v = Value::Integer(-100);
assert_eq!(v.as_i32(), Some(-100));

// Counter32 is not an Integer
let v = Value::Counter32(42);
assert_eq!(v.as_i32(), None);
Source

pub fn as_u32(&self) -> Option<u32>

Try to get as u32.

Returns Some(u32) for Value::Counter32, Value::Gauge32, Value::TimeTicks, or non-negative Value::Integer. Returns None otherwise.

§Examples
use async_snmp::Value;

// Works for Counter32, Gauge32, TimeTicks
assert_eq!(Value::Counter32(100).as_u32(), Some(100));
assert_eq!(Value::Gauge32(200).as_u32(), Some(200));
assert_eq!(Value::TimeTicks(300).as_u32(), Some(300));

// Works for non-negative integers
assert_eq!(Value::Integer(50).as_u32(), Some(50));

// Returns None for negative integers
assert_eq!(Value::Integer(-1).as_u32(), None);

// Counter64 returns None (use as_u64 instead)
assert_eq!(Value::Counter64(100).as_u32(), None);
Source

pub fn as_u64(&self) -> Option<u64>

Try to get as u64.

Returns Some(u64) for Value::Counter64, or any 32-bit unsigned type (Value::Counter32, Value::Gauge32, Value::TimeTicks), or non-negative Value::Integer. Returns None otherwise.

§Examples
use async_snmp::Value;

// Counter64 is the primary use case
assert_eq!(Value::Counter64(10_000_000_000).as_u64(), Some(10_000_000_000));

// Also works for 32-bit unsigned types
assert_eq!(Value::Counter32(100).as_u64(), Some(100));
assert_eq!(Value::Gauge32(200).as_u64(), Some(200));

// Non-negative integers work
assert_eq!(Value::Integer(50).as_u64(), Some(50));

// Negative integers return None
assert_eq!(Value::Integer(-1).as_u64(), None);
Source

pub fn as_bytes(&self) -> Option<&[u8]>

Try to get as bytes.

Returns Some(&[u8]) for Value::OctetString or Value::Opaque. Returns None otherwise.

§Examples
use async_snmp::Value;
use bytes::Bytes;

let v = Value::OctetString(Bytes::from_static(b"hello"));
assert_eq!(v.as_bytes(), Some(b"hello".as_slice()));

// Works for Opaque too
let v = Value::Opaque(Bytes::from_static(&[0xDE, 0xAD, 0xBE, 0xEF]));
assert_eq!(v.as_bytes(), Some(&[0xDE, 0xAD, 0xBE, 0xEF][..]));

// Other types return None
assert_eq!(Value::Integer(42).as_bytes(), None);
Source

pub fn as_str(&self) -> Option<&str>

Try to get as string (UTF-8).

Returns Some(&str) if the value is an Value::OctetString or Value::Opaque containing valid UTF-8. Returns None for other types or invalid UTF-8.

§Examples
use async_snmp::Value;
use bytes::Bytes;

let v = Value::OctetString(Bytes::from_static(b"Linux router1 5.4.0"));
assert_eq!(v.as_str(), Some("Linux router1 5.4.0"));

// Invalid UTF-8 returns None
let v = Value::OctetString(Bytes::from_static(&[0xFF, 0xFE]));
assert_eq!(v.as_str(), None);

// Binary data with valid UTF-8 bytes still works, but use as_bytes() for clarity
let binary = Value::OctetString(Bytes::from_static(&[0x80, 0x81, 0x82]));
assert_eq!(binary.as_str(), None); // Invalid UTF-8 sequence
assert!(binary.as_bytes().is_some());
Source

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

Try to get as OID.

Returns Some(&Oid) for Value::ObjectIdentifier, None otherwise.

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

let v = Value::ObjectIdentifier(oid!(1, 3, 6, 1, 2, 1, 1, 2, 0));
let oid = v.as_oid().unwrap();
assert_eq!(oid.to_string(), "1.3.6.1.2.1.1.2.0");

// Other types return None
assert_eq!(Value::Integer(42).as_oid(), None);
Source

pub fn as_ip(&self) -> Option<Ipv4Addr>

Try to get as IP address.

Returns Some(Ipv4Addr) for Value::IpAddress, None otherwise.

§Examples
use async_snmp::Value;
use std::net::Ipv4Addr;

let v = Value::IpAddress([192, 168, 1, 1]);
assert_eq!(v.as_ip(), Some(Ipv4Addr::new(192, 168, 1, 1)));

// Other types return None
assert_eq!(Value::Integer(42).as_ip(), None);
Source

pub fn is_exception(&self) -> bool

Check if this is an exception value.

Source

pub fn format_with_hint(&self, hint: &str) -> Option<String>

Format an OctetString or Opaque value using RFC 2579 DISPLAY-HINT.

Returns None if this is not an OctetString or Opaque value. On invalid hint syntax, falls back to hex encoding.

§Example
use async_snmp::Value;
use bytes::Bytes;

let mac = Value::OctetString(Bytes::from_static(&[0x00, 0x1a, 0x2b, 0x3c, 0x4d, 0x5e]));
assert_eq!(mac.format_with_hint("1x:"), Some("00:1a:2b:3c:4d:5e".into()));

let integer = Value::Integer(42);
assert_eq!(integer.format_with_hint("1d"), None);
Source

pub fn encode(&self, buf: &mut EncodeBuf)

Encode to BER.

Source

pub fn decode(decoder: &mut Decoder) -> Result<Self>

Decode from BER.

Trait Implementations§

Source§

impl Clone for Value

Source§

fn clone(&self) -> Value

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 Value

Source§

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

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

impl Display for Value

Source§

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

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

impl From<&[u8]> for Value

Source§

fn from(data: &[u8]) -> Self

Converts to this type from the input type.
Source§

impl From<&str> for Value

Source§

fn from(s: &str) -> Self

Converts to this type from the input type.
Source§

impl From<[u8; 4]> for Value

Source§

fn from(addr: [u8; 4]) -> Self

Converts to this type from the input type.
Source§

impl From<Bytes> for Value

Source§

fn from(data: Bytes) -> Self

Converts to this type from the input type.
Source§

impl From<Ipv4Addr> for Value

Source§

fn from(addr: Ipv4Addr) -> 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 From<String> for Value

Source§

fn from(s: String) -> Self

Converts to this type from the input type.
Source§

impl From<Value> for GetResult

Source§

fn from(value: Value) -> Self

Converts to this type from the input type.
Source§

impl From<i32> for Value

Convenience conversions for creating Value from common Rust types.

§Examples

use async_snmp::Value;
use bytes::Bytes;

// From integers
let v: Value = 42i32.into();
assert_eq!(v.as_i32(), Some(42));

// From strings (creates OctetString)
let v: Value = "hello".into();
assert_eq!(v.as_str(), Some("hello"));

// From String
let v: Value = String::from("world").into();
assert_eq!(v.as_str(), Some("world"));

// From byte slices
let v: Value = (&[1u8, 2, 3][..]).into();
assert_eq!(v.as_bytes(), Some(&[1, 2, 3][..]));

// From Bytes
let v: Value = Bytes::from_static(b"data").into();
assert_eq!(v.as_bytes(), Some(b"data".as_slice()));

// From u64 (creates Counter64)
let v: Value = 10_000_000_000u64.into();
assert_eq!(v.as_u64(), Some(10_000_000_000));

// From Ipv4Addr
use std::net::Ipv4Addr;
let v: Value = Ipv4Addr::new(10, 0, 0, 1).into();
assert_eq!(v.as_ip(), Some(Ipv4Addr::new(10, 0, 0, 1)));

// From [u8; 4] (creates IpAddress)
let v: Value = [192u8, 168, 1, 1].into();
assert!(matches!(v, Value::IpAddress([192, 168, 1, 1])));
Source§

fn from(v: i32) -> Self

Converts to this type from the input type.
Source§

impl From<u64> for Value

Source§

fn from(v: u64) -> Self

Converts to this type from the input type.
Source§

impl PartialEq for Value

Source§

fn eq(&self, other: &Value) -> 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 StructuralPartialEq for Value

Auto Trait Implementations§

§

impl !Freeze for Value

§

impl RefUnwindSafe for Value

§

impl Send for Value

§

impl Sync for Value

§

impl Unpin for Value

§

impl UnwindSafe for Value

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