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_structUse 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 suchsugar::session(&mut device)function; the entry point isdevice.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 Style | Call |
|---|---|
| Standards API | device.start()?; … device.stop()?; |
| Syntax Sugar | let _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(())
}
sessionReturns a failureErr; it will not leave thestartcommand half-open.- No
mem::forget, otherwise it wouldn't be that waystop(). #[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 myselfstop().
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 Style | Call |
|---|---|
| Standards API | let c = device.connected(); let s = device.state(); |
| Syntax Sugar | let (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
| Grouping | Content |
|---|---|
| RAII Conversation | PnSService::session(): "Create" means "start," "Drop" means "stop," and there is no "close" |
| Runtime Tuples | as_tuple() → ; (connected, state)``as_tuple3() → (connected, state, error). error For PnSRuntimeErrorCode |