use better name for Pointers

This commit is contained in:
Nikolay Kim 2017-07-18 11:12:35 -07:00
parent 69963d75ec
commit 01d688fc98
2 changed files with 39 additions and 39 deletions

View File

@ -79,7 +79,7 @@ pub trait IntoPyPointer {
fn into_ptr(self) -> *mut ffi::PyObject;
}
/// Convert None into a null pointer.
/// Convert `None` into a null pointer.
impl<'p, T> ToPyPointer for Option<&'p T> where T: ToPyPointer {
#[inline]
default fn as_ptr(&self) -> *mut ffi::PyObject {
@ -90,7 +90,7 @@ impl<'p, T> ToPyPointer for Option<&'p T> where T: ToPyPointer {
}
}
/// Convert None into a null pointer.
/// Convert `None` into a null pointer.
impl <T> IntoPyPointer for Option<T> where T: IntoPyPointer {
#[inline]
fn into_ptr(self) -> *mut ffi::PyObject {
@ -123,7 +123,7 @@ impl<'p> Python<'p> {
GILGuard::acquire()
}
/// Temporarily releases the GIL, thus allowing other Python threads to run.
/// Temporarily releases the `GIL`, thus allowing other Python threads to run.
pub fn allow_threads<T, F>(self, f: F) -> T where F : Send + FnOnce() -> T {
// The `Send` bound on the closure prevents the user from
// transferring the `Python` token into the closure.
@ -181,7 +181,7 @@ impl<'p> Python<'p> {
}
}
/// Gets the Python type object for type T.
/// Gets the Python type object for type `T`.
pub fn get_type<T>(self) -> &'p PyType where T: PyTypeObject {
T::type_object(self)
}
@ -221,7 +221,7 @@ impl<'p> Python<'p> {
impl<'p> Python<'p> {
/// Create new instance of T and move under python management.
/// Create new instance of `T` and move it under python management.
/// Returns `Py<T>`.
#[inline]
pub fn init<T, F>(self, f: F) -> PyResult<Py<T>>
@ -231,7 +231,7 @@ impl<'p> Python<'p> {
Py::new(self, f)
}
/// Create new instance of T and move under python management.
/// Create new instance of `T` and move it under python management.
/// Created object get registered in release pool. Returns references to `T`
#[inline]
pub fn init_ref<T, F>(self, f: F) -> PyResult<&'p T>
@ -241,7 +241,7 @@ impl<'p> Python<'p> {
Py::new_ref(self, f)
}
/// Create new instance of T and move under python management.
/// Create new instance of `T` and move it under python management.
/// Created object get registered in release pool. Returns mutable references to `T`
#[inline]
pub fn init_mut<T, F>(self, f: F) -> PyResult<&'p mut T>
@ -254,7 +254,7 @@ impl<'p> Python<'p> {
impl<'p> Python<'p> {
/// Register object in release pool, and try to downcast to specific Python object.
/// Register object in release pool, and try to downcast to specific type.
pub fn checked_cast_as<T>(self, obj: PyObject) -> Result<&'p T, PyDowncastError<'p>>
where T: PyDowncastFrom
{
@ -264,7 +264,7 @@ impl<'p> Python<'p> {
}
}
/// Register object in release pool, and do unchecked downcast to specific Python object.
/// Register object in release pool, and do unchecked downcast to specific type.
pub unsafe fn cast_as<T>(self, obj: PyObject) -> &'p T
where T: PyDowncastFrom
{
@ -273,7 +273,7 @@ impl<'p> Python<'p> {
}
/// Register `ffi::PyObject` pointer in release pool,
/// and do unchecked downcast to specific Python object.
/// and do unchecked downcast to specific type.
pub unsafe fn cast_from_ptr<T>(self, ptr: *mut ffi::PyObject) -> &'p T
where T: PyDowncastFrom
{
@ -286,7 +286,7 @@ impl<'p> Python<'p> {
}
/// Register `ffi::PyObject` pointer in release pool,
/// Do unchecked downcast to specific Python object. Returns mutable reference.
/// Do unchecked downcast to specific type. Returns mutable reference.
pub unsafe fn mut_cast_from_ptr<T>(self, ptr: *mut ffi::PyObject) -> &'p mut T
where T: PyDowncastFrom
{
@ -300,7 +300,7 @@ impl<'p> Python<'p> {
/// Register owned `ffi::PyObject` pointer in release pool.
/// Returns `Err(PyErr)` if the pointer is `null`.
/// do unchecked downcast to specific Python object.
/// do unchecked downcast to specific type.
pub unsafe fn cast_from_ptr_or_err<T>(self, ptr: *mut ffi::PyObject) -> PyResult<&'p T>
where T: PyDowncastFrom
{
@ -314,7 +314,7 @@ impl<'p> Python<'p> {
/// Register owned `ffi::PyObject` pointer in release pool.
/// Returns `None` if the pointer is `null`.
/// do unchecked downcast to specific Python object.
/// do unchecked downcast to specific type.
pub unsafe fn cast_from_ptr_or_opt<T>(self, ptr: *mut ffi::PyObject) -> Option<&'p T>
where T: PyDowncastFrom
{
@ -328,7 +328,7 @@ impl<'p> Python<'p> {
/// Register borrowed `ffi::PyObject` pointer in release pool.
/// Panics if the pointer is `null`.
/// do unchecked downcast to specific Python object.
/// do unchecked downcast to specific type.
pub unsafe fn cast_from_borrowed_ptr<T>(self, ptr: *mut ffi::PyObject) -> &'p T
where T: PyDowncastFrom
{
@ -338,7 +338,7 @@ impl<'p> Python<'p> {
/// Register borrowed `ffi::PyObject` pointer in release pool.
/// Returns `Err(PyErr)` if the pointer is `null`.
/// do unchecked downcast to specific Python object.
/// do unchecked downcast to specific type.
pub unsafe fn cast_from_borrowed_ptr_or_err<T>(self, ptr: *mut ffi::PyObject)
-> PyResult<&'p T>
where T: PyDowncastFrom
@ -353,7 +353,7 @@ impl<'p> Python<'p> {
/// Register borrowed `ffi::PyObject` pointer in release pool.
/// Returns `None` if the pointer is `null`.
/// do unchecked downcast to specific Python object.
/// do unchecked downcast to specific `T`.
pub unsafe fn cast_from_borrowed_ptr_or_opt<T>(self, ptr: *mut ffi::PyObject)
-> Option<&'p T>
where T: PyDowncastFrom
@ -368,7 +368,7 @@ impl<'p> Python<'p> {
/// Register borrowed `ffi::PyObject` pointer in release pool.
/// Panics if the pointer is `null`.
/// do unchecked downcast to specific Python object, returns mutable reference.
/// do unchecked downcast to specific `T`, returns mutable reference.
pub unsafe fn mut_cast_from_borrowed_ptr<T>(self, ptr: *mut ffi::PyObject) -> &'p mut T
where T: PyDowncastFrom
{

View File

@ -30,7 +30,7 @@ static START_PYO3: sync::Once = sync::ONCE_INIT;
/// thread (the thread which originally initialized Python) also initializes
/// threading.
///
/// When writing an extension module, the `py_module_initializer!` macro
/// When writing an extension module, the `#[py::modinit(..)]` macro
/// will ensure that Python threading is initialized.
///
pub fn prepare_freethreaded_python() {
@ -70,7 +70,7 @@ pub fn prepare_freethreaded_python() {
pub fn prepare_pyo3_library() {
START_PYO3.call_once(|| unsafe {
// initialize release pool
POINTERS = Box::into_raw(Box::new(Pointers::new()));
POOL = Box::into_raw(Box::new(ReleasePool::new()));
});
}
@ -99,7 +99,7 @@ pub struct GILGuard {
impl Drop for GILGuard {
fn drop(&mut self) {
unsafe {
let pool: &'static mut Pointers = &mut *POINTERS;
let pool: &'static mut ReleasePool = &mut *POOL;
pool.drain(self.owned, self.borrowed, true);
ffi::PyGILState_Release(self.gstate);
@ -108,16 +108,17 @@ impl Drop for GILGuard {
}
struct Pointers {
/// Release pool
struct ReleasePool {
owned: Vec<*mut ffi::PyObject>,
borrowed: Vec<*mut ffi::PyObject>,
pointers: *mut Vec<*mut ffi::PyObject>,
p: spin::Mutex<*mut Vec<*mut ffi::PyObject>>,
}
impl Pointers {
fn new() -> Pointers {
Pointers {
impl ReleasePool {
fn new() -> ReleasePool {
ReleasePool {
owned: Vec::with_capacity(250),
borrowed: Vec::with_capacity(250),
pointers: Box::into_raw(Box::new(Vec::with_capacity(250))),
@ -167,7 +168,7 @@ impl Pointers {
}
}
static mut POINTERS: *mut Pointers = ::std::ptr::null_mut();
static mut POOL: *mut ReleasePool = ::std::ptr::null_mut();
pub struct Pool {
owned: usize,
@ -179,7 +180,7 @@ pub struct Pool {
impl Pool {
#[inline]
pub unsafe fn new() -> Pool {
let p: &'static mut Pointers = &mut *POINTERS;
let p: &'static mut ReleasePool = &mut *POOL;
Pool {owned: p.owned.len(),
borrowed: p.borrowed.len(),
pointers: true,
@ -187,7 +188,7 @@ impl Pool {
}
#[inline]
pub unsafe fn new_no_pointers() -> Pool {
let p: &'static mut Pointers = &mut *POINTERS;
let p: &'static mut ReleasePool = &mut *POOL;
Pool {owned: p.owned.len(),
borrowed: p.borrowed.len(),
pointers: false,
@ -198,7 +199,7 @@ impl Pool {
impl Drop for Pool {
fn drop(&mut self) {
unsafe {
let pool: &'static mut Pointers = &mut *POINTERS;
let pool: &'static mut ReleasePool = &mut *POOL;
pool.drain(self.owned, self.borrowed, self.pointers);
}
}
@ -207,7 +208,7 @@ impl Drop for Pool {
pub unsafe fn register_pointer(obj: *mut ffi::PyObject)
{
let pool: &'static mut Pointers = &mut *POINTERS;
let pool: &'static mut ReleasePool = &mut *POOL;
let mut v = pool.p.lock();
let pool: &'static mut Vec<*mut ffi::PyObject> = &mut *(*v);
@ -216,14 +217,14 @@ pub unsafe fn register_pointer(obj: *mut ffi::PyObject)
pub unsafe fn register_owned(_py: Python, obj: *mut ffi::PyObject) -> &PyObjectRef
{
let pool: &'static mut Pointers = &mut *POINTERS;
let pool: &'static mut ReleasePool = &mut *POOL;
pool.owned.push(obj);
mem::transmute(&pool.owned[pool.owned.len()-1])
}
pub unsafe fn register_borrowed(_py: Python, obj: *mut ffi::PyObject) -> &PyObjectRef
{
let pool: &'static mut Pointers = &mut *POINTERS;
let pool: &'static mut ReleasePool = &mut *POOL;
pool.borrowed.push(obj);
mem::transmute(&pool.borrowed[pool.borrowed.len()-1])
}
@ -238,7 +239,7 @@ impl GILGuard {
unsafe {
let gstate = ffi::PyGILState_Ensure(); // acquire GIL
let pool: &'static mut Pointers = &mut *POINTERS;
let pool: &'static mut ReleasePool = &mut *POOL;
GILGuard { owned: pool.owned.len(),
borrowed: pool.borrowed.len(),
gstate: gstate,
@ -255,18 +256,17 @@ impl GILGuard {
#[cfg(test)]
mod test {
use std;
use {ffi, pythonrun};
use python::Python;
use object::PyObject;
use super::{Pool, Pointers, POINTERS};
use super::{Pool, ReleasePool, POOL};
#[test]
fn test_owned() {
pythonrun::prepare_pyo3_library();
unsafe {
let p: &'static mut Pointers = std::mem::transmute(POINTERS);
let p: &'static mut ReleasePool = &mut *POOL;
let cnt;
let empty;
@ -293,7 +293,7 @@ mod test {
pythonrun::prepare_pyo3_library();
unsafe {
let p: &'static mut Pointers = std::mem::transmute(POINTERS);
let p: &'static mut ReleasePool = &mut *POOL;
let cnt;
let empty;
@ -330,7 +330,7 @@ mod test {
pythonrun::prepare_pyo3_library();
unsafe {
let p: &'static mut Pointers = std::mem::transmute(POINTERS);
let p: &'static mut ReleasePool = &mut *POOL;
let cnt;
{
@ -357,7 +357,7 @@ mod test {
pythonrun::prepare_pyo3_library();
unsafe {
let p: &'static mut Pointers = std::mem::transmute(POINTERS);
let p: &'static mut ReleasePool = &mut *POOL;
let cnt;
{
@ -394,7 +394,7 @@ mod test {
pythonrun::prepare_pyo3_library();
unsafe {
let p: &'static mut Pointers = std::mem::transmute(POINTERS);
let p: &'static mut ReleasePool = &mut *POOL;
let ob;
let cnt;