| 1 |
// copy_map.rs |
| 2 |
// |
| 3 |
// Copyright 2019 Raphaël Gomès <[email protected]> |
| 4 |
// 2025 Georges Racinet <[email protected]> |
| 5 |
// |
| 6 |
// This software may be used and distributed according to the terms of the |
| 7 |
// GNU General Public License version 2 or any later version. |
| 8 |
//! Bindings for `hg::dirstate::dirs_multiset` file provided by the |
| 9 |
//! `hg-core` package. |
| 10 |
use std::sync::RwLockReadGuard; |
| 11 |
use std::sync::RwLockWriteGuard; |
| 12 |
|
| 13 |
use hg::dirstate::dirs_multiset::DirsMultiset; |
| 14 |
use hg::dirstate::dirs_multiset::DirsMultisetIter; |
| 15 |
use hg::utils::hg_path::HgPath; |
| 16 |
use hg::utils::hg_path::HgPathBuf; |
| 17 |
use pyo3::exceptions::PyTypeError; |
| 18 |
use pyo3::prelude::*; |
| 19 |
use pyo3::types::PyBytes; |
| 20 |
use pyo3::types::PyDict; |
| 21 |
use pyo3_sharedref::py_shared_iterator; |
| 22 |
use pyo3_sharedref::PyShareable; |
| 23 |
|
| 24 |
use crate::exceptions::map_try_lock_error; |
| 25 |
use crate::exceptions::to_string_value_error; |
| 26 |
use crate::path::PyHgPathRef; |
| 27 |
|
| 28 |
#[pyclass(mapping)] |
| 29 |
pub struct Dirs { |
| 30 |
pub(super) inner: PyShareable<DirsMultiset>, |
| 31 |
} |
| 32 |
|
| 33 |
#[pymethods] |
| 34 |
impl Dirs { |
| 35 |
#[new] |
| 36 |
fn new(map: &Bound<'_, PyAny>) -> PyResult<Self> { |
| 37 |
if map.cast::<PyDict>().is_ok() { |
| 38 |
return Err(PyTypeError::new_err( |
| 39 |
"pathutil.dirs() with a dict should only be used by the \ |
| 40 |
Python dirstatemap and should not be used \ |
| 41 |
when Rust is enabled", |
| 42 |
)); |
| 43 |
} |
| 44 |
let map: Result<Vec<_>, PyErr> = map |
| 45 |
.try_iter()? |
| 46 |
.map(|o| Ok(HgPathBuf::from_bytes(o?.extract()?))) |
| 47 |
.collect(); |
| 48 |
Ok(Self { |
| 49 |
inner: DirsMultiset::from_manifest(&map?) |
| 50 |
.map_err(to_string_value_error)? |
| 51 |
.into(), |
| 52 |
}) |
| 53 |
} |
| 54 |
|
| 55 |
fn addpath( |
| 56 |
slf: &Bound<'_, Self>, |
| 57 |
path: &Bound<'_, PyBytes>, |
| 58 |
) -> PyResult<()> { |
| 59 |
let path = HgPath::new(path.as_bytes()); |
| 60 |
Self::with_inner_write(slf, |mut inner| { |
| 61 |
inner.add_path(path).map_err(to_string_value_error) |
| 62 |
}) |
| 63 |
} |
| 64 |
|
| 65 |
fn delpath( |
| 66 |
slf: &Bound<'_, Self>, |
| 67 |
path: &Bound<'_, PyBytes>, |
| 68 |
) -> PyResult<()> { |
| 69 |
let path = HgPath::new(path.as_bytes()); |
| 70 |
Self::with_inner_write(slf, |mut inner| { |
| 71 |
inner.delete_path(path).map_err(to_string_value_error) |
| 72 |
}) |
| 73 |
} |
| 74 |
|
| 75 |
fn __iter__(slf: &Bound<'_, Self>) -> PyResult<DirsMultisetKeysIterator> { |
| 76 |
DirsMultisetKeysIterator::new(slf) |
| 77 |
} |
| 78 |
|
| 79 |
fn __contains__( |
| 80 |
slf: &Bound<'_, Self>, |
| 81 |
key: &Bound<'_, PyAny>, |
| 82 |
) -> PyResult<bool> { |
| 83 |
let path = if let Ok(k) = key.extract::<&[u8]>() { |
| 84 |
HgPath::new(k) |
| 85 |
} else { |
| 86 |
return Ok(false); |
| 87 |
}; |
| 88 |
|
| 89 |
Self::with_inner_read(slf, |inner| Ok(inner.contains(path))) |
| 90 |
} |
| 91 |
} |
| 92 |
|
| 93 |
py_shared_iterator!( |
| 94 |
DirsMultisetKeysIterator, |
| 95 |
PyBytes, |
| 96 |
Dirs, |
| 97 |
inner, |
| 98 |
DirsMultisetIter<'static>, |
| 99 |
|ms| ms.iter(), |
| 100 |
Dirs::keys_next_result |
| 101 |
); |
| 102 |
|
| 103 |
impl Dirs { |
| 104 |
fn keys_next_result( |
| 105 |
py: Python, |
| 106 |
res: &HgPathBuf, |
| 107 |
) -> PyResult<Option<Py<PyBytes>>> { |
| 108 |
Ok(Some(PyHgPathRef(res).into_pyobject(py)?.unbind())) |
| 109 |
} |
| 110 |
|
| 111 |
pub(super) fn with_inner_read<T>( |
| 112 |
slf: &Bound<'_, Self>, |
| 113 |
f: impl FnOnce(RwLockReadGuard<DirsMultiset>) -> PyResult<T>, |
| 114 |
) -> PyResult<T> { |
| 115 |
let self_ref = slf.borrow(); |
| 116 |
// Safety: the owner is the right one. We will anyway |
| 117 |
// not actually `share` it. |
| 118 |
let shareable_ref = unsafe { self_ref.inner.borrow_with_owner(slf) }; |
| 119 |
let guard = shareable_ref.try_read().map_err(map_try_lock_error)?; |
| 120 |
f(guard) |
| 121 |
} |
| 122 |
|
| 123 |
pub(super) fn with_inner_write<T>( |
| 124 |
slf: &Bound<'_, Self>, |
| 125 |
f: impl FnOnce(RwLockWriteGuard<DirsMultiset>) -> PyResult<T>, |
| 126 |
) -> PyResult<T> { |
| 127 |
let self_ref = slf.borrow(); |
| 128 |
// Safety: the owner is the right one. We will anyway |
| 129 |
// not actually `share` it. |
| 130 |
let shareable_ref = unsafe { self_ref.inner.borrow_with_owner(slf) }; |
| 131 |
let guard = shareable_ref.try_write().map_err(map_try_lock_error)?; |
| 132 |
f(guard) |
| 133 |
} |
| 134 |
} |