File tree Expand file tree Collapse file tree 3 files changed +36
-0
lines changed Expand file tree Collapse file tree 3 files changed +36
-0
lines changed Original file line number Diff line number Diff line change @@ -313,6 +313,22 @@ def __ne__(self, other):
313
313
"the same enum type" )
314
314
return self .as_value () != other .as_value ()
315
315
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
+
316
332
def __repr__ (self ):
317
333
return f"{ type (self ).__qualname__ } ({ self .__enum .__qualname__ } , { self .__target !r} )"
318
334
Original file line number Diff line number Diff line change @@ -23,6 +23,14 @@ Version 0.6.0 (unreleased)
23
23
==========================
24
24
25
25
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
+
26
34
Language changes
27
35
----------------
28
36
@@ -45,6 +53,7 @@ Standard library changes
45
53
.. currentmodule :: amaranth.lib
46
54
47
55
* Added: :py: `payload_init= ` argument in :class: `amaranth.lib.stream.Signature `.
56
+ * Added: :meth: `enum.EnumView.matches `. (`RFC 71 `_)
48
57
* Changed: (deprecated in 0.5.1) providing :meth: `io.PortLike.__add__ ` is now mandatory. (`RFC 69 `_)
49
58
* Removed: (deprecated in 0.5.0) :mod: `amaranth.lib.coding `. (`RFC 63 `_)
50
59
Original file line number Diff line number Diff line change @@ -147,6 +147,7 @@ class EnumA(Enum, shape=signed(4)):
147
147
class EnumB (Enum , shape = signed (4 )):
148
148
C = 0
149
149
D = 5
150
+ E = 7
150
151
a = Signal (EnumA )
151
152
b = Signal (EnumB )
152
153
c = Signal (EnumA )
@@ -201,6 +202,16 @@ class EnumB(Enum, shape=signed(4)):
201
202
self .assertRepr (a == EnumA .B , "(== (sig a) (const 4'sd-3))" )
202
203
self .assertRepr (EnumA .B == a , "(== (sig a) (const 4'sd-3))" )
203
204
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 )
204
215
205
216
def test_flag_view (self ):
206
217
class FlagA (Flag , shape = unsigned (4 )):
You can’t perform that action at this time.
0 commit comments