1
1
use crate :: builtins:: PyType ;
2
+ use crate :: builtins:: PyTypeRef ;
3
+ use crate :: stdlib:: ctypes:: PyCData ;
4
+ use crate :: types:: Constructor ;
5
+ use crate :: types:: Representable ;
6
+ use crate :: { Py , PyResult , VirtualMachine } ;
2
7
3
8
#[ pyclass( name = "PyCFieldType" , base = "PyType" , module = "_ctypes" ) ]
4
- #[ derive( PyPayload ) ]
9
+ #[ derive( PyPayload , Debug ) ]
5
10
pub struct PyCFieldType {
6
11
pub ( super ) inner : PyCField ,
7
12
}
@@ -15,6 +20,7 @@ impl PyCFieldType {}
15
20
metaclass = "PyCFieldType" ,
16
21
module = "_ctypes"
17
22
) ]
23
+ #[ derive( Debug , PyPayload ) ]
18
24
pub struct PyCField {
19
25
byte_offset : usize ,
20
26
byte_size : usize ,
@@ -26,33 +32,53 @@ pub struct PyCField {
26
32
name : String ,
27
33
}
28
34
29
- impl Representable for PyCFuncPtr {
35
+ impl Representable for PyCField {
30
36
fn repr_str ( zelf : & Py < Self > , _vm : & VirtualMachine ) -> PyResult < String > {
31
- let field = zelf. inner . read ( ) ;
32
- let tp_name = field. proto . name ( ) . to_string ( ) ;
33
- if field. bitfield_size != 0 {
37
+ let tp_name = zelf. proto . name ( ) . to_string ( ) ;
38
+ if zelf. bitfield_size != false {
34
39
Ok ( format ! (
35
- "<{} {} type={}, ofs={}, bit_size={}, bit_offset={}" ,
36
- field. name, tp_name, field. byte_offset, field. bitfield_size, field. bit_offset
40
+ "<{} type={}, ofs={byte_offset}, bit_size={bitfield_size}, bit_offset={bit_offset}" ,
41
+ zelf. name,
42
+ tp_name,
43
+ byte_offset = zelf. byte_offset,
44
+ bitfield_size = zelf. bitfield_size,
45
+ bit_offset = zelf. bit_offset
37
46
) )
38
47
} else {
39
48
Ok ( format ! (
40
- "<{} {} type={}, ofs={}, size={}" ,
41
- field . name, tp_name , field . byte_offset, field . byte_size
49
+ "<{} type={tp_name }, ofs={}, size={}" ,
50
+ zelf . name, zelf . byte_offset, zelf . byte_size
42
51
) )
43
52
}
44
53
}
45
54
}
46
55
47
- #[ pyclass( flags( BASETYPE , IMMUTABLETYPE ) , with( Representable ) ) ]
56
+ #[ derive( Debug , FromArgs ) ]
57
+ struct PyCFieldConstructorArgs {
58
+ // PyObject *name, PyObject *proto,
59
+ // Py_ssize_t byte_size, Py_ssize_t byte_offset,
60
+ // Py_ssize_t index, int _internal_use,
61
+ // PyObject *bit_size_obj, PyObject *bit_offset_obj
62
+
63
+ }
64
+
65
+ impl Constructor for PyCField {
66
+ type Args = PyCFieldConstructorArgs ;
67
+
68
+ fn py_new ( _cls : PyTypeRef , _args : Self :: Args , vm : & VirtualMachine ) -> PyResult {
69
+ Err ( vm. new_type_error ( "Cannot instantiate a PyCField" . to_string ( ) ) )
70
+ }
71
+ }
72
+
73
+ #[ pyclass( flags( BASETYPE , IMMUTABLETYPE ) , with( Constructor , Representable ) ) ]
48
74
impl PyCField {
49
75
#[ pygetset]
50
76
fn size ( & self ) -> usize {
51
77
self . byte_size
52
78
}
53
79
54
80
#[ pygetset]
55
- fn bit_size ( & self ) -> u8 {
81
+ fn bit_size ( & self ) -> bool {
56
82
self . bitfield_size
57
83
}
58
84
@@ -65,4 +91,42 @@ impl PyCField {
65
91
fn is_anonymous ( & self ) -> bool {
66
92
self . anonymous
67
93
}
94
+
95
+ #[ pygetset]
96
+ fn name ( & self ) -> String {
97
+ self . name . clone ( )
98
+ }
99
+
100
+ #[ pygetset( name = "type" ) ]
101
+ fn type_ ( & self ) -> PyTypeRef {
102
+ self . proto . clone ( )
103
+ }
104
+
105
+ #[ pygetset]
106
+ fn offset ( & self ) -> usize {
107
+ self . byte_offset
108
+ }
109
+
110
+ #[ pygetset]
111
+ fn byte_offset ( & self ) -> usize {
112
+ self . byte_offset
113
+ }
114
+
115
+ #[ pygetset]
116
+ fn byte_size ( & self ) -> usize {
117
+ self . byte_size
118
+ }
119
+
120
+ #[ pygetset]
121
+ fn bit_offset ( & self ) -> u8 {
122
+ self . bit_offset
123
+ }
124
+ }
125
+
126
+ pub ( crate ) fn low_bit ( offset : usize ) -> usize {
127
+ offset & 0xFFFF
128
+ }
129
+
130
+ pub ( crate ) fn high_bit ( offset : usize ) -> usize {
131
+ offset >> 16
68
132
}
0 commit comments