From 34f110100ae25dc251d30b3cf0e7d554f38358bb Mon Sep 17 00:00:00 2001 From: whitequark Date: Tue, 2 Jul 2019 17:35:00 +0000 Subject: [PATCH] hdl.rec: implement Record.like. Fixes #120. --- nmigen/hdl/rec.py | 10 ++++++++++ nmigen/test/test_hdl_rec.py | 10 ++++++++++ 2 files changed, 20 insertions(+) diff --git a/nmigen/hdl/rec.py b/nmigen/hdl/rec.py index 26b26f7..2c6694c 100644 --- a/nmigen/hdl/rec.py +++ b/nmigen/hdl/rec.py @@ -69,6 +69,16 @@ class Layout: # Unlike most Values, Record *can* be subclassed. class Record(Value): + @classmethod + def like(cls, other, name=None, name_suffix=None, src_loc_at=0): + if name is not None: + new_name = str(name) + elif name_suffix is not None: + new_name = other.name + str(name_suffix) + else: + new_name = tracer.get_var_name(depth=2 + src_loc_at, default=None) + return cls(other.layout, new_name) + def __init__(self, layout, name=None, *, fields=None): if name is None: name = tracer.get_var_name(default=None) diff --git a/nmigen/test/test_hdl_rec.py b/nmigen/test/test_hdl_rec.py index fc1959f..9c72a16 100644 --- a/nmigen/test/test_hdl_rec.py +++ b/nmigen/test/test_hdl_rec.py @@ -129,6 +129,16 @@ class RecordTestCase(FHDLTestCase): self.assertIs(r.stb, ns) self.assertIs(r.info, nr) + def test_like(self): + r1 = Record([("a", 1), ("b", 2)]) + r2 = Record.like(r1) + self.assertEqual(r1.layout, r2.layout) + self.assertEqual(r2.name, "r2") + r3 = Record.like(r1, name="foo") + self.assertEqual(r3.name, "foo") + r4 = Record.like(r1, name_suffix="foo") + self.assertEqual(r4.name, "r1foo") + class ConnectTestCase(FHDLTestCase): def setUp_flat(self): -- 2.30.2