You signed in with another tab or window. Reload to refresh your session.You signed out in another tab or window. Reload to refresh your session.You switched accounts on another tab or window. Reload to refresh your session.Dismiss alert
* Changed: :meth:`Value.cast` treats instances of classes derived from both :class:`enum.Enum` and :class:`int` (including :class:`enum.IntEnum`) as enumerations rather than integers.
37
-
* Changed: ``Value.matches()`` with an empty list of patterns returns ``Const(1)`` rather than ``Const(0)``, to match ``with m.Case():``.
38
-
* Changed: :class:`Cat` accepts instances of classes derived from both :class:`enum.Enum` and :class:`int` (including :class:`enum.IntEnum`) without warning.
48
+
* Changed: :meth:`Value.matches` with an empty list of patterns returns ``Const(1)`` rather than ``Const(0)``, to match the behavior of ``with m.Case():``.
49
+
* Changed: :class:`Cat` warns if an enumeration without an explicitly specified shape is used.
39
50
* Deprecated: :meth:`Const.normalize`. (`RFC 5`_)
40
51
* Removed: (deprecated in 0.1) casting of :class:`Shape` to and from a ``(width, signed)`` tuple.
41
52
* Removed: (deprecated in 0.3) :class:`ast.UserValue`.
42
53
* Removed: (deprecated in 0.3) support for ``# nmigen:`` linter instructions at the beginning of file.
Copy file name to clipboardExpand all lines: docs/lang.rst
+20-6Lines changed: 20 additions & 6 deletions
Display the source diff
Display the rich diff
Original file line number
Diff line number
Diff line change
@@ -183,11 +183,7 @@ Specifying a shape with a range is convenient for counters, indexes, and all oth
183
183
Shapes from enumerations
184
184
------------------------
185
185
186
-
Casting a shape from an :class:`enum.Enum` subclass ``E``:
187
-
188
-
* fails if any of the enumeration members have non-integer values,
189
-
* has a width large enough to represent both ``min(m.value for m in E)`` and ``max(m.value for m in E)``, and
190
-
* is signed if either ``min(m.value for m in E)`` or ``max(m.value for m in E)`` are negative, unsigned otherwise.
186
+
Casting a shape from an :class:`enum.Enum` subclass requires all of the enumeration members to have :ref:`constant-castable <lang-constcasting>` values. The shape has a width large enough to represent the value of every member, and is signed only if there is a member with a negative value.
191
187
192
188
Specifying a shape with an enumeration is convenient for finite state machines, multiplexers, complex control signals, and all other values whose width is derived from a few distinct choices they must be able to fit:
193
189
@@ -208,9 +204,27 @@ Specifying a shape with an enumeration is convenient for finite state machines,
208
204
>>> Shape.cast(Direction)
209
205
unsigned(2)
210
206
207
+
The :mod:`amaranth.lib.enum` module extends the standard enumerations such that their shape can be specified explicitly when they are defined:
208
+
209
+
.. testsetup::
210
+
211
+
import amaranth.lib.enum
212
+
213
+
.. testcode::
214
+
215
+
class Funct4(amaranth.lib.enum.Enum, shape=unsigned(4)):
216
+
ADD = 0
217
+
SUB = 1
218
+
MUL = 2
219
+
220
+
.. doctest::
221
+
222
+
>>> Shape.cast(Funct4)
223
+
unsigned(4)
224
+
211
225
.. note::
212
226
213
-
The enumeration does not have to subclass :class:`enum.IntEnum`; it only needs to have integers as values of every member. Using enumerations based on :class:`enum.Enum` rather than :class:`enum.IntEnum` prevents unwanted implicit conversion of enum members to integers.
227
+
The enumeration does not have to subclass :class:`enum.IntEnum` or have :class:`int` as one of its base classes; it only needs to have integers as values of every member. Using enumerations based on :class:`enum.Enum` rather than :class:`enum.IntEnum` prevents unwanted implicit conversion of enum members to integers.
The :mod:`amaranth.lib.enum` module is a drop-in replacement for the standard :mod:`enum` module that provides extended :class:`Enum`, :class:`IntEnum`, :class:`Flag`, and :class:`IntFlag` classes with the ability to specify a shape explicitly.
7
+
8
+
A shape can be specified for an enumeration with the ``shape=`` keyword argument:
9
+
10
+
.. testsetup::
11
+
12
+
from amaranth import *
13
+
14
+
.. testcode::
15
+
16
+
from amaranth.lib import enum
17
+
18
+
class Funct4(enum.Enum, shape=4):
19
+
ADD = 0
20
+
SUB = 1
21
+
MUL = 2
22
+
23
+
.. doctest::
24
+
25
+
>>> Shape.cast(Funct4)
26
+
unsigned(4)
27
+
28
+
This module is a drop-in replacement for the standard :mod:`enum` module, and re-exports all of its members (not just the ones described below). In an Amaranth project, all ``import enum`` statements may be replaced with ``from amaranth.lib import enum``.
0 commit comments