From: whitequark Date: Tue, 2 Jul 2019 17:35:00 +0000 (+0000) Subject: hdl.rec: implement Record.like. X-Git-Url: https://git.libre-soc.org/?a=commitdiff_plain;h=3147e2dbb80222e5a4ec045fa8e5b5c207389c5b;p=nmigen.git hdl.rec: implement Record.like. Fixes #120. --- 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):