Skip to content

Commit

Permalink
Modify PyIterProtocol to take PyRefMut
Browse files Browse the repository at this point in the history
  • Loading branch information
kngwyu committed Feb 5, 2019
1 parent 76e30b5 commit 22687c3
Show file tree
Hide file tree
Showing 9 changed files with 31 additions and 54 deletions.
10 changes: 6 additions & 4 deletions pyo3-derive-backend/src/func.rs
Original file line number Diff line number Diff line change
Expand Up @@ -492,10 +492,12 @@ fn modify_arg_ty(sig: &mut syn::MethodSig, idx: usize, decl1: &syn::FnDecl, decl
}

fn modify_self_ty(sig: &mut syn::MethodSig) {
if let syn::FnArg::SelfRef(ref mut r) = sig.decl.inputs[0] {
r.lifetime = Some(syn::parse_quote! {'p});
} else {
panic!("not supported")
match sig.decl.inputs[0] {
syn::FnArg::SelfRef(ref mut slf) => {
slf.lifetime = Some(syn::parse_quote! {'p});
}
syn::FnArg::Captured(_) => {}
_ => panic!("not supported"),
}
}

Expand Down
26 changes: 0 additions & 26 deletions pyo3-derive-backend/src/py_class.rs
Original file line number Diff line number Diff line change
Expand Up @@ -210,34 +210,8 @@ fn impl_class(
}
}

<<<<<<< HEAD
impl ::pyo3::ToPyObject for #cls {
fn to_object(&self, py: ::pyo3::Python) -> ::pyo3::PyObject {
use ::pyo3::python::ToPyPointer;
unsafe { ::pyo3::PyObject::from_borrowed_ptr(py, self.as_ptr()) }
}
}

impl ::pyo3::ToPyPointer for #cls {
fn as_ptr(&self) -> *mut ::pyo3::ffi::PyObject {
unsafe {
{self as *const _ as *mut u8}
.offset(-<#cls as ::pyo3::typeob::PyTypeInfo>::OFFSET) as *mut ::pyo3::ffi::PyObject
}
}
}

impl<'a> ::pyo3::ToPyObject for &'a mut #cls {
fn to_object(&self, py: ::pyo3::Python) -> ::pyo3::PyObject {
use ::pyo3::python::ToPyPointer;
unsafe { ::pyo3::PyObject::from_borrowed_ptr(py, self.as_ptr()) }
}
}

#inventory_impl

=======
>>>>>>> Remove ToPyPointer and so on from pyclass
#extra
}
}
Expand Down
11 changes: 6 additions & 5 deletions src/class/iter.rs
Original file line number Diff line number Diff line change
Expand Up @@ -8,6 +8,7 @@ use crate::callback::{CallbackConverter, PyObjectCallbackConverter};
use crate::conversion::IntoPyObject;
use crate::err::PyResult;
use crate::ffi;
use crate::instance::PyRefMut;
use crate::python::{IntoPyPointer, Python};
use crate::typeob::PyTypeInfo;

