Skip to content

Commit 71c584a

Browse files
committed
De-specialize ToBorrowedObject
1 parent b5902af commit 71c584a

File tree

9 files changed

+28
-61
lines changed

9 files changed

+28
-61
lines changed

CHANGELOG.md

Lines changed: 1 addition & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -13,6 +13,7 @@ and this project adheres to [Semantic Versioning](http://semver.org/spec/v2.0.0.
1313
* `PyDict::from_sequence()`, equivalent to `dict([(key, val), ...])`
1414
* 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).
1515
* `PyString`, `PyUnicode`, and `PyBytes` now have an `as_bytes()` method that returns `&[u8]`.
16+
* `PyObjectProtocol::get_type_ptr()` by ijl in [#242](https://github.com/PyO3/pyo3/pull/242)
1617

1718
### Removed
1819
* Removed most entries from the prelude. The new prelude is small and clear.

pyo3-derive-backend/src/py_class.rs

Lines changed: 0 additions & 18 deletions
Original file line numberDiff line numberDiff line change
@@ -257,24 +257,6 @@ fn impl_class(
257257
}
258258
}
259259

260-
impl ::pyo3::ToBorrowedObject for #cls {
261-
fn with_borrowed_ptr<F, R>(&self, _py: ::pyo3::Python, f: F) -> R
262-
where F: FnOnce(*mut ::pyo3::ffi::PyObject) -> R
263-
{
264-
use ::pyo3::python::ToPyPointer;
265-
f(self.as_ptr())
266-
}
267-
}
268-
269-
impl<'a> ::pyo3::ToBorrowedObject for &'a mut #cls {
270-
fn with_borrowed_ptr<F, R>(&self, _py: ::pyo3::Python, f: F) -> R
271-
where F: FnOnce(*mut ::pyo3::ffi::PyObject) -> R
272-
{
273-
use ::pyo3::python::ToPyPointer;
274-
f(self.as_ptr())
275-
}
276-
}
277-
278260
#extra
279261
}
280262
}

src/conversion.rs

Lines changed: 16 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -16,6 +16,10 @@ pub trait ToPyObject {
1616
fn to_object(&self, py: Python) -> PyObject;
1717
}
1818

