@@ -174,7 +174,6 @@ impl AsNumber for PyCStructType {
174174}
175175
176176/// Structure field info stored in instance
177- #[ allow( dead_code) ]
178177#[ derive( Debug , Clone ) ]
179178pub struct FieldInfo {
180179 pub name : String ,
@@ -195,7 +194,6 @@ pub struct PyCStructure {
195194 /// Raw memory buffer for the structure
196195 pub ( super ) buffer : PyRwLock < Vec < u8 > > ,
197196 /// Field information (name -> FieldInfo)
198- #[ allow( dead_code) ]
199197 pub ( super ) fields : PyRwLock < HashMap < String , FieldInfo > > ,
200198 /// Total size of the structure
201199 pub ( super ) size : AtomicCell < usize > ,
@@ -302,6 +300,58 @@ impl Constructor for PyCStructure {
302300// Note: GetAttr and SetAttr are not implemented here.
303301// Field access is handled by CField descriptors registered on the class.
304302
303+ impl PyCStructure {
304+ /// Convert bytes to a Python value
305+ fn bytes_to_value ( bytes : & [ u8 ] , _type_ref : & PyTypeRef , vm : & VirtualMachine ) -> PyResult {
306+ match bytes. len ( ) {
307+ 1 => Ok ( vm. ctx . new_int ( bytes[ 0 ] as i8 ) . into ( ) ) ,
308+ 2 => {
309+ let val = i16:: from_ne_bytes ( [ bytes[ 0 ] , bytes[ 1 ] ] ) ;
310+ Ok ( vm. ctx . new_int ( val) . into ( ) )
311+ }
312+ 4 => {
313+ let val = i32:: from_ne_bytes ( [ bytes[ 0 ] , bytes[ 1 ] , bytes[ 2 ] , bytes[ 3 ] ] ) ;
314+ Ok ( vm. ctx . new_int ( val) . into ( ) )
315+ }
316+ 8 => {
317+ let val = i64:: from_ne_bytes ( [
318+ bytes[ 0 ] , bytes[ 1 ] , bytes[ 2 ] , bytes[ 3 ] , bytes[ 4 ] , bytes[ 5 ] , bytes[ 6 ] , bytes[ 7 ] ,
319+ ] ) ;
320+ Ok ( vm. ctx . new_int ( val) . into ( ) )
321+ }
322+ _ => Ok ( vm. ctx . new_int ( 0 ) . into ( ) ) ,
323+ }
324+ }
325+
326+ /// Convert a Python value to bytes
327+ fn value_to_bytes ( value : & PyObjectRef , size : usize , vm : & VirtualMachine ) -> PyResult < Vec < u8 > > {
328+ if let Ok ( int_val) = value. try_int ( vm) {
329+ let i = int_val. as_bigint ( ) ;
330+ match size {
331+ 1 => {
332+ let val = i. to_i8 ( ) . unwrap_or ( 0 ) ;
333+ Ok ( val. to_ne_bytes ( ) . to_vec ( ) )
334+ }
335+ 2 => {
336+ let val = i. to_i16 ( ) . unwrap_or ( 0 ) ;
337+ Ok ( val. to_ne_bytes ( ) . to_vec ( ) )
338+ }
339+ 4 => {
340+ let val = i. to_i32 ( ) . unwrap_or ( 0 ) ;
341+ Ok ( val. to_ne_bytes ( ) . to_vec ( ) )
342+ }
343+ 8 => {
344+ let val = i. to_i64 ( ) . unwrap_or ( 0 ) ;
345+ Ok ( val. to_ne_bytes ( ) . to_vec ( ) )
346+ }
347+ _ => Ok ( vec ! [ 0u8 ; size] ) ,
348+ }
349+ } else {
350+ Ok ( vec ! [ 0u8 ; size] )
351+ }
352+ }
353+ }
354+
305355#[ pyclass( flags( BASETYPE , IMMUTABLETYPE ) , with( Constructor ) ) ]
306356impl PyCStructure {
307357 #[ pygetset]
0 commit comments