Skip to content

Commit

Permalink
FromPy implies IntoPy
Browse files Browse the repository at this point in the history
  • Loading branch information
konstin committed Aug 24, 2019
1 parent 30e82a3 commit c06ae70
Show file tree
Hide file tree
Showing 6 changed files with 23 additions and 31 deletions.
4 changes: 2 additions & 2 deletions src/conversion.rs
Original file line number Diff line number Diff line change
Expand Up @@ -221,8 +221,8 @@ impl ToPyObject for () {
}
}

impl IntoPy<PyObject> for () {
fn into_py(self, py: Python) -> PyObject {
impl FromPy<()> for PyObject {
fn from_py(_: (), py: Python) -> Self {
py.None()
}
}
Expand Down
8 changes: 4 additions & 4 deletions src/err.rs
Original file line number Diff line number Diff line change
@@ -1,6 +1,5 @@
// Copyright (c) 2017-present PyO3 Project and Contributors

use crate::ffi;
use crate::instance::Py;
use crate::object::PyObject;
use crate::type_object::PyTypeObject;
Expand All @@ -9,6 +8,7 @@ use crate::AsPyPointer;
use crate::IntoPyPointer;
use crate::Python;
use crate::{exceptions, IntoPy};
use crate::{ffi, FromPy};
use crate::{ToBorrowedObject, ToPyObject};
use libc::c_int;
use std::ffi::CString;
Expand Down Expand Up @@ -357,9 +357,9 @@ impl std::fmt::Debug for PyErr {
}
}

impl IntoPy<PyObject> for PyErr {
fn into_py(self, py: Python) -> PyObject {
self.instance(py)
impl FromPy<PyErr> for PyObject {
fn from_py(other: PyErr, py: Python) -> Self {
other.instance(py)
}
}

Expand Down
7 changes: 0 additions & 7 deletions src/object.rs
Original file line number Diff line number Diff line change
Expand Up @@ -297,13 +297,6 @@ impl PartialEq for PyObject {
}
}

impl IntoPy<PyObject> for PyObject {
#[inline]
fn into_py(self, _py: Python) -> PyObject {
self
}
}

impl<'a> FromPyObject<'a> for PyObject {
#[inline]
/// Extracts `Self` from the source `PyObject`.
Expand Down
10 changes: 5 additions & 5 deletions src/types/boolobject.rs
Original file line number Diff line number Diff line change
@@ -1,11 +1,11 @@
// Copyright (c) 2017-present PyO3 Project and Contributors
use crate::ffi;
use crate::object::PyObject;
use crate::types::PyAny;
use crate::AsPyPointer;
use crate::FromPyObject;
use crate::PyResult;
use crate::Python;
use crate::{ffi, IntoPy};
use crate::{AsPyPointer, FromPy};
use crate::{PyTryFrom, ToPyObject};

/// Represents a Python `bool`.
Expand Down Expand Up @@ -45,10 +45,10 @@ impl ToPyObject for bool {
}
}

impl IntoPy<PyObject> for bool {
impl FromPy<bool> for PyObject {
#[inline]
fn into_py(self, py: Python) -> PyObject {
PyBool::new(py, self).into()
fn from_py(other: bool, py: Python) -> Self {
PyBool::new(py, other).into()
}
}

Expand Down
16 changes: 8 additions & 8 deletions src/types/floatob.rs
Original file line number Diff line number Diff line change
Expand Up @@ -3,16 +3,16 @@
// based on Daniel Grunwald's https://github.com/dgrunwald/rust-cpython

use crate::err::PyErr;
use crate::ffi;
use crate::instance::PyNativeType;
use crate::object::PyObject;
use crate::objectprotocol::ObjectProtocol;
use crate::types::PyAny;
use crate::AsPyPointer;
use crate::FromPyObject;
use crate::PyResult;
use crate::Python;
use crate::ToPyObject;
use crate::{ffi, IntoPy};
use crate::{AsPyPointer, FromPy};
use std::os::raw::c_double;

/// Represents a Python `float` object.
Expand Down Expand Up @@ -44,9 +44,9 @@ impl ToPyObject for f64 {
}
}

impl IntoPy<PyObject> for f64 {
fn into_py(self, py: Python) -> PyObject {
PyFloat::new(py, self).into()
impl FromPy<f64> for PyObject {
fn from_py(other: f64, py: Python) -> Self {
PyFloat::new(py, other).into()
}
}

Expand All @@ -70,9 +70,9 @@ impl ToPyObject for f32 {
}
}

impl IntoPy<PyObject> for f32 {
fn into_py(self, py: Python) -> PyObject {
PyFloat::new(py, f64::from(self)).into()
impl FromPy<f32> for PyObject {
fn from_py(other: f32, py: Python) -> Self {
PyFloat::new(py, f64::from(other)).into()
}
}

Expand Down
9 changes: 4 additions & 5 deletions src/types/string.rs
Original file line number Diff line number Diff line change
Expand Up @@ -3,7 +3,7 @@
use crate::conversion::FromPyObject;
use crate::conversion::{PyTryFrom, ToPyObject};
use crate::err::{PyErr, PyResult};
use crate::ffi;
use crate::{ffi, FromPy};
use crate::instance::PyNativeType;
use crate::object::PyObject;
use crate::types::PyAny;
Expand Down Expand Up @@ -169,10 +169,9 @@ impl ToPyObject for String {
}
}

impl IntoPy<PyObject> for String {
#[inline]
fn into_py(self, py: Python) -> PyObject {
PyString::new(py, &self).into()
impl FromPy<String> for PyObject {
fn from_py(other: String, py: Python) -> Self {
PyString::new(py, &other).into()
}
}

Expand Down

0 comments on commit c06ae70

Please sign in to comment.