fix PyNumberProtocol methods #48

This commit is contained in:
Nikolay Kim 2017-07-17 16:49:19 -07:00
parent 149aa3a0eb
commit 2d06b07717
6 changed files with 302 additions and 101 deletions

View File

@ -329,75 +329,89 @@ pub const SEQ: Proto = Proto {
pub const NUM: Proto = Proto {
name: "Number",
methods: &[
MethodProto::Binary {
MethodProto::BinaryS {
name: "__add__",
arg: "Other",
arg1: "Left",
arg2: "Right",
pyres: true,
proto: "::pyo3::class::number::PyNumberAddProtocol"},
MethodProto::Binary {
MethodProto::BinaryS {
name: "__sub__",
arg: "Other",
arg1: "Left",
arg2: "Right",
pyres: true,
proto: "::pyo3::class::number::PyNumberSubProtocol"},
MethodProto::Binary {
MethodProto::BinaryS {
name: "__mul__",
arg: "Other",
arg1: "Left",
arg2: "Right",
pyres: true,
proto: "::pyo3::class::number::PyNumberMulProtocol"},
MethodProto::Binary {
MethodProto::BinaryS {
name: "__matmul__",
arg: "Other",
arg1: "Left",
arg2: "Right",
pyres: true,
proto: "::pyo3::class::number::PyNumberMatmulProtocol"},
MethodProto::Binary {
MethodProto::BinaryS {
name: "__truediv__",
arg: "Other",
arg1: "Left",
arg2: "Right",
pyres: true,
proto: "::pyo3::class::number::PyNumberTruedivProtocol"},
MethodProto::Binary {
MethodProto::BinaryS {
name: "__floordiv__",
arg: "Other",
arg1: "Left",
arg2: "Right",
pyres: true,
proto: "::pyo3::class::number::PyNumberFloordivProtocol"},
MethodProto::Binary {
MethodProto::BinaryS {
name: "__mod__",
arg: "Other",
arg1: "Left",
arg2: "Right",
pyres: true,
proto: "::pyo3::class::number::PyNumberModProtocol"},
MethodProto::Binary {
MethodProto::BinaryS {
name: "__divmod__",
arg: "Other",
arg1: "Left",
arg2: "Right",
pyres: true,
proto: "::pyo3::class::number::PyNumberDivmodProtocol"},
MethodProto::Ternary {
MethodProto::TernaryS {
name: "__pow__",
arg1: "Other",
arg2: "Modulo",
arg1: "Left",
arg2: "Right",
arg3: "Modulo",
pyres: true,
proto: "::pyo3::class::number::PyNumberPowProtocol"},
MethodProto::Binary {
MethodProto::BinaryS {
name: "__lshift__",
arg: "Other",
arg1: "Left",
arg2: "Right",
pyres: true,
proto: "::pyo3::class::number::PyNumberLShiftProtocol"},
MethodProto::Binary {
MethodProto::BinaryS {
name: "__rshift__",
arg: "Other",
arg1: "Left",
arg2: "Right",
pyres: true,
proto: "::pyo3::class::number::PyNumberRShiftProtocol"},
MethodProto::Binary {
MethodProto::BinaryS {
name: "__and__",
arg: "Other",
arg1: "Left",
arg2: "Right",
pyres: true,
proto: "::pyo3::class::number::PyNumberAndProtocol"},
MethodProto::Binary {
MethodProto::BinaryS {
name: "__xor__",
arg: "Other",
arg1: "Left",
arg2: "Right",
pyres: true,
proto: "::pyo3::class::number::PyNumberXorProtocol"},
MethodProto::Binary {
MethodProto::BinaryS {
name: "__or__",
arg: "Other",
arg1: "Left",
arg2: "Right",
pyres: true,
proto: "::pyo3::class::number::PyNumberOrProtocol"},

View File

@ -11,10 +11,17 @@ pub enum MethodProto {
Free{name: &'static str, proto: &'static str, },
Unary{name: &'static str, pyres: bool, proto: &'static str, },
Binary{name: &'static str, arg: &'static str, pyres: bool, proto: &'static str},
BinaryS{name: &'static str,
arg1: &'static str, arg2: &'static str, pyres: bool, proto: &'static str},
Ternary{name: &'static str,
arg1: &'static str,
arg2: &'static str,
pyres: bool, proto: &'static str},
TernaryS{name: &'static str,
arg1: &'static str,
arg2: &'static str,
arg3: &'static str,
pyres: bool, proto: &'static str},
Quaternary{name: &'static str,
arg1: &'static str,
arg2: &'static str,
@ -28,7 +35,10 @@ impl MethodProto {
MethodProto::Free{name: n, proto: _} => n == name,
MethodProto::Unary{name: n, pyres: _, proto: _} => n == name,
MethodProto::Binary{name: n, arg: _, pyres: _, proto: _} => n == name,
MethodProto::BinaryS{name: n, arg1: _, arg2: _, pyres: _, proto: _} => n == name,
MethodProto::Ternary{name: n, arg1: _, arg2: _, pyres: _, proto: _} => n == name,
MethodProto::TernaryS{name: n, arg1: _, arg2: _, arg3: _,
pyres: _, proto: _} => n == name,
MethodProto::Quaternary{name: n, arg1: _, arg2: _, arg3: _, proto: _} => n == name,
}
}
@ -119,6 +129,51 @@ pub fn impl_method_proto(cls: &Box<syn::Ty>,
}
}
},
MethodProto::BinaryS{name: n, arg1, arg2, pyres, proto} => {
if sig.decl.inputs.len() <= 1 {
print_err(format!("Not enough arguments {}", n), quote!(sig));
return Tokens::new();
}
let p = syn::Ident::from(proto);
let arg1_name = syn::Ident::from(arg1);
let arg1_ty = get_arg_ty(sig, 0);
let arg2_name = syn::Ident::from(arg2);
let arg2_ty = get_arg_ty(sig, 1);
let (ty, succ) = get_res_success(ty);
// rewrite ty
let tmp = extract_decl(syn::parse_item(
quote! {fn test(
arg1: <#cls as #p<'p>>::#arg1_name,
arg2: <#cls as #p<'p>>::#arg2_name)
-> <#cls as #p<'p>>::Result {}}.as_str()).unwrap());
let tmp2 = extract_decl(syn::parse_item(
quote! {fn test(
arg1: Option<<#cls as #p<'p>>::#arg1_name>,
arg2: Option<<#cls as #p<'p>>::#arg2_name>)
-> <#cls as #p<'p>>::Result {}}.as_str()).unwrap());
modify_arg_ty(sig, 0, &tmp, &tmp2);
modify_arg_ty(sig, 1, &tmp, &tmp2);
if pyres {
quote! {
impl<'p> #p<'p> for #cls {
type #arg1_name = #arg1_ty;
type #arg2_name = #arg2_ty;
type Success = #succ;
type Result = #ty;
}
}
} else {
quote! {
impl<'p> #p<'p> for #cls {
type #arg1_name = #arg1_ty;
type #arg2_name = #arg2_ty;
type Result = #ty;
}
}
}
},
MethodProto::Ternary{name: n, arg1, arg2, pyres, proto} => {
if sig.decl.inputs.len() <= 2 {
print_err(format!("Not enough arguments {}", n), quote!(sig));
@ -167,6 +222,58 @@ pub fn impl_method_proto(cls: &Box<syn::Ty>,
}
}
},
MethodProto::TernaryS{name: n, arg1, arg2, arg3, pyres, proto} => {
if sig.decl.inputs.len() <= 2 {
print_err(format!("Not enough arguments {}", n), quote!(sig));
return Tokens::new();
}
let p = syn::Ident::from(proto);
let arg1_name = syn::Ident::from(arg1);
let arg1_ty = get_arg_ty(sig, 0);
let arg2_name = syn::Ident::from(arg2);
let arg2_ty = get_arg_ty(sig, 1);
let arg3_name = syn::Ident::from(arg3);
let arg3_ty = get_arg_ty(sig, 2);
let (ty, succ) = get_res_success(ty);
// rewrite ty
let tmp = extract_decl(syn::parse_item(
quote! {fn test(
arg1: <#cls as #p<'p>>::#arg1_name,
arg2: <#cls as #p<'p>>::#arg2_name,
arg3: <#cls as #p<'p>>::#arg3_name)
-> <#cls as #p<'p>>::Result {}}.as_str()).unwrap());
let tmp2 = extract_decl(syn::parse_item(
quote! {fn test(
arg1: Option<<#cls as #p<'p>>::#arg1_name>,
arg2: Option<<#cls as #p<'p>>::#arg2_name>,
arg3: Option<<#cls as #p<'p>>::#arg3_name>)
-> <#cls as #p<'p>>::Result {}}.as_str()).unwrap());
modify_arg_ty(sig, 0, &tmp, &tmp2);
modify_arg_ty(sig, 1, &tmp, &tmp2);
modify_arg_ty(sig, 2, &tmp, &tmp2);
if pyres {
quote! {
impl<'p> #p<'p> for #cls {
type #arg1_name = #arg1_ty;
type #arg2_name = #arg2_ty;
type #arg3_name = #arg3_ty;
type Success = #succ;
type Result = #ty;
}
}
} else {
quote! {
impl<'p> #p<'p> for #cls {
type #arg1_name = #arg1_ty;
type #arg2_name = #arg2_ty;
type #arg3_name = #arg3_ty;
type Result = #ty;
}
}
}
},
MethodProto::Quaternary{name: n, arg1, arg2, arg3, proto} => {
if sig.decl.inputs.len() <= 3 {
print_err(format!("Not enough arguments {}", n), quote!(sig));

View File

@ -21,9 +21,6 @@ use objectprotocol::ObjectProtocol;
use callback::{PyObjectCallbackConverter, HashConverter, BoolCallbackConverter};
use class::methods::PyMethodDef;
// __instancecheck__
// __subclasscheck__
/// Basic python class customization
#[allow(unused_variables)]

View File

@ -91,6 +91,36 @@ macro_rules! py_binary_func{
}}
}
#[macro_export]
#[doc(hidden)]
macro_rules! py_binary_num_func{
($trait:ident, $class:ident :: $f:ident, $res_type:ty, $conv:expr) => {{
#[allow(unused_mut)]
unsafe extern "C" fn wrap<T>(lhs: *mut ffi::PyObject,
rhs: *mut ffi::PyObject) -> *mut $crate::ffi::PyObject
where T: for<'p> $trait<'p> + $crate::PyDowncastFrom
{
use $crate::ObjectProtocol;
const LOCATION: &'static str = concat!(stringify!($class), ".", stringify!($f), "()");
$crate::callback::cb_pyfunc::<_, _, $res_type>(LOCATION, $conv, |py| {
let lhs = py.cast_from_borrowed_ptr::<$crate::PyObjectRef>(lhs);
let rhs = py.cast_from_borrowed_ptr::<$crate::PyObjectRef>(rhs);
let result = match lhs.extract() {
Ok(lhs) => match rhs.extract() {
Ok(rhs) => $class::$f(lhs, rhs).into(),
Err(e) => Err(e.into()),
},
Err(e) => Err(e.into()),
};
$crate::callback::cb_convert($conv, py, result)
})
}
Some(wrap::<$class>)
}}
}
#[macro_export]
#[doc(hidden)]
macro_rules! py_binary_self_func{
@ -184,6 +214,41 @@ macro_rules! py_ternary_func{
}}
}
#[macro_export]
#[doc(hidden)]
macro_rules! py_ternary_num_func{
($trait:ident, $class:ident :: $f:ident, $res_type:ty, $conv:expr) => {{
unsafe extern "C" fn wrap<T>(arg1: *mut $crate::ffi::PyObject,
arg2: *mut $crate::ffi::PyObject,
arg3: *mut $crate::ffi::PyObject) -> *mut $crate::ffi::PyObject
where T: for<'p> $trait<'p> + $crate::PyDowncastFrom
{
use $crate::ObjectProtocol;
const LOCATION: &'static str = concat!(stringify!($class), ".", stringify!($f), "()");
$crate::callback::cb_pyfunc::<_, _, $res_type>(LOCATION, $conv, |py| {
let arg1 = py.cast_from_borrowed_ptr::<$crate::PyObjectRef>(arg1);
let arg2 = py.cast_from_borrowed_ptr::<$crate::PyObjectRef>(arg2);
let arg3 = py.cast_from_borrowed_ptr::<$crate::PyObjectRef>(arg3);
let result = match arg1.extract() {
Ok(arg1) => match arg2.extract() {
Ok(arg2) => match arg3.extract() {
Ok(arg3) => $class::$f(arg1, arg2, arg3).into(),
Err(e) => Err(e.into())
},
Err(e) => Err(e.into())
},
Err(e) => Err(e.into()),
};
$crate::callback::cb_convert($conv, py, result)
})
}
Some(wrap::<T>)
}}
}
#[macro_export]
#[doc(hidden)]
macro_rules! py_ternary_self_func{

View File

@ -15,33 +15,33 @@ use {IntoPyObject, FromPyObject, PyDowncastFrom};
#[allow(unused_variables)]
pub trait PyNumberProtocol<'p>: PyTypeInfo + PyDowncastFrom {
fn __add__(&'p self, other: Self::Other)
fn __add__(lhs: Self::Left, rhs: Self::Right)
-> Self::Result where Self: PyNumberAddProtocol<'p> { unimplemented!() }
fn __sub__(&'p self, other: Self::Other)
fn __sub__(lhs: Self::Left, rhs: Self::Right)
-> Self::Result where Self: PyNumberSubProtocol<'p> { unimplemented!() }
fn __mul__(&'p self, other: Self::Other)
fn __mul__(lhs: Self::Left, rhs: Self::Right)
-> Self::Result where Self: PyNumberMulProtocol<'p> { unimplemented!() }
fn __matmul__(&'p self, other: Self::Other)
fn __matmul__(lhs: Self::Left, rhs: Self::Right)
-> Self::Result where Self: PyNumberMatmulProtocol<'p> { unimplemented!() }
fn __truediv__(&'p self, other: Self::Other)
fn __truediv__(lhs: Self::Left, rhs: Self::Right)
-> Self::Result where Self: PyNumberTruedivProtocol<'p> { unimplemented!() }
fn __floordiv__(&'p self, other: Self::Other)
fn __floordiv__(lhs: Self::Left, rhs: Self::Right)
-> Self::Result where Self: PyNumberFloordivProtocol<'p> { unimplemented!() }
fn __mod__(&'p self, other: Self::Other)
fn __mod__(lhs: Self::Left, rhs: Self::Right)
-> Self::Result where Self: PyNumberModProtocol<'p> { unimplemented!() }
fn __divmod__(&'p self, other: Self::Other)
fn __divmod__(lhs: Self::Left, rhs: Self::Right)
-> Self::Result where Self: PyNumberDivmodProtocol<'p> { unimplemented!() }
fn __pow__(&'p self, other: Self::Other, modulo: Self::Modulo)
fn __pow__(lhs: Self::Left, rhs: Self::Right, modulo: Self::Modulo)
-> Self::Result where Self: PyNumberPowProtocol<'p> { unimplemented!() }
fn __lshift__(&'p self, other: Self::Other)
fn __lshift__(lhs: Self::Left, rhs: Self::Right)
-> Self::Result where Self: PyNumberLShiftProtocol<'p> { unimplemented!() }
fn __rshift__(&'p self, other: Self::Other)
fn __rshift__(lhs: Self::Left, rhs: Self::Right)
-> Self::Result where Self: PyNumberRShiftProtocol<'p> { unimplemented!() }
fn __and__(&'p self, other: Self::Other)
fn __and__(lhs: Self::Left, rhs: Self::Right)
-> Self::Result where Self: PyNumberAndProtocol<'p> { unimplemented!() }
fn __xor__(&'p self, other: Self::Other)
fn __xor__(lhs: Self::Left, rhs: Self::Right)
-> Self::Result where Self: PyNumberXorProtocol<'p> { unimplemented!() }
fn __or__(&'p self, other: Self::Other)
fn __or__(lhs: Self::Left, rhs: Self::Right)
-> Self::Result where Self: PyNumberOrProtocol<'p> { unimplemented!() }
fn __radd__(&'p self, other: Self::Other)
@ -123,73 +123,87 @@ pub trait PyNumberProtocol<'p>: PyTypeInfo + PyDowncastFrom {
pub trait PyNumberAddProtocol<'p>: PyNumberProtocol<'p> {
type Other: FromPyObject<'p>;
type Left: FromPyObject<'p>;
type Right: FromPyObject<'p>;
type Success: IntoPyObject;
type Result: Into<PyResult<Self::Success>>;
}
pub trait PyNumberSubProtocol<'p>: PyNumberProtocol<'p> {
type Other: FromPyObject<'p>;
type Left: FromPyObject<'p>;
type Right: FromPyObject<'p>;
type Success: IntoPyObject;
type Result: Into<PyResult<Self::Success>>;
}
pub trait PyNumberMulProtocol<'p>: PyNumberProtocol<'p> {
type Other: FromPyObject<'p>;
type Left: FromPyObject<'p>;
type Right: FromPyObject<'p>;
type Success: IntoPyObject;
type Result: Into<PyResult<Self::Success>>;
}
pub trait PyNumberMatmulProtocol<'p>: PyNumberProtocol<'p> {
type Other: FromPyObject<'p>;
type Left: FromPyObject<'p>;
type Right: FromPyObject<'p>;
type Success: IntoPyObject;
type Result: Into<PyResult<Self::Success>>;
}
pub trait PyNumberTruedivProtocol<'p>: PyNumberProtocol<'p> {
type Other: FromPyObject<'p>;
type Left: FromPyObject<'p>;
type Right: FromPyObject<'p>;
type Success: IntoPyObject;
type Result: Into<PyResult<Self::Success>>;
}
pub trait PyNumberFloordivProtocol<'p>: PyNumberProtocol<'p> {
type Other: FromPyObject<'p>;
type Left: FromPyObject<'p>;
type Right: FromPyObject<'p>;
type Success: IntoPyObject;
type Result: Into<PyResult<Self::Success>>;
}
pub trait PyNumberModProtocol<'p>: PyNumberProtocol<'p> {
type Other: FromPyObject<'p>;
type Left: FromPyObject<'p>;
type Right: FromPyObject<'p>;
type Success: IntoPyObject;
type Result: Into<PyResult<Self::Success>>;
}
pub trait PyNumberDivmodProtocol<'p>: PyNumberProtocol<'p> {
type Other: FromPyObject<'p>;
type Left: FromPyObject<'p>;
type Right: FromPyObject<'p>;
type Success: IntoPyObject;
type Result: Into<PyResult<Self::Success>>;
}
pub trait PyNumberPowProtocol<'p>: PyNumberProtocol<'p> {
type Other: FromPyObject<'p>;
type Left: FromPyObject<'p>;
type Right: FromPyObject<'p>;
type Modulo: FromPyObject<'p>;
type Success: IntoPyObject;
type Result: Into<PyResult<Self::Success>>;
}
pub trait PyNumberLShiftProtocol<'p>: PyNumberProtocol<'p> {
type Other: FromPyObject<'p>;
type Left: FromPyObject<'p>;
type Right: FromPyObject<'p>;
type Success: IntoPyObject;
type Result: Into<PyResult<Self::Success>>;
}
pub trait PyNumberRShiftProtocol<'p>: PyNumberProtocol<'p> {
type Other: FromPyObject<'p>;
type Left: FromPyObject<'p>;
type Right: FromPyObject<'p>;
type Success: IntoPyObject;
type Result: Into<PyResult<Self::Success>>;
}
pub trait PyNumberAndProtocol<'p>: PyNumberProtocol<'p> {
type Other: FromPyObject<'p>;
type Left: FromPyObject<'p>;
type Right: FromPyObject<'p>;
type Success: IntoPyObject;
type Result: Into<PyResult<Self::Success>>;
}
pub trait PyNumberXorProtocol<'p>: PyNumberProtocol<'p> {
type Other: FromPyObject<'p>;
type Left: FromPyObject<'p>;
type Right: FromPyObject<'p>;
type Success: IntoPyObject;
type Result: Into<PyResult<Self::Success>>;
}
pub trait PyNumberOrProtocol<'p>: PyNumberProtocol<'p> {
type Other: FromPyObject<'p>;
type Left: FromPyObject<'p>;
type Right: FromPyObject<'p>;
type Success: IntoPyObject;
type Result: Into<PyResult<Self::Success>>;
}
@ -556,7 +570,8 @@ impl<'p, T> PyNumberAddProtocolImpl for T where T: PyNumberProtocol<'p> {
}
impl<T> PyNumberAddProtocolImpl for T where T: for<'p> PyNumberAddProtocol<'p> {
fn nb_add() -> Option<ffi::binaryfunc> {
py_binary_func!(PyNumberAddProtocol, T::__add__, T::Success, PyObjectCallbackConverter)
py_binary_num_func!(
PyNumberAddProtocol, T::__add__, T::Success, PyObjectCallbackConverter)
}
}
@ -568,7 +583,8 @@ impl<'p, T> PyNumberSubProtocolImpl for T where T: PyNumberProtocol<'p> {
}
impl<T> PyNumberSubProtocolImpl for T where T: for<'p> PyNumberSubProtocol<'p> {
fn nb_subtract() -> Option<ffi::binaryfunc> {
py_binary_func!(PyNumberSubProtocol, T::__sub__, T::Success, PyObjectCallbackConverter)
py_binary_num_func!(
PyNumberSubProtocol, T::__sub__, T::Success, PyObjectCallbackConverter)
}
}
@ -580,7 +596,8 @@ impl<'p, T> PyNumberMulProtocolImpl for T where T: PyNumberProtocol<'p> {
}
impl<T> PyNumberMulProtocolImpl for T where T: for<'p> PyNumberMulProtocol<'p> {
fn nb_multiply() -> Option<ffi::binaryfunc> {
py_binary_func!(PyNumberMulProtocol, T::__mul__, T::Success, PyObjectCallbackConverter)
py_binary_num_func!(
PyNumberMulProtocol, T::__mul__, T::Success, PyObjectCallbackConverter)
}
}
@ -592,8 +609,8 @@ impl<'p, T> PyNumberMatmulProtocolImpl for T where T: PyNumberProtocol<'p> {
}
impl<T> PyNumberMatmulProtocolImpl for T where T: for<'p> PyNumberMatmulProtocol<'p> {
fn nb_matrix_multiply() -> Option<ffi::binaryfunc> {
py_binary_func!(PyNumberMatmulProtocol,
T::__matmul__, T::Success, PyObjectCallbackConverter)
py_binary_num_func!(
PyNumberMatmulProtocol, T::__matmul__, T::Success, PyObjectCallbackConverter)
}
}
@ -607,8 +624,8 @@ impl<T> PyNumberTruedivProtocolImpl for T
where T: for<'p> PyNumberTruedivProtocol<'p>
{
fn nb_true_divide() -> Option<ffi::binaryfunc> {
py_binary_func!(PyNumberTruedivProtocol,
T::__truediv__, T::Success, PyObjectCallbackConverter)
py_binary_num_func!(
PyNumberTruedivProtocol, T::__truediv__, T::Success, PyObjectCallbackConverter)
}
}
@ -622,8 +639,8 @@ impl<T> PyNumberFloordivProtocolImpl for T
where T: for<'p> PyNumberFloordivProtocol<'p>
{
fn nb_floor_divide() -> Option<ffi::binaryfunc> {
py_binary_func!(PyNumberFloordivProtocol,
T::__floordiv__, T::Success, PyObjectCallbackConverter)
py_binary_num_func!(
PyNumberFloordivProtocol, T::__floordiv__, T::Success, PyObjectCallbackConverter)
}
}
@ -635,7 +652,8 @@ impl<'p, T> PyNumberModProtocolImpl for T where T: PyNumberProtocol<'p> {
}
impl<T> PyNumberModProtocolImpl for T where T: for<'p> PyNumberModProtocol<'p> {
fn nb_remainder() -> Option<ffi::binaryfunc> {
py_binary_func!(PyNumberModProtocol, T::__mod__, T::Success, PyObjectCallbackConverter)
py_binary_num_func!(
PyNumberModProtocol, T::__mod__, T::Success, PyObjectCallbackConverter)
}
}
@ -647,8 +665,8 @@ impl<'p, T> PyNumberDivmodProtocolImpl for T where T: PyNumberProtocol<'p> {
}
impl<T> PyNumberDivmodProtocolImpl for T where T: for<'p> PyNumberDivmodProtocol<'p> {
fn nb_divmod() -> Option<ffi::binaryfunc> {
py_binary_func!(PyNumberDivmodProtocol,
T::__divmod__, T::Success, PyObjectCallbackConverter)
py_binary_num_func!(
PyNumberDivmodProtocol, T::__divmod__, T::Success, PyObjectCallbackConverter)
}
}
@ -660,9 +678,9 @@ impl<'p, T> PyNumberPowProtocolImpl for T where T: PyNumberProtocol<'p> {
}
impl<T> PyNumberPowProtocolImpl for T where T: for<'p> PyNumberPowProtocol<'p> {
fn nb_power() -> Option<ffi::ternaryfunc> {
py_ternary_func!(PyNumberPowProtocol,
T::__pow__, <T as PyNumberPowProtocol>::Success,
PyObjectCallbackConverter)
py_ternary_num_func!(
PyNumberPowProtocol,
T::__pow__, <T as PyNumberPowProtocol>::Success, PyObjectCallbackConverter)
}
}
@ -674,8 +692,8 @@ impl<'p, T> PyNumberLShiftProtocolImpl for T where T: PyNumberProtocol<'p> {
}
impl<T> PyNumberLShiftProtocolImpl for T where T: for<'p> PyNumberLShiftProtocol<'p> {
fn nb_lshift() -> Option<ffi::binaryfunc> {
py_binary_func!(PyNumberLShiftProtocol,
T::__lshift__, T::Success, PyObjectCallbackConverter)
py_binary_num_func!(
PyNumberLShiftProtocol, T::__lshift__, T::Success, PyObjectCallbackConverter)
}
}
@ -687,8 +705,8 @@ impl<'p, T> PyNumberRShiftProtocolImpl for T where T: PyNumberProtocol<'p> {
}
impl<T> PyNumberRShiftProtocolImpl for T where T: for<'p> PyNumberRShiftProtocol<'p> {
fn nb_rshift() -> Option<ffi::binaryfunc> {
py_binary_func!(PyNumberRShiftProtocol,
T::__rshift__, T::Success, PyObjectCallbackConverter)
py_binary_num_func!(
PyNumberRShiftProtocol, T::__rshift__, T::Success, PyObjectCallbackConverter)
}
}
@ -701,8 +719,8 @@ impl<'p, T> PyNumberAndProtocolImpl for T where T: PyNumberProtocol<'p> {
}
impl<T> PyNumberAndProtocolImpl for T where T: for<'p> PyNumberAndProtocol<'p> {
fn nb_and() -> Option<ffi::binaryfunc> {
py_binary_func!(PyNumberAndProtocol,
T::__and__, T::Success, PyObjectCallbackConverter)
py_binary_num_func!(
PyNumberAndProtocol, T::__and__, T::Success, PyObjectCallbackConverter)
}
}
@ -714,8 +732,8 @@ impl<'p, T> PyNumberXorProtocolImpl for T where T: PyNumberProtocol<'p> {
}
impl<T> PyNumberXorProtocolImpl for T where T: for<'p> PyNumberXorProtocol<'p> {
fn nb_xor() -> Option<ffi::binaryfunc> {
py_binary_func!(PyNumberXorProtocol,
T::__xor__, T::Success, PyObjectCallbackConverter)
py_binary_num_func!(
PyNumberXorProtocol, T::__xor__, T::Success, PyObjectCallbackConverter)
}
}
@ -727,8 +745,8 @@ impl<'p, T> PyNumberOrProtocolImpl for T where T: PyNumberProtocol<'p> {
}
impl<T> PyNumberOrProtocolImpl for T where T: for<'p> PyNumberOrProtocol<'p> {
fn nb_or() -> Option<ffi::binaryfunc> {
py_binary_func!(PyNumberOrProtocol,
T::__or__, T::Success, PyObjectCallbackConverter)
py_binary_num_func!(
PyNumberOrProtocol, T::__or__, T::Success, PyObjectCallbackConverter)
}
}

View File

@ -852,36 +852,36 @@ impl PyObjectProtocol for BinaryArithmetic {
#[py::proto]
impl PyNumberProtocol for BinaryArithmetic {
fn __add__(&self, rhs: &PyObjectRef) -> PyResult<String> {
Ok(format!("{:?} + {:?}", self, rhs))
fn __add__(lhs: &PyObjectRef, rhs: &PyObjectRef) -> PyResult<String> {
Ok(format!("{:?} + {:?}", lhs, rhs))
}
fn __sub__(&self, rhs: &PyObjectRef) -> PyResult<String> {
Ok(format!("{:?} - {:?}", self, rhs))
fn __sub__(lhs: &PyObjectRef, rhs: &PyObjectRef) -> PyResult<String> {
Ok(format!("{:?} - {:?}", lhs, rhs))
}
fn __mul__(&self, rhs: &PyObjectRef) -> PyResult<String> {
Ok(format!("{:?} * {:?}", self, rhs))
fn __mul__(lhs: &PyObjectRef, rhs: &PyObjectRef) -> PyResult<String> {
Ok(format!("{:?} * {:?}", lhs, rhs))
}
fn __lshift__(&self, rhs: &PyObjectRef) -> PyResult<String> {
Ok(format!("{:?} << {:?}", self, rhs))
fn __lshift__(lhs: &PyObjectRef, rhs: &PyObjectRef) -> PyResult<String> {
Ok(format!("{:?} << {:?}", lhs, rhs))
}
fn __rshift__(&self, rhs: &PyObjectRef) -> PyResult<String> {
Ok(format!("{:?} >> {:?}", self, rhs))
fn __rshift__(lhs: &PyObjectRef, rhs: &PyObjectRef) -> PyResult<String> {
Ok(format!("{:?} >> {:?}", lhs, rhs))
}
fn __and__(&self, rhs: &PyObjectRef) -> PyResult<String> {
Ok(format!("{:?} & {:?}", self, rhs))
fn __and__(lhs: &PyObjectRef, rhs: &PyObjectRef) -> PyResult<String> {
Ok(format!("{:?} & {:?}", lhs, rhs))
}
fn __xor__(&self, rhs: &PyObjectRef) -> PyResult<String> {
Ok(format!("{:?} ^ {:?}", self, rhs))
fn __xor__(lhs: &PyObjectRef, rhs: &PyObjectRef) -> PyResult<String> {
Ok(format!("{:?} ^ {:?}", lhs, rhs))
}
fn __or__(&self, rhs: &PyObjectRef) -> PyResult<String> {
Ok(format!("{:?} | {:?}", self, rhs))
fn __or__(lhs: &PyObjectRef, rhs: &PyObjectRef) -> PyResult<String> {
Ok(format!("{:?} | {:?}", lhs, rhs))
}
}