Trait Node

Source
pub trait Node: Send + 'static {
    const DEFAULT_NAME: &'static str;

    // Required methods
    fn run<'async_trait>(
        self,
        context: RuntimeContext,
    ) -> Pin<Box<dyn Future<Output = Result<()>> + Send + 'async_trait>>
       where Self: 'async_trait;
    fn get_intrinsics(&mut self) -> &mut NodeIntrinsics<Self>;
}
Expand description

A Node just represents a long running task.

Nodes are only required to run once, and may terminate at any point in time. Nodes in ROS also serve as forms of isolation. If a thread faces an exception while running code in ROS, other code in the same thread will stop executing. Developers would then segment their code into nodes such that each node could operate in a different thread.

Rust allows developers to make stronger guarantees regarding when and where their code will panic. As such, Nodes are expected to never panic. Instead, they must return an anyhow::Error when facing an unrecoverable error, or log using the error! macro if normal functionality can be continued.

In the event that a Node panics, the thread running the node will not be taken down, nor will any other node. An error message including the name of the node that panicked will be logged. Even so, panics should be avoided.

Required Associated Constants§

Source

const DEFAULT_NAME: &'static str

Required Methods§

Source

fn run<'async_trait>( self, context: RuntimeContext, ) -> Pin<Box<dyn Future<Output = Result<()>> + Send + 'async_trait>>
where Self: 'async_trait,

The entry point of the node.

Nodes are always expected to be asynchronous, as asynchronous code is much easier to manage.

If a node needs to run blocking code, it is recommended to use asyncify_run instead of rayon::spawn or std::thread::spawn, as asyncify_run allows you to await the spawned thread in a non-blocking way. If you spawn a thread and do not wait on it, you may accidentally exit this method while threads are still running. While this is not unsafe or incorrect, it can lead to misleading logs. Unros automatically logs all nodes whose run methods have returned as terminated, even if they have spawned threads that are still running.

Do keep in mind that asyncify_run threads do not terminate if not awaited or dropped, which relates back to the issue previously mentioned.

Source

fn get_intrinsics(&mut self) -> &mut NodeIntrinsics<Self>

Get a mutable reference to the NodeIntrinsics in this Node.

Implementors only need to store 1 NodeIntrinsics object.

Dyn Compatibility§

This trait is not dyn compatible.

In older versions of Rust, dyn compatibility was called "object safety", so this trait is not object safe.

Implementors§