Mercurial > public > mercurial-scm > hg
changeset 52827:50c0c74ca266
rust-pyo3: add a transaction module
This mirrors the transaction module from hg-cpython.
author | Rapha?l Gom?s <rgomes@octobus.net> |
---|---|
date | Fri, 03 Jan 2025 14:57:53 +0100 |
parents | 9dcba9b379cb |
children | e49794d16657 |
files | rust/hg-pyo3/src/lib.rs rust/hg-pyo3/src/transaction.rs |
diffstat | 2 files changed, 34 insertions(+), 0 deletions(-) [+] |
line wrap: on
line diff
--- a/rust/hg-pyo3/src/lib.rs Fri Jan 03 12:51:25 2025 +0100 +++ b/rust/hg-pyo3/src/lib.rs Fri Jan 03 14:57:53 2025 +0100 @@ -7,6 +7,7 @@ mod node; mod revision; mod revlog; +mod transaction; mod store; mod util;
--- /dev/null Thu Jan 01 00:00:00 1970 +0000 +++ b/rust/hg-pyo3/src/transaction.rs Fri Jan 03 14:57:53 2025 +0100 @@ -0,0 +1,33 @@ +use hg::{transaction::Transaction, utils::files::get_bytes_from_path}; +use pyo3::{intern, types::PyBytes, PyObject, Python}; + +/// Wrapper around a Python transaction object, to keep `hg-core` oblivious +/// of the fact it's being called from Python. +pub struct PyTransaction { + inner: PyObject, +} + +impl PyTransaction { + pub fn new(inner: PyObject) -> Self { + Self { inner } + } +} + +impl Clone for PyTransaction { + fn clone(&self) -> Self { + Python::with_gil(|py| Self { + inner: self.inner.clone_ref(py), + }) + } +} + +impl Transaction for PyTransaction { + fn add(&mut self, file: impl AsRef<std::path::Path>, offset: usize) { + Python::with_gil(|py| { + let file = PyBytes::new(py, &get_bytes_from_path(file.as_ref())); + self.inner + .call_method(py, intern!(py, "add"), (file, offset), None) + .expect("transaction add failed"); + }) + } +}