@@ -580,6 +580,10 @@ class View(ValueCastable):
580
580
an Amaranth value instead of a constant integer. The returned element is chosen dynamically
581
581
in that case.
582
582
583
+ A view can only be compared for equality with another view of the same layout,
584
+ returning a single-bit value. No other operators are supported on views. If required,
585
+ a view can be converted back to its underlying value via :meth:`as_value`.
586
+
583
587
Custom view classes
584
588
###################
585
589
@@ -609,7 +613,7 @@ def __init__(self, layout, target):
609
613
warnings .warn ("View layout includes a field {!r} that will be shadowed by "
610
614
"the view attribute '{}.{}.{}'"
611
615
.format (name , type (self ).__module__ , type (self ).__qualname__ , name ),
612
- SyntaxWarning , stacklevel = 1 )
616
+ SyntaxWarning , stacklevel = 2 )
613
617
self .__orig_layout = layout
614
618
self .__layout = cast_layout
615
619
self .__target = cast_target
@@ -732,6 +736,49 @@ def __getattr__(self, name):
732
736
.format (self .__target , name ))
733
737
return item
734
738
739
+ def __eq__ (self , other ):
740
+ if not isinstance (other , View ) or self .__layout != other .__layout :
741
+ raise TypeError (f"View of { self .__layout !r} can only be compared to another view of the same layout, not { other !r} " )
742
+ return self .__target == other .__target
743
+
744
+ def __ne__ (self , other ):
745
+ if not isinstance (other , View ) or self .__layout != other .__layout :
746
+ raise TypeError (f"View of { self .__layout !r} can only be compared to another view of the same layout, not { other !r} " )
747
+ return self .__target != other .__target
748
+
749
+ def __add__ (self , other ):
750
+ raise TypeError ("Cannot perform arithmetic operations on a View" )
751
+
752
+ __radd__ = __add__
753
+ __sub__ = __add__
754
+ __rsub__ = __add__
755
+ __mul__ = __add__
756
+ __rmul__ = __add__
757
+ __floordiv__ = __add__
758
+ __rfloordiv__ = __add__
759
+ __mod__ = __add__
760
+ __rmod__ = __add__
761
+ __lshift__ = __add__
762
+ __rlshift__ = __add__
763
+ __rshift__ = __add__
764
+ __rrshift__ = __add__
765
+ __lt__ = __add__
766
+ __le__ = __add__
767
+ __gt__ = __add__
768
+ __ge__ = __add__
769
+
770
+ def __and__ (self , other ):
771
+ raise TypeError ("Cannot perform bitwise operations on a View" )
772
+
773
+ __rand__ = __and__
774
+ __or__ = __and__
775
+ __ror__ = __and__
776
+ __xor__ = __and__
777
+ __rxor__ = __and__
778
+
779
+ def __repr__ (self ):
780
+ return f"{ self .__class__ .__name__ } ({ self .__layout !r} , { self .__target !r} )"
781
+
735
782
736
783
class _AggregateMeta (ShapeCastable , type ):
737
784
def __new__ (metacls , name , bases , namespace ):
0 commit comments