@@ -42,6 +42,12 @@ The class bool is a subclass of the class int, and cannot be subclassed.";
4242 extend_class ! ( context, bool_type, {
4343 "__new__" => context. new_rustfunc( bool_new) ,
4444 "__repr__" => context. new_rustfunc( bool_repr) ,
45+ "__or__" => context. new_rustfunc( bool_or) ,
46+ "__ror__" => context. new_rustfunc( bool_ror) ,
47+ "__and__" => context. new_rustfunc( bool_and) ,
48+ "__rand__" => context. new_rustfunc( bool_rand) ,
49+ "__xor__" => context. new_rustfunc( bool_xor) ,
50+ "__rxor__" => context. new_rustfunc( bool_rxor) ,
4551 "__doc__" => context. new_str( bool_doc. to_string( ) )
4652 } ) ;
4753}
@@ -71,6 +77,72 @@ fn bool_repr(vm: &VirtualMachine, args: PyFuncArgs) -> Result<PyObjectRef, PyObj
7177 Ok ( vm. new_str ( s) )
7278}
7379
80+ fn do_bool_or ( vm : & VirtualMachine , lhs : & PyObjectRef , rhs : & PyObjectRef ) -> PyResult {
81+ if objtype:: isinstance ( lhs, & vm. ctx . bool_type ( ) )
82+ && objtype:: isinstance ( rhs, & vm. ctx . bool_type ( ) )
83+ {
84+ let lhs = get_value ( lhs) ;
85+ let rhs = get_value ( rhs) ;
86+ ( lhs || rhs) . into_pyobject ( vm)
87+ } else {
88+ Ok ( lhs. payload :: < PyInt > ( ) . unwrap ( ) . or ( rhs. clone ( ) , vm) )
89+ }
90+ }
91+
92+ fn bool_or ( vm : & VirtualMachine , args : PyFuncArgs ) -> PyResult {
93+ arg_check ! ( vm, args, required = [ ( lhs, None ) , ( rhs, None ) ] ) ;
94+ do_bool_or ( vm, lhs, rhs)
95+ }
96+
97+ fn bool_ror ( vm : & VirtualMachine , args : PyFuncArgs ) -> PyResult {
98+ arg_check ! ( vm, args, required = [ ( rhs, None ) , ( lhs, None ) ] ) ;
99+ do_bool_or ( vm, lhs, rhs)
100+ }
101+
102+ fn do_bool_and ( vm : & VirtualMachine , lhs : & PyObjectRef , rhs : & PyObjectRef ) -> PyResult {
103+ if objtype:: isinstance ( lhs, & vm. ctx . bool_type ( ) )
104+ && objtype:: isinstance ( rhs, & vm. ctx . bool_type ( ) )
105+ {
106+ let lhs = get_value ( lhs) ;
107+ let rhs = get_value ( rhs) ;
108+ ( lhs && rhs) . into_pyobject ( vm)
109+ } else {
110+ Ok ( lhs. payload :: < PyInt > ( ) . unwrap ( ) . and ( rhs. clone ( ) , vm) )
111+ }
112+ }
113+
114+ fn bool_and ( vm : & VirtualMachine , args : PyFuncArgs ) -> PyResult {
115+ arg_check ! ( vm, args, required = [ ( lhs, None ) , ( rhs, None ) ] ) ;
116+ do_bool_and ( vm, lhs, rhs)
117+ }
118+
119+ fn bool_rand ( vm : & VirtualMachine , args : PyFuncArgs ) -> PyResult {
120+ arg_check ! ( vm, args, required = [ ( rhs, None ) , ( lhs, None ) ] ) ;
121+ do_bool_and ( vm, lhs, rhs)
122+ }
123+
124+ fn do_bool_xor ( vm : & VirtualMachine , lhs : & PyObjectRef , rhs : & PyObjectRef ) -> PyResult {
125+ if objtype:: isinstance ( lhs, & vm. ctx . bool_type ( ) )
126+ && objtype:: isinstance ( rhs, & vm. ctx . bool_type ( ) )
127+ {
128+ let lhs = get_value ( lhs) ;
129+ let rhs = get_value ( rhs) ;
130+ ( lhs ^ rhs) . into_pyobject ( vm)
131+ } else {
132+ Ok ( lhs. payload :: < PyInt > ( ) . unwrap ( ) . xor ( rhs. clone ( ) , vm) )
133+ }
134+ }
135+
136+ fn bool_xor ( vm : & VirtualMachine , args : PyFuncArgs ) -> PyResult {
137+ arg_check ! ( vm, args, required = [ ( lhs, None ) , ( rhs, None ) ] ) ;
138+ do_bool_xor ( vm, lhs, rhs)
139+ }
140+
141+ fn bool_rxor ( vm : & VirtualMachine , args : PyFuncArgs ) -> PyResult {
142+ arg_check ! ( vm, args, required = [ ( rhs, None ) , ( lhs, None ) ] ) ;
143+ do_bool_xor ( vm, lhs, rhs)
144+ }
145+
74146fn bool_new ( vm : & VirtualMachine , args : PyFuncArgs ) -> PyResult {
75147 arg_check ! (
76148 vm,
0 commit comments