File tree Expand file tree Collapse file tree 2 files changed +20
-0
lines changed Expand file tree Collapse file tree 2 files changed +20
-0
lines changed Original file line number Diff line number Diff line change @@ -65,6 +65,9 @@ def width(self):
65
65
"""
66
66
return Shape .cast (self .shape ).width
67
67
68
+ def __hash__ (self ):
69
+ return hash ((self .shape , self .offset ))
70
+
68
71
def __eq__ (self , other ):
69
72
"""Compare fields.
70
73
@@ -166,6 +169,11 @@ def as_shape(self):
166
169
"""
167
170
return unsigned (self .size )
168
171
172
+ def __hash__ (self ):
173
+ if not hasattr (self , "_Layout__hash" ):
174
+ setattr (self , "_Layout__hash" , hash ((self .size , frozenset (iter (self )))))
175
+ return self .__hash
176
+
169
177
def __eq__ (self , other ):
170
178
"""Compare layouts.
171
179
@@ -1123,6 +1131,9 @@ def __len__(self):
1123
1131
f"`len()` can only be used on constants of array layout, not { self .__layout !r} " )
1124
1132
return self .__layout .length
1125
1133
1134
+ def __hash__ (self ):
1135
+ return hash ((self .__target , self .__layout ))
1136
+
1126
1137
def __eq__ (self , other ):
1127
1138
if isinstance (other , View ) and self .__layout == other ._View__layout :
1128
1139
return self .as_value () == other ._View__target
Original file line number Diff line number Diff line change @@ -74,6 +74,9 @@ def test_immutable(self):
74
74
with self .assertRaises (AttributeError ):
75
75
data .Field (1 , 0 ).offset = 1
76
76
77
+ def test_hash (self ):
78
+ hash (data .Field (unsigned (2 ), 1 ))
79
+
77
80
78
81
class StructLayoutTestCase (FHDLTestCase ):
79
82
def test_construct (self ):
@@ -520,6 +523,9 @@ def test_signal_init(self):
520
523
self .assertEqual (Signal (sl ).as_value ().init , 0 )
521
524
self .assertEqual (Signal (sl , init = {"a" : 0b1 , "b" : 0b10 }).as_value ().init , 5 )
522
525
526
+ def test_hash (self ):
527
+ hash (data .StructLayout ({}))
528
+
523
529
524
530
class ViewTestCase (FHDLTestCase ):
525
531
def test_construct (self ):
@@ -1167,6 +1173,9 @@ def test_repr(self):
1167
1173
s1 = data .Const (data .StructLayout ({"a" : unsigned (2 )}), 2 )
1168
1174
self .assertRepr (s1 , "Const(StructLayout({'a': unsigned(2)}), 2)" )
1169
1175
1176
+ def test_hash (self ):
1177
+ hash (data .Const (data .StructLayout ({"a" : unsigned (2 )}), 2 ))
1178
+
1170
1179
1171
1180
class StructTestCase (FHDLTestCase ):
1172
1181
def test_construct (self ):
You can’t perform that action at this time.
0 commit comments