19+
/// This trait has two implementations: The slow one is implemented for
20+
/// all [ToPyObject] and creates a new object using [ToPyObject::to_object],
21+
/// while the fast one is only implemented for ToPyPointer (we know
22+
/// that every ToPyObject is also ToPyObject) and uses [ToPyPointer::as_ptr()]
1923
pub trait ToBorrowedObject: ToPyObject {
2024
/// Converts self into a Python object and calls the specified closure
2125
/// on the native FFI pointer underlying the Python object.
@@ -37,6 +41,18 @@ pub trait ToBorrowedObject: ToPyObject {
3741

3842
impl<T> ToBorrowedObject for T where T: ToPyObject {}
3943

44+
impl<T> ToBorrowedObject for T
45+
where
46+
T: ToPyObject + ToPyPointer,
47+
{
48+
fn with_borrowed_ptr<F, R>(&self, _py: Python, f: F) -> R
49+
where
50+
F: FnOnce(*mut ffi::PyObject) -> R,
51+
{
52+
f(self.as_ptr())
53+
}
54+
}
55+
4056
/// Conversion trait that allows various objects to be converted into `PyObject`
4157
/// by consuming original object.
4258
pub trait IntoPyObject {

src/object.rs

Lines changed: 0 additions & 10 deletions
Original file line numberDiff line numberDiff line change
@@ -258,16 +258,6 @@ impl ToPyObject for PyObject {
258258
}
259259
}
260260

261-
impl ToBorrowedObject for PyObject {
262-
#[inline]
263-
fn with_borrowed_ptr<F, R>(&self, _py: Python, f: F) -> R
264-
where
265-
F: FnOnce(*mut ffi::PyObject) -> R,
266-
{
267-
f(self.as_ptr())
268-
}
269-
}
270-
271261
impl ToPyPointer for PyObject {
272262
/// Gets the underlying FFI pointer, returns a borrowed pointer.
273263
#[inline]

src/objectprotocol.rs

Lines changed: 1 addition & 1 deletion
Original file line numberDiff line numberDiff line change
@@ -44,7 +44,7 @@ pub trait ObjectProtocol {
4444
/// On Python 2, this is equivalent to the Python expression `cmp(self, other)`.
4545
///
4646
/// On Python 3, this is equivalent to:
47-
/// ```python,ignore
47+
/// ```python
4848
/// if self == other:
4949
/// return Equal
5050
/// elif a < b:

src/typeob.rs

Lines changed: 4 additions & 3 deletions
Original file line numberDiff line numberDiff line change
@@ -72,7 +72,9 @@ pub const PY_TYPE_FLAG_DICT: usize = 1 << 3;
7272
/// Calling of `__new__` method of base class is developer's responsibility.
7373
///
7474
/// Example of custom class implementation with `__new__` method:
75-
/// ```rust,ignore
75+
/// ```
76+
/// #![feature(specialization)]
77+
///
7678
/// use pyo3::prelude::*;
7779
///
7880
/// #[pyclass]
@@ -84,8 +86,7 @@ pub const PY_TYPE_FLAG_DICT: usize = 1 << 3;
8486
/// impl MyClass {
8587
/// #[new]
8688
/// fn __new__(obj: &PyRawObject) -> PyResult<()> {
87-
/// obj.init(|token| MyClass{token: token});
88-
/// MyClass::BaseType::__new__(obj)
89+
/// obj.init(|token| MyClass { token })
8990
/// }
9091
/// }
9192
/// ```

src/types/boolobject.rs

Lines changed: 1 addition & 18 deletions
Original file line numberDiff line numberDiff line change
@@ -1,5 +1,5 @@
11
// Copyright (c) 2017-present PyO3 Project and Contributors
2-
use conversion::{IntoPyObject, PyTryFrom, ToBorrowedObject, ToPyObject};
2+
use conversion::{IntoPyObject, PyTryFrom, ToPyObject};
33
use ffi;
44
use object::PyObject;
55
use python::{Python, ToPyPointer};
@@ -44,23 +44,6 @@ impl ToPyObject for bool {
4444
}
4545
}
4646

47-
impl ToBorrowedObject for bool {
48-
#[inline]
49-
fn with_borrowed_ptr<F, R>(&self, _py: Python, f: F) -> R
50-
where
51-
F: FnOnce(*mut ffi::PyObject) -> R,
52-
{
53-
// Avoid unnecessary Py_INCREF/Py_DECREF pair
54-
f(unsafe {
55-
if *self {
56-
ffi::Py_True()
57-
} else {
58-
ffi::Py_False()
59-
}
60-
})
61-
}
62-
}
63-
6447
impl IntoPyObject for bool {
6548
#[inline]
6649
fn into_object(self, py: Python) -> PyObject {

src/types/mod.rs

Lines changed: 0 additions & 10 deletions
Original file line numberDiff line numberDiff line change
@@ -143,16 +143,6 @@ macro_rules! pyobject_native_type_convert(
143143
}
144144
}
145145

146-
impl<$($type_param,)*> $crate::ToBorrowedObject for $name
147-
{
148-
#[inline]
149-
fn with_borrowed_ptr<F, R>(&self, _py: $crate::Python, f: F) -> R
150-
where F: FnOnce(*mut $crate::ffi::PyObject) -> R
151-
{
152-
f(self.0.as_ptr())
153-
}
154-
}
155-
156146
impl<$($type_param,)*> ::std::fmt::Debug for $name {
157147
fn fmt(&self, f: &mut ::std::fmt::Formatter)
158148
-> Result<(), ::std::fmt::Error>

tests/test_gc.rs

Lines changed: 5 additions & 1 deletion
Original file line numberDiff line numberDiff line change
@@ -52,6 +52,7 @@ fn class_with_freelist() {
5252
struct TestDropCall {
5353
drop_called: Arc<AtomicBool>,
5454
}
55+
5556
impl Drop for TestDropCall {
5657
fn drop(&mut self) {
5758
self.drop_called.store(true, Ordering::Relaxed);
@@ -98,6 +99,7 @@ fn data_is_dropped() {
9899
struct ClassWithDrop {
99100
token: PyToken,
100101
}
102+
101103
impl Drop for ClassWithDrop {
102104
fn drop(&mut self) {
103105
unsafe {
@@ -188,6 +190,7 @@ fn gc_integration() {
188190
struct GCIntegration2 {
189191
token: PyToken,
190192
}
193+
191194
#[test]
192195
fn gc_integration2() {
193196
let gil = Python::acquire_gil();
@@ -200,6 +203,7 @@ fn gc_integration2() {
200203
struct WeakRefSupport {
201204
token: PyToken,
202205
}
206+
203207
#[test]
204208
fn weakref_support() {
205209
let gil = Python::acquire_gil();
@@ -237,7 +241,7 @@ impl Drop for BaseClassWithDrop {
237241
}
238242
}
239243

240-
#[pyclass(extends=BaseClassWithDrop)]
244+
#[pyclass(extends = BaseClassWithDrop)]
241245
struct SubClassWithDrop {
242246
token: PyToken,
243247
data: Option<Arc<AtomicBool>>,

0 commit comments

Comments
 (0)