use super::prelude::*;
use std::cell::RefCell;
use std::rc::{Weak, Rc};
use std::hash::{Hash, Hasher};
pub struct PinInstance {
pub(super) circuit_instance_id: CircuitInstIndex,
pub(super) circuit_instance: RefCell<Weak<CircuitInstance>>,
pub(super) pin: Rc<Pin>,
pub(super) net: RefCell<Option<Rc<Net>>>,
}
impl std::fmt::Debug for PinInstance {
fn fmt(&self, f: &mut std::fmt::Formatter<'_>) -> std::fmt::Result {
let net_name = self.net.borrow().as_ref().and_then(|n| n.name());
f.debug_struct("PinInstance")
.field("circuit_instance_id", &self.circuit_instance_id)
.field("pin.name()", &self.pin.name())
.field("net.name()", &net_name)
.finish()
}
}
impl PinInstance {
pub fn circuit_instance(&self) -> Weak<CircuitInstance> {
return self.circuit_instance.borrow().clone();
}
pub fn connect_net(&self, net: Option<Rc<Net>>) -> Option<Rc<Net>> {
self.circuit_instance().upgrade()
.expect("Cannot connect a pin instance to a net if the circuit instance does not exist anymore.")
.connect_pin_by_id(self.id(), net)
}
pub fn disconnect_net(&self) -> Option<Rc<Net>> {
self.connect_net(None)
}
pub fn net(&self) -> Option<Rc<Net>> {
self.net.borrow().clone()
}
pub fn id(&self) -> usize {
self.pin.id()
}
pub fn pin(&self) -> &Rc<Pin> {
&self.pin
}
}
impl Eq for PinInstance {}
impl PartialEq for PinInstance {
fn eq(&self, other: &Self) -> bool {
self.circuit_instance_id == other.circuit_instance_id
&& self.pin.eq(&other.pin)
}
}
impl Hash for PinInstance {
fn hash<H: Hasher>(&self, state: &mut H) {
self.circuit_instance_id.hash(state);
self.pin.hash(state);
}
}