Skip to content

Commit ce39ca7

Browse files
authored
Implement Debug for NetRef (#40)
1 parent 23debd2 commit ce39ca7

File tree

4 files changed

+33
-3
lines changed

4 files changed

+33
-3
lines changed

Cargo.toml

Lines changed: 1 addition & 1 deletion
Original file line numberDiff line numberDiff line change
@@ -1,6 +1,6 @@
11
[package]
22
name = "safety-net"
3-
version = "0.1.6"
3+
version = "0.1.7"
44
edition = "2024"
55
license = "MIT OR Apache-2.0"
66

README.md

Lines changed: 1 addition & 1 deletion
Original file line numberDiff line numberDiff line change
@@ -6,7 +6,7 @@
66

77
## Description
88

9-
A Rust library for compiling and mutating Safety Net netlists in a memory-safe way
9+
A Rust library for compiling and mutating netlists in a memory-safe way
1010

1111
You can read the docs [here](https://matth2k.github.io/safety-net/).
1212

src/netlist.rs

Lines changed: 20 additions & 1 deletion
Original file line numberDiff line numberDiff line change
@@ -432,14 +432,33 @@ type NetRefT<I> = Rc<RefCell<OwnedObject<I, Netlist<I>>>>;
432432

433433
/// Provides an idiomatic interface
434434
/// to the interior mutability of the netlist
435-
#[derive(Debug, Clone)]
435+
#[derive(Clone)]
436436
pub struct NetRef<I>
437437
where
438438
I: Instantiable,
439439
{
440440
netref: NetRefT<I>,
441441
}
442442

443+
impl<I> std::fmt::Debug for NetRef<I>
444+
where
445+
I: Instantiable,
446+
{
447+
fn fmt(&self, f: &mut std::fmt::Formatter<'_>) -> std::fmt::Result {
448+
let b = self.netref.borrow();
449+
let o = b.get();
450+
let i = b.index;
451+
let owner = &b.owner;
452+
match owner.upgrade() {
453+
Some(owner) => {
454+
let n = owner.get_name();
455+
write!(f, "{{ owner: \"{n}\", index: {i}, val: \"{o}\" }}")
456+
}
457+
None => write!(f, "{{ owner: None, index: {i}, val: \"{o}\" }}"),
458+
}
459+
}
460+
}
461+
443462
impl<I> PartialEq for NetRef<I>
444463
where
445464
I: Instantiable,

tests/api.rs

Lines changed: 11 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -62,6 +62,17 @@ fn get_simple_example() -> Rc<GateNetlist> {
6262
netlist
6363
}
6464

65+
#[test]
66+
fn test_netref_printing() {
67+
let netlist = get_simple_example();
68+
let gate = netlist.last().unwrap();
69+
let output = format!("{gate:?}");
70+
assert_eq!(
71+
output,
72+
"{ owner: \"example\", index: 2, val: \"AND(inst_0)\" }"
73+
);
74+
}
75+
6576
#[test]
6677
fn test_io() {
6778
let netlist = get_simple_example();

0 commit comments

Comments
 (0)