@@ -234,16 +234,16 @@ def __init__(self, enum, target):
234
234
.format (target )) from e
235
235
if cast_target .shape () != enum .as_shape ():
236
236
raise TypeError ("EnumView target must have the same shape as the enum" )
237
- self .enum = enum
238
- self .target = cast_target
237
+ self .__enum = enum
238
+ self .__target = cast_target
239
239
240
240
def shape (self ):
241
241
"""Returns the underlying enum type."""
242
- return self .enum
242
+ return self .__enum
243
243
244
244
def as_value (self ):
245
245
"""Returns the underlying value."""
246
- return self .target
246
+ return self .__target
247
247
248
248
def eq (self , other ):
249
249
"""Assign to the underlying value.
@@ -296,21 +296,25 @@ def __eq__(self, other):
296
296
:class:`Value`
297
297
The result of the equality comparison, as a single-bit value.
298
298
"""
299
- if isinstance (other , self .enum ):
300
- other = self .enum (Value .cast (other ))
301
- if not isinstance (other , EnumView ) or other .enum is not self .enum :
302
- raise TypeError ("an EnumView can only be compared to value or other EnumView of the same enum type" )
303
- return self .target == other .target
299
+ enum_cls = self .shape ()
300
+ if isinstance (other , enum_cls ):
301
+ other = enum_cls (Value .cast (other ))
302
+ if not isinstance (other , EnumView ) or other .shape () is not enum_cls :
303
+ raise TypeError ("an EnumView can only be compared to value or other EnumView of "
304
+ "the same enum type" )
305
+ return self .as_value () == other .as_value ()
304
306
305
307
def __ne__ (self , other ):
306
- if isinstance (other , self .enum ):
307
- other = self .enum (Value .cast (other ))
308
- if not isinstance (other , EnumView ) or other .enum is not self .enum :
309
- raise TypeError ("an EnumView can only be compared to value or other EnumView of the same enum type" )
310
- return self .target != other .target
308
+ enum_cls = self .shape ()
309
+ if isinstance (other , enum_cls ):
310
+ other = enum_cls (Value .cast (other ))
311
+ if not isinstance (other , EnumView ) or other .shape () is not enum_cls :
312
+ raise TypeError ("an EnumView can only be compared to value or other EnumView of "
313
+ "the same enum type" )
314
+ return self .as_value () != other .as_value ()
311
315
312
316
def __repr__ (self ):
313
- return f"{ type (self ).__qualname__ } ({ self .enum .__qualname__ } , { self .target !r} )"
317
+ return f"{ type (self ).__qualname__ } ({ self .__enum .__qualname__ } , { self .__target !r} )"
314
318
315
319
316
320
class FlagView (EnumView ):
@@ -330,21 +334,23 @@ def __invert__(self):
330
334
-------
331
335
:class:`FlagView`
332
336
"""
333
- if hasattr (self .enum , "_boundary_" ) and self .enum ._boundary_ in (EJECT , KEEP ):
334
- return self .enum ._amaranth_view_class_ (self .enum , ~ self .target )
337
+ enum_cls = self .shape ()
338
+ if hasattr (enum_cls , "_boundary_" ) and enum_cls ._boundary_ in (EJECT , KEEP ):
339
+ return enum_cls ._amaranth_view_class_ (enum_cls , ~ self .as_value ())
335
340
else :
336
341
singles_mask = 0
337
- for flag in self . enum :
342
+ for flag in enum_cls :
338
343
if (flag .value & (flag .value - 1 )) == 0 :
339
344
singles_mask |= flag .value
340
- return self . enum . _amaranth_view_class_ (self . enum , ~ self .target & singles_mask )
345
+ return enum_cls . _amaranth_view_class_ (enum_cls , ~ self .as_value () & singles_mask )
341
346
342
347
def __bitop (self , other , op ):
343
- if isinstance (other , self .enum ):
344
- other = self .enum (Value .cast (other ))
345
- if not isinstance (other , FlagView ) or other .enum is not self .enum :
348
+ enum_cls = self .shape ()
349
+ if isinstance (other , enum_cls ):
350
+ other = enum_cls (Value .cast (other ))
351
+ if not isinstance (other , FlagView ) or other .shape () is not enum_cls :
346
352
raise TypeError ("a FlagView can only perform bitwise operation with a value or other FlagView of the same enum type" )
347
- return self . enum . _amaranth_view_class_ (self . enum , op (self .target , other .target ))
353
+ return enum_cls . _amaranth_view_class_ (enum_cls , op (self .as_value () , other .as_value () ))
348
354
349
355
def __and__ (self , other ):
350
356
"""Performs a bitwise AND and returns another :class:`FlagView`.
0 commit comments