substrate/block/
mod.rs

1//! A block that can be instantiated by Substrate.
2
3use std::any::Any;
4use std::hash::Hash;
5use std::sync::Arc;
6
7pub use codegen::Block;
8
9use crate::arcstr::ArcStr;
10use crate::types::Io;
11
12#[cfg(test)]
13mod tests;
14
15/// A block that can be instantiated by Substrate.
16///
17/// # Examples
18///
19#[doc = snippets::include_snippet!("substrate", "inverter")]
20pub trait Block: Hash + Eq + Send + Sync + Any {
21    /// The ports of this block.
22    type Io: Io;
23
24    /// A name for a specific parametrization of this block.
25    ///
26    /// Instances of this block will initially be assigned this name,
27    /// although Substrate may need to change the name
28    /// (e.g. to avoid duplicates).
29    fn name(&self) -> ArcStr {
30        arcstr::literal!("unnamed")
31    }
32
33    /// Returns a fully-specified instance of this cell's `Io`.
34    fn io(&self) -> Self::Io;
35}
36
37impl<T: Block> Block for Arc<T> {
38    type Io = T::Io;
39
40    fn name(&self) -> ArcStr {
41        T::name(self)
42    }
43
44    fn io(&self) -> Self::Io {
45        T::io(self)
46    }
47}