Expand All @@ -16,15 +17,15 @@ use crate::typeob::PyTypeInfo;
/// more information
/// `https://docs.python.org/3/c-api/typeobj.html#c.PyTypeObject.tp_iter`
#[allow(unused_variables)]
pub trait PyIterProtocol<'p>: PyTypeInfo {
fn __iter__(&'p mut self) -> Self::Result
pub trait PyIterProtocol<'p>: PyTypeInfo + Sized {
fn __iter__(slf: PyRefMut<'p, Self>) -> Self::Result
where
Self: PyIterIterProtocol<'p>,
{
unimplemented!()
}

fn __next__(&'p mut self) -> Self::Result
fn __next__(slf: PyRefMut<'p, Self>) -> Self::Result
where
Self: PyIterNextProtocol<'p>,
{
Expand Down Expand Up @@ -74,7 +75,7 @@ where
{
#[inline]
fn tp_iter() -> Option<ffi::getiterfunc> {
py_unary_func!(
py_unary_pyref_func!(
PyIterIterProtocol,
T::__iter__,
T::Success,
Expand All @@ -97,7 +98,7 @@ where
{
#[inline]
fn tp_iternext() -> Option<ffi::iternextfunc> {
py_unary_func!(
py_unary_pyref_func!(
PyIterNextProtocol,
T::__next__,
Option<T::Success>,
Expand Down
8 changes: 4 additions & 4 deletions src/class/macros.rs
Original file line number Diff line number Diff line change
Expand Up @@ -29,17 +29,17 @@ macro_rules! py_unary_func {

#[macro_export]
#[doc(hidden)]
macro_rules! py_unary_func_self {
($trait:ident, $class:ident :: $f:ident, $res_type:ty, $conv:ty) => {{
macro_rules! py_unary_pyref_func {
($trait:ident, $class:ident :: $f:ident, $res_type:ty, $conv:expr) => {{
unsafe extern "C" fn wrap<T>(slf: *mut $crate::ffi::PyObject) -> *mut $crate::ffi::PyObject
where
T: for<'p> $trait<'p>,
{
use $crate::ObjectProtocol;
use $crate::instance::PyRefMut;
let _pool = $crate::GILPool::new();
let py = $crate::Python::assume_gil_acquired();
let slf = py.mut_from_borrowed_ptr::<T>(slf);
let res = slf.$f().into();
let res = $class::$f(PyRefMut::new(slf)).into();
$crate::callback::cb_convert($conv, py, res)
}
Some(wrap::<$class>)
Expand Down
12 changes: 6 additions & 6 deletions src/instance.rs
Original file line number Diff line number Diff line change
Expand Up @@ -374,20 +374,20 @@ impl<T> std::convert::From<Py<T>> for PyObject {
}
}

impl<'a, T> std::convert::From<&'a T> for Py<T>
impl<'a, T> std::convert::From<PyRef<'a, T>> for Py<T>
where
T: ToPyPointer,
T: PyTypeInfo,
{
fn from(ob: &'a T) -> Self {
fn from(ob: PyRef<'a, T>) -> Self {
unsafe { Py::from_borrowed_ptr(ob.as_ptr()) }
}
}

impl<'a, T> std::convert::From<&'a mut T> for Py<T>
impl<'a, T> std::convert::From<PyRefMut<'a, T>> for Py<T>
where
T: ToPyPointer,
T: PyTypeInfo,
{
fn from(ob: &'a mut T) -> Self {
fn from(ob: PyRefMut<'a, T>) -> Self {
unsafe { Py::from_borrowed_ptr(ob.as_ptr()) }
}
}
Expand Down
2 changes: 1 addition & 1 deletion src/lib.rs
Original file line number Diff line number Diff line change
Expand Up @@ -133,7 +133,7 @@ pub use crate::conversion::{
ToBorrowedObject, ToPyObject,
};
pub use crate::err::{PyDowncastError, PyErr, PyErrArguments, PyErrValue, PyResult};
pub use crate::instance::{AsPyRef, Py, PyNativeType, PyObjectWithGIL};
pub use crate::instance::{AsPyRef, Py, PyNativeType, PyObjectWithGIL, PyRef, PyRefMut};
pub use crate::noargs::NoArgs;
pub use crate::object::PyObject;
pub use crate::objectprotocol::ObjectProtocol;
Expand Down
2 changes: 1 addition & 1 deletion src/prelude.rs
Original file line number Diff line number Diff line change
Expand Up @@ -12,7 +12,7 @@

pub use crate::conversion::{FromPyObject, IntoPyObject, PyTryFrom, PyTryInto, ToPyObject};
pub use crate::err::{PyErr, PyResult};
pub use crate::instance::{AsPyRef, Py};
pub use crate::instance::{AsPyRef, Py, PyRef, PyRefMut};
pub use crate::noargs::NoArgs;
pub use crate::object::PyObject;
pub use crate::objectprotocol::ObjectProtocol;
Expand Down
2 changes: 1 addition & 1 deletion src/types/tuple.rs
Original file line number Diff line number Diff line change
Expand Up @@ -133,7 +133,7 @@ impl<'a> IntoIterator for &'a PyTuple {

impl<'a> IntoPyTuple for &'a PyTuple {
fn into_tuple(self, _py: Python) -> Py<PyTuple> {
self.into()
unsafe { Py::from_borrowed_ptr(self.as_ptr()) }
}
}

Expand Down
12 changes: 6 additions & 6 deletions tests/test_dunder.rs
Original file line number Diff line number Diff line change
Expand Up @@ -51,13 +51,13 @@ struct Iterator {
}

#[pyproto]
impl PyIterProtocol for Iterator {
fn __iter__(&mut self) -> PyResult<Py<Iterator>> {
Ok(self.into())
impl<'p> PyIterProtocol for Iterator {
fn __iter__(slf: PyRefMut<Self>) -> PyResult<Py<Iterator>> {
Ok(slf.into())
}

fn __next__(&mut self) -> PyResult<Option<i32>> {
Ok(self.iter.next())
fn __next__(mut slf: PyRefMut<Self>) -> PyResult<Option<i32>> {
Ok(slf.iter.next())
}
}

Expand Down Expand Up @@ -364,7 +364,7 @@ fn context_manager() {
let gil = Python::acquire_gil();
let py = gil.python();

let c = py
let mut c = py
.init_mut(|| ContextManager { exit_called: false })
.unwrap();
py_run!(py, c, "with c as x: assert x == 42");
Expand Down

0 comments on commit 22687c3

Please sign in to comment.