iowo/crates/ir/src/id.rs

64 lines
2.2 KiB
Rust
Raw Normal View History

//! Instance identification for instructions and their glue.
//!
//! Instructions as defined in [`crate::instruction::Kind`] and descendants are very useful,
//! but they cannot be directly used as vertices in the graph IR,
//! as there may easily be multiple instructions of the same kind in the same program.
//!
//! Instead, this module offers an alternative way to refer to specific instances:
//!
//! - [`Instruction`]s are effectively just a number floating in space,
//! incremented each time a new instruction is referred to.
//! - [`Socket`]s contain
//! - what [`Instruction`] they belong to
//! - which index they occupy on it
//!
//! The distinction between [`Input`] and [`Output`] is implemented
//! as them being different types.
use serde::{Deserialize, Serialize};
/// One specific instruction.
///
/// It does **not** contain what kind of instruction this is.
/// Refer to [`crate::instruction::Kind`] for this instead.
#[derive(Clone, Debug, PartialEq, Eq, PartialOrd, Ord, Hash, Serialize, Deserialize)]
pub struct Instruction(pub(super) u64);
/// On an **instruction**, accepts incoming data.
///
/// An **instruction** cannot run if any of these are not connected.
#[derive(Clone, Debug, PartialEq, Eq, PartialOrd, Ord, Hash, Serialize, Deserialize)]
pub struct Input(pub(super) Socket);
impl Input {
#[must_use]
pub fn socket(&self) -> &Socket {
&self.0
}
}
/// On an **instruction**, returns outgoing data to be fed to [`Input`]s.
///
/// In contrast to [`Input`]s, [`Output`]s may be used or unused.
#[derive(Clone, Debug, PartialEq, Eq, PartialOrd, Ord, Hash, Serialize, Deserialize)]
pub struct Output(pub(super) Socket);
impl Output {
#[must_use]
pub fn socket(&self) -> &Socket {
&self.0
}
}
/// An unspecified socket on a specific **instruction**,
/// and where it is on that **instruction**.
#[derive(Clone, Debug, PartialEq, Eq, PartialOrd, Ord, Hash, Serialize, Deserialize)]
pub struct Socket {
pub belongs_to: Instruction,
pub idx: SocketIdx,
}
/// Where a [`Socket`] is on an **instruction**.
#[derive(Clone, Debug, PartialEq, Eq, PartialOrd, Ord, Hash, Serialize, Deserialize)]
pub struct SocketIdx(pub u16);