Remove `FromPy`

This commit is contained in:
David Hewitt 2020-07-22 00:02:11 +01:00
parent ff9cde46e1
commit 7901890974
13 changed files with 66 additions and 81 deletions

View File

@ -132,35 +132,12 @@ where
}
}
/// Similar to [std::convert::From], just that it requires a gil token.
pub trait FromPy<T>: Sized {
/// Performs the conversion.
fn from_py(_: T, py: Python) -> Self;
}
/// Similar to [std::convert::Into], just that it requires a gil token.
pub trait IntoPy<T>: Sized {
/// Performs the conversion.
fn into_py(self, py: Python) -> T;
}
// From implies Into
impl<T, U> IntoPy<U> for T
where
U: FromPy<T>,
{
fn into_py(self, py: Python) -> U {
U::from_py(self, py)
}
}
// From (and thus Into) is reflexive
impl<T> FromPy<T> for T {
fn from_py(t: T, _: Python) -> T {
t
}
}
/// `FromPyObject` is implemented by various types that can be extracted from
/// a Python object reference.
///
@ -234,19 +211,19 @@ impl ToPyObject for () {
}
}
impl FromPy<()> for PyObject {
fn from_py(_: (), py: Python) -> Self {
impl IntoPy<PyObject> for () {
fn into_py(self, py: Python) -> PyObject {
py.None()
}
}
impl<'a, T> FromPy<&'a T> for PyObject
impl<T> IntoPy<PyObject> for &'_ T
where
T: AsPyPointer,
{
#[inline]
fn from_py(other: &'a T, py: Python) -> PyObject {
unsafe { PyObject::from_borrowed_ptr(py, other.as_ptr()) }
fn into_py(self, py: Python) -> PyObject {
unsafe { PyObject::from_borrowed_ptr(py, self.as_ptr()) }
}
}
@ -404,9 +381,9 @@ where
}
/// Converts `()` to an empty Python tuple.
impl FromPy<()> for Py<PyTuple> {
fn from_py(_: (), py: Python) -> Py<PyTuple> {
Py::from_py(PyTuple::empty(py), py)
impl IntoPy<Py<PyTuple>> for () {
fn into_py(self, py: Python) -> Py<PyTuple> {
PyTuple::empty(py).into_py(py)
}
}

View File

@ -6,7 +6,7 @@ use crate::type_object::PyTypeObject;
use crate::types::PyType;
use crate::{exceptions, ffi};
use crate::{
AsPyPointer, FromPy, FromPyPointer, IntoPy, IntoPyPointer, Py, PyAny, PyNativeType, PyObject,
AsPyPointer, FromPyPointer, IntoPy, IntoPyPointer, Py, PyAny, PyNativeType, PyObject,
Python, ToBorrowedObject, ToPyObject,
};
use libc::c_int;
@ -438,15 +438,15 @@ impl std::fmt::Debug for PyErr {
}
}
impl FromPy<PyErr> for PyObject {
fn from_py(other: PyErr, py: Python) -> Self {
other.instance(py).into()
impl IntoPy<PyObject> for PyErr {
fn into_py(self, py: Python) -> PyObject {
self.instance(py).into()
}
}
impl FromPy<PyErr> for Py<exceptions::PyBaseException> {
fn from_py(other: PyErr, py: Python) -> Self {
other.instance(py).into()
impl IntoPy<Py<exceptions::PyBaseException>> for PyErr {
fn into_py(self, py: Python) -> Py<exceptions::PyBaseException> {
self.instance(py).into()
}
}

View File

@ -135,7 +135,7 @@
pub use crate::class::*;
pub use crate::conversion::{
AsPyPointer, FromPy, FromPyObject, FromPyPointer, IntoPy, IntoPyPointer, PyTryFrom, PyTryInto,
AsPyPointer, FromPyObject, FromPyPointer, IntoPy, IntoPyPointer, PyTryFrom, PyTryInto,
ToBorrowedObject, ToPyObject,
};
pub use crate::err::{PyDowncastError, PyErr, PyErrArguments, PyErrValue, PyResult};

View File

@ -259,6 +259,12 @@ impl PyObject {
}
}
impl IntoPy<PyObject> for PyObject {
fn into_py(self, _py: Python) -> PyObject {
self
}
}
impl AsPyRef for PyObject {
type Target = PyAny;
fn as_ref<'p>(&'p self, _py: Python<'p>) -> &'p PyAny {

View File

@ -17,7 +17,7 @@ pub use crate::object::PyObject;
pub use crate::pycell::{PyCell, PyRef, PyRefMut};
pub use crate::pyclass_init::PyClassInitializer;
pub use crate::python::Python;
pub use crate::{FromPy, FromPyObject, IntoPy, IntoPyPointer, PyTryFrom, PyTryInto, ToPyObject};
pub use crate::{FromPyObject, IntoPy, IntoPyPointer, PyTryFrom, PyTryInto, ToPyObject};
// PyModule is only part of the prelude because we need it for the pymodule function
pub use crate::types::{PyAny, PyModule};
#[cfg(feature = "macros")]

View File

@ -6,7 +6,7 @@ use crate::pyclass_init::PyClassInitializer;
use crate::pyclass_slots::{PyClassDict, PyClassWeakRef};
use crate::type_object::{PyBorrowFlagLayout, PyLayout, PySizedLayout, PyTypeInfo};
use crate::types::PyAny;
use crate::{ffi, FromPy, PyErr, PyNativeType, PyObject, PyResult, Python};
use crate::{ffi, IntoPy, PyErr, PyNativeType, PyObject, PyResult, Python};
use std::cell::{Cell, UnsafeCell};
use std::mem::ManuallyDrop;
use std::ops::{Deref, DerefMut};
@ -572,9 +572,9 @@ impl<'p, T: PyClass> Drop for PyRef<'p, T> {
}
}
impl<'p, T: PyClass> FromPy<PyRef<'p, T>> for PyObject {
fn from_py(pyref: PyRef<'p, T>, py: Python<'_>) -> PyObject {
unsafe { PyObject::from_borrowed_ptr(py, pyref.inner.as_ptr()) }
impl<T: PyClass> IntoPy<PyObject> for PyRef<'_, T> {
fn into_py(self, py: Python<'_>) -> PyObject {
unsafe { PyObject::from_borrowed_ptr(py, self.inner.as_ptr()) }
}
}
@ -670,9 +670,9 @@ impl<'p, T: PyClass> Drop for PyRefMut<'p, T> {
}
}
impl<'p, T: PyClass> FromPy<PyRefMut<'p, T>> for PyObject {
fn from_py(pyref: PyRefMut<'p, T>, py: Python<'_>) -> PyObject {
unsafe { PyObject::from_borrowed_ptr(py, pyref.inner.as_ptr()) }
impl<T: PyClass> IntoPy<PyObject> for PyRefMut<'_, T> {
fn into_py(self, py: Python) -> PyObject {
unsafe { PyObject::from_borrowed_ptr(py, self.inner.as_ptr()) }
}
}

View File

@ -1,6 +1,6 @@
// Copyright (c) 2017-present PyO3 Project and Contributors
use crate::{
ffi, AsPyPointer, FromPy, FromPyObject, PyAny, PyObject, PyResult, PyTryFrom, Python,
ffi, AsPyPointer, IntoPy, FromPyObject, PyAny, PyObject, PyResult, PyTryFrom, Python,
ToPyObject,
};
@ -41,10 +41,10 @@ impl ToPyObject for bool {
}
}
impl FromPy<bool> for PyObject {
impl IntoPy<PyObject> for bool {
#[inline]
fn from_py(other: bool, py: Python) -> Self {
PyBool::new(py, other).into()
fn into_py(self, py: Python) -> PyObject {
PyBool::new(py, self).into()
}
}

View File

@ -1,5 +1,5 @@
use crate::{
ffi, AsPyPointer, FromPy, FromPyObject, PyAny, PyObject, PyResult, PyTryFrom, Python,
ffi, AsPyPointer, IntoPy, FromPyObject, PyAny, PyObject, PyResult, PyTryFrom, Python,
ToPyObject,
};
use std::ops::Index;
@ -93,9 +93,9 @@ impl<I: SliceIndex<[u8]>> Index<I> for PyBytes {
}
}
impl<'a> FromPy<&'a [u8]> for PyObject {
fn from_py(other: &'a [u8], py: Python) -> Self {
PyBytes::new(py, other).to_object(py)
impl<'a> IntoPy<PyObject> for &'a [u8] {
fn into_py(self, py: Python) -> PyObject {
PyBytes::new(py, self).to_object(py)
}
}

View File

@ -2,7 +2,7 @@
//
// based on Daniel Grunwald's https://github.com/dgrunwald/rust-cpython
use crate::{
ffi, AsPyPointer, FromPy, FromPyObject, PyAny, PyErr, PyNativeType, PyObject, PyResult, Python,
ffi, AsPyPointer, IntoPy, FromPyObject, PyAny, PyErr, PyNativeType, PyObject, PyResult, Python,
ToPyObject,
};
use std::os::raw::c_double;
@ -41,9 +41,9 @@ impl ToPyObject for f64 {
}
}
impl FromPy<f64> for PyObject {
fn from_py(other: f64, py: Python) -> Self {
PyFloat::new(py, other).into()
impl IntoPy<PyObject> for f64 {
fn into_py(self, py: Python) -> PyObject {
PyFloat::new(py, self).into()
}
}
@ -67,9 +67,9 @@ impl ToPyObject for f32 {
}
}
impl FromPy<f32> for PyObject {
fn from_py(other: f32, py: Python) -> Self {
PyFloat::new(py, f64::from(other)).into()
impl IntoPy<PyObject> for f32 {
fn into_py(self, py: Python) -> PyObject {
PyFloat::new(py, f64::from(self)).into()
}
}

View File

@ -62,6 +62,14 @@ macro_rules! pyobject_native_type_named (
self.as_ptr() == o.as_ptr()
}
}
impl<$($type_param,)*> $crate::IntoPy<$crate::Py<$name>> for &'_ $name {
#[inline]
fn into_py(self, py: $crate::Python) -> $crate::Py<$name> {
use $crate::IntoPyPointer;
unsafe { $crate::Py::from_owned_ptr(py, self.into_ptr()) }
}
}
};
);

View File

@ -3,7 +3,7 @@
use crate::err::{self, PyErr, PyResult};
use crate::{
ffi, AsPyPointer, FromPy, FromPyObject, IntoPy, PyAny, PyNativeType, PyObject, Python,
ffi, AsPyPointer, FromPyObject, IntoPy, PyAny, PyNativeType, PyObject, Python,
ToBorrowedObject, ToPyObject,
};
use std::cmp;
@ -185,15 +185,15 @@ where
}
}
impl<K, S> FromPy<HashSet<K, S>> for PyObject
impl<K, S> IntoPy<PyObject> for HashSet<K, S>
where
K: IntoPy<PyObject> + Eq + hash::Hash,
S: hash::BuildHasher + Default,
{
fn from_py(src: HashSet<K, S>, py: Python) -> Self {
fn into_py(self, py: Python) -> PyObject {
let set = PySet::empty(py).expect("Failed to construct empty set");
{
for val in src {
for val in self {
set.add(val.into_py(py)).expect("Failed to add to set");
}
}
@ -212,14 +212,14 @@ where
}
}
impl<K> FromPy<BTreeSet<K>> for PyObject
impl<K> IntoPy<PyObject> for BTreeSet<K>
where
K: IntoPy<PyObject> + cmp::Ord + ToPyObject,
K: IntoPy<PyObject> + cmp::Ord,
{
fn from_py(src: BTreeSet<K>, py: Python) -> Self {
fn into_py(self, py: Python) -> PyObject {
let set = PySet::empty(py).expect("Failed to construct empty set");
{
for val in src {
for val in self {
set.add(val.into_py(py)).expect("Failed to add to set");
}
}

View File

@ -2,7 +2,7 @@
use crate::types::PyBytes;
use crate::{
ffi, AsPyPointer, FromPy, FromPyObject, IntoPy, PyAny, PyErr, PyNativeType, PyObject, PyResult,
ffi, AsPyPointer, FromPyObject, IntoPy, PyAny, PyErr, PyNativeType, PyObject, PyResult,
PyTryFrom, Python, ToPyObject,
};
use std::borrow::Cow;
@ -112,9 +112,9 @@ impl ToPyObject for String {
}
}
impl FromPy<String> for PyObject {
fn from_py(other: String, py: Python) -> Self {
PyString::new(py, &other).into()
impl IntoPy<PyObject> for String {
fn into_py(self, py: Python) -> PyObject {
PyString::new(py, &self).into()
}
}

View File

@ -2,7 +2,7 @@
use crate::ffi::{self, Py_ssize_t};
use crate::{
exceptions, AsPyPointer, FromPy, FromPyObject, IntoPy, IntoPyPointer, Py, PyAny, PyErr,
exceptions, AsPyPointer, FromPyObject, IntoPy, IntoPyPointer, Py, PyAny, PyErr,
PyNativeType, PyObject, PyResult, PyTryFrom, Python, ToPyObject,
};
use std::slice;
@ -129,12 +129,6 @@ impl<'a> IntoIterator for &'a PyTuple {
}
}
impl<'a> FromPy<&'a PyTuple> for Py<PyTuple> {
fn from_py(tuple: &'a PyTuple, _py: Python) -> Py<PyTuple> {
unsafe { Py::from_borrowed_ptr(tuple.py(), tuple.as_ptr()) }
}
}
fn wrong_tuple_length(t: &PyTuple, expected_length: usize) -> PyErr {
let msg = format!(
"Expected tuple of length {}, but got tuple of length {}.",