2017-05-29 04:19:29 +00:00
|
|
|
// Copyright (c) 2017-present PyO3 Project and Contributors
|
|
|
|
|
2017-06-01 16:45:00 +00:00
|
|
|
use std::rc::Rc;
|
2017-05-29 04:19:29 +00:00
|
|
|
use std::marker::PhantomData;
|
|
|
|
|
2017-06-02 16:23:48 +00:00
|
|
|
use err::PyResult;
|
2017-05-31 00:23:23 +00:00
|
|
|
use pointers::Py;
|
|
|
|
use python::Python;
|
2017-05-29 04:19:29 +00:00
|
|
|
use typeob::{PyTypeInfo, PyObjectAlloc};
|
|
|
|
|
|
|
|
|
2017-06-01 22:06:48 +00:00
|
|
|
pub struct PyToken(PhantomData<Rc<()>>);
|
2017-05-29 04:19:29 +00:00
|
|
|
|
2017-06-01 22:06:48 +00:00
|
|
|
impl PyToken {
|
2017-05-29 04:19:29 +00:00
|
|
|
pub fn token<'p>(&'p self) -> Python<'p> {
|
|
|
|
unsafe { Python::assume_gil_acquired() }
|
|
|
|
}
|
|
|
|
}
|
|
|
|
|
|
|
|
#[inline]
|
2017-06-02 16:23:48 +00:00
|
|
|
pub fn with<'p, T, F>(py: Python<'p>, f: F) -> PyResult<Py<'p, T>>
|
2017-06-01 22:06:48 +00:00
|
|
|
where F: FnOnce(PyToken) -> T,
|
2017-05-29 04:19:29 +00:00
|
|
|
T: PyTypeInfo + PyObjectAlloc<Type=T>
|
|
|
|
{
|
2017-06-02 16:23:48 +00:00
|
|
|
Py::new(py, f(PyToken(PhantomData)))
|
2017-05-29 04:19:29 +00:00
|
|
|
}
|
|
|
|
|
2017-06-01 22:06:48 +00:00
|
|
|
pub trait PyObjectWithToken : Sized {
|
2017-05-29 04:19:29 +00:00
|
|
|
fn token<'p>(&'p self) -> Python<'p>;
|
|
|
|
}
|