forked from RustPython/RustPython
-
Notifications
You must be signed in to change notification settings - Fork 0
Expand file tree
/
Copy pathobjmemory.rs
More file actions
32 lines (27 loc) · 837 Bytes
/
objmemory.rs
File metadata and controls
32 lines (27 loc) · 837 Bytes
1
2
3
4
5
6
7
8
9
10
11
12
13
14
15
16
17
18
19
20
21
22
23
24
25
26
27
28
29
30
31
32
use crate::obj::objtype::PyClassRef;
use crate::pyobject::{PyContext, PyObjectRef, PyRef, PyResult, PyValue};
use crate::vm::VirtualMachine;
pub type PyMemoryViewRef = PyRef<PyMemoryView>;
#[derive(Debug)]
pub struct PyMemoryView {
obj: PyObjectRef,
}
impl PyValue for PyMemoryView {
fn class(vm: &VirtualMachine) -> PyClassRef {
vm.ctx.memoryview_type()
}
}
pub fn new_memory_view(
cls: PyClassRef,
bytes_object: PyObjectRef,
vm: &VirtualMachine,
) -> PyResult<PyMemoryViewRef> {
vm.set_attr(cls.as_object(), "obj", bytes_object.clone())?;
PyMemoryView { obj: bytes_object }.into_ref_with_type(vm, cls)
}
pub fn init(ctx: &PyContext) {
let memoryview_type = &ctx.memoryview_type;
extend_class!(ctx, memoryview_type, {
"__new__" => ctx.new_rustfunc(new_memory_view)
});
}