#[non_exhaustive]pub enum Value {
}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
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)
Implementations§
Source§impl Value
impl Value
Sourcepub fn as_i32(&self) -> Option<i32>
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);Sourcepub fn as_u32(&self) -> Option<u32>
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);Sourcepub fn as_u64(&self) -> Option<u64>
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);Sourcepub fn as_bytes(&self) -> Option<&[u8]>
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);Sourcepub fn as_str(&self) -> Option<&str>
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());Sourcepub fn as_oid(&self) -> Option<&Oid>
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);Sourcepub fn as_ip(&self) -> Option<Ipv4Addr>
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);Sourcepub fn as_f64(&self) -> Option<f64>
pub fn as_f64(&self) -> Option<f64>
Extract any numeric value as f64.
Useful for metrics systems and graphing where all values become f64. Counter64 values above 2^53 may lose precision.
§Examples
use async_snmp::Value;
assert_eq!(Value::Integer(42).as_f64(), Some(42.0));
assert_eq!(Value::Counter32(1000).as_f64(), Some(1000.0));
assert_eq!(Value::Counter64(10_000_000_000).as_f64(), Some(10_000_000_000.0));
assert_eq!(Value::Null.as_f64(), None);Sourcepub fn as_f64_wrapped(&self) -> Option<f64>
pub fn as_f64_wrapped(&self) -> Option<f64>
Extract Counter64 as f64 with wrapping at 2^53.
Prevents precision loss for large counters. IEEE 754 double-precision floats have a 53-bit mantissa, so Counter64 values above 2^53 lose precision when converted directly. This method wraps at the mantissa limit, preserving precision for rate calculations.
Use when computing rates where precision matters more than absolute
magnitude. For Counter32 and other types, behaves identically to as_f64().
§Examples
use async_snmp::Value;
// Small values behave the same as as_f64()
assert_eq!(Value::Counter64(1000).as_f64_wrapped(), Some(1000.0));
// Large Counter64 wraps at 2^53
let large = 1u64 << 54; // 2^54
let wrapped = Value::Counter64(large).as_f64_wrapped().unwrap();
assert!(wrapped < large as f64); // Wrapped to smaller valueSourcepub fn as_decimal(&self, places: u8) -> Option<f64>
pub fn as_decimal(&self, places: u8) -> Option<f64>
Extract integer with implied decimal places.
Many SNMP sensors report fixed-point values as integers with an implied decimal point. This method applies the scaling directly, returning a usable f64 value.
This complements format_with_hint("d-2") which returns a String
for display. Use as_decimal() when you need the numeric value
for computation or metrics.
§Examples
use async_snmp::Value;
// Temperature 2350 with places=2 → 23.50
assert_eq!(Value::Integer(2350).as_decimal(2), Some(23.50));
// Percentage 9999 with places=2 → 99.99
assert_eq!(Value::Integer(9999).as_decimal(2), Some(99.99));
// Voltage 12500 with places=3 → 12.500
assert_eq!(Value::Integer(12500).as_decimal(3), Some(12.5));
// Non-numeric types return None
assert_eq!(Value::Null.as_decimal(2), None);Sourcepub fn as_duration(&self) -> Option<Duration>
pub fn as_duration(&self) -> Option<Duration>
TimeTicks as Duration (hundredths of seconds).
TimeTicks represents time in hundredths of a second. This method
converts to std::time::Duration for idiomatic Rust time handling.
Common use: sysUpTime, interface last-change timestamps.
§Examples
use async_snmp::Value;
use std::time::Duration;
// 360000 ticks = 3600 seconds = 1 hour
let uptime = Value::TimeTicks(360000);
assert_eq!(uptime.as_duration(), Some(Duration::from_secs(3600)));
// Non-TimeTicks return None
assert_eq!(Value::Integer(100).as_duration(), None);Sourcepub fn as_opaque_float(&self) -> Option<f32>
pub fn as_opaque_float(&self) -> Option<f32>
Extract IEEE 754 float from Opaque value (net-snmp extension).
Decodes the two-layer ASN.1 structure used by net-snmp to encode floats inside Opaque values: extension tag (0x9f) + float type (0x78) + length (4)
- 4 bytes IEEE 754 big-endian float.
This is a non-standard extension supported by net-snmp for agents that need to report floating-point values. Standard SNMP uses implied decimal points via DISPLAY-HINT instead.
§Examples
use async_snmp::Value;
use bytes::Bytes;
// Opaque-encoded float for pi
let data = Bytes::from_static(&[0x9f, 0x78, 0x04, 0x40, 0x49, 0x0f, 0xdb]);
let value = Value::Opaque(data);
let pi = value.as_opaque_float().unwrap();
assert!((pi - std::f32::consts::PI).abs() < 0.0001);
// Non-Opaque or wrong format returns None
assert_eq!(Value::Integer(42).as_opaque_float(), None);Sourcepub fn as_opaque_double(&self) -> Option<f64>
pub fn as_opaque_double(&self) -> Option<f64>
Extract IEEE 754 double from Opaque value (net-snmp extension).
Decodes the two-layer ASN.1 structure used by net-snmp to encode doubles inside Opaque values: extension tag (0x9f) + double type (0x79) + length (8)
- 8 bytes IEEE 754 big-endian double.
§Examples
use async_snmp::Value;
use bytes::Bytes;
// Opaque-encoded double for pi ≈ 3.141592653589793
let data = Bytes::from_static(&[
0x9f, 0x79, 0x08, // extension tag, double type, length
0x40, 0x09, 0x21, 0xfb, 0x54, 0x44, 0x2d, 0x18 // IEEE 754 double
]);
let value = Value::Opaque(data);
let pi = value.as_opaque_double().unwrap();
assert!((pi - std::f64::consts::PI).abs() < 1e-10);Sourcepub fn as_opaque_counter64(&self) -> Option<u64>
pub fn as_opaque_counter64(&self) -> Option<u64>
Extract Counter64 from Opaque value (net-snmp extension for SNMPv1).
SNMPv1 doesn’t support Counter64 natively. net-snmp encodes 64-bit counters inside Opaque for SNMPv1 compatibility using extension tag (0x9f) + counter64 type (0x76) + length + big-endian bytes.
§Examples
use async_snmp::Value;
use bytes::Bytes;
// Opaque-encoded Counter64 with value 0x0123456789ABCDEF
let data = Bytes::from_static(&[
0x9f, 0x76, 0x08, // extension tag, counter64 type, length
0x01, 0x23, 0x45, 0x67, 0x89, 0xAB, 0xCD, 0xEF
]);
let value = Value::Opaque(data);
assert_eq!(value.as_opaque_counter64(), Some(0x0123456789ABCDEF));Sourcepub fn as_opaque_i64(&self) -> Option<i64>
pub fn as_opaque_i64(&self) -> Option<i64>
Extract signed 64-bit integer from Opaque value (net-snmp extension).
Uses extension tag (0x9f) + i64 type (0x7a) + length + big-endian bytes.
§Examples
use async_snmp::Value;
use bytes::Bytes;
// Opaque-encoded I64 with value -1 (0xFFFFFFFFFFFFFFFF)
let data = Bytes::from_static(&[
0x9f, 0x7a, 0x08,
0xFF, 0xFF, 0xFF, 0xFF, 0xFF, 0xFF, 0xFF, 0xFF
]);
let value = Value::Opaque(data);
assert_eq!(value.as_opaque_i64(), Some(-1i64));Sourcepub fn as_opaque_u64(&self) -> Option<u64>
pub fn as_opaque_u64(&self) -> Option<u64>
Extract unsigned 64-bit integer from Opaque value (net-snmp extension).
Uses extension tag (0x9f) + u64 type (0x7b) + length + big-endian bytes.
§Examples
use async_snmp::Value;
use bytes::Bytes;
// Opaque-encoded U64 with value 0x0123456789ABCDEF
let data = Bytes::from_static(&[
0x9f, 0x7b, 0x08,
0x01, 0x23, 0x45, 0x67, 0x89, 0xAB, 0xCD, 0xEF
]);
let value = Value::Opaque(data);
assert_eq!(value.as_opaque_u64(), Some(0x0123456789ABCDEF));Sourcepub fn as_truth_value(&self) -> Option<bool>
pub fn as_truth_value(&self) -> Option<bool>
Extract RFC 2579 TruthValue as bool.
TruthValue is an INTEGER with: true(1), false(2).
Returns None for non-Integer values or values outside {1, 2}.
§Examples
use async_snmp::Value;
assert_eq!(Value::Integer(1).as_truth_value(), Some(true));
assert_eq!(Value::Integer(2).as_truth_value(), Some(false));
// Invalid values return None
assert_eq!(Value::Integer(0).as_truth_value(), None);
assert_eq!(Value::Integer(3).as_truth_value(), None);
assert_eq!(Value::Null.as_truth_value(), None);Sourcepub fn as_row_status(&self) -> Option<RowStatus>
pub fn as_row_status(&self) -> Option<RowStatus>
Extract RFC 2579 RowStatus.
Returns None for non-Integer values or values outside {1-6}.
§Examples
use async_snmp::{Value, RowStatus};
assert_eq!(Value::Integer(1).as_row_status(), Some(RowStatus::Active));
assert_eq!(Value::Integer(6).as_row_status(), Some(RowStatus::Destroy));
// Invalid values return None
assert_eq!(Value::Integer(0).as_row_status(), None);
assert_eq!(Value::Integer(7).as_row_status(), None);
assert_eq!(Value::Null.as_row_status(), None);Sourcepub fn as_storage_type(&self) -> Option<StorageType>
pub fn as_storage_type(&self) -> Option<StorageType>
Extract RFC 2579 StorageType.
Returns None for non-Integer values or values outside {1-5}.
§Examples
use async_snmp::{Value, StorageType};
assert_eq!(Value::Integer(2).as_storage_type(), Some(StorageType::Volatile));
assert_eq!(Value::Integer(3).as_storage_type(), Some(StorageType::NonVolatile));
// Invalid values return None
assert_eq!(Value::Integer(0).as_storage_type(), None);
assert_eq!(Value::Integer(6).as_storage_type(), None);
assert_eq!(Value::Null.as_storage_type(), None);Sourcepub fn is_exception(&self) -> bool
pub fn is_exception(&self) -> bool
Check if this is an exception value.
Sourcepub fn format_with_hint(&self, hint: &str) -> Option<String>
pub fn format_with_hint(&self, hint: &str) -> Option<String>
Format an OctetString, Opaque, or Integer value using RFC 2579 DISPLAY-HINT.
For OctetString and Opaque, uses the OCTET STRING hint format (e.g., “1x:”). For Integer, uses the INTEGER hint format (e.g., “d-2” for decimal places).
Returns None for other value types or invalid hint syntax.
On invalid OCTET STRING hint syntax, falls back to hex encoding.
§Example
use async_snmp::Value;
use bytes::Bytes;
// OctetString: MAC address
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()));
// Integer: Temperature with 2 decimal places
let temp = Value::Integer(2350);
assert_eq!(temp.format_with_hint("d-2"), Some("23.50".into()));
// Integer: Hex format
let int = Value::Integer(255);
assert_eq!(int.format_with_hint("x"), Some("ff".into()));Trait Implementations§
Source§impl From<StorageType> for Value
impl From<StorageType> for Value
Source§fn from(storage: StorageType) -> Self
fn from(storage: StorageType) -> Self
Source§impl From<i32> for Value
Convenience conversions for creating Value from common Rust types.
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])));