@@ -2,7 +2,8 @@ use super::{PyStrRef, PyType, PyTypeRef, PyWeak};
2
2
use crate :: {
3
3
class:: PyClassImpl ,
4
4
function:: OptionalArg ,
5
- types:: { Constructor , GetAttr , SetAttr } ,
5
+ protocol:: { PySequence , PySequenceMethods } ,
6
+ types:: { AsSequence , Constructor , GetAttr , SetAttr } ,
6
7
Context , Py , PyObjectRef , PyPayload , PyRef , PyResult , VirtualMachine ,
7
8
} ;
8
9
@@ -57,7 +58,7 @@ crate::common::static_cell! {
57
58
static WEAK_SUBCLASS : PyTypeRef ;
58
59
}
59
60
60
- #[ pyimpl( with( GetAttr , SetAttr , Constructor ) ) ]
61
+ #[ pyimpl( with( GetAttr , SetAttr , Constructor , AsSequence ) ) ]
61
62
impl PyWeakProxy {
62
63
#[ pymethod( magic) ]
63
64
fn str ( & self , vm : & VirtualMachine ) -> PyResult < PyStrRef > {
@@ -66,6 +67,20 @@ impl PyWeakProxy {
66
67
None => Err ( new_reference_error ( vm) ) ,
67
68
}
68
69
}
70
+
71
+ fn len ( & self , vm : & VirtualMachine ) -> PyResult < usize > {
72
+ match self . weak . upgrade ( ) {
73
+ Some ( obj) => obj. length ( vm) ,
74
+ None => Err ( new_reference_error ( vm) ) ,
75
+ }
76
+ }
77
+
78
+ fn contains ( & self , needle : PyObjectRef , vm : & VirtualMachine ) -> PyResult < bool > {
79
+ match self . weak . upgrade ( ) {
80
+ Some ( obj) => PySequence :: contains ( & obj, & needle, vm) ,
81
+ None => Err ( new_reference_error ( vm) ) ,
82
+ }
83
+ }
69
84
}
70
85
71
86
fn new_reference_error ( vm : & VirtualMachine ) -> PyRef < super :: PyBaseException > {
@@ -100,6 +115,16 @@ impl SetAttr for PyWeakProxy {
100
115
}
101
116
}
102
117
118
+ impl AsSequence for PyWeakProxy {
119
+ const AS_SEQUENCE : PySequenceMethods = PySequenceMethods {
120
+ length : Some ( |seq, vm| Self :: sequence_downcast ( seq) . len ( vm) ) ,
121
+ contains : Some ( |seq, needle, vm| {
122
+ Self :: sequence_downcast ( seq) . contains ( needle. to_owned ( ) , vm)
123
+ } ) ,
124
+ ..PySequenceMethods :: NOT_IMPLEMENTED
125
+ } ;
126
+ }
127
+
103
128
pub fn init ( context : & Context ) {
104
129
PyWeakProxy :: extend_class ( context, context. types . weakproxy_type ) ;
105
130
}
0 commit comments