From 7901890974663f27620c97f1096d771fe66adbba Mon Sep 17 00:00:00 2001 From: David Hewitt <1939362+davidhewitt@users.noreply.github.com> Date: Wed, 22 Jul 2020 00:02:11 +0100 Subject: [PATCH] Remove `FromPy` --- src/conversion.rs | 39 ++++++++------------------------------- src/err.rs | 14 +++++++------- src/lib.rs | 2 +- src/object.rs | 6 ++++++ src/prelude.rs | 2 +- src/pycell.rs | 14 +++++++------- src/types/boolobject.rs | 8 ++++---- src/types/bytes.rs | 8 ++++---- src/types/floatob.rs | 14 +++++++------- src/types/mod.rs | 8 ++++++++ src/types/set.rs | 16 ++++++++-------- src/types/string.rs | 8 ++++---- src/types/tuple.rs | 8 +------- 13 files changed, 66 insertions(+), 81 deletions(-) diff --git a/src/conversion.rs b/src/conversion.rs index beb7c026..51569e60 100644 --- a/src/conversion.rs +++ b/src/conversion.rs @@ -132,35 +132,12 @@ where } } -/// Similar to [std::convert::From], just that it requires a gil token. -pub trait FromPy: 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: Sized { /// Performs the conversion. fn into_py(self, py: Python) -> T; } -// From implies Into -impl IntoPy for T -where - U: FromPy, -{ - fn into_py(self, py: Python) -> U { - U::from_py(self, py) - } -} - -// From (and thus Into) is reflexive -impl FromPy 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 for () { + fn into_py(self, py: Python) -> PyObject { py.None() } } -impl<'a, T> FromPy<&'a T> for PyObject +impl IntoPy 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 { - fn from_py(_: (), py: Python) -> Py { - Py::from_py(PyTuple::empty(py), py) +impl IntoPy> for () { + fn into_py(self, py: Python) -> Py { + PyTuple::empty(py).into_py(py) } } diff --git a/src/err.rs b/src/err.rs index e25b045b..110c355f 100644 --- a/src/err.rs +++ b/src/err.rs @@ -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 for PyObject { - fn from_py(other: PyErr, py: Python) -> Self { - other.instance(py).into() +impl IntoPy for PyErr { + fn into_py(self, py: Python) -> PyObject { + self.instance(py).into() } } -impl FromPy for Py { - fn from_py(other: PyErr, py: Python) -> Self { - other.instance(py).into() +impl IntoPy> for PyErr { + fn into_py(self, py: Python) -> Py { + self.instance(py).into() } } diff --git a/src/lib.rs b/src/lib.rs index 4be265f5..d6ff5a47 100644 --- a/src/lib.rs +++ b/src/lib.rs @@ -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}; diff --git a/src/object.rs b/src/object.rs index a30359e9..10606859 100644 --- a/src/object.rs +++ b/src/object.rs @@ -259,6 +259,12 @@ impl PyObject { } } +impl IntoPy 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 { diff --git a/src/prelude.rs b/src/prelude.rs index b41b4b9c..343969cd 100644 --- a/src/prelude.rs +++ b/src/prelude.rs @@ -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")] diff --git a/src/pycell.rs b/src/pycell.rs index 22c6dde7..ef1acb92 100644 --- a/src/pycell.rs +++ b/src/pycell.rs @@ -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> for PyObject { - fn from_py(pyref: PyRef<'p, T>, py: Python<'_>) -> PyObject { - unsafe { PyObject::from_borrowed_ptr(py, pyref.inner.as_ptr()) } +impl IntoPy 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> for PyObject { - fn from_py(pyref: PyRefMut<'p, T>, py: Python<'_>) -> PyObject { - unsafe { PyObject::from_borrowed_ptr(py, pyref.inner.as_ptr()) } +impl IntoPy for PyRefMut<'_, T> { + fn into_py(self, py: Python) -> PyObject { + unsafe { PyObject::from_borrowed_ptr(py, self.inner.as_ptr()) } } } diff --git a/src/types/boolobject.rs b/src/types/boolobject.rs index 44078fb2..32373792 100644 --- a/src/types/boolobject.rs +++ b/src/types/boolobject.rs @@ -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 for PyObject { +impl IntoPy 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() } } diff --git a/src/types/bytes.rs b/src/types/bytes.rs index 8f8d3f76..540c5233 100644 --- a/src/types/bytes.rs +++ b/src/types/bytes.rs @@ -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> Index 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 for &'a [u8] { + fn into_py(self, py: Python) -> PyObject { + PyBytes::new(py, self).to_object(py) } } diff --git a/src/types/floatob.rs b/src/types/floatob.rs index 2512ebfd..7e65e995 100644 --- a/src/types/floatob.rs +++ b/src/types/floatob.rs @@ -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 for PyObject { - fn from_py(other: f64, py: Python) -> Self { - PyFloat::new(py, other).into() +impl IntoPy for f64 { + fn into_py(self, py: Python) -> PyObject { + PyFloat::new(py, self).into() } } @@ -67,9 +67,9 @@ impl ToPyObject for f32 { } } -impl FromPy for PyObject { - fn from_py(other: f32, py: Python) -> Self { - PyFloat::new(py, f64::from(other)).into() +impl IntoPy for f32 { + fn into_py(self, py: Python) -> PyObject { + PyFloat::new(py, f64::from(self)).into() } } diff --git a/src/types/mod.rs b/src/types/mod.rs index 797d8613..493b87bd 100644 --- a/src/types/mod.rs +++ b/src/types/mod.rs @@ -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()) } + } + } }; ); diff --git a/src/types/set.rs b/src/types/set.rs index a815f81a..333f0178 100644 --- a/src/types/set.rs +++ b/src/types/set.rs @@ -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 FromPy> for PyObject +impl IntoPy for HashSet where K: IntoPy + Eq + hash::Hash, S: hash::BuildHasher + Default, { - fn from_py(src: HashSet, 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 FromPy> for PyObject +impl IntoPy for BTreeSet where - K: IntoPy + cmp::Ord + ToPyObject, + K: IntoPy + cmp::Ord, { - fn from_py(src: BTreeSet, 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"); } } diff --git a/src/types/string.rs b/src/types/string.rs index 825cf490..c8f4a826 100644 --- a/src/types/string.rs +++ b/src/types/string.rs @@ -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 for PyObject { - fn from_py(other: String, py: Python) -> Self { - PyString::new(py, &other).into() +impl IntoPy for String { + fn into_py(self, py: Python) -> PyObject { + PyString::new(py, &self).into() } } diff --git a/src/types/tuple.rs b/src/types/tuple.rs index 046adf6e..92e7fc8c 100644 --- a/src/types/tuple.rs +++ b/src/types/tuple.rs @@ -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 { - fn from_py(tuple: &'a PyTuple, _py: Python) -> Py { - 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 {}.",