// Copyright (c) 2017-present PyO3 Project and Contributors use std::rc::Rc; use std::marker::PhantomData; use ffi; use err::PyResult; use python::{Python, IntoPyPointer}; use typeob::{PyTypeInfo, PyObjectAlloc}; pub struct PyToken(PhantomData>); impl PyToken { pub fn token<'p>(&'p self) -> Python<'p> { unsafe { Python::assume_gil_acquired() } } } /// Create new python object and move T instance under python management #[inline] pub fn with<'p, T, F>(py: Python<'p>, f: F) -> PyResult where F: FnOnce(PyToken) -> T, T: Park + PyTypeInfo + PyObjectAlloc { let ob = f(PyToken(PhantomData)); let ob = unsafe { let ob = try!(::alloc(py, ob)); T::from_owned_ptr(ob) }; Ok(ob) } pub trait PyObjectWithToken : Sized { fn token<'p>(&'p self) -> Python<'p>; } pub trait Park : Sized { type ParkTarget: PythonPtr + IntoPyPointer; fn park(&self) -> Self::ParkTarget; unsafe fn from_owned_ptr(*mut ffi::PyObject) -> Self::ParkTarget; unsafe fn from_borrowed_ptr(*mut ffi::PyObject) -> Self::ParkTarget; } pub trait PythonPtr : Sized { fn as_ref(&self, py: Python) -> &T; fn as_mut(&self, py: Python) -> &mut T; }