##// END OF EJS Templates
rust-cpython: rename PyLeakedRef to PyLeaked...
rust-cpython: rename PyLeakedRef to PyLeaked This series will make PyLeaked* behave more like a Python iterator, which means mutation of the owner object will be allowed and the leaked reference (i.e. the iterator) will be invalidated instead. I'll add PyLeakedRef/PyLeakedRefMut structs which will represent a "borrowed" state, and prevent the underlying value from being mutably borrowed while the leaked reference is in use: let shared = self.inner_shared(py); let leaked = shared.leak_immutable(); { let leaked_ref: PyLeakedRef<_> = leaked.borrow(py); shared.borrow_mut(); // panics since the underlying value is borrowed } shared.borrow_mut(); // allowed The relation between PyLeaked* structs is quite similar to RefCell/Ref/RefMut, but the implementation can't be reused because the borrowing state will have to be shared across objects having no lifetime relation. PyLeaked isn't named as PyLeakedCell since it isn't actually a cell in that leaked.borrow_mut() will require &mut self.

File last commit:

r43603:b9f79109 default
r43603:b9f79109 default
Show More
ref_sharing.rs
523 lines | 16.5 KiB | application/rls-services+xml | RustLexer
Yuya Nishihara
rust-cpython: change license of ref_sharing.rs to MIT...
r43352 // ref_sharing.rs
Raphaël Gomès
rust-cpython: add macro for sharing references...
r42997 //
// Copyright 2019 Raphaël Gomès <rgomes@octobus.net>
//
Yuya Nishihara
rust-cpython: change license of ref_sharing.rs to MIT...
r43352 // Permission is hereby granted, free of charge, to any person obtaining a copy
// of this software and associated documentation files (the "Software"), to
// deal in the Software without restriction, including without limitation the
// rights to use, copy, modify, merge, publish, distribute, sublicense, and/or
// sell copies of the Software, and to permit persons to whom the Software is
// furnished to do so, subject to the following conditions:
//
// The above copyright notice and this permission notice shall be included in
// all copies or substantial portions of the Software.
//
// THE SOFTWARE IS PROVIDED "AS IS", WITHOUT WARRANTY OF ANY KIND, EXPRESS OR
// IMPLIED, INCLUDING BUT NOT LIMITED TO THE WARRANTIES OF MERCHANTABILITY,
// FITNESS FOR A PARTICULAR PURPOSE AND NONINFRINGEMENT. IN NO EVENT SHALL THE
// AUTHORS OR COPYRIGHT HOLDERS BE LIABLE FOR ANY CLAIM, DAMAGES OR OTHER
// LIABILITY, WHETHER IN AN ACTION OF CONTRACT, TORT OR OTHERWISE, ARISING
// FROM, OUT OF OR IN CONNECTION WITH THE SOFTWARE OR THE USE OR OTHER DEALINGS
// IN THE SOFTWARE.
Raphaël Gomès
rust-cpython: add macro for sharing references...
r42997
//! Macros for use in the `hg-cpython` bridge library.
use crate::exceptions::AlreadyBorrowed;
Yuya Nishihara
rust-cpython: move $leaked struct out of macro...
r43447 use cpython::{PyClone, PyObject, PyResult, Python};
Yuya Nishihara
rust-cpython: introduce restricted variant of RefCell...
r43115 use std::cell::{Cell, Ref, RefCell, RefMut};
Raphaël Gomès
rust-cpython: add macro for sharing references...
r42997
/// Manages the shared state between Python and Rust
Yuya Nishihara
rust-cpython: move py_shared_state to PySharedRefCell object...
r43443 #[derive(Debug, Default)]
Yuya Nishihara
rust-cpython: make inner functions and structs of ref_sharing private...
r43582 struct PySharedState {
Raphaël Gomès
rust-cpython: add macro for sharing references...
r42997 leak_count: Cell<usize>,
mutably_borrowed: Cell<bool>,
}
Yuya Nishihara
rust-cpython: mark PySharedState as Sync so &'PySharedState can be Send...
r43445 // &PySharedState can be Send because any access to inner cells is
// synchronized by the GIL.
unsafe impl Sync for PySharedState {}
Raphaël Gomès
rust-cpython: add macro for sharing references...
r42997 impl PySharedState {
Yuya Nishihara
rust-cpython: make inner functions and structs of ref_sharing private...
r43582 fn borrow_mut<'a, T>(
Raphaël Gomès
rust-cpython: add macro for sharing references...
r42997 &'a self,
py: Python<'a>,
pyrefmut: RefMut<'a, T>,
) -> PyResult<PyRefMut<'a, T>> {
if self.mutably_borrowed.get() {
return Err(AlreadyBorrowed::new(
py,
"Cannot borrow mutably while there exists another \
mutable reference in a Python object",
));
}
match self.leak_count.get() {
0 => {
self.mutably_borrowed.replace(true);
Ok(PyRefMut::new(py, pyrefmut, self))
}
// TODO
// For now, this works differently than Python references
// in the case of iterators.
// Python does not complain when the data an iterator
// points to is modified if the iterator is never used
// afterwards.
// Here, we are stricter than this by refusing to give a
// mutable reference if it is already borrowed.
// While the additional safety might be argued for, it
// breaks valid programming patterns in Python and we need
// to fix this issue down the line.
_ => Err(AlreadyBorrowed::new(
py,
"Cannot borrow mutably while there are \
immutable references in Python objects",
)),
}
}
Yuya Nishihara
rust-cpython: store leaked reference to PySharedState in $leaked struct...
r43446 /// Return a reference to the wrapped data and its state with an
/// artificial static lifetime.
Raphaël Gomès
rust-cpython: add macro for sharing references...
r42997 /// We need to be protected by the GIL for thread-safety.
Yuya Nishihara
rust-cpython: mark unsafe functions as such...
r43117 ///
/// # Safety
///
/// This is highly unsafe since the lifetime of the given data can be
/// extended. Do not call this function directly.
Yuya Nishihara
rust-cpython: make inner functions and structs of ref_sharing private...
r43582 unsafe fn leak_immutable<T>(
Raphaël Gomès
rust-cpython: add macro for sharing references...
r42997 &self,
py: Python,
Yuya Nishihara
rust-cpython: introduce restricted variant of RefCell...
r43115 data: &PySharedRefCell<T>,
Yuya Nishihara
rust-cpython: store leaked reference to PySharedState in $leaked struct...
r43446 ) -> PyResult<(&'static T, &'static PySharedState)> {
Raphaël Gomès
rust-cpython: add macro for sharing references...
r42997 if self.mutably_borrowed.get() {
return Err(AlreadyBorrowed::new(
py,
"Cannot borrow immutably while there is a \
mutable reference in Python objects",
));
}
Yuya Nishihara
rust-cpython: store leaked reference to PySharedState in $leaked struct...
r43446 // TODO: it's weird that self is data.py_shared_state. Maybe we
// can move stuff to PySharedRefCell?
Raphaël Gomès
rust-cpython: add macro for sharing references...
r42997 let ptr = data.as_ptr();
Yuya Nishihara
rust-cpython: store leaked reference to PySharedState in $leaked struct...
r43446 let state_ptr: *const PySharedState = &data.py_shared_state;
Raphaël Gomès
rust-cpython: add macro for sharing references...
r42997 self.leak_count.replace(self.leak_count.get() + 1);
Yuya Nishihara
rust-cpython: store leaked reference to PySharedState in $leaked struct...
r43446 Ok((&*ptr, &*state_ptr))
Raphaël Gomès
rust-cpython: add macro for sharing references...
r42997 }
Yuya Nishihara
rust-cpython: mark unsafe functions as such...
r43117 /// # Safety
///
Yuya Nishihara
rust-cpython: make inner functions and structs of ref_sharing private...
r43582 /// It's up to you to make sure the reference is about to be deleted
/// when updating the leak count.
fn decrease_leak_count(&self, _py: Python, mutable: bool) {
Raphaël Gomès
rust-cpython: add macro for sharing references...
r42997 if mutable {
Yuya Nishihara
rust-cpython: add sanity check to PySharedState::decrease_leak_count()...
r43209 assert_eq!(self.leak_count.get(), 0);
assert!(self.mutably_borrowed.get());
Raphaël Gomès
rust-cpython: add macro for sharing references...
r42997 self.mutably_borrowed.replace(false);
Yuya Nishihara
rust-cpython: add sanity check to PySharedState::decrease_leak_count()...
r43209 } else {
let count = self.leak_count.get();
assert!(count > 0);
self.leak_count.replace(count - 1);
Raphaël Gomès
rust-cpython: add macro for sharing references...
r42997 }
}
}
Yuya Nishihara
rust-cpython: introduce restricted variant of RefCell...
r43115 /// `RefCell` wrapper to be safely used in conjunction with `PySharedState`.
///
Yuya Nishihara
rust-cpython: make inner functions and structs of ref_sharing private...
r43582 /// This object can be stored in a `py_class!` object as a data field. Any
/// operation is allowed through the `PySharedRef` interface.
Yuya Nishihara
rust-cpython: introduce restricted variant of RefCell...
r43115 #[derive(Debug)]
pub struct PySharedRefCell<T> {
inner: RefCell<T>,
Yuya Nishihara
rust-cpython: add safe wrapper representing shared data borrowed from PyObject...
r43448 py_shared_state: PySharedState,
Yuya Nishihara
rust-cpython: introduce restricted variant of RefCell...
r43115 }
impl<T> PySharedRefCell<T> {
Yuya Nishihara
rust-cpython: move py_shared_state to PySharedRefCell object...
r43443 pub fn new(value: T) -> PySharedRefCell<T> {
Yuya Nishihara
rust-cpython: introduce restricted variant of RefCell...
r43115 Self {
inner: RefCell::new(value),
Yuya Nishihara
rust-cpython: move py_shared_state to PySharedRefCell object...
r43443 py_shared_state: PySharedState::default(),
Yuya Nishihara
rust-cpython: introduce restricted variant of RefCell...
r43115 }
}
Yuya Nishihara
rust-cpython: make inner functions and structs of ref_sharing private...
r43582 fn borrow<'a>(&'a self, _py: Python<'a>) -> Ref<'a, T> {
Yuya Nishihara
rust-cpython: introduce restricted variant of RefCell...
r43115 // py_shared_state isn't involved since
// - inner.borrow() would fail if self is mutably borrowed,
// - and inner.borrow_mut() would fail while self is borrowed.
self.inner.borrow()
}
Yuya Nishihara
rust-cpython: make inner functions and structs of ref_sharing private...
r43582 fn as_ptr(&self) -> *mut T {
Yuya Nishihara
rust-cpython: introduce restricted variant of RefCell...
r43115 self.inner.as_ptr()
}
Yuya Nishihara
rust-cpython: move borrow_mut() to PySharedRefCell...
r43444 // TODO: maybe this should be named as try_borrow_mut(), and use
// inner.try_borrow_mut(). The current implementation panics if
// self.inner has been borrowed, but returns error if py_shared_state
// refuses to borrow.
Yuya Nishihara
rust-cpython: make inner functions and structs of ref_sharing private...
r43582 fn borrow_mut<'a>(&'a self, py: Python<'a>) -> PyResult<PyRefMut<'a, T>> {
Yuya Nishihara
rust-cpython: move borrow_mut() to PySharedRefCell...
r43444 self.py_shared_state.borrow_mut(py, self.inner.borrow_mut())
Yuya Nishihara
rust-cpython: introduce restricted variant of RefCell...
r43115 }
}
Yuya Nishihara
rust-cpython: add safe wrapper representing shared data borrowed from PyObject...
r43448 /// Sharable data member of type `T` borrowed from the `PyObject`.
pub struct PySharedRef<'a, T> {
py: Python<'a>,
owner: &'a PyObject,
data: &'a PySharedRefCell<T>,
}
impl<'a, T> PySharedRef<'a, T> {
/// # Safety
///
/// The `data` must be owned by the `owner`. Otherwise, the leak count
/// would get wrong.
pub unsafe fn new(
py: Python<'a>,
owner: &'a PyObject,
data: &'a PySharedRefCell<T>,
) -> Self {
Self { py, owner, data }
}
Raphaël Gomès
rust-refsharing: add missing lifetime parameter in ref_sharing...
r43566 pub fn borrow(&self) -> Ref<'a, T> {
Yuya Nishihara
rust-cpython: require GIL to borrow immutable reference from PySharedRefCell...
r43580 self.data.borrow(self.py)
Yuya Nishihara
rust-cpython: add safe wrapper representing shared data borrowed from PyObject...
r43448 }
pub fn borrow_mut(&self) -> PyResult<PyRefMut<'a, T>> {
self.data.borrow_mut(self.py)
}
Yuya Nishihara
rust-cpython: make PyLeakedRef operations relatively safe...
r43579 /// Returns a leaked reference.
Yuya Nishihara
rust-cpython: rename PyLeakedRef to PyLeaked...
r43603 pub fn leak_immutable(&self) -> PyResult<PyLeaked<&'static T>> {
Yuya Nishihara
rust-cpython: make PyLeakedRef operations relatively safe...
r43579 let state = &self.data.py_shared_state;
unsafe {
let (static_ref, static_state_ref) =
state.leak_immutable(self.py, self.data)?;
Yuya Nishihara
rust-cpython: rename PyLeakedRef to PyLeaked...
r43603 Ok(PyLeaked::new(
Yuya Nishihara
rust-cpython: make PyLeakedRef operations relatively safe...
r43579 self.py,
self.owner,
static_ref,
static_state_ref,
))
}
Yuya Nishihara
rust-cpython: add safe wrapper representing shared data borrowed from PyObject...
r43448 }
}
Raphaël Gomès
rust-cpython: add macro for sharing references...
r42997 /// Holds a mutable reference to data shared between Python and Rust.
pub struct PyRefMut<'a, T> {
Yuya Nishihara
rust-cpython: keep Python<'a> token in PyRefMut...
r43581 py: Python<'a>,
Raphaël Gomès
rust-cpython: add macro for sharing references...
r42997 inner: RefMut<'a, T>,
py_shared_state: &'a PySharedState,
}
impl<'a, T> PyRefMut<'a, T> {
Yuya Nishihara
rust-cpython: mark unsafe functions as such...
r43117 // Must be constructed by PySharedState after checking its leak_count.
// Otherwise, drop() would incorrectly update the state.
Raphaël Gomès
rust-cpython: add macro for sharing references...
r42997 fn new(
Yuya Nishihara
rust-cpython: keep Python<'a> token in PyRefMut...
r43581 py: Python<'a>,
Raphaël Gomès
rust-cpython: add macro for sharing references...
r42997 inner: RefMut<'a, T>,
py_shared_state: &'a PySharedState,
) -> Self {
Self {
Yuya Nishihara
rust-cpython: keep Python<'a> token in PyRefMut...
r43581 py,
Raphaël Gomès
rust-cpython: add macro for sharing references...
r42997 inner,
py_shared_state,
}
}
}
impl<'a, T> std::ops::Deref for PyRefMut<'a, T> {
type Target = RefMut<'a, T>;
fn deref(&self) -> &Self::Target {
&self.inner
}
}
impl<'a, T> std::ops::DerefMut for PyRefMut<'a, T> {
fn deref_mut(&mut self) -> &mut Self::Target {
&mut self.inner
}
}
impl<'a, T> Drop for PyRefMut<'a, T> {
fn drop(&mut self) {
Yuya Nishihara
rust-cpython: make inner functions and structs of ref_sharing private...
r43582 self.py_shared_state.decrease_leak_count(self.py, true);
Raphaël Gomès
rust-cpython: add macro for sharing references...
r42997 }
}
/// Allows a `py_class!` generated struct to share references to one of its
/// data members with Python.
///
/// # Warning
///
/// TODO allow Python container types: for now, integration with the garbage
/// collector does not extend to Rust structs holding references to Python
/// objects. Should the need surface, `__traverse__` and `__clear__` will
/// need to be written as per the `rust-cpython` docs on GC integration.
///
/// # Parameters
///
/// * `$name` is the same identifier used in for `py_class!` macro call.
/// * `$inner_struct` is the identifier of the underlying Rust struct
/// * `$data_member` is the identifier of the data member of `$inner_struct`
/// that will be shared.
Yuya Nishihara
rust-cpython: add safe wrapper representing shared data borrowed from PyObject...
r43448 /// * `$shared_accessor` is the function name to be generated, which allows
/// safe access to the data member.
///
/// # Safety
///
/// `$data_member` must persist while the `$name` object is alive. In other
/// words, it must be an accessor to a data field of the Python object.
Raphaël Gomès
rust-cpython: add macro for sharing references...
r42997 ///
/// # Example
///
/// ```
/// struct MyStruct {
/// inner: Vec<u32>;
/// }
///
/// py_class!(pub class MyType |py| {
Yuya Nishihara
rust-cpython: introduce restricted variant of RefCell...
r43115 /// data inner: PySharedRefCell<MyStruct>;
Raphaël Gomès
rust-cpython: add macro for sharing references...
r42997 /// });
///
Yuya Nishihara
rust-cpython: add safe wrapper representing shared data borrowed from PyObject...
r43448 /// py_shared_ref!(MyType, MyStruct, inner, inner_shared);
Raphaël Gomès
rust-cpython: add macro for sharing references...
r42997 /// ```
macro_rules! py_shared_ref {
(
$name: ident,
$inner_struct: ident,
Yuya Nishihara
rust-cpython: add safe wrapper representing shared data borrowed from PyObject...
r43448 $data_member: ident,
$shared_accessor: ident
Raphaël Gomès
rust-cpython: add macro for sharing references...
r42997 ) => {
impl $name {
Yuya Nishihara
rust-cpython: add safe wrapper representing shared data borrowed from PyObject...
r43448 /// Returns a safe reference to the shared `$data_member`.
///
/// This function guarantees that `PySharedRef` is created with
/// the valid `self` and `self.$data_member(py)` pair.
fn $shared_accessor<'a>(
&'a self,
py: Python<'a>,
) -> $crate::ref_sharing::PySharedRef<'a, $inner_struct> {
use cpython::PythonObject;
use $crate::ref_sharing::PySharedRef;
let owner = self.as_object();
let data = self.$data_member(py);
unsafe { PySharedRef::new(py, owner, data) }
}
Raphaël Gomès
rust-cpython: add macro for sharing references...
r42997 }
Yuya Nishihara
rust-cpython: move $leaked struct out of macro...
r43447 };
}
Raphaël Gomès
rust-cpython: add macro for sharing references...
r42997
Yuya Nishihara
rust-cpython: move $leaked struct out of macro...
r43447 /// Manage immutable references to `PyObject` leaked into Python iterators.
Yuya Nishihara
rust-cpython: rename PyLeakedRef to PyLeaked...
r43603 pub struct PyLeaked<T> {
Yuya Nishihara
rust-cpython: make PyLeakedRef operations relatively safe...
r43579 inner: PyObject,
data: Option<T>,
Yuya Nishihara
rust-cpython: move $leaked struct out of macro...
r43447 py_shared_state: &'static PySharedState,
}
Raphaël Gomès
rust-cpython: add macro for sharing references...
r42997
Yuya Nishihara
rust-cpython: rename PyLeakedRef to PyLeaked...
r43603 // DO NOT implement Deref for PyLeaked<T>! Dereferencing PyLeaked
Yuya Nishihara
rust-cpython: make PyLeakedRef operations relatively safe...
r43579 // without taking Python GIL wouldn't be safe.
Yuya Nishihara
rust-cpython: rename PyLeakedRef to PyLeaked...
r43603 impl<T> PyLeaked<T> {
Yuya Nishihara
rust-cpython: move $leaked struct out of macro...
r43447 /// # Safety
///
/// The `py_shared_state` must be owned by the `inner` Python object.
Yuya Nishihara
rust-cpython: make inner functions and structs of ref_sharing private...
r43582 fn new(
Yuya Nishihara
rust-cpython: move $leaked struct out of macro...
r43447 py: Python,
inner: &PyObject,
Yuya Nishihara
rust-cpython: put leaked reference in PyLeakedRef...
r43578 data: T,
Yuya Nishihara
rust-cpython: move $leaked struct out of macro...
r43447 py_shared_state: &'static PySharedState,
) -> Self {
Self {
Yuya Nishihara
rust-cpython: make PyLeakedRef operations relatively safe...
r43579 inner: inner.clone_ref(py),
Yuya Nishihara
rust-cpython: put leaked reference in PyLeakedRef...
r43578 data: Some(data),
Yuya Nishihara
rust-cpython: move $leaked struct out of macro...
r43447 py_shared_state,
Raphaël Gomès
rust-cpython: add macro for sharing references...
r42997 }
Yuya Nishihara
rust-cpython: move $leaked struct out of macro...
r43447 }
Yuya Nishihara
rust-cpython: make PyLeakedRef operations relatively safe...
r43579
/// Returns an immutable reference to the inner value.
pub fn get_ref<'a>(&'a self, _py: Python<'a>) -> &'a T {
self.data.as_ref().unwrap()
}
/// Returns a mutable reference to the inner value.
///
/// Typically `T` is an iterator. If `T` is an immutable reference,
/// `get_mut()` is useless since the inner value can't be mutated.
pub fn get_mut<'a>(&'a mut self, _py: Python<'a>) -> &'a mut T {
self.data.as_mut().unwrap()
}
/// Converts the inner value by the given function.
///
/// Typically `T` is a static reference to a container, and `U` is an
/// iterator of that container.
///
/// # Safety
///
/// The lifetime of the object passed in to the function `f` is cheated.
/// It's typically a static reference, but is valid only while the
Yuya Nishihara
rust-cpython: rename PyLeakedRef to PyLeaked...
r43603 /// corresponding `PyLeaked` is alive. Do not copy it out of the
Yuya Nishihara
rust-cpython: make PyLeakedRef operations relatively safe...
r43579 /// function call.
pub unsafe fn map<U>(
mut self,
py: Python,
f: impl FnOnce(T) -> U,
Yuya Nishihara
rust-cpython: rename PyLeakedRef to PyLeaked...
r43603 ) -> PyLeaked<U> {
Yuya Nishihara
rust-cpython: make PyLeakedRef operations relatively safe...
r43579 // f() could make the self.data outlive. That's why map() is unsafe.
// In order to make this function safe, maybe we'll need a way to
// temporarily restrict the lifetime of self.data and translate the
// returned object back to Something<'static>.
let new_data = f(self.data.take().unwrap());
Yuya Nishihara
rust-cpython: rename PyLeakedRef to PyLeaked...
r43603 PyLeaked {
Yuya Nishihara
rust-cpython: make PyLeakedRef operations relatively safe...
r43579 inner: self.inner.clone_ref(py),
data: Some(new_data),
py_shared_state: self.py_shared_state,
}
}
Yuya Nishihara
rust-cpython: move $leaked struct out of macro...
r43447 }
Raphaël Gomès
rust-cpython: add macro for sharing references...
r42997
Yuya Nishihara
rust-cpython: rename PyLeakedRef to PyLeaked...
r43603 impl<T> Drop for PyLeaked<T> {
Yuya Nishihara
rust-cpython: move $leaked struct out of macro...
r43447 fn drop(&mut self) {
// py_shared_state should be alive since we do have
// a Python reference to the owner object. Taking GIL makes
// sure that the state is only accessed by this thread.
let gil = Python::acquire_gil();
let py = gil.python();
Yuya Nishihara
rust-cpython: make PyLeakedRef operations relatively safe...
r43579 if self.data.is_none() {
Yuya Nishihara
rust-cpython: rename PyLeakedRef to PyLeaked...
r43603 return; // moved to another PyLeaked
Yuya Nishihara
rust-cpython: make PyLeakedRef operations relatively safe...
r43579 }
Yuya Nishihara
rust-cpython: make inner functions and structs of ref_sharing private...
r43582 self.py_shared_state.decrease_leak_count(py, false);
Yuya Nishihara
rust-cpython: move $leaked struct out of macro...
r43447 }
Raphaël Gomès
rust-cpython: add macro for sharing references...
r42997 }
/// Defines a `py_class!` that acts as a Python iterator over a Rust iterator.
Yuya Nishihara
rust-cpython: replace dyn Iterator<..> of mapping with concrete type...
r43158 ///
/// TODO: this is a bit awkward to use, and a better (more complicated)
/// procedural macro would simplify the interface a lot.
///
/// # Parameters
///
/// * `$name` is the identifier to give to the resulting Rust struct.
/// * `$leaked` corresponds to `$leaked` in the matching `py_shared_ref!` call.
/// * `$iterator_type` is the type of the Rust iterator.
/// * `$success_func` is a function for processing the Rust `(key, value)`
/// tuple on iteration success, turning it into something Python understands.
/// * `$success_func` is the return type of `$success_func`
///
/// # Example
///
/// ```
/// struct MyStruct {
/// inner: HashMap<Vec<u8>, Vec<u8>>;
/// }
///
/// py_class!(pub class MyType |py| {
/// data inner: PySharedRefCell<MyStruct>;
///
/// def __iter__(&self) -> PyResult<MyTypeItemsIterator> {
Yuya Nishihara
rust-cpython: make PyLeakedRef operations relatively safe...
r43579 /// let leaked_ref = self.inner_shared(py).leak_immutable()?;
Yuya Nishihara
rust-cpython: leverage py_shared_iterator::from_inner() where appropriate
r43161 /// MyTypeItemsIterator::from_inner(
Yuya Nishihara
rust-cpython: replace dyn Iterator<..> of mapping with concrete type...
r43158 /// py,
Yuya Nishihara
rust-cpython: make PyLeakedRef operations relatively safe...
r43579 /// unsafe { leaked_ref.map(py, |o| o.iter()) },
Yuya Nishihara
rust-cpython: replace dyn Iterator<..> of mapping with concrete type...
r43158 /// )
/// }
/// });
///
/// impl MyType {
/// fn translate_key_value(
/// py: Python,
/// res: (&Vec<u8>, &Vec<u8>),
/// ) -> PyResult<Option<(PyBytes, PyBytes)>> {
/// let (f, entry) = res;
/// Ok(Some((
/// PyBytes::new(py, f),
/// PyBytes::new(py, entry),
/// )))
/// }
/// }
///
/// py_shared_ref!(MyType, MyStruct, inner, MyTypeLeakedRef);
///
Yuya Nishihara
rust-cpython: rename py_shared_iterator_impl to py_shared_iterator...
r43159 /// py_shared_iterator!(
Yuya Nishihara
rust-cpython: replace dyn Iterator<..> of mapping with concrete type...
r43158 /// MyTypeItemsIterator,
Yuya Nishihara
rust-cpython: rename PyLeakedRef to PyLeaked...
r43603 /// PyLeaked<HashMap<'static, Vec<u8>, Vec<u8>>>,
Yuya Nishihara
rust-cpython: replace dyn Iterator<..> of mapping with concrete type...
r43158 /// MyType::translate_key_value,
/// Option<(PyBytes, PyBytes)>
/// );
/// ```
Yuya Nishihara
rust-cpython: rename py_shared_iterator_impl to py_shared_iterator...
r43159 macro_rules! py_shared_iterator {
Raphaël Gomès
rust-cpython: add macro for sharing references...
r42997 (
$name: ident,
Yuya Nishihara
rust-cpython: move $leaked struct out of macro...
r43447 $leaked: ty,
Raphaël Gomès
rust-cpython: add macro for sharing references...
r42997 $success_func: expr,
$success_type: ty
) => {
py_class!(pub class $name |py| {
data inner: RefCell<Option<$leaked>>;
def __next__(&self) -> PyResult<$success_type> {
let mut inner_opt = self.inner(py).borrow_mut();
Yuya Nishihara
rust-cpython: make PyLeakedRef operations relatively safe...
r43579 if let Some(leaked) = inner_opt.as_mut() {
match leaked.get_mut(py).next() {
Raphaël Gomès
rust-cpython: add macro for sharing references...
r42997 None => {
// replace Some(inner) by None, drop $leaked
inner_opt.take();
Ok(None)
}
Some(res) => {
$success_func(py, res)
}
}
} else {
Ok(None)
}
}
def __iter__(&self) -> PyResult<Self> {
Ok(self.clone_ref(py))
}
});
impl $name {
pub fn from_inner(
py: Python,
Yuya Nishihara
rust-cpython: remove Option<_> from interface of py_shared_iterator...
r43160 leaked: $leaked,
Raphaël Gomès
rust-cpython: add macro for sharing references...
r42997 ) -> PyResult<Self> {
Self::create_instance(
py,
Yuya Nishihara
rust-cpython: remove Option<_> from interface of py_shared_iterator...
r43160 RefCell::new(Some(leaked)),
Raphaël Gomès
rust-cpython: add macro for sharing references...
r42997 )
}
}
};
}
Yuya Nishihara
rust-cpython: prepare for writing tests that require libpython...
r43583
#[cfg(test)]
#[cfg(any(feature = "python27-bin", feature = "python3-bin"))]
mod test {
use super::*;
use cpython::{GILGuard, Python};
py_class!(class Owner |py| {
data string: PySharedRefCell<String>;
});
py_shared_ref!(Owner, String, string, string_shared);
fn prepare_env() -> (GILGuard, Owner) {
let gil = Python::acquire_gil();
let py = gil.python();
let owner =
Owner::create_instance(py, PySharedRefCell::new("new".to_owned()))
.unwrap();
(gil, owner)
}
#[test]
fn test_borrow_mut_while_leaked() {
let (gil, owner) = prepare_env();
let py = gil.python();
assert!(owner.string_shared(py).borrow_mut().is_ok());
let _leaked = owner.string_shared(py).leak_immutable().unwrap();
// TODO: will be allowed
assert!(owner.string_shared(py).borrow_mut().is_err());
}
}