1
1
use crate :: obj:: objbyteinner:: try_as_byte;
2
2
use crate :: obj:: objtype:: PyClassRef ;
3
- use crate :: pyobject:: { PyContext , PyObjectRef , PyRef , PyResult , PyValue } ;
3
+ use crate :: pyobject:: { PyClassImpl , PyContext , PyObjectRef , PyRef , PyResult , PyValue } ;
4
4
use crate :: vm:: VirtualMachine ;
5
5
6
- pub type PyMemoryViewRef = PyRef < PyMemoryView > ;
7
-
6
+ #[ pyclass( name = "memoryview" ) ]
8
7
#[ derive( Debug ) ]
9
8
pub struct PyMemoryView {
10
- obj : PyObjectRef ,
9
+ obj_ref : PyObjectRef ,
11
10
}
12
11
12
+ pub type PyMemoryViewRef = PyRef < PyMemoryView > ;
13
+
14
+ #[ pyimpl]
13
15
impl PyMemoryView {
14
16
pub fn get_obj_value ( & self ) -> Option < Vec < u8 > > {
15
- try_as_byte ( & self . obj )
17
+ try_as_byte ( & self . obj_ref )
18
+ }
19
+
20
+ #[ pymethod( name = "__new__" ) ]
21
+ fn new (
22
+ cls : PyClassRef ,
23
+ bytes_object : PyObjectRef ,
24
+ vm : & VirtualMachine ,
25
+ ) -> PyResult < PyMemoryViewRef > {
26
+ PyMemoryView {
27
+ obj_ref : bytes_object. clone ( ) ,
28
+ }
29
+ . into_ref_with_type ( vm, cls)
30
+ }
31
+
32
+ #[ pyproperty]
33
+ fn obj ( & self , __vm : & VirtualMachine ) -> PyObjectRef {
34
+ self . obj_ref . clone ( )
16
35
}
17
36
}
18
37
@@ -22,18 +41,6 @@ impl PyValue for PyMemoryView {
22
41
}
23
42
}
24
43
25
- pub fn new_memory_view (
26
- cls : PyClassRef ,
27
- bytes_object : PyObjectRef ,
28
- vm : & VirtualMachine ,
29
- ) -> PyResult < PyMemoryViewRef > {
30
- vm. set_attr ( cls. as_object ( ) , "obj" , bytes_object. clone ( ) ) ?;
31
- PyMemoryView { obj : bytes_object } . into_ref_with_type ( vm, cls)
32
- }
33
-
34
44
pub fn init ( ctx : & PyContext ) {
35
- let memoryview_type = & ctx. memoryview_type ;
36
- extend_class ! ( ctx, memoryview_type, {
37
- "__new__" => ctx. new_rustfunc( new_memory_view)
38
- } ) ;
45
+ PyMemoryView :: extend_class ( ctx, & ctx. memoryview_type )
39
46
}
0 commit comments