@@ -312,7 +312,14 @@ def skip(self, decoder: BinaryDecoder) -> None:
312312
313313
314314class StructReader (Reader ):
315- __slots__ = ("field_readers" , "create_struct" , "struct" , "_create_with_keyword" , "_field_reader_functions" , "_hash" )
315+ __slots__ = (
316+ "field_readers" ,
317+ "create_struct" ,
318+ "struct" ,
319+ "_field_reader_functions" ,
320+ "_hash" ,
321+ "_max_pos" ,
322+ )
316323 field_readers : Tuple [Tuple [Optional [int ], Reader ], ...]
317324 create_struct : Callable [..., StructProtocol ]
318325 struct : StructType
@@ -326,34 +333,28 @@ def __init__(
326333 ) -> None :
327334 self .field_readers = field_readers
328335 self .create_struct = create_struct
336+ # TODO: Implement struct-reuse
329337 self .struct = struct
330338
331- try :
332- # Try initializing the struct, first with the struct keyword argument
333- created_struct = self .create_struct (struct = self .struct )
334- self ._create_with_keyword = True
335- except TypeError as e :
336- if "'struct' is an invalid keyword argument for" in str (e ):
337- created_struct = self .create_struct ()
338- self ._create_with_keyword = False
339- else :
340- raise ValueError (f"Unable to initialize struct: { self .create_struct } " ) from e
341-
342- if not isinstance (created_struct , StructProtocol ):
339+ if not isinstance (self .create_struct (), StructProtocol ):
343340 raise ValueError (f"Incompatible with StructProtocol: { self .create_struct } " )
344341
345342 reading_callbacks : List [Tuple [Optional [int ], Callable [[BinaryDecoder ], Any ]]] = []
343+ max_pos = - 1
346344 for pos , field in field_readers :
347345 if pos is not None :
348346 reading_callbacks .append ((pos , field .read ))
347+ max_pos = max (max_pos , pos )
349348 else :
350349 reading_callbacks .append ((None , field .skip ))
351350
352351 self ._field_reader_functions = tuple (reading_callbacks )
353352 self ._hash = hash (self ._field_reader_functions )
353+ self ._max_pos = 1 + max_pos
354354
355355 def read (self , decoder : BinaryDecoder ) -> StructProtocol :
356- struct = self .create_struct (struct = self .struct ) if self ._create_with_keyword else self .create_struct ()
356+ # TODO: Implement struct-reuse
357+ struct = self .create_struct (* [None ] * self ._max_pos )
357358 for pos , field_reader in self ._field_reader_functions :
358359 if pos is not None :
359360 struct [pos ] = field_reader (decoder ) # later: pass reuse in here
0 commit comments