Skip to content

Commit 392ead8

Browse files
zypwhitequark
authored andcommitted
lib.data: return View from .const()
1 parent 7582ec3 commit 392ead8

File tree

2 files changed

+9
-9
lines changed

2 files changed

+9
-9
lines changed

amaranth/lib/data.py

Lines changed: 1 addition & 1 deletion
Original file line numberDiff line numberDiff line change
@@ -229,7 +229,7 @@ def const(self, init):
229229
mask = ((1 << cast_field_shape.width) - 1) << field.offset
230230
int_value &= ~mask
231231
int_value |= (key_value.value << field.offset) & mask
232-
return Const(int_value, self.as_shape())
232+
return View(self, Const(int_value, self.as_shape()))
233233

234234

235235
class StructLayout(Layout):

tests/test_lib_data.py

Lines changed: 8 additions & 8 deletions
Original file line numberDiff line numberDiff line change
@@ -382,22 +382,22 @@ def test_const(self):
382382
"a": unsigned(1),
383383
"b": unsigned(2)
384384
})
385-
self.assertRepr(sl.const(None), "(const 3'd0)")
386-
self.assertRepr(sl.const({"a": 0b1, "b": 0b10}), "(const 3'd5)")
385+
self.assertRepr(sl.const(None).as_value(), "(const 3'd0)")
386+
self.assertRepr(sl.const({"a": 0b1, "b": 0b10}).as_value(), "(const 3'd5)")
387387

388388
fl = FlexibleLayout(2, {
389389
"a": Field(unsigned(1), 0),
390390
"b": Field(unsigned(2), 0)
391391
})
392-
self.assertRepr(fl.const({"a": 0b11}), "(const 2'd1)")
393-
self.assertRepr(fl.const({"b": 0b10}), "(const 2'd2)")
394-
self.assertRepr(fl.const({"a": 0b1, "b": 0b10}), "(const 2'd2)")
392+
self.assertRepr(fl.const({"a": 0b11}).as_value(), "(const 2'd1)")
393+
self.assertRepr(fl.const({"b": 0b10}).as_value(), "(const 2'd2)")
394+
self.assertRepr(fl.const({"a": 0b1, "b": 0b10}).as_value(), "(const 2'd2)")
395395

396396
sls = StructLayout({
397397
"a": signed(4),
398398
"b": signed(4)
399399
})
400-
self.assertRepr(sls.const({"b": 0, "a": -1}), "(const 8'd15)")
400+
self.assertRepr(sls.const({"b": 0, "a": -1}).as_value(), "(const 8'd15)")
401401

402402
def test_const_wrong(self):
403403
sl = StructLayout({"f": unsigned(1)})
@@ -418,7 +418,7 @@ def const(self, init):
418418
return int(init, 16)
419419

420420
sl = StructLayout({"f": CastableFromHex()})
421-
self.assertRepr(sl.const({"f": "aa"}), "(const 8'd170)")
421+
self.assertRepr(sl.const({"f": "aa"}).as_value(), "(const 8'd170)")
422422

423423
with self.assertRaisesRegex(ValueError,
424424
r"^Constant returned by <.+?CastableFromHex.+?>\.const\(\) must have the shape "
@@ -427,7 +427,7 @@ def const(self, init):
427427

428428
def test_const_field_const(self):
429429
sl = StructLayout({"f": unsigned(1)})
430-
self.assertRepr(sl.const({"f": Const(1)}), "(const 1'd1)")
430+
self.assertRepr(sl.const({"f": Const(1)}).as_value(), "(const 1'd1)")
431431

432432
def test_signal_reset(self):
433433
sl = StructLayout({

0 commit comments

Comments
 (0)