libreda-db 0.0.12

Layout and netlist datastructures for chip design.
Documentation
// Copyright (c) 2020-2022 Thomas Kramer.
// SPDX-FileCopyrightText: 2022 Thomas Kramer
//
// SPDX-License-Identifier: AGPL-3.0-or-later

//! Generalization of pins and pin instances.

use super::prelude::*;
use std::hash::{Hash, Hasher};

/// A terminal is a generalization of pins and pin instances.
pub enum TerminalId<N: NetlistIds + ?Sized> {
    /// Terminal is a pin.
    PinId(N::PinId),
    /// Terminal is a pin instance.
    PinInstId(N::PinInstId),
}

impl<N1> TerminalId<N1>
where
    N1: NetlistIds,
{
    /// Cast the ID to other netlist types.
    pub fn cast<N2>(self) -> TerminalId<N2>
    where
        N2: NetlistIds<PinId = N1::PinId, PinInstId = N1::PinInstId>,
    {
        match self {
            TerminalId::PinId(p) => TerminalId::PinId(p),
            TerminalId::PinInstId(p) => TerminalId::PinInstId(p),
        }
    }
}

impl<N: NetlistIds + ?Sized> std::fmt::Debug for TerminalId<N>
where
    N::PinId: std::fmt::Debug,
    N::PinInstId: std::fmt::Debug,
{
    fn fmt(&self, f: &mut std::fmt::Formatter<'_>) -> std::fmt::Result {
        match self {
            TerminalId::PinId(p) => write!(f, "{:?}", p),
            TerminalId::PinInstId(p) => write!(f, "{:?}", p),
        }
    }
}

impl<N: NetlistIds + ?Sized> Hash for TerminalId<N> {
    fn hash<H: Hasher>(&self, state: &mut H) {
        match self {
            TerminalId::PinId(p) => p.hash(state),
            TerminalId::PinInstId(p) => p.hash(state),
        }
    }
}

impl<N: NetlistIds + ?Sized> Eq for TerminalId<N> {}

impl<N: NetlistIds + ?Sized> PartialEq for TerminalId<N> {
    fn eq(&self, other: &Self) -> bool {
        match (self, other) {
            (Self::PinId(p1), Self::PinId(p2)) => p1 == p2,
            (Self::PinInstId(p1), Self::PinInstId(p2)) => p1 == p2,
            (_, _) => false,
        }
    }
}

impl<N: NetlistIds + ?Sized> Clone for TerminalId<N>
where
    N::PinId: Clone,
    N::PinInstId: Clone,
{
    fn clone(&self) -> Self {
        match self {
            TerminalId::PinId(p) => Self::PinId(p.clone()),
            TerminalId::PinInstId(p) => Self::PinInstId(p.clone()),
        }
    }
}