Remove ObjectProtocol::get_base and fix class.md

This commit is contained in:
kngwyu 2020-03-02 13:05:27 +09:00
parent ca6227c739
commit ee0c178fed
4 changed files with 9 additions and 27 deletions

View File

@ -44,7 +44,7 @@ and this project adheres to [Semantic Versioning](http://semver.org/spec/v2.0.0.
* `AsPyRef::as_mut/with/with_mut/into_py/into_mut_py`. [#770](https://github.com/PyO3/pyo3/pull/770)
* `PyTryFrom::try_from_mut/try_from_mut_exact/try_from_mut_unchecked`. [#770](https://github.com/PyO3/pyo3/pull/770)
* `Python::mut_from_owned_ptr/mut_from_borrowed_ptr`. [#770](https://github.com/PyO3/pyo3/pull/770)
* `ObjectProtocol::get_mut_base`. [#770](https://github.com/PyO3/pyo3/pull/770)
* `ObjectProtocol::get_base/get_mut_base`. [#770](https://github.com/PyO3/pyo3/pull/770)
## [0.8.5]

View File

@ -114,7 +114,7 @@ from Rust code (e.g., for testing it).
[`PyCell`](https://pyo3.rs/master/doc/pyo3/pycell/struct.PyCell.html) is our primary interface for that.
`PyCell<T: PyClass>` is always allocated in the Python heap, so we don't have the ownership of it.
We can get `&PyCell<T>`, not `PyCell<T>`.
We can only get `&PyCell<T>`, not `PyCell<T>`.
Thus, to mutate data behind `&PyCell` safely, we employ the
[Interior Mutability Pattern](https://doc.rust-lang.org/book/ch15-05-interior-mutability.html)
@ -242,8 +242,10 @@ baseclass of `T`.
But for more deeply nested inheritance, you have to return `PyClassInitializer<T>`
explicitly.
To get a parent class from child, use `PyRef<T>` instead of `&self`,
To get a parent class from a child, use `PyRef<T>` instead of `&self`,
or `PyRefMut<T>` instead of `&mut self`.
Then you can access a parent class by `self_.as_ref()` as `&Self::BaseClass`,
or by `self_.into_super()` as `PyRef<Self::BaseClass>`.
```rust
# use pyo3::prelude::*;
@ -311,12 +313,6 @@ impl SubSubClass {
# pyo3::py_run!(py, subsub, "assert subsub.method3() == 3000")
```
To access the super class, you can use either of these two ways:
- Use `self_: &PyCell<Self>` instead of `self`, and call `get_super()`
- `ObjectProtocol::get_base`
We recommend `PyCell` here, since it makes the context much clearer.
If `SubClass` does not provide a baseclass initialization, the compilation fails.
```compile_fail
# use pyo3::prelude::*;

View File

@ -29,9 +29,9 @@ pub enum FnType {
FnCall,
FnClass,
FnStatic,
// self_: &PyCell<Self>,
/// For methods taht have `self_: &PyCell<Self>` instead of self receiver
PySelfRef(syn::TypeReference),
// self_: PyRef<Self> or PyRefMut<Self>
/// For methods taht have `self_: PyRef<Self>` or `PyRefMut<Self>` instead of self receiver
PySelfPath(syn::TypePath),
}

View File

@ -5,8 +5,8 @@ use crate::err::{self, PyDowncastError, PyErr, PyResult};
use crate::exceptions::TypeError;
use crate::types::{PyAny, PyDict, PyIterator, PyString, PyTuple, PyType};
use crate::{
ffi, AsPyPointer, FromPyObject, FromPyPointer, IntoPy, IntoPyPointer, Py, PyNativeType,
PyObject, PyTryFrom, PyTypeInfo, Python, ToBorrowedObject, ToPyObject,
ffi, AsPyPointer, FromPyObject, IntoPy, IntoPyPointer, Py, PyNativeType, PyObject, PyTryFrom,
Python, ToBorrowedObject, ToPyObject,
};
use std::cmp::Ordering;
use std::os::raw::c_int;
@ -170,12 +170,6 @@ pub trait ObjectProtocol {
/// Gets the Python type pointer for this object.
fn get_type_ptr(&self) -> *mut ffi::PyTypeObject;
/// Gets the Python base object for this object.
fn get_base(&self) -> &<Self as PyTypeInfo>::BaseType
where
Self: PyTypeInfo,
<Self as PyTypeInfo>::BaseType: for<'py> FromPyPointer<'py>;
/// Casts the PyObject to a concrete Python object type.
fn cast_as<'a, D>(&'a self) -> Result<&'a D, PyDowncastError>
where
@ -445,14 +439,6 @@ where
unsafe { (*self.as_ptr()).ob_type }
}
fn get_base(&self) -> &<Self as PyTypeInfo>::BaseType
where
Self: PyTypeInfo,
<Self as PyTypeInfo>::BaseType: for<'py> FromPyPointer<'py>,
{
unsafe { self.py().from_borrowed_ptr(self.as_ptr()) }
}
fn cast_as<'a, D>(&'a self) -> Result<&'a D, PyDowncastError>
where
D: PyTryFrom<'a>,