rust: made the crate of hg-cpython importable
The crate name is actually `rusthg`. This has the side effect
of running the doctest of the `py_shared_iterator` macro which
was really inconsistent, and after basic fixes, exposed that the
macro itself was poorly scoped.
--- a/rust/hg-cpython/Cargo.toml Sat Nov 30 20:30:18 2024 +0100
+++ b/rust/hg-cpython/Cargo.toml Thu Dec 05 18:58:56 2024 +0100
@@ -6,7 +6,7 @@
[lib]
name='rusthg'
-crate-type = ["cdylib"]
+crate-type = ["cdylib", "rlib"]
[dependencies]
cpython = { version = "0.7.2", features = ["extension-module"] }
--- a/rust/hg-cpython/src/dirstate/copymap.rs Sat Nov 30 20:30:18 2024 +0100
+++ b/rust/hg-cpython/src/dirstate/copymap.rs Thu Dec 05 18:58:56 2024 +0100
@@ -11,7 +11,6 @@
use cpython::{
PyBytes, PyClone, PyDict, PyObject, PyResult, Python, UnsafePyLeaked,
};
-use std::cell::RefCell;
use crate::dirstate::dirstate_map::v2_error;
use crate::dirstate::dirstate_map::DirstateMap;
--- a/rust/hg-cpython/src/dirstate/dirs_multiset.rs Sat Nov 30 20:30:18 2024 +0100
+++ b/rust/hg-cpython/src/dirstate/dirs_multiset.rs Thu Dec 05 18:58:56 2024 +0100
@@ -8,8 +8,6 @@
//! Bindings for the `hg::dirstate::dirs_multiset` file provided by the
//! `hg-core` package.
-use std::cell::RefCell;
-
use cpython::{
exc, ObjectProtocol, PyBytes, PyClone, PyDict, PyErr, PyObject, PyResult,
Python, UnsafePyLeaked,
--- a/rust/hg-cpython/src/dirstate/dirstate_map.rs Sat Nov 30 20:30:18 2024 +0100
+++ b/rust/hg-cpython/src/dirstate/dirstate_map.rs Thu Dec 05 18:58:56 2024 +0100
@@ -8,7 +8,7 @@
//! Bindings for the `hg::dirstate::dirstate_map` file provided by the
//! `hg-core` package.
-use std::cell::{RefCell, RefMut};
+use std::cell::RefMut;
use cpython::{
exc, PyBool, PyBytes, PyClone, PyDict, PyErr, PyList, PyNone, PyObject,
--- a/rust/hg-cpython/src/ref_sharing.rs Sat Nov 30 20:30:18 2024 +0100
+++ b/rust/hg-cpython/src/ref_sharing.rs Thu Dec 05 18:58:56 2024 +0100
@@ -43,20 +43,23 @@
/// Do not copy it out of the function call.
///
/// # Example
+/// ```
+/// use cpython::*;
+/// use std::collections::hash_map::{HashMap, Iter as HashMapIter};
+/// use rusthg::py_shared_iterator;
///
-/// ```
-/// struct MyStruct {
-/// inner: HashMap<Vec<u8>, Vec<u8>>;
+/// pub struct MyStruct {
+/// inner: HashMap<Vec<u8>, Vec<u8>>
/// }
///
/// py_class!(pub class MyType |py| {
-/// @shared data inner: MyStruct;
+/// @shared data inner_shared: MyStruct;
///
/// def __iter__(&self) -> PyResult<MyTypeItemsIterator> {
/// let leaked_ref = self.inner_shared(py).leak_immutable();
/// MyTypeItemsIterator::from_inner(
/// py,
-/// unsafe { leaked_ref.map(py, |o| o.iter()) },
+/// unsafe { leaked_ref.map(py, |o| o.inner.iter()) },
/// )
/// }
/// });
@@ -76,11 +79,12 @@
///
/// py_shared_iterator!(
/// MyTypeItemsIterator,
-/// UnsafePyLeaked<HashMap<'static, Vec<u8>, Vec<u8>>>,
+/// UnsafePyLeaked<HashMapIter<'static, Vec<u8>, Vec<u8>>>,
/// MyType::translate_key_value,
/// Option<(PyBytes, PyBytes)>
/// );
/// ```
+#[macro_export]
macro_rules! py_shared_iterator {
(
$name: ident,
@@ -89,9 +93,9 @@
$success_type: ty
) => {
py_class!(pub class $name |py| {
- data inner: RefCell<$leaked>;
+ data inner: std::cell::RefCell<$leaked>;
- def __next__(&self) -> PyResult<$success_type> {
+ def __next__(&self) -> cpython::PyResult<$success_type> {
let mut leaked = self.inner(py).borrow_mut();
let mut iter = unsafe { leaked.try_borrow_mut(py)? };
match iter.next() {
@@ -101,7 +105,7 @@
}
}
- def __iter__(&self) -> PyResult<Self> {
+ def __iter__(&self) -> cpython::PyResult<Self> {
Ok(self.clone_ref(py))
}
});
@@ -110,10 +114,10 @@
pub fn from_inner(
py: Python,
leaked: $leaked,
- ) -> PyResult<Self> {
+ ) -> cpython::PyResult<Self> {
Self::create_instance(
py,
- RefCell::new(leaked),
+ std::cell::RefCell::new(leaked),
)
}
}