1
2
3
4
5
6
7
8
9
10
11
12
13
14
15
16
17
18
19
20
21
22
23
24
25
26
27
28
29
30
31
32
33
34
35
36
37
38
39
40
41
42
43
44
45
46
47
48
49
50
51
52
53
54
55
56
57
58
59
60
61
62
63
64
65
66
67
68
69
70
71
72
73
74
75
76
77
78
79
80
// 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()),
        }
    }
}