Skip to main content

Rust-Specific Syntax Sugar

This page only covers darra_pns's Unique and Convenient Syntax. They Not a new feature. ..it simply repackages existing entry points into a more intuitive Rust syntax (RAII scopes, tuples).

Optional; leaving it out does not affect the standard entry. —— connected() / state() / error_code(), read_int16 / write_int16, read_struct / write_struct Use them as usual. Syntax sugar is just another way of writing code.

crate — darra_pns. use darra_pns::PnSService; — that's it. There is no such sugar::session(&mut device) function; the entry point is device.session().


I.RAII Conversation​

Created start(), then out of scope stop(). No close(the session is still active).IO Whether the controller is updating the cycle data still depends on connected().

Writing StyleCall
Standards APIdevice.start()?; … device.stop()?;
Syntax Sugarlet _session = device.session()?;
use darra_pns::PnSService;

fn main() -> darra_pns::ServiceResult<()> {
let device = PnSService::new();
{
let _session = device.session()?;
if device.connected() {
device.write_bool("Q0.0", true)?;
let _ = device.read_bool("I0.0")?;
}
} // Stop here; the session is still active.
Ok(())
}
  • session Returns a failure Err; it will not leave the start command half-open.
  • No mem::forget, otherwise it wouldn't be that way stop().
  • #[must_use]: If you don't bind it, it will stop immediately.

When to Use​

  • Let's take a quick detour: Use session(), and stop immediately when the scope ends.
  • Keep RUN: Directly start(), by myself stop().

II. Tuples​

A single line breaks down the link ST, runtime ST, and fault codes. It's not a new ST source; it simply bundles existing getters together.

Get Connection + Status​

Writing StyleCall
Standards APIlet c = device.connected(); let s = device.state();
Syntax Sugarlet (connected, state) = device.as_tuple();
let (connected, state) = device.as_tuple();
if !connected {
println!("{state:?}");
}

Get Connection + Status + Error Code​

let (connected, state, error) = device.as_tuple3();
if error != darra_pns::PnSRuntimeErrorCode::Ok {
println!("{}", device.error_message());
}
let _ = (connected, state);

When to Use​

  • Logging / Diagnostic Loop: Write it a few times less device.connected(), device.state().

List of Skills​

GroupingContent
RAII ConversationPnSService::session(): "Create" means "start," "Drop" means "stop," and there is no "close"
Runtime Tuplesas_tuple() → ; (connected, state)``as_tuple3() → (connected, state, error). error For PnSRuntimeErrorCode