Skip to main content

Safe API — A variant that never returns Err

Overview​

By default, Rust uses Result<T, OpcUaError> to handle failures.Safe The variant Err(OpcUaError) folds into (StatusCode, T::default), returning a tuple instead of a Result, so that batch loops do not need to unwrap each ? or, and the execution flow is not interrupted.

Corresponding sections of the standard: Browse Part 4 §5.8, Read §5.10.2, Write §5.10.4, Call§5.11.2, HistoryRead Part 11§6.5.

API​

MethodsCategoryReading and WritingNote
ua.browse_safe(node_id, filter)BrowseReadReturns (StatusCode, Vec<Reference>)
ua.read_safe(node_id, attribute)ReadReadReturns (StatusCode, Option<DataValue>)
ua.write_safe(node_id, attribute, &value)WriteWriteReturns StatusCode, value is &Variant
ua.call_safe(object_id, method_id, &inputs)MethodsWriteReturns (StatusCode, Vec<Variant>)
ua.read_history_safe(node_id, t0, t1, max)HistoryReadReturns (StatusCode, Vec<DataValue>)

write_safe The order of the formal parameters is (node_id, attr, value) — The property comes first, followed by the value, and the value is passed by reference.

Code Example​

use darra_opcua::{AttributeId, NodeClass, Session, StatusCode, Variant};

fn main() -> Result<(), Box<dyn std::error::Error>> {
let mut ua = Session::new("opc.tcp://localhost:4840")?;
ua.connect()?;

// 1) Browse - Deleting a node should not cause an error for the entire tree
let (st, refs) = ua.browse_safe("ns=2;s=Boiler1", NodeClass::Unspecified);
if st != StatusCode::GOOD {
println!("Browse 失败 {:?}", st);
} else {
for r in &refs {
println!("{}", r.browse_name);
}
}

// 2) Batch Polling - A single failure does not interrupt the process
let node_list = ["ns=2;s=Temp", "ns=2;s=Pressure"];
for nid in &node_list {
let (rs, dv) = ua.read_safe(nid, AttributeId::Value);
match (rs, dv) {
(StatusCode::GOOD, Some(v)) => println!("{} = {:?}", nid, v.value()),
_ => println!("{} = <{:?}>", nid, rs),
}
}

// 3) Batch parameter assignment — write_safe(nid, attr, &v), with attributes first, values second, and values passed by reference
let setpoints = [("ns=2;s=Setpoint", 42.0_f64)];
for (nid, val) in setpoints {
let mut v = Variant::new();
v.set_f64(val);
let rc = ua.write_safe(nid, AttributeId::Value, &v);
if !rc.is_good() { eprintln!("写 {} 失败 {:?}", nid, rc); }
}

// 4) Calling the Method
let (cst, _outs) = ua.call_safe("ns=2;s=Robot", "ns=2;s=Robot.Home", &[]);
if !cst.is_good() { eprintln!("Home 失败: {:?}", cst); }

// 5) History
let (_hst, vals) = ua.read_history_safe("ns=2;s=Temp", t0, t1, 5000);
println!("{} 点", vals.len());
Ok(())
}

Relationship with Error Propagation​

The "Normal" and read/write/browse/call/read_history "Safe" variants followThe same underlying framework RPC: Returns immediately on failure Err(OpcUaError) (the "Safe" version is collapsed to StatusCode), No automatic reconnection, no retries. For session-level failures, the business side must decide whether to rebuild the session or prompt the user; see Automatic Reconnection。

Best Practices​

  • On the UI main thread always use Safe — propagating ? into a UI task will exit the thread.
  • Safe for Batch Processing / Inspection Cycles
  • The business critical path still uses the standard Result version, with explicit ? error propagation.

Cross-Language Comparison​

C#PythonJavaC++RustC
BrowseSafebrowse_safebrowseSafeBrowseSafebrowse_safeDarraUa_Session_BrowseSafe
ReadSaferead_safereadSafeReadSaferead_safeDarraUa_Session_ReadSafe
WriteSafewrite_safewriteSafeWriteSafewrite_safeDarraUa_Session_WriteSafe
CallSafecall_safecallSafeCallSafecall_safeDarraUa_Session_CallSafe
ReadHistorySaferead_history_safereadHistorySafeReadHistorySaferead_history_safeDarraUa_Session_ReadHistorySafe

Next Step​