De-specialize ToBorrowedObject

This commit is contained in:
konstin 2018-10-03 21:04:24 +02:00
parent b5902af548
commit 71c584a110
9 changed files with 28 additions and 61 deletions

View File

@ -13,6 +13,7 @@ and this project adheres to [Semantic Versioning](http://semver.org/spec/v2.0.0.
* `PyDict::from_sequence()`, equivalent to `dict([(key, val), ...])`
* Bindings for the `datetime` standard library types: `PyDate`, `PyTime`, `PyDateTime`, `PyTzInfo`, `PyDelta` with associated `ffi` types, by pganssle [#200](https://github.com/PyO3/pyo3/pull/200).
* `PyString`, `PyUnicode`, and `PyBytes` now have an `as_bytes()` method that returns `&[u8]`.
* `PyObjectProtocol::get_type_ptr()` by ijl in [#242](https://github.com/PyO3/pyo3/pull/242)
### Removed
* Removed most entries from the prelude. The new prelude is small and clear.

View File

@ -257,24 +257,6 @@ fn impl_class(
}
}
impl ::pyo3::ToBorrowedObject for #cls {
fn with_borrowed_ptr<F, R>(&self, _py: ::pyo3::Python, f: F) -> R
where F: FnOnce(*mut ::pyo3::ffi::PyObject) -> R
{
use ::pyo3::python::ToPyPointer;
f(self.as_ptr())
}
}
impl<'a> ::pyo3::ToBorrowedObject for &'a mut #cls {
fn with_borrowed_ptr<F, R>(&self, _py: ::pyo3::Python, f: F) -> R
where F: FnOnce(*mut ::pyo3::ffi::PyObject) -> R
{
use ::pyo3::python::ToPyPointer;
f(self.as_ptr())
}
}
#extra
}
}

View File

@ -16,6 +16,10 @@ pub trait ToPyObject {
fn to_object(&self, py: Python) -> PyObject;
}
/// This trait has two implementations: The slow one is implemented for
/// all [ToPyObject] and creates a new object using [ToPyObject::to_object],
/// while the fast one is only implemented for ToPyPointer (we know
/// that every ToPyObject is also ToPyObject) and uses [ToPyPointer::as_ptr()]
pub trait ToBorrowedObject: ToPyObject {
/// Converts self into a Python object and calls the specified closure
/// on the native FFI pointer underlying the Python object.
@ -37,6 +41,18 @@ pub trait ToBorrowedObject: ToPyObject {
impl<T> ToBorrowedObject for T where T: ToPyObject {}
impl<T> ToBorrowedObject for T
where
T: ToPyObject + ToPyPointer,
{
fn with_borrowed_ptr<F, R>(&self, _py: Python, f: F) -> R
where
F: FnOnce(*mut ffi::PyObject) -> R,
{
f(self.as_ptr())
}
}
/// Conversion trait that allows various objects to be converted into `PyObject`
/// by consuming original object.
pub trait IntoPyObject {

View File

@ -258,16 +258,6 @@ impl ToPyObject for PyObject {
}
}
impl ToBorrowedObject for PyObject {
#[inline]
fn with_borrowed_ptr<F, R>(&self, _py: Python, f: F) -> R
where
F: FnOnce(*mut ffi::PyObject) -> R,
{
f(self.as_ptr())
}
}
impl ToPyPointer for PyObject {
/// Gets the underlying FFI pointer, returns a borrowed pointer.
#[inline]

View File

@ -44,7 +44,7 @@ pub trait ObjectProtocol {
/// On Python 2, this is equivalent to the Python expression `cmp(self, other)`.
///
/// On Python 3, this is equivalent to:
/// ```python,ignore
/// ```python
/// if self == other:
/// return Equal
/// elif a < b:

View File

@ -72,7 +72,9 @@ pub const PY_TYPE_FLAG_DICT: usize = 1 << 3;
/// Calling of `__new__` method of base class is developer's responsibility.
///
/// Example of custom class implementation with `__new__` method:
/// ```rust,ignore
/// ```
/// #![feature(specialization)]
///
/// use pyo3::prelude::*;
///
/// #[pyclass]
@ -84,8 +86,7 @@ pub const PY_TYPE_FLAG_DICT: usize = 1 << 3;
/// impl MyClass {
/// #[new]
/// fn __new__(obj: &PyRawObject) -> PyResult<()> {
/// obj.init(|token| MyClass{token: token});
/// MyClass::BaseType::__new__(obj)
/// obj.init(|token| MyClass { token })
/// }
/// }
/// ```

View File

@ -1,5 +1,5 @@
// Copyright (c) 2017-present PyO3 Project and Contributors
use conversion::{IntoPyObject, PyTryFrom, ToBorrowedObject, ToPyObject};
use conversion::{IntoPyObject, PyTryFrom, ToPyObject};
use ffi;
use object::PyObject;
use python::{Python, ToPyPointer};
@ -44,23 +44,6 @@ impl ToPyObject for bool {
}
}
impl ToBorrowedObject for bool {
#[inline]
fn with_borrowed_ptr<F, R>(&self, _py: Python, f: F) -> R
where
F: FnOnce(*mut ffi::PyObject) -> R,
{
// Avoid unnecessary Py_INCREF/Py_DECREF pair
f(unsafe {
if *self {
ffi::Py_True()
} else {
ffi::Py_False()
}
})
}
}
impl IntoPyObject for bool {
#[inline]
fn into_object(self, py: Python) -> PyObject {

View File

@ -143,16 +143,6 @@ macro_rules! pyobject_native_type_convert(
}
}
impl<$($type_param,)*> $crate::ToBorrowedObject for $name
{
#[inline]
fn with_borrowed_ptr<F, R>(&self, _py: $crate::Python, f: F) -> R
where F: FnOnce(*mut $crate::ffi::PyObject) -> R
{
f(self.0.as_ptr())
}
}
impl<$($type_param,)*> ::std::fmt::Debug for $name {
fn fmt(&self, f: &mut ::std::fmt::Formatter)
-> Result<(), ::std::fmt::Error>

View File

@ -52,6 +52,7 @@ fn class_with_freelist() {
struct TestDropCall {
drop_called: Arc<AtomicBool>,
}
impl Drop for TestDropCall {
fn drop(&mut self) {
self.drop_called.store(true, Ordering::Relaxed);
@ -98,6 +99,7 @@ fn data_is_dropped() {
struct ClassWithDrop {
token: PyToken,
}
impl Drop for ClassWithDrop {
fn drop(&mut self) {
unsafe {
@ -188,6 +190,7 @@ fn gc_integration() {
struct GCIntegration2 {
token: PyToken,
}
#[test]
fn gc_integration2() {
let gil = Python::acquire_gil();
@ -200,6 +203,7 @@ fn gc_integration2() {
struct WeakRefSupport {
token: PyToken,
}
#[test]
fn weakref_support() {
let gil = Python::acquire_gil();