diff --git a/CHANGELOG.md b/CHANGELOG.md index df50e9d2..5ae40e68 100644 --- a/CHANGELOG.md +++ b/CHANGELOG.md @@ -10,6 +10,7 @@ and this project adheres to [Semantic Versioning](http://semver.org/spec/v2.0.0. ### Fixed * Make sure the right Python interpreter is used in OSX builds. [#604](https://github.com/PyO3/pyo3/pull/604) + * Patch specialization being broken by Rust 1.40 [#614](https://github.com/PyO3/pyo3/issues/614) ## [0.8.0] - 2018-09-05 diff --git a/pyo3-derive-backend/src/pymethod.rs b/pyo3-derive-backend/src/pymethod.rs index 4c102a21..0245ce4f 100644 --- a/pyo3-derive-backend/src/pymethod.rs +++ b/pyo3-derive-backend/src/pymethod.rs @@ -38,7 +38,7 @@ pub fn gen_py_method( return Err(syn::Error::new_spanned( spec.args[0].ty, "Getter function can only have one argument of type pyo3::Python!", - )) + )); } }; impl_py_getter_def(name, doc, getter, &impl_wrap_getter(cls, name, takes_py)) @@ -60,10 +60,10 @@ fn check_generic(name: &syn::Ident, sig: &syn::Signature) -> syn::Result<()> { match param { syn::GenericParam::Lifetime(_) => {} syn::GenericParam::Type(_) => { - return Err(syn::Error::new_spanned(param, err_msg("type"))) + return Err(syn::Error::new_spanned(param, err_msg("type"))); } syn::GenericParam::Const(_) => { - return Err(syn::Error::new_spanned(param, err_msg("const"))) + return Err(syn::Error::new_spanned(param, err_msg("const"))); } } } diff --git a/src/class/basic.rs b/src/class/basic.rs index c3153dfa..8c670bf8 100644 --- a/src/class/basic.rs +++ b/src/class/basic.rs @@ -152,17 +152,21 @@ pub trait PyObjectRichcmpProtocol<'p>: PyObjectProtocol<'p> { #[doc(hidden)] pub trait PyObjectProtocolImpl { - fn methods() -> Vec { + fn methods() -> Vec; + fn tp_as_object(_type_object: &mut ffi::PyTypeObject); + fn nb_bool_fn() -> Option; +} + +impl PyObjectProtocolImpl for T { + default fn methods() -> Vec { Vec::new() } - fn tp_as_object(_type_object: &mut ffi::PyTypeObject) {} - fn nb_bool_fn() -> Option { + default fn tp_as_object(_type_object: &mut ffi::PyTypeObject) {} + default fn nb_bool_fn() -> Option { None } } -impl PyObjectProtocolImpl for T {} - impl<'p, T> PyObjectProtocolImpl for T where T: PyObjectProtocol<'p>, @@ -195,13 +199,18 @@ where } trait GetAttrProtocolImpl { - fn tp_getattro() -> Option { + fn tp_getattro() -> Option; +} + +impl<'p, T> GetAttrProtocolImpl for T +where + T: PyObjectProtocol<'p>, +{ + default fn tp_getattro() -> Option { None } } -impl<'p, T> GetAttrProtocolImpl for T where T: PyObjectProtocol<'p> {} - impl GetAttrProtocolImpl for T where T: for<'p> PyObjectGetAttrProtocol<'p>, @@ -268,13 +277,15 @@ mod tp_setattro_impl { } trait SetAttr { - fn set_attr() -> Option { + fn set_attr() -> Option; + } + + impl<'p, T: PyObjectProtocol<'p>> SetAttr for T { + default fn set_attr() -> Option { None } } - impl<'p, T: PyObjectProtocol<'p>> SetAttr for T {} - impl SetAttr for T where T: for<'p> PyObjectSetAttrProtocol<'p>, @@ -285,13 +296,18 @@ mod tp_setattro_impl { } trait DelAttr { - fn del_attr() -> Option { + fn del_attr() -> Option; + } + + impl<'p, T> DelAttr for T + where + T: PyObjectProtocol<'p>, + { + default fn del_attr() -> Option { None } } - impl<'p, T> DelAttr for T where T: PyObjectProtocol<'p> {} - impl DelAttr for T where T: for<'p> PyObjectDelAttrProtocol<'p>, @@ -302,13 +318,18 @@ mod tp_setattro_impl { } trait SetDelAttr { - fn set_del_attr() -> Option { + fn set_del_attr() -> Option; + } + + impl<'p, T> SetDelAttr for T + where + T: PyObjectProtocol<'p>, + { + default fn set_del_attr() -> Option { None } } - impl<'p, T> SetDelAttr for T where T: PyObjectProtocol<'p> {} - impl SetDelAttr for T where T: for<'p> PyObjectSetAttrProtocol<'p> + for<'p> PyObjectDelAttrProtocol<'p>, @@ -326,11 +347,16 @@ mod tp_setattro_impl { } trait StrProtocolImpl { - fn tp_str() -> Option { + fn tp_str() -> Option; +} +impl<'p, T> StrProtocolImpl for T +where + T: PyObjectProtocol<'p>, +{ + default fn tp_str() -> Option { None } } -impl<'p, T> StrProtocolImpl for T where T: PyObjectProtocol<'p> {} impl StrProtocolImpl for T where T: for<'p> PyObjectStrProtocol<'p>, @@ -346,11 +372,16 @@ where } trait ReprProtocolImpl { - fn tp_repr() -> Option { + fn tp_repr() -> Option; +} +impl<'p, T> ReprProtocolImpl for T +where + T: PyObjectProtocol<'p>, +{ + default fn tp_repr() -> Option { None } } -impl<'p, T> ReprProtocolImpl for T where T: PyObjectProtocol<'p> {} impl ReprProtocolImpl for T where T: for<'p> PyObjectReprProtocol<'p>, @@ -367,34 +398,54 @@ where #[doc(hidden)] pub trait FormatProtocolImpl { - fn __format__() -> Option { + fn __format__() -> Option; +} +impl<'p, T> FormatProtocolImpl for T +where + T: PyObjectProtocol<'p>, +{ + default fn __format__() -> Option { None } } -impl<'p, T> FormatProtocolImpl for T where T: PyObjectProtocol<'p> {} #[doc(hidden)] pub trait BytesProtocolImpl { - fn __bytes__() -> Option { + fn __bytes__() -> Option; +} +impl<'p, T> BytesProtocolImpl for T +where + T: PyObjectProtocol<'p>, +{ + default fn __bytes__() -> Option { None } } -impl<'p, T> BytesProtocolImpl for T where T: PyObjectProtocol<'p> {} #[doc(hidden)] pub trait UnicodeProtocolImpl { - fn __unicode__() -> Option { + fn __unicode__() -> Option; +} +impl<'p, T> UnicodeProtocolImpl for T +where + T: PyObjectProtocol<'p>, +{ + default fn __unicode__() -> Option { None } } -impl<'p, T> UnicodeProtocolImpl for T where T: PyObjectProtocol<'p> {} trait HashProtocolImpl { - fn tp_hash() -> Option { + fn tp_hash() -> Option; +} +impl<'p, T> HashProtocolImpl for T +where + T: PyObjectProtocol<'p>, +{ + default fn tp_hash() -> Option { None } } -impl<'p, T> HashProtocolImpl for T where T: PyObjectProtocol<'p> {} impl HashProtocolImpl for T where T: for<'p> PyObjectHashProtocol<'p>, @@ -411,11 +462,16 @@ where } trait BoolProtocolImpl { - fn nb_bool() -> Option { + fn nb_bool() -> Option; +} +impl<'p, T> BoolProtocolImpl for T +where + T: PyObjectProtocol<'p>, +{ + default fn nb_bool() -> Option { None } } -impl<'p, T> BoolProtocolImpl for T where T: PyObjectProtocol<'p> {} impl BoolProtocolImpl for T where T: for<'p> PyObjectBoolProtocol<'p>, @@ -432,11 +488,16 @@ where } trait RichcmpProtocolImpl { - fn tp_richcompare() -> Option { + fn tp_richcompare() -> Option; +} +impl<'p, T> RichcmpProtocolImpl for T +where + T: PyObjectProtocol<'p>, +{ + default fn tp_richcompare() -> Option { None } } -impl<'p, T> RichcmpProtocolImpl for T where T: PyObjectProtocol<'p> {} impl RichcmpProtocolImpl for T where T: for<'p> PyObjectRichcmpProtocol<'p>, diff --git a/src/class/buffer.rs b/src/class/buffer.rs index d36524e7..f0f462db 100644 --- a/src/class/buffer.rs +++ b/src/class/buffer.rs @@ -41,13 +41,15 @@ pub trait PyBufferReleaseBufferProtocol<'p>: PyBufferProtocol<'p> { #[doc(hidden)] pub trait PyBufferProtocolImpl { - fn tp_as_buffer() -> Option { + fn tp_as_buffer() -> Option; +} + +impl PyBufferProtocolImpl for T { + default fn tp_as_buffer() -> Option { None } } -impl PyBufferProtocolImpl for T {} - impl<'p, T> PyBufferProtocolImpl for T where T: PyBufferProtocol<'p>, @@ -64,13 +66,18 @@ where } trait PyBufferGetBufferProtocolImpl { - fn cb_bf_getbuffer() -> Option { + fn cb_bf_getbuffer() -> Option; +} + +impl<'p, T> PyBufferGetBufferProtocolImpl for T +where + T: PyBufferProtocol<'p>, +{ + default fn cb_bf_getbuffer() -> Option { None } } -impl<'p, T> PyBufferGetBufferProtocolImpl for T where T: PyBufferProtocol<'p> {} - impl PyBufferGetBufferProtocolImpl for T where T: for<'p> PyBufferGetBufferProtocol<'p>, diff --git a/src/class/context.rs b/src/class/context.rs index 092527ba..50aea33e 100644 --- a/src/class/context.rs +++ b/src/class/context.rs @@ -47,13 +47,15 @@ pub trait PyContextExitProtocol<'p>: PyContextProtocol<'p> { #[doc(hidden)] pub trait PyContextProtocolImpl { - fn methods() -> Vec { + fn methods() -> Vec; +} + +impl PyContextProtocolImpl for T { + default fn methods() -> Vec { Vec::new() } } -impl PyContextProtocolImpl for T {} - impl<'p, T> PyContextProtocolImpl for T where T: PyContextProtocol<'p>, @@ -75,18 +77,28 @@ where #[doc(hidden)] pub trait PyContextEnterProtocolImpl { - fn __enter__() -> Option { + fn __enter__() -> Option; +} + +impl<'p, T> PyContextEnterProtocolImpl for T +where + T: PyContextProtocol<'p>, +{ + default fn __enter__() -> Option { None } } -impl<'p, T> PyContextEnterProtocolImpl for T where T: PyContextProtocol<'p> {} - #[doc(hidden)] pub trait PyContextExitProtocolImpl { - fn __exit__() -> Option { + fn __exit__() -> Option; +} + +impl<'p, T> PyContextExitProtocolImpl for T +where + T: PyContextProtocol<'p>, +{ + default fn __exit__() -> Option { None } } - -impl<'p, T> PyContextExitProtocolImpl for T where T: PyContextProtocol<'p> {} diff --git a/src/class/descr.rs b/src/class/descr.rs index 5f7e6830..57d9119c 100644 --- a/src/class/descr.rs +++ b/src/class/descr.rs @@ -70,11 +70,16 @@ pub trait PyDescrSetNameProtocol<'p>: PyDescrProtocol<'p> { } trait PyDescrGetProtocolImpl { - fn tp_descr_get() -> Option { + fn tp_descr_get() -> Option; +} +impl<'p, T> PyDescrGetProtocolImpl for T +where + T: PyDescrProtocol<'p>, +{ + default fn tp_descr_get() -> Option { None } } -impl<'p, T> PyDescrGetProtocolImpl for T where T: PyDescrProtocol<'p> {} impl PyDescrGetProtocolImpl for T where @@ -91,11 +96,16 @@ where } trait PyDescrSetProtocolImpl { - fn tp_descr_set() -> Option { + fn tp_descr_set() -> Option; +} +impl<'p, T> PyDescrSetProtocolImpl for T +where + T: PyDescrProtocol<'p>, +{ + default fn tp_descr_set() -> Option { None } } -impl<'p, T> PyDescrSetProtocolImpl for T where T: PyDescrProtocol<'p> {} impl PyDescrSetProtocolImpl for T where T: for<'p> PyDescrSetProtocol<'p>, @@ -127,13 +137,16 @@ impl<'p, T> PyDescrSetNameProtocolImpl for T where T: PyDescrProtocol<'p> {} #[doc(hidden)] pub trait PyDescrProtocolImpl { - fn methods() -> Vec { - Vec::new() - } - fn tp_as_descr(_type_object: &mut ffi::PyTypeObject) {} + fn methods() -> Vec; + fn tp_as_descr(_type_object: &mut ffi::PyTypeObject); } -impl PyDescrProtocolImpl for T {} +impl PyDescrProtocolImpl for T { + default fn methods() -> Vec { + Vec::new() + } + default fn tp_as_descr(_type_object: &mut ffi::PyTypeObject) {} +} impl<'p, T> PyDescrProtocolImpl for T where diff --git a/src/class/gc.rs b/src/class/gc.rs index eaa4d901..591b686a 100644 --- a/src/class/gc.rs +++ b/src/class/gc.rs @@ -23,10 +23,12 @@ pub trait PyGCClearProtocol<'p>: PyGCProtocol<'p> {} #[doc(hidden)] pub trait PyGCProtocolImpl { - fn update_type_object(_type_object: &mut ffi::PyTypeObject) {} + fn update_type_object(_type_object: &mut ffi::PyTypeObject); } -impl<'p, T> PyGCProtocolImpl for T {} +impl<'p, T> PyGCProtocolImpl for T { + default fn update_type_object(_type_object: &mut ffi::PyTypeObject) {} +} impl<'p, T> PyGCProtocolImpl for T where @@ -63,13 +65,18 @@ impl<'p> PyVisit<'p> { } trait PyGCTraverseProtocolImpl { - fn tp_traverse() -> Option { + fn tp_traverse() -> Option; +} + +impl<'p, T> PyGCTraverseProtocolImpl for T +where + T: PyGCProtocol<'p>, +{ + default fn tp_traverse() -> Option { None } } -impl<'p, T> PyGCTraverseProtocolImpl for T where T: PyGCProtocol<'p> {} - #[doc(hidden)] impl PyGCTraverseProtocolImpl for T where @@ -105,13 +112,18 @@ where } trait PyGCClearProtocolImpl { - fn tp_clear() -> Option { + fn tp_clear() -> Option; +} + +impl<'p, T> PyGCClearProtocolImpl for T +where + T: PyGCProtocol<'p>, +{ + default fn tp_clear() -> Option { None } } -impl<'p, T> PyGCClearProtocolImpl for T where T: PyGCProtocol<'p> {} - impl PyGCClearProtocolImpl for T where T: for<'p> PyGCClearProtocol<'p>, diff --git a/src/class/iter.rs b/src/class/iter.rs index 9001d53c..a528289d 100644 --- a/src/class/iter.rs +++ b/src/class/iter.rs @@ -44,10 +44,12 @@ pub trait PyIterNextProtocol<'p>: PyIterProtocol<'p> { #[doc(hidden)] pub trait PyIterProtocolImpl { - fn tp_as_iter(_typeob: &mut ffi::PyTypeObject) {} + fn tp_as_iter(_typeob: &mut ffi::PyTypeObject); } -impl PyIterProtocolImpl for T {} +impl PyIterProtocolImpl for T { + default fn tp_as_iter(_typeob: &mut ffi::PyTypeObject) {} +} impl<'p, T> PyIterProtocolImpl for T where @@ -61,13 +63,18 @@ where } trait PyIterIterProtocolImpl { - fn tp_iter() -> Option { + fn tp_iter() -> Option; +} + +impl<'p, T> PyIterIterProtocolImpl for T +where + T: PyIterProtocol<'p>, +{ + default fn tp_iter() -> Option { None } } -impl<'p, T> PyIterIterProtocolImpl for T where T: PyIterProtocol<'p> {} - impl PyIterIterProtocolImpl for T where T: for<'p> PyIterIterProtocol<'p>, @@ -84,13 +91,18 @@ where } trait PyIterNextProtocolImpl { - fn tp_iternext() -> Option { + fn tp_iternext() -> Option; +} + +impl<'p, T> PyIterNextProtocolImpl for T +where + T: PyIterProtocol<'p>, +{ + default fn tp_iternext() -> Option { None } } -impl<'p, T> PyIterNextProtocolImpl for T where T: PyIterProtocol<'p> {} - impl PyIterNextProtocolImpl for T where T: for<'p> PyIterNextProtocol<'p>, diff --git a/src/class/mapping.rs b/src/class/mapping.rs index 61676350..32326ff4 100644 --- a/src/class/mapping.rs +++ b/src/class/mapping.rs @@ -106,16 +106,19 @@ pub trait PyMappingReversedProtocol<'p>: PyMappingProtocol<'p> { #[doc(hidden)] pub trait PyMappingProtocolImpl { - fn tp_as_mapping() -> Option { + fn tp_as_mapping() -> Option; + fn methods() -> Vec; +} + +impl PyMappingProtocolImpl for T { + default fn tp_as_mapping() -> Option { None } - fn methods() -> Vec { + default fn methods() -> Vec { Vec::new() } } -impl PyMappingProtocolImpl for T {} - impl<'p, T> PyMappingProtocolImpl for T where T: PyMappingProtocol<'p>, @@ -154,13 +157,18 @@ where } trait PyMappingLenProtocolImpl { - fn mp_length() -> Option { + fn mp_length() -> Option; +} + +impl<'p, T> PyMappingLenProtocolImpl for T +where + T: PyMappingProtocol<'p>, +{ + default fn mp_length() -> Option { None } } -impl<'p, T> PyMappingLenProtocolImpl for T where T: PyMappingProtocol<'p> {} - impl PyMappingLenProtocolImpl for T where T: for<'p> PyMappingLenProtocol<'p>, @@ -172,13 +180,18 @@ where } trait PyMappingGetItemProtocolImpl { - fn mp_subscript() -> Option { + fn mp_subscript() -> Option; +} + +impl<'p, T> PyMappingGetItemProtocolImpl for T +where + T: PyMappingProtocol<'p>, +{ + default fn mp_subscript() -> Option { None } } -impl<'p, T> PyMappingGetItemProtocolImpl for T where T: PyMappingProtocol<'p> {} - impl PyMappingGetItemProtocolImpl for T where T: for<'p> PyMappingGetItemProtocol<'p>, @@ -195,13 +208,18 @@ where } trait PyMappingSetItemProtocolImpl { - fn mp_ass_subscript() -> Option { + fn mp_ass_subscript() -> Option; +} + +impl<'p, T> PyMappingSetItemProtocolImpl for T +where + T: PyMappingProtocol<'p>, +{ + default fn mp_ass_subscript() -> Option { None } } -impl<'p, T> PyMappingSetItemProtocolImpl for T where T: PyMappingProtocol<'p> {} - impl PyMappingSetItemProtocolImpl for T where T: for<'p> PyMappingSetItemProtocol<'p>, @@ -215,22 +233,32 @@ where /// Returns `None` if PyMappingDelItemProtocol isn't implemented, otherwise dispatches to /// `DelSetItemDispatch` trait DeplItemDipatch { - fn mp_del_subscript() -> Option { + fn mp_del_subscript() -> Option; +} + +impl<'p, T> DeplItemDipatch for T +where + T: PyMappingProtocol<'p>, +{ + default fn mp_del_subscript() -> Option { None } } -impl<'p, T> DeplItemDipatch for T where T: PyMappingProtocol<'p> {} - /// Returns `py_func_set_del` if PyMappingSetItemProtocol is implemented, otherwise `py_func_del` trait DelSetItemDispatch: Sized + for<'p> PyMappingDelItemProtocol<'p> { - fn det_set_dispatch() -> Option { + fn det_set_dispatch() -> Option; +} + +impl DelSetItemDispatch for T +where + T: Sized + for<'p> PyMappingDelItemProtocol<'p>, +{ + default fn det_set_dispatch() -> Option { py_func_del!(PyMappingDelItemProtocol, Self, __delitem__) } } -impl DelSetItemDispatch for T where T: Sized + for<'p> PyMappingDelItemProtocol<'p> {} - impl DelSetItemDispatch for T where T: for<'p> PyMappingSetItemProtocol<'p> + for<'p> PyMappingDelItemProtocol<'p>, @@ -257,27 +285,42 @@ where #[doc(hidden)] pub trait PyMappingContainsProtocolImpl { - fn __contains__() -> Option { + fn __contains__() -> Option; +} + +impl<'p, T> PyMappingContainsProtocolImpl for T +where + T: PyMappingProtocol<'p>, +{ + default fn __contains__() -> Option { None } } -impl<'p, T> PyMappingContainsProtocolImpl for T where T: PyMappingProtocol<'p> {} - #[doc(hidden)] pub trait PyMappingReversedProtocolImpl { - fn __reversed__() -> Option { + fn __reversed__() -> Option; +} + +impl<'p, T> PyMappingReversedProtocolImpl for T +where + T: PyMappingProtocol<'p>, +{ + default fn __reversed__() -> Option { None } } -impl<'p, T> PyMappingReversedProtocolImpl for T where T: PyMappingProtocol<'p> {} - #[doc(hidden)] pub trait PyMappingIterProtocolImpl { - fn __iter__() -> Option { + fn __iter__() -> Option; +} + +impl<'p, T> PyMappingIterProtocolImpl for T +where + T: PyMappingProtocol<'p>, +{ + default fn __iter__() -> Option { None } } - -impl<'p, T> PyMappingIterProtocolImpl for T where T: PyMappingProtocol<'p> {} diff --git a/src/class/number.rs b/src/class/number.rs index 2ee3a57d..796b8bad 100644 --- a/src/class/number.rs +++ b/src/class/number.rs @@ -622,10 +622,15 @@ pub trait PyNumberIndexProtocol<'p>: PyNumberProtocol<'p> { #[doc(hidden)] pub trait PyNumberProtocolImpl: PyObjectProtocolImpl { - fn methods() -> Vec { + fn methods() -> Vec; + fn tp_as_number() -> Option; +} + +impl<'p, T> PyNumberProtocolImpl for T { + default fn methods() -> Vec { Vec::new() } - fn tp_as_number() -> Option { + default fn tp_as_number() -> Option { if let Some(nb_bool) = ::nb_bool_fn() { let meth = ffi::PyNumberMethods { nb_bool: Some(nb_bool), @@ -638,8 +643,6 @@ pub trait PyNumberProtocolImpl: PyObjectProtocolImpl { } } -impl<'p, T> PyNumberProtocolImpl for T {} - impl<'p, T> PyNumberProtocolImpl for T where T: PyNumberProtocol<'p>, @@ -743,13 +746,18 @@ where } trait PyNumberAddProtocolImpl { - fn nb_add() -> Option { + fn nb_add() -> Option; +} + +impl<'p, T> PyNumberAddProtocolImpl for T +where + T: PyNumberProtocol<'p>, +{ + default fn nb_add() -> Option { None } } -impl<'p, T> PyNumberAddProtocolImpl for T where T: PyNumberProtocol<'p> {} - impl PyNumberAddProtocolImpl for T where T: for<'p> PyNumberAddProtocol<'p>, @@ -765,13 +773,18 @@ where } trait PyNumberSubProtocolImpl { - fn nb_subtract() -> Option { + fn nb_subtract() -> Option; +} + +impl<'p, T> PyNumberSubProtocolImpl for T +where + T: PyNumberProtocol<'p>, +{ + default fn nb_subtract() -> Option { None } } -impl<'p, T> PyNumberSubProtocolImpl for T where T: PyNumberProtocol<'p> {} - impl PyNumberSubProtocolImpl for T where T: for<'p> PyNumberSubProtocol<'p>, @@ -787,13 +800,18 @@ where } trait PyNumberMulProtocolImpl { - fn nb_multiply() -> Option { + fn nb_multiply() -> Option; +} + +impl<'p, T> PyNumberMulProtocolImpl for T +where + T: PyNumberProtocol<'p>, +{ + default fn nb_multiply() -> Option { None } } -impl<'p, T> PyNumberMulProtocolImpl for T where T: PyNumberProtocol<'p> {} - impl PyNumberMulProtocolImpl for T where T: for<'p> PyNumberMulProtocol<'p>, @@ -809,13 +827,18 @@ where } trait PyNumberMatmulProtocolImpl { - fn nb_matrix_multiply() -> Option { + fn nb_matrix_multiply() -> Option; +} + +impl<'p, T> PyNumberMatmulProtocolImpl for T +where + T: PyNumberProtocol<'p>, +{ + default fn nb_matrix_multiply() -> Option { None } } -impl<'p, T> PyNumberMatmulProtocolImpl for T where T: PyNumberProtocol<'p> {} - impl PyNumberMatmulProtocolImpl for T where T: for<'p> PyNumberMatmulProtocol<'p>, @@ -831,13 +854,18 @@ where } trait PyNumberTruedivProtocolImpl { - fn nb_true_divide() -> Option { + fn nb_true_divide() -> Option; +} + +impl<'p, T> PyNumberTruedivProtocolImpl for T +where + T: PyNumberProtocol<'p>, +{ + default fn nb_true_divide() -> Option { None } } -impl<'p, T> PyNumberTruedivProtocolImpl for T where T: PyNumberProtocol<'p> {} - impl PyNumberTruedivProtocolImpl for T where T: for<'p> PyNumberTruedivProtocol<'p>, @@ -853,13 +881,18 @@ where } trait PyNumberFloordivProtocolImpl { - fn nb_floor_divide() -> Option { + fn nb_floor_divide() -> Option; +} + +impl<'p, T> PyNumberFloordivProtocolImpl for T +where + T: PyNumberProtocol<'p>, +{ + default fn nb_floor_divide() -> Option { None } } -impl<'p, T> PyNumberFloordivProtocolImpl for T where T: PyNumberProtocol<'p> {} - impl PyNumberFloordivProtocolImpl for T where T: for<'p> PyNumberFloordivProtocol<'p>, @@ -875,13 +908,18 @@ where } trait PyNumberModProtocolImpl { - fn nb_remainder() -> Option { + fn nb_remainder() -> Option; +} + +impl<'p, T> PyNumberModProtocolImpl for T +where + T: PyNumberProtocol<'p>, +{ + default fn nb_remainder() -> Option { None } } -impl<'p, T> PyNumberModProtocolImpl for T where T: PyNumberProtocol<'p> {} - impl PyNumberModProtocolImpl for T where T: for<'p> PyNumberModProtocol<'p>, @@ -897,13 +935,18 @@ where } trait PyNumberDivmodProtocolImpl { - fn nb_divmod() -> Option { + fn nb_divmod() -> Option; +} + +impl<'p, T> PyNumberDivmodProtocolImpl for T +where + T: PyNumberProtocol<'p>, +{ + default fn nb_divmod() -> Option { None } } -impl<'p, T> PyNumberDivmodProtocolImpl for T where T: PyNumberProtocol<'p> {} - impl PyNumberDivmodProtocolImpl for T where T: for<'p> PyNumberDivmodProtocol<'p>, @@ -919,13 +962,18 @@ where } trait PyNumberPowProtocolImpl { - fn nb_power() -> Option { + fn nb_power() -> Option; +} + +impl<'p, T> PyNumberPowProtocolImpl for T +where + T: PyNumberProtocol<'p>, +{ + default fn nb_power() -> Option { None } } -impl<'p, T> PyNumberPowProtocolImpl for T where T: PyNumberProtocol<'p> {} - impl PyNumberPowProtocolImpl for T where T: for<'p> PyNumberPowProtocol<'p>, @@ -941,13 +989,18 @@ where } trait PyNumberLShiftProtocolImpl { - fn nb_lshift() -> Option { + fn nb_lshift() -> Option; +} + +impl<'p, T> PyNumberLShiftProtocolImpl for T +where + T: PyNumberProtocol<'p>, +{ + default fn nb_lshift() -> Option { None } } -impl<'p, T> PyNumberLShiftProtocolImpl for T where T: PyNumberProtocol<'p> {} - impl PyNumberLShiftProtocolImpl for T where T: for<'p> PyNumberLShiftProtocol<'p>, @@ -963,13 +1016,18 @@ where } trait PyNumberRShiftProtocolImpl { - fn nb_rshift() -> Option { + fn nb_rshift() -> Option; +} + +impl<'p, T> PyNumberRShiftProtocolImpl for T +where + T: PyNumberProtocol<'p>, +{ + default fn nb_rshift() -> Option { None } } -impl<'p, T> PyNumberRShiftProtocolImpl for T where T: PyNumberProtocol<'p> {} - impl PyNumberRShiftProtocolImpl for T where T: for<'p> PyNumberRShiftProtocol<'p>, @@ -985,13 +1043,18 @@ where } trait PyNumberAndProtocolImpl { - fn nb_and() -> Option { + fn nb_and() -> Option; +} + +impl<'p, T> PyNumberAndProtocolImpl for T +where + T: PyNumberProtocol<'p>, +{ + default fn nb_and() -> Option { None } } -impl<'p, T> PyNumberAndProtocolImpl for T where T: PyNumberProtocol<'p> {} - impl PyNumberAndProtocolImpl for T where T: for<'p> PyNumberAndProtocol<'p>, @@ -1007,13 +1070,18 @@ where } trait PyNumberXorProtocolImpl { - fn nb_xor() -> Option { + fn nb_xor() -> Option; +} + +impl<'p, T> PyNumberXorProtocolImpl for T +where + T: PyNumberProtocol<'p>, +{ + default fn nb_xor() -> Option { None } } -impl<'p, T> PyNumberXorProtocolImpl for T where T: PyNumberProtocol<'p> {} - impl PyNumberXorProtocolImpl for T where T: for<'p> PyNumberXorProtocol<'p>, @@ -1029,13 +1097,18 @@ where } trait PyNumberOrProtocolImpl { - fn nb_or() -> Option { + fn nb_or() -> Option; +} + +impl<'p, T> PyNumberOrProtocolImpl for T +where + T: PyNumberProtocol<'p>, +{ + default fn nb_or() -> Option { None } } -impl<'p, T> PyNumberOrProtocolImpl for T where T: PyNumberProtocol<'p> {} - impl PyNumberOrProtocolImpl for T where T: for<'p> PyNumberOrProtocol<'p>, @@ -1051,13 +1124,18 @@ where } trait PyNumberIAddProtocolImpl { - fn nb_inplace_add() -> Option { + fn nb_inplace_add() -> Option; +} + +impl<'p, T> PyNumberIAddProtocolImpl for T +where + T: PyNumberProtocol<'p>, +{ + default fn nb_inplace_add() -> Option { None } } -impl<'p, T> PyNumberIAddProtocolImpl for T where T: PyNumberProtocol<'p> {} - impl PyNumberIAddProtocolImpl for T where T: for<'p> PyNumberIAddProtocol<'p>, @@ -1068,13 +1146,18 @@ where } trait PyNumberISubProtocolImpl { - fn nb_inplace_subtract() -> Option { + fn nb_inplace_subtract() -> Option; +} + +impl<'p, T> PyNumberISubProtocolImpl for T +where + T: PyNumberProtocol<'p>, +{ + default fn nb_inplace_subtract() -> Option { None } } -impl<'p, T> PyNumberISubProtocolImpl for T where T: PyNumberProtocol<'p> {} - impl PyNumberISubProtocolImpl for T where T: for<'p> PyNumberISubProtocol<'p>, @@ -1085,13 +1168,18 @@ where } trait PyNumberIMulProtocolImpl { - fn nb_inplace_multiply() -> Option { + fn nb_inplace_multiply() -> Option; +} + +impl<'p, T> PyNumberIMulProtocolImpl for T +where + T: PyNumberProtocol<'p>, +{ + default fn nb_inplace_multiply() -> Option { None } } -impl<'p, T> PyNumberIMulProtocolImpl for T where T: PyNumberProtocol<'p> {} - impl PyNumberIMulProtocolImpl for T where T: for<'p> PyNumberIMulProtocol<'p>, @@ -1102,13 +1190,18 @@ where } trait PyNumberIMatmulProtocolImpl { - fn nb_inplace_matrix_multiply() -> Option { + fn nb_inplace_matrix_multiply() -> Option; +} + +impl<'p, T> PyNumberIMatmulProtocolImpl for T +where + T: PyNumberProtocol<'p>, +{ + default fn nb_inplace_matrix_multiply() -> Option { None } } -impl<'p, T> PyNumberIMatmulProtocolImpl for T where T: PyNumberProtocol<'p> {} - impl PyNumberIMatmulProtocolImpl for T where T: for<'p> PyNumberIMatmulProtocol<'p>, @@ -1119,13 +1212,18 @@ where } trait PyNumberITruedivProtocolImpl { - fn nb_inplace_true_divide() -> Option { + fn nb_inplace_true_divide() -> Option; +} + +impl<'p, T> PyNumberITruedivProtocolImpl for T +where + T: PyNumberProtocol<'p>, +{ + default fn nb_inplace_true_divide() -> Option { None } } -impl<'p, T> PyNumberITruedivProtocolImpl for T where T: PyNumberProtocol<'p> {} - impl PyNumberITruedivProtocolImpl for T where T: for<'p> PyNumberITruedivProtocol<'p>, @@ -1136,13 +1234,18 @@ where } trait PyNumberIFloordivProtocolImpl { - fn nb_inplace_floor_divide() -> Option { + fn nb_inplace_floor_divide() -> Option; +} + +impl<'p, T> PyNumberIFloordivProtocolImpl for T +where + T: PyNumberProtocol<'p>, +{ + default fn nb_inplace_floor_divide() -> Option { None } } -impl<'p, T> PyNumberIFloordivProtocolImpl for T where T: PyNumberProtocol<'p> {} - impl PyNumberIFloordivProtocolImpl for T where T: for<'p> PyNumberIFloordivProtocol<'p>, @@ -1153,13 +1256,18 @@ where } trait PyNumberIModProtocolImpl { - fn nb_inplace_remainder() -> Option { + fn nb_inplace_remainder() -> Option; +} + +impl<'p, T> PyNumberIModProtocolImpl for T +where + T: PyNumberProtocol<'p>, +{ + default fn nb_inplace_remainder() -> Option { None } } -impl<'p, T> PyNumberIModProtocolImpl for T where T: PyNumberProtocol<'p> {} - impl PyNumberIModProtocolImpl for T where T: for<'p> PyNumberIModProtocol<'p>, @@ -1170,13 +1278,18 @@ where } trait PyNumberIPowProtocolImpl { - fn nb_inplace_power() -> Option { + fn nb_inplace_power() -> Option; +} + +impl<'p, T> PyNumberIPowProtocolImpl for T +where + T: PyNumberProtocol<'p>, +{ + default fn nb_inplace_power() -> Option { None } } -impl<'p, T> PyNumberIPowProtocolImpl for T where T: PyNumberProtocol<'p> {} - impl PyNumberIPowProtocolImpl for T where T: for<'p> PyNumberIPowProtocol<'p>, @@ -1187,13 +1300,18 @@ where } trait PyNumberILShiftProtocolImpl { - fn nb_inplace_lshift() -> Option { + fn nb_inplace_lshift() -> Option; +} + +impl<'p, T> PyNumberILShiftProtocolImpl for T +where + T: PyNumberProtocol<'p>, +{ + default fn nb_inplace_lshift() -> Option { None } } -impl<'p, T> PyNumberILShiftProtocolImpl for T where T: PyNumberProtocol<'p> {} - impl PyNumberILShiftProtocolImpl for T where T: for<'p> PyNumberILShiftProtocol<'p>, @@ -1204,13 +1322,18 @@ where } trait PyNumberIRShiftProtocolImpl { - fn nb_inplace_rshift() -> Option { + fn nb_inplace_rshift() -> Option; +} + +impl<'p, T> PyNumberIRShiftProtocolImpl for T +where + T: PyNumberProtocol<'p>, +{ + default fn nb_inplace_rshift() -> Option { None } } -impl<'p, T> PyNumberIRShiftProtocolImpl for T where T: PyNumberProtocol<'p> {} - impl PyNumberIRShiftProtocolImpl for T where T: for<'p> PyNumberIRShiftProtocol<'p>, @@ -1221,13 +1344,18 @@ where } trait PyNumberIAndProtocolImpl { - fn nb_inplace_and() -> Option { + fn nb_inplace_and() -> Option; +} + +impl<'p, T> PyNumberIAndProtocolImpl for T +where + T: PyNumberProtocol<'p>, +{ + default fn nb_inplace_and() -> Option { None } } -impl<'p, T> PyNumberIAndProtocolImpl for T where T: PyNumberProtocol<'p> {} - impl PyNumberIAndProtocolImpl for T where T: for<'p> PyNumberIAndProtocol<'p>, @@ -1238,13 +1366,18 @@ where } trait PyNumberIXorProtocolImpl { - fn nb_inplace_xor() -> Option { + fn nb_inplace_xor() -> Option; +} + +impl<'p, T> PyNumberIXorProtocolImpl for T +where + T: PyNumberProtocol<'p>, +{ + default fn nb_inplace_xor() -> Option { None } } -impl<'p, T> PyNumberIXorProtocolImpl for T where T: PyNumberProtocol<'p> {} - impl PyNumberIXorProtocolImpl for T where T: for<'p> PyNumberIXorProtocol<'p>, @@ -1255,13 +1388,18 @@ where } trait PyNumberIOrProtocolImpl { - fn nb_inplace_or() -> Option { + fn nb_inplace_or() -> Option; +} + +impl<'p, T> PyNumberIOrProtocolImpl for T +where + T: PyNumberProtocol<'p>, +{ + default fn nb_inplace_or() -> Option { None } } -impl<'p, T> PyNumberIOrProtocolImpl for T where T: PyNumberProtocol<'p> {} - impl PyNumberIOrProtocolImpl for T where T: for<'p> PyNumberIOrProtocol<'p>, @@ -1273,13 +1411,18 @@ where #[doc(hidden)] pub trait PyNumberRAddProtocolImpl { - fn __radd__() -> Option { + fn __radd__() -> Option; +} + +impl<'p, T> PyNumberRAddProtocolImpl for T +where + T: PyNumberProtocol<'p>, +{ + default fn __radd__() -> Option { None } } -impl<'p, T> PyNumberRAddProtocolImpl for T where T: PyNumberProtocol<'p> {} - #[doc(hidden)] pub trait PyNumberRSubProtocolImpl { fn __rsub__() -> Option { @@ -1291,120 +1434,185 @@ impl<'p, T> PyNumberRSubProtocolImpl for T where T: PyNumberProtocol<'p> {} #[doc(hidden)] pub trait PyNumberRMulProtocolImpl { - fn __rmul__() -> Option { + fn __rmul__() -> Option; +} + +impl<'p, T> PyNumberRMulProtocolImpl for T +where + T: PyNumberProtocol<'p>, +{ + default fn __rmul__() -> Option { None } } -impl<'p, T> PyNumberRMulProtocolImpl for T where T: PyNumberProtocol<'p> {} - #[doc(hidden)] pub trait PyNumberRMatmulProtocolImpl { - fn __rmatmul__() -> Option { + fn __rmatmul__() -> Option; +} + +impl<'p, T> PyNumberRMatmulProtocolImpl for T +where + T: PyNumberProtocol<'p>, +{ + default fn __rmatmul__() -> Option { None } } -impl<'p, T> PyNumberRMatmulProtocolImpl for T where T: PyNumberProtocol<'p> {} - #[doc(hidden)] pub trait PyNumberRTruedivProtocolImpl { - fn __rtruediv__() -> Option { + fn __rtruediv__() -> Option; +} + +impl<'p, T> PyNumberRTruedivProtocolImpl for T +where + T: PyNumberProtocol<'p>, +{ + default fn __rtruediv__() -> Option { None } } -impl<'p, T> PyNumberRTruedivProtocolImpl for T where T: PyNumberProtocol<'p> {} - #[doc(hidden)] pub trait PyNumberRFloordivProtocolImpl { - fn __rfloordiv__() -> Option { + fn __rfloordiv__() -> Option; +} + +impl<'p, T> PyNumberRFloordivProtocolImpl for T +where + T: PyNumberProtocol<'p>, +{ + default fn __rfloordiv__() -> Option { None } } -impl<'p, T> PyNumberRFloordivProtocolImpl for T where T: PyNumberProtocol<'p> {} - #[doc(hidden)] pub trait PyNumberRModProtocolImpl { - fn __rmod__() -> Option { + fn __rmod__() -> Option; +} + +impl<'p, T> PyNumberRModProtocolImpl for T +where + T: PyNumberProtocol<'p>, +{ + default fn __rmod__() -> Option { None } } -impl<'p, T> PyNumberRModProtocolImpl for T where T: PyNumberProtocol<'p> {} - #[doc(hidden)] pub trait PyNumberRDivmodProtocolImpl { - fn __rdivmod__() -> Option { + fn __rdivmod__() -> Option; +} + +impl<'p, T> PyNumberRDivmodProtocolImpl for T +where + T: PyNumberProtocol<'p>, +{ + default fn __rdivmod__() -> Option { None } } -impl<'p, T> PyNumberRDivmodProtocolImpl for T where T: PyNumberProtocol<'p> {} - #[doc(hidden)] pub trait PyNumberRPowProtocolImpl { - fn __rpow__() -> Option { + fn __rpow__() -> Option; +} + +impl<'p, T> PyNumberRPowProtocolImpl for T +where + T: PyNumberProtocol<'p>, +{ + default fn __rpow__() -> Option { None } } -impl<'p, T> PyNumberRPowProtocolImpl for T where T: PyNumberProtocol<'p> {} - #[doc(hidden)] pub trait PyNumberRLShiftProtocolImpl { - fn __rlshift__() -> Option { + fn __rlshift__() -> Option; +} + +impl<'p, T> PyNumberRLShiftProtocolImpl for T +where + T: PyNumberProtocol<'p>, +{ + default fn __rlshift__() -> Option { None } } -impl<'p, T> PyNumberRLShiftProtocolImpl for T where T: PyNumberProtocol<'p> {} - #[doc(hidden)] pub trait PyNumberRRShiftProtocolImpl { - fn __rrshift__() -> Option { + fn __rrshift__() -> Option; +} + +impl<'p, T> PyNumberRRShiftProtocolImpl for T +where + T: PyNumberProtocol<'p>, +{ + default fn __rrshift__() -> Option { None } } -impl<'p, T> PyNumberRRShiftProtocolImpl for T where T: PyNumberProtocol<'p> {} - #[doc(hidden)] pub trait PyNumberRAndProtocolImpl { - fn __rand__() -> Option { + fn __rand__() -> Option; +} + +impl<'p, T> PyNumberRAndProtocolImpl for T +where + T: PyNumberProtocol<'p>, +{ + default fn __rand__() -> Option { None } } -impl<'p, T> PyNumberRAndProtocolImpl for T where T: PyNumberProtocol<'p> {} - #[doc(hidden)] pub trait PyNumberRXorProtocolImpl { - fn __rxor__() -> Option { + fn __rxor__() -> Option; +} + +impl<'p, T> PyNumberRXorProtocolImpl for T +where + T: PyNumberProtocol<'p>, +{ + default fn __rxor__() -> Option { None } } -impl<'p, T> PyNumberRXorProtocolImpl for T where T: PyNumberProtocol<'p> {} - #[doc(hidden)] pub trait PyNumberROrProtocolImpl { - fn __ror__() -> Option { + fn __ror__() -> Option; +} + +impl<'p, T> PyNumberROrProtocolImpl for T +where + T: PyNumberProtocol<'p>, +{ + default fn __ror__() -> Option { None } } -impl<'p, T> PyNumberROrProtocolImpl for T where T: PyNumberProtocol<'p> {} - trait PyNumberNegProtocolImpl { - fn nb_negative() -> Option { + fn nb_negative() -> Option; +} + +impl<'p, T> PyNumberNegProtocolImpl for T +where + T: PyNumberProtocol<'p>, +{ + default fn nb_negative() -> Option { None } } -impl<'p, T> PyNumberNegProtocolImpl for T where T: PyNumberProtocol<'p> {} - impl PyNumberNegProtocolImpl for T where T: for<'p> PyNumberNegProtocol<'p>, @@ -1421,13 +1629,18 @@ where } trait PyNumberPosProtocolImpl { - fn nb_positive() -> Option { + fn nb_positive() -> Option; +} + +impl<'p, T> PyNumberPosProtocolImpl for T +where + T: PyNumberProtocol<'p>, +{ + default fn nb_positive() -> Option { None } } -impl<'p, T> PyNumberPosProtocolImpl for T where T: PyNumberProtocol<'p> {} - impl PyNumberPosProtocolImpl for T where T: for<'p> PyNumberPosProtocol<'p>, @@ -1443,13 +1656,18 @@ where } trait PyNumberAbsProtocolImpl { - fn nb_absolute() -> Option { + fn nb_absolute() -> Option; +} + +impl<'p, T> PyNumberAbsProtocolImpl for T +where + T: PyNumberProtocol<'p>, +{ + default fn nb_absolute() -> Option { None } } -impl<'p, T> PyNumberAbsProtocolImpl for T where T: PyNumberProtocol<'p> {} - impl PyNumberAbsProtocolImpl for T where T: for<'p> PyNumberAbsProtocol<'p>, @@ -1465,13 +1683,18 @@ where } trait PyNumberInvertProtocolImpl { - fn nb_invert() -> Option { + fn nb_invert() -> Option; +} + +impl<'p, T> PyNumberInvertProtocolImpl for T +where + T: PyNumberProtocol<'p>, +{ + default fn nb_invert() -> Option { None } } -impl<'p, T> PyNumberInvertProtocolImpl for T where T: PyNumberProtocol<'p> {} - impl PyNumberInvertProtocolImpl for T where T: for<'p> PyNumberInvertProtocol<'p>, @@ -1487,13 +1710,18 @@ where } trait PyNumberIntProtocolImpl { - fn nb_int() -> Option { + fn nb_int() -> Option; +} + +impl<'p, T> PyNumberIntProtocolImpl for T +where + T: PyNumberProtocol<'p>, +{ + default fn nb_int() -> Option { None } } -impl<'p, T> PyNumberIntProtocolImpl for T where T: PyNumberProtocol<'p> {} - impl PyNumberIntProtocolImpl for T where T: for<'p> PyNumberIntProtocol<'p>, @@ -1509,13 +1737,18 @@ where } trait PyNumberFloatProtocolImpl { - fn nb_float() -> Option { + fn nb_float() -> Option; +} + +impl<'p, T> PyNumberFloatProtocolImpl for T +where + T: PyNumberProtocol<'p>, +{ + default fn nb_float() -> Option { None } } -impl<'p, T> PyNumberFloatProtocolImpl for T where T: PyNumberProtocol<'p> {} - impl PyNumberFloatProtocolImpl for T where T: for<'p> PyNumberFloatProtocol<'p>, @@ -1531,13 +1764,18 @@ where } trait PyNumberIndexProtocolImpl { - fn nb_index() -> Option { + fn nb_index() -> Option; +} + +impl<'p, T> PyNumberIndexProtocolImpl for T +where + T: PyNumberProtocol<'p>, +{ + default fn nb_index() -> Option { None } } -impl<'p, T> PyNumberIndexProtocolImpl for T where T: PyNumberProtocol<'p> {} - impl PyNumberIndexProtocolImpl for T where T: for<'p> PyNumberIndexProtocol<'p>, @@ -1553,17 +1791,27 @@ where } trait PyNumberComplexProtocolImpl { - fn __complex__() -> Option { + fn __complex__() -> Option; +} + +impl<'p, T> PyNumberComplexProtocolImpl for T +where + T: PyNumberProtocol<'p>, +{ + default fn __complex__() -> Option { None } } -impl<'p, T> PyNumberComplexProtocolImpl for T where T: PyNumberProtocol<'p> {} - trait PyNumberRoundProtocolImpl { - fn __round__() -> Option { + fn __round__() -> Option; +} + +impl<'p, T> PyNumberRoundProtocolImpl for T +where + T: PyNumberProtocol<'p>, +{ + default fn __round__() -> Option { None } } - -impl<'p, T> PyNumberRoundProtocolImpl for T where T: PyNumberProtocol<'p> {} diff --git a/src/class/pyasync.rs b/src/class/pyasync.rs index 9afb72c1..2536d4ce 100644 --- a/src/class/pyasync.rs +++ b/src/class/pyasync.rs @@ -91,17 +91,20 @@ pub trait PyAsyncAexitProtocol<'p>: PyAsyncProtocol<'p> { #[doc(hidden)] pub trait PyAsyncProtocolImpl { - fn tp_as_async() -> Option { + fn tp_as_async() -> Option; + fn methods() -> Vec; +} + +impl PyAsyncProtocolImpl for T { + default fn tp_as_async() -> Option { None } - fn methods() -> Vec { + default fn methods() -> Vec { Vec::new() } } -impl PyAsyncProtocolImpl for T {} - impl<'p, T> PyAsyncProtocolImpl for T where T: PyAsyncProtocol<'p>, @@ -131,13 +134,18 @@ where } trait PyAsyncAwaitProtocolImpl { - fn am_await() -> Option { + fn am_await() -> Option; +} + +impl<'p, T> PyAsyncAwaitProtocolImpl for T +where + T: PyAsyncProtocol<'p>, +{ + default fn am_await() -> Option { None } } -impl<'p, T> PyAsyncAwaitProtocolImpl for T where T: PyAsyncProtocol<'p> {} - impl PyAsyncAwaitProtocolImpl for T where T: for<'p> PyAsyncAwaitProtocol<'p>, @@ -154,13 +162,18 @@ where } trait PyAsyncAiterProtocolImpl { - fn am_aiter() -> Option { + fn am_aiter() -> Option; +} + +impl<'p, T> PyAsyncAiterProtocolImpl for T +where + T: PyAsyncProtocol<'p>, +{ + default fn am_aiter() -> Option { None } } -impl<'p, T> PyAsyncAiterProtocolImpl for T where T: PyAsyncProtocol<'p> {} - impl PyAsyncAiterProtocolImpl for T where T: for<'p> PyAsyncAiterProtocol<'p>, @@ -177,13 +190,18 @@ where } trait PyAsyncAnextProtocolImpl { - fn am_anext() -> Option { + fn am_anext() -> Option; +} + +impl<'p, T> PyAsyncAnextProtocolImpl for T +where + T: PyAsyncProtocol<'p>, +{ + default fn am_anext() -> Option { None } } -impl<'p, T> PyAsyncAnextProtocolImpl for T where T: PyAsyncProtocol<'p> {} - mod anext { use super::{PyAsyncAnextProtocol, PyAsyncAnextProtocolImpl}; use crate::callback::CallbackConverter; @@ -233,17 +251,27 @@ mod anext { } trait PyAsyncAenterProtocolImpl { - fn __aenter__() -> Option { + fn __aenter__() -> Option; +} + +impl<'p, T> PyAsyncAenterProtocolImpl for T +where + T: PyAsyncProtocol<'p>, +{ + default fn __aenter__() -> Option { None } } -impl<'p, T> PyAsyncAenterProtocolImpl for T where T: PyAsyncProtocol<'p> {} - trait PyAsyncAexitProtocolImpl { - fn __aexit__() -> Option { + fn __aexit__() -> Option; +} + +impl<'p, T> PyAsyncAexitProtocolImpl for T +where + T: PyAsyncProtocol<'p>, +{ + default fn __aexit__() -> Option { None } } - -impl<'p, T> PyAsyncAexitProtocolImpl for T where T: PyAsyncProtocol<'p> {} diff --git a/src/class/sequence.rs b/src/class/sequence.rs index 1f969722..0f9ca44b 100644 --- a/src/class/sequence.rs +++ b/src/class/sequence.rs @@ -134,13 +134,15 @@ pub trait PySequenceInplaceRepeatProtocol<'p>: PySequenceProtocol<'p> + IntoPy

Option { + fn tp_as_sequence() -> Option; +} + +impl PySequenceProtocolImpl for T { + default fn tp_as_sequence() -> Option { None } } -impl PySequenceProtocolImpl for T {} - impl<'p, T> PySequenceProtocolImpl for T where T: PySequenceProtocol<'p>, @@ -162,13 +164,18 @@ where } trait PySequenceLenProtocolImpl { - fn sq_length() -> Option { + fn sq_length() -> Option; +} + +impl<'p, T> PySequenceLenProtocolImpl for T +where + T: PySequenceProtocol<'p>, +{ + default fn sq_length() -> Option { None } } -impl<'p, T> PySequenceLenProtocolImpl for T where T: PySequenceProtocol<'p> {} - impl PySequenceLenProtocolImpl for T where T: for<'p> PySequenceLenProtocol<'p>, @@ -179,13 +186,18 @@ where } trait PySequenceGetItemProtocolImpl { - fn sq_item() -> Option { + fn sq_item() -> Option; +} + +impl<'p, T> PySequenceGetItemProtocolImpl for T +where + T: PySequenceProtocol<'p>, +{ + default fn sq_item() -> Option { None } } -impl<'p, T> PySequenceGetItemProtocolImpl for T where T: PySequenceProtocol<'p> {} - impl PySequenceGetItemProtocolImpl for T where T: for<'p> PySequenceGetItemProtocol<'p>, @@ -201,13 +213,18 @@ where } trait PySequenceSetItemProtocolImpl { - fn sq_ass_item() -> Option { + fn sq_ass_item() -> Option; +} + +impl<'p, T> PySequenceSetItemProtocolImpl for T +where + T: PySequenceProtocol<'p>, +{ + default fn sq_ass_item() -> Option { None } } -impl<'p, T> PySequenceSetItemProtocolImpl for T where T: PySequenceProtocol<'p> {} - impl PySequenceSetItemProtocolImpl for T where T: for<'p> PySequenceSetItemProtocol<'p>, @@ -275,13 +292,18 @@ mod sq_ass_item_impl { } trait DelItem { - fn del_item() -> Option { + fn del_item() -> Option; + } + + impl<'p, T> DelItem for T + where + T: PySequenceProtocol<'p>, + { + default fn del_item() -> Option { None } } - impl<'p, T> DelItem for T where T: PySequenceProtocol<'p> {} - impl DelItem for T where T: for<'p> PySequenceDelItemProtocol<'p>, @@ -321,13 +343,18 @@ mod sq_ass_item_impl { } trait DelSetItem { - fn del_set_item() -> Option { + fn del_set_item() -> Option; + } + + impl<'p, T> DelSetItem for T + where + T: PySequenceProtocol<'p>, + { + default fn del_set_item() -> Option { None } } - impl<'p, T> DelSetItem for T where T: PySequenceProtocol<'p> {} - impl DelSetItem for T where T: for<'p> PySequenceSetItemProtocol<'p> + for<'p> PySequenceDelItemProtocol<'p>, @@ -368,13 +395,18 @@ mod sq_ass_item_impl { } trait PySequenceContainsProtocolImpl { - fn sq_contains() -> Option { + fn sq_contains() -> Option; +} + +impl<'p, T> PySequenceContainsProtocolImpl for T +where + T: PySequenceProtocol<'p>, +{ + default fn sq_contains() -> Option { None } } -impl<'p, T> PySequenceContainsProtocolImpl for T where T: PySequenceProtocol<'p> {} - impl PySequenceContainsProtocolImpl for T where T: for<'p> PySequenceContainsProtocol<'p>, @@ -391,13 +423,18 @@ where } trait PySequenceConcatProtocolImpl { - fn sq_concat() -> Option { + fn sq_concat() -> Option; +} + +impl<'p, T> PySequenceConcatProtocolImpl for T +where + T: PySequenceProtocol<'p>, +{ + default fn sq_concat() -> Option { None } } -impl<'p, T> PySequenceConcatProtocolImpl for T where T: PySequenceProtocol<'p> {} - impl PySequenceConcatProtocolImpl for T where T: for<'p> PySequenceConcatProtocol<'p>, @@ -413,13 +450,18 @@ where } trait PySequenceRepeatProtocolImpl { - fn sq_repeat() -> Option { + fn sq_repeat() -> Option; +} + +impl<'p, T> PySequenceRepeatProtocolImpl for T +where + T: PySequenceProtocol<'p>, +{ + default fn sq_repeat() -> Option { None } } -impl<'p, T> PySequenceRepeatProtocolImpl for T where T: PySequenceProtocol<'p> {} - impl PySequenceRepeatProtocolImpl for T where T: for<'p> PySequenceRepeatProtocol<'p>, @@ -435,13 +477,18 @@ where } trait PySequenceInplaceConcatProtocolImpl { - fn sq_inplace_concat() -> Option { + fn sq_inplace_concat() -> Option; +} + +impl<'p, T> PySequenceInplaceConcatProtocolImpl for T +where + T: PySequenceProtocol<'p>, +{ + default fn sq_inplace_concat() -> Option { None } } -impl<'p, T> PySequenceInplaceConcatProtocolImpl for T where T: PySequenceProtocol<'p> {} - impl PySequenceInplaceConcatProtocolImpl for T where T: for<'p> PySequenceInplaceConcatProtocol<'p>, @@ -457,13 +504,18 @@ where } trait PySequenceInplaceRepeatProtocolImpl { - fn sq_inplace_repeat() -> Option { + fn sq_inplace_repeat() -> Option; +} + +impl<'p, T> PySequenceInplaceRepeatProtocolImpl for T +where + T: PySequenceProtocol<'p>, +{ + default fn sq_inplace_repeat() -> Option { None } } -impl<'p, T> PySequenceInplaceRepeatProtocolImpl for T where T: PySequenceProtocol<'p> {} - impl PySequenceInplaceRepeatProtocolImpl for T where T: for<'p> PySequenceInplaceRepeatProtocol<'p>, diff --git a/src/conversion.rs b/src/conversion.rs index a3bca117..38f436de 100644 --- a/src/conversion.rs +++ b/src/conversion.rs @@ -96,6 +96,15 @@ pub trait ToBorrowedObject: ToPyObject { /// May be more efficient than `to_object` because it does not need /// to touch any reference counts when the input object already is a Python object. fn with_borrowed_ptr(&self, py: Python, f: F) -> R + where + F: FnOnce(*mut ffi::PyObject) -> R; +} + +impl ToBorrowedObject for T +where + T: ToPyObject, +{ + default fn with_borrowed_ptr(&self, py: Python, f: F) -> R where F: FnOnce(*mut ffi::PyObject) -> R, { @@ -108,8 +117,6 @@ pub trait ToBorrowedObject: ToPyObject { } } -impl ToBorrowedObject for T where T: ToPyObject {} - impl ToBorrowedObject for T where T: ToPyObject + AsPyPointer, diff --git a/src/instance.rs b/src/instance.rs index 68bd45ec..779d1861 100644 --- a/src/instance.rs +++ b/src/instance.rs @@ -359,13 +359,18 @@ impl Py { /// Specialization workaround trait AsPyRefDispatch: AsPyPointer { - fn as_ref_dispatch(&self, _py: Python) -> &T { + fn as_ref_dispatch(&self, _py: Python) -> &T; + fn as_mut_dispatch(&mut self, _py: Python) -> &mut T; +} + +impl AsPyRefDispatch for Py { + default fn as_ref_dispatch(&self, _py: Python) -> &T { unsafe { let ptr = (self.as_ptr() as *mut u8).offset(T::OFFSET) as *mut T; ptr.as_ref().expect("Py has a null pointer") } } - fn as_mut_dispatch(&mut self, _py: Python) -> &mut T { + default fn as_mut_dispatch(&mut self, _py: Python) -> &mut T { unsafe { let ptr = (self.as_ptr() as *mut u8).offset(T::OFFSET) as *mut T; ptr.as_mut().expect("Py has a null pointer") @@ -373,8 +378,6 @@ trait AsPyRefDispatch: AsPyPointer { } } -impl AsPyRefDispatch for Py {} - impl AsPyRefDispatch for Py { fn as_ref_dispatch(&self, _py: Python) -> &T { unsafe { &*(self as *const instance::Py as *const T) } @@ -553,22 +556,10 @@ impl<'p, T: ToPyObject> AsPyPointer for ManagedPyRef<'p, T> { /// Helper trait to choose the right implementation for [ManagedPyRef] pub trait ManagedPyRefDispatch: ToPyObject { /// Optionally converts into a python object and stores the pointer to the python heap. - /// - /// Contains the case 1 impl (with to_object) to avoid a specialization error - fn to_managed_py_ref<'p>(&self, py: Python<'p>) -> ManagedPyRef<'p, Self> { - ManagedPyRef { - data: self.to_object(py).into_ptr(), - data_type: PhantomData, - _py: py, - } - } + fn to_managed_py_ref<'p>(&self, py: Python<'p>) -> ManagedPyRef<'p, Self>; /// Dispatch over a xdecref and a noop drop impl - /// - /// Contains the case 1 impl (decref) to avoid a specialization error - fn drop_impl(borrowed: &mut ManagedPyRef) { - unsafe { ffi::Py_DECREF(borrowed.data) }; - } + fn drop_impl(borrowed: &mut ManagedPyRef); } /// Case 1: It's a rust object which still needs to be converted to a python object. @@ -577,7 +568,21 @@ pub trait ManagedPyRefDispatch: ToPyObject { /// /// Note that the actual implementations are part of the trait declaration to avoid /// a specialization error -impl ManagedPyRefDispatch for T {} +impl ManagedPyRefDispatch for T { + /// Contains the case 1 impl (with to_object) to avoid a specialization error + default fn to_managed_py_ref<'p>(&self, py: Python<'p>) -> ManagedPyRef<'p, Self> { + ManagedPyRef { + data: self.to_object(py).into_ptr(), + data_type: PhantomData, + _py: py, + } + } + + /// Contains the case 1 impl (decref) to avoid a specialization error + default fn drop_impl(borrowed: &mut ManagedPyRef) { + unsafe { ffi::Py_DECREF(borrowed.data) }; + } +} /// Case 2: It's an object on the python heap, we're just storing a borrowed pointer. /// The object we're getting is an owned pointer, it might have it's own drop impl.