clone_ref return Self

This commit is contained in:
Nikolay Kim 2017-06-03 17:42:12 -07:00
parent 57cc139c81
commit a448aa54cc
7 changed files with 17 additions and 36 deletions

View File

@ -24,9 +24,9 @@ pub fn build_ptr(cls: syn::Ident, ast: &mut syn::DeriveInput) -> Tokens {
fn park(&self) -> #ptr {
let token = _pyo3::PyObjectWithToken::token(self);
let ptr = self.clone_ref(token).into_ptr();
let ptr = self.as_ptr();
#ptr(unsafe{_pyo3::PyPtr::from_owned_ptr(ptr)})
#ptr(unsafe{_pyo3::PyPtr::from_borrowed_ptr(ptr)})
}
unsafe fn from_owned_ptr(ptr: *mut _pyo3::ffi::PyObject) -> #ptr {
std::mem::transmute(ptr)
@ -66,12 +66,7 @@ pub fn build_ptr(cls: syn::Ident, ast: &mut syn::DeriveInput) -> Tokens {
}
impl _pyo3::PyClone for #ptr {
fn clone_ref(&self, py: _pyo3::Python) -> _pyo3::PyObject {
_pyo3::PyObject::from_borrowed_ptr(py, self.as_ptr())
}
}
impl _pyo3::PyClonePtr for #ptr {
fn clone_ptr(&self, _py: _pyo3::Python) -> #ptr {
fn clone_ref(&self, _py: _pyo3::Python) -> #ptr {
#ptr(unsafe{ _pyo3::PyPtr::from_borrowed_ptr(self.as_ptr()) })
}
}
@ -93,6 +88,11 @@ pub fn build_ptr(cls: syn::Ident, ast: &mut syn::DeriveInput) -> Tokens {
self.0.into_ptr()
}
}
impl std::convert::From<#ptr> for _pyo3::PyObject {
fn from(ob: #ptr) -> Self {
unsafe{std::mem::transmute(ob)}
}
}
};
}
}

View File

@ -42,15 +42,3 @@ pub enum CompareOp {
}
pub trait PyCustomObject : PyTypeInfo + Sized {}
impl<T> ::python::PyClone for T where T: ::PyObjectWithToken + PyTypeInfo {
#[inline]
fn clone_ref(&self, py: ::Python) -> ::PyObject {
unsafe {
let offset = <T as PyTypeInfo>::offset();
let ptr = (self as *const _ as *mut u8).offset(-offset) as *mut ffi::PyObject;
::PyObject::from_borrowed_ptr(py, ptr)
}
}
}

View File

@ -299,7 +299,7 @@ impl PyErr {
/// Retrieves the exception type.
pub fn get_type(&self, py: Python) -> PyType {
self.ptype.clone_ref(py).cast_into(py).unwrap()
self.ptype.clone_ref(py)
}
/// Retrieves the exception instance for this error.

View File

@ -71,8 +71,7 @@ pub use token::{PyToken, PyObjectWithToken, Park, PythonPtr};
pub use err::{PyErr, PyResult, PyDowncastError};
pub use objects::*;
pub use objectprotocol::ObjectProtocol;
pub use python::{Python, ToPyPointer, IntoPyPointer,
PyClone, PyClonePtr, PyDowncastFrom, PyDowncastInto};
pub use python::{Python, ToPyPointer, IntoPyPointer, PyClone, PyDowncastFrom, PyDowncastInto};
pub use pythonrun::{GILGuard, GILProtected, prepare_freethreaded_python};
pub use conversion::{FromPyObject, RefFromPyObject, ToPyObject, IntoPyObject, ToPyTuple};
pub use class::{CompareOp};

View File

@ -26,8 +26,8 @@ macro_rules! pyobject_nativetype(
}
impl $crate::PyClone for $name {
fn clone_ref<'p>(&self, py: $crate::Python<'p>) -> $crate::PyObject {
$crate::PyObject::from_borrowed_ptr(py, self.as_ptr())
fn clone_ref(&self, _py: $crate::Python) -> Self {
$name(unsafe{$crate::PyPtr::from_borrowed_ptr(self.as_ptr())})
}
}
);

View File

@ -92,20 +92,14 @@ impl <T> IntoPyPointer for Option<T> where T: IntoPyPointer {
pub trait PyClone {
fn clone_ref(&self, py: Python) -> PyObject;
fn clone_ref(&self, py: Python) -> Self;
}
pub trait PyClonePtr {
fn clone_ptr(&self, py: Python) -> Self;
}
impl<T> PyClonePtr for Option<T> where T: PyClonePtr {
fn clone_ptr(&self, py: Python) -> Option<T> {
impl<T> PyClone for Option<T> where T: PyClone {
fn clone_ref(&self, py: Python) -> Option<T> {
match *self {
Some(ref p) => Some(p.clone_ptr(py)),
Some(ref p) => Some(p.clone_ref(py)),
None => None,
}
}

View File

@ -369,7 +369,7 @@ fn gc_integration() {
dropped: TestDropCall { drop_called: drop_called.clone() },
token: t}).unwrap();
*inst.as_mut(py).self_ref.borrow_mut() = inst.clone_ref(py);
*inst.as_mut(py).self_ref.borrow_mut() = inst.clone_ref(py).into();
drop(inst);
py.run("import gc; gc.collect()", None, None).unwrap();