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
33
34
35
36
37
38
39
40
41
42
43
44
45
46
47
48
49
50
51
52
53
54
55
use crate::function::OptionalArg;
use crate::obj::objtype::PyClassRef;
use crate::pyobject::PyValue;
use crate::pyobject::{PyContext, PyObject, PyObjectPayload, PyObjectRef, PyRef, PyResult};
use crate::vm::VirtualMachine;
use std::rc::{Rc, Weak};
#[derive(Debug)]
pub struct PyWeak {
referent: Weak<PyObject<dyn PyObjectPayload>>,
}
impl PyWeak {
pub fn downgrade(obj: &PyObjectRef) -> PyWeak {
PyWeak {
referent: Rc::downgrade(obj),
}
}
pub fn upgrade(&self) -> Option<PyObjectRef> {
self.referent.upgrade()
}
}
impl PyValue for PyWeak {
fn class(vm: &VirtualMachine) -> PyClassRef {
vm.ctx.weakref_type()
}
}
pub type PyWeakRef = PyRef<PyWeak>;
impl PyWeakRef {
fn create(
cls: PyClassRef,
referent: PyObjectRef,
_callback: OptionalArg<PyObjectRef>,
vm: &VirtualMachine,
) -> PyResult<Self> {
PyWeak::downgrade(&referent).into_ref_with_type(vm, cls)
}
fn call(self, vm: &VirtualMachine) -> PyObjectRef {
self.referent.upgrade().unwrap_or_else(|| vm.get_none())
}
}
pub fn init(context: &PyContext) {
extend_class!(context, &context.types.weakref_type, {
"__new__" => context.new_rustfunc(PyWeakRef::create),
"__call__" => context.new_rustfunc(PyWeakRef::call)
});
}