pyo3/src/token.rs

39 lines
838 B
Rust
Raw Normal View History

2017-05-29 04:19:29 +00:00
// Copyright (c) 2017-present PyO3 Project and Contributors
use std::marker::PhantomData;
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};
pub struct PythonToken<T>(PhantomData<T>);
impl<T> PythonToken<T> {
pub fn token<'p>(&'p self) -> Python<'p> {
unsafe { Python::assume_gil_acquired() }
}
}
#[inline]
pub fn with_token<'p, T, F>(py: Python<'p>, f: F) -> Py<'p, T>
where F: FnOnce(PythonToken<T>) -> T,
T: PyTypeInfo + PyObjectAlloc<Type=T>
{
let value = f(PythonToken(PhantomData));
if let Ok(ob) = Py::new(py, value) {
ob
} else {
::err::panic_after_error()
}
}
2017-05-29 20:30:38 +00:00
pub trait PythonObjectWithGilToken<'p> : Sized {
fn gil(&self) -> Python<'p>;
}
2017-05-29 04:19:29 +00:00
pub trait PythonObjectWithToken : Sized {
fn token<'p>(&'p self) -> Python<'p>;
}