Skip to main content

Variant

Variant It is a OPC UA generic value container that covers all OPC UA built-in data types. SDK Use it to pass values between Rust and native code.

Supporting

Public Methods​

CategoryPropertyTypeAccessDescription
Typedata_type()BuiltinTypeRBuilt-in type
StructureVariant::new()VariantRNull, Variant owns=true
Variant::default()VariantRSame as new()

Create​

use darra_opcua::Variant;

// Variant Null
let v = Variant::new();
let v = Variant::default(); // Ibid.

Variant::new() Create an instance with owns=true; when it is dropped, the underlying native memory is automatically released.

Setter (Chain Builder)​

Each setter returns &mut Self:

let mut v = Variant::new();
v.set_bool(true);
v.set_byte(255);
v.set_sbyte(-1);
v.set_i16(42);
v.set_u16(42);
v.set_i32(42);
v.set_u32(42);
v.set_i64(42);
v.set_f32(3.14);
v.set_f64(3.14);
v.set_string("hello");
v.set_datetime(std::time::SystemTime::now());

Getter (returns an Option)​

Returns when there is a type mismatch None, Don't panic:

let b: Option<bool>       = v.try_get_bool();
let i: Option<i32> = v.try_get_i32();
let u: Option<u32> = v.try_get_u32();
let n: Option<i64> = v.try_get_i64();
let f: Option<f64> = v.try_get_f64();
let s: Option<String> = v.try_get_string();
let t: Option<SystemTime> = v.try_get_datetime();
let ft: Option<i64> = v.try_get_filetime(); // Original 100ns ticks

Backward Compatibility (Aligned with Previous 0.5 Versions):

v.as_bool();
v.as_i32() / v.as_u32() / v.as_i64() / v.as_f64();
v.as_string() / v.as_datetime() / v.as_filetime();

Data Type Lookup​

use darra_opcua::BuiltinType;

let t: BuiltinType = v.data_type();
match t {
BuiltinType::Boolean => { /* ... */ }
BuiltinType::Int32 => { /* ... */ }
BuiltinType::Double => { /* ... */ }
BuiltinType::String => { /* ... */ }
BuiltinType::DateTime => { /* ... */ }
BuiltinType::Null => { /* null */ }
_ => { /* Other */ }
}

Display / Debug​

println!("{}", v);          // Go to_display_string()
println!("{:?}", v); // "Variant(Double=3.14)"
let s = v.to_display_string();

BuiltinType Complete List​

pub enum BuiltinType {
Null = 0, Boolean, SByte, Byte,
Int16, UInt16, Int32, UInt32, Int64, UInt64,
Float, Double, String, DateTime, Guid,
ByteString, XmlElement,
NodeId, ExpandedNodeId, StatusCode,
QualifiedName, LocalizedText, ExtensionObject,
DataValue, Variant, DiagnosticInfo,
}

Numbers 0–25.

Memory Ownership (Drop)​

Variant Wrap the native handle; implementation Drop:

{
let mut v = Variant::new();
v.set_f64(42.0);
s.write("ns=2;s=X", &v)?;
} // <- v Drop, automatic native memory deallocation

s.call It returns Vec<Variant> owns=true, which is also automatically released, Will not leak. s.read It returns DataValue, whose variant() is a borrowed handle (owns=false), and its lifecycle follows DataValue that of the outer.

In the subscription callback, the value obtained wrap_borrowed via Variant is owns=false; the underlying resource is not released (the C-level Publish thread manages the lifecycle, and it becomes invalid after the callback returns).

StatusCode​

StatusCode It is a newtype pub struct StatusCode(pub u32) , not an enum (because it allows OPC UA any integer not listed):

use darra_opcua::StatusCode;

let st = StatusCode(0);
assert_eq!(st, StatusCode::GOOD);
assert!(st.is_good());

// Associated Constants (Excerpt; see Associated Constants StatusCode for the complete list)
StatusCode::GOOD;
StatusCode::UNCERTAIN;
StatusCode::BAD;
StatusCode::BAD_NODE_ID_UNKNOWN;
StatusCode::BAD_NODE_ID_INVALID;
StatusCode::BAD_NOT_READABLE;
StatusCode::BAD_NOT_WRITABLE;
StatusCode::BAD_TYPE_MISMATCH;
StatusCode::BAD_USER_ACCESS_DENIED;
StatusCode::BAD_COMMUNICATION_ERROR;
StatusCode::BAD_TIMEOUT;
StatusCode::BAD_HISTORY_OPERATION_UNSUPPORTED;
StatusCode::BAD_NOT_SUPPORTED;
// Darra (Private)
StatusCode::INVALID_ARGUMENT;
StatusCode::INVALID_HANDLE;
StatusCode::OUT_OF_MEMORY;
StatusCode::NOT_IMPLEMENTED;

// Instance Methods
st.is_good(); // Upper 2 bits = 00
st.is_uncertain(); // Highest 2 bits = 01
st.is_bad(); // Highest 2 digits = 1x
st.raw(); // Original u32

Display Output in hexadecimal format, etc "0x00000000".

ExtensionObject (Complex Structure)​

OPC UA server-side custom Struct DataTypes are wrapped in ExtensionObjects. The SDK currently provides only basic ExtensionObject containers; full schema parsing arrives in a later version.

Cross-thread​

Variant is Send + Sync (internal native locking); it can be passed between threads. However, avoid using clone borrowed in subscription callbacks for cross-thread Variant use—instead to_display_string(), "flatten" the data using before passing it out.

Next Step​