Skip to content

Commit ba18605

Browse files
wanda-phiwhitequark
authored andcommitted
Implement RFC 71: EnumView.matches.
1 parent dd1d8d5 commit ba18605

File tree

3 files changed

+36
-0
lines changed

3 files changed

+36
-0
lines changed

amaranth/lib/enum.py

Lines changed: 16 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -313,6 +313,22 @@ def __ne__(self, other):
313313
"the same enum type")
314314
return self.as_value() != other.as_value()
315315

316+
def matches(self, *patterns):
317+
"""Pattern matching.
318+
319+
Matches against a set of patterns, which must be values of the same enum as the view.
320+
321+
Returns
322+
-------
323+
:class:`Value`, :py:`unsigned(1)`
324+
"""
325+
patterns = tuple(patterns)
326+
for pattern in patterns:
327+
if not isinstance(pattern, self.__enum):
328+
raise TypeError(f"a pattern must be an enum value of the same type as the "
329+
f"EnumView ({self.__enum.__qualname__}), not {pattern!r}")
330+
return self.__target.matches(*patterns)
331+
316332
def __repr__(self):
317333
return f"{type(self).__qualname__}({self.__enum.__qualname__}, {self.__target!r})"
318334

docs/changes.rst

Lines changed: 9 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -23,6 +23,14 @@ Version 0.6.0 (unreleased)
2323
==========================
2424

2525

26+
Implemented RFCs
27+
----------------
28+
29+
.. _RFC 71: https://amaranth-lang.org/rfcs/0071-enumview-matches.html
30+
31+
* `RFC 71`_: ``EnumView.matches``
32+
33+
2634
Language changes
2735
----------------
2836

@@ -45,6 +53,7 @@ Standard library changes
4553
.. currentmodule:: amaranth.lib
4654

4755
* Added: :py:`payload_init=` argument in :class:`amaranth.lib.stream.Signature`.
56+
* Added: :meth:`enum.EnumView.matches`. (`RFC 71`_)
4857
* Changed: (deprecated in 0.5.1) providing :meth:`io.PortLike.__add__` is now mandatory. (`RFC 69`_)
4958
* Removed: (deprecated in 0.5.0) :mod:`amaranth.lib.coding`. (`RFC 63`_)
5059

tests/test_lib_enum.py

Lines changed: 11 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -147,6 +147,7 @@ class EnumA(Enum, shape=signed(4)):
147147
class EnumB(Enum, shape=signed(4)):
148148
C = 0
149149
D = 5
150+
E = 7
150151
a = Signal(EnumA)
151152
b = Signal(EnumB)
152153
c = Signal(EnumA)
@@ -201,6 +202,16 @@ class EnumB(Enum, shape=signed(4)):
201202
self.assertRepr(a == EnumA.B, "(== (sig a) (const 4'sd-3))")
202203
self.assertRepr(EnumA.B == a, "(== (sig a) (const 4'sd-3))")
203204
self.assertRepr(a != EnumA.B, "(!= (sig a) (const 4'sd-3))")
205+
self.assertRepr(b.matches(EnumB.C, EnumB.D), """
206+
(r| (cat
207+
(== (sig b) (const 1'd0))
208+
(== (sig b) (const 3'd5))
209+
))
210+
""")
211+
with self.assertRaisesRegex(TypeError,
212+
r"^a pattern must be an enum value of the same type as the EnumView \(.*EnumB\), "
213+
r"not .*$"):
214+
b.matches(EnumA.A)
204215

205216
def test_flag_view(self):
206217
class FlagA(Flag, shape=unsigned(4)):

0 commit comments

Comments
 (0)