iridis_runtime_core/node.rs
1//! This module defines the `RuntimeNode` enum, which can represent either a statically linked or dynamically linked node.
2//! It's separated from other modules because it's fundamentally a brick of the runtime.
3
4use crate::prelude::{thirdparty::libloading, *};
5
6/// This struct represents a dynamically linked node.
7/// It loads the node from a shared library at runtime, storing the handle as a `Box<dyn Node>`.
8/// It's really important to store the library as well, because once the library is dropped the handle will be invalid.
9pub struct DynamicallyLinkedNode {
10    /// The `Node` object the runtime will use
11    pub handle: Box<dyn Node>,
12
13    #[cfg(not(target_family = "unix"))]
14    pub _library: libloading::Library,
15    #[cfg(target_family = "unix")]
16    pub _library: libloading::os::unix::Library,
17}
18
19/// This is the main enum of this module. It represents a node that can be either statically linked or dynamically linked,
20/// allowing the runtime to use either type of node interchangeably.
21pub enum RuntimeNode {
22    /// A statically linked node, which is a concrete implementation of the `Node` trait.
23    StaticallyLinked(Box<dyn Node>),
24
25    /// A dynamically linked node, which is loaded from a shared library at runtime.
26    DynamicallyLinked(DynamicallyLinkedNode),
27}
28
29impl RuntimeNode {
30    /// This function will either start the statically linked node or the dynamically linked node,
31    /// awaiting for the result.
32    pub async fn run(self) -> Result<()> {
33        match self {
34            RuntimeNode::StaticallyLinked(node) => node.start().await?,
35            RuntimeNode::DynamicallyLinked(node) => node.handle.start().await?,
36        }
37    }
38}