author | Pierre-Yves David <pierre-yves.david@octobus.net> |
Tue, 18 Feb 2025 22:24:08 +0100 | |
changeset 52964 | 469b9a628b51 |
parent 52827 | 50c0c74ca266 |
permissions | -rw-r--r-- |
52827
50c0c74ca266
rust-pyo3: add a transaction module
Rapha?l Gom?s <rgomes@octobus.net>
parents:
diff
changeset
|
1 |
use hg::{transaction::Transaction, utils::files::get_bytes_from_path}; |
50c0c74ca266
rust-pyo3: add a transaction module
Rapha?l Gom?s <rgomes@octobus.net>
parents:
diff
changeset
|
2 |
use pyo3::{intern, types::PyBytes, PyObject, Python}; |
50c0c74ca266
rust-pyo3: add a transaction module
Rapha?l Gom?s <rgomes@octobus.net>
parents:
diff
changeset
|
3 |
|
50c0c74ca266
rust-pyo3: add a transaction module
Rapha?l Gom?s <rgomes@octobus.net>
parents:
diff
changeset
|
4 |
/// Wrapper around a Python transaction object, to keep `hg-core` oblivious |
50c0c74ca266
rust-pyo3: add a transaction module
Rapha?l Gom?s <rgomes@octobus.net>
parents:
diff
changeset
|
5 |
/// of the fact it's being called from Python. |
50c0c74ca266
rust-pyo3: add a transaction module
Rapha?l Gom?s <rgomes@octobus.net>
parents:
diff
changeset
|
6 |
pub struct PyTransaction { |
50c0c74ca266
rust-pyo3: add a transaction module
Rapha?l Gom?s <rgomes@octobus.net>
parents:
diff
changeset
|
7 |
inner: PyObject, |
50c0c74ca266
rust-pyo3: add a transaction module
Rapha?l Gom?s <rgomes@octobus.net>
parents:
diff
changeset
|
8 |
} |
50c0c74ca266
rust-pyo3: add a transaction module
Rapha?l Gom?s <rgomes@octobus.net>
parents:
diff
changeset
|
9 |
|
50c0c74ca266
rust-pyo3: add a transaction module
Rapha?l Gom?s <rgomes@octobus.net>
parents:
diff
changeset
|
10 |
impl PyTransaction { |
50c0c74ca266
rust-pyo3: add a transaction module
Rapha?l Gom?s <rgomes@octobus.net>
parents:
diff
changeset
|
11 |
pub fn new(inner: PyObject) -> Self { |
50c0c74ca266
rust-pyo3: add a transaction module
Rapha?l Gom?s <rgomes@octobus.net>
parents:
diff
changeset
|
12 |
Self { inner } |
50c0c74ca266
rust-pyo3: add a transaction module
Rapha?l Gom?s <rgomes@octobus.net>
parents:
diff
changeset
|
13 |
} |
50c0c74ca266
rust-pyo3: add a transaction module
Rapha?l Gom?s <rgomes@octobus.net>
parents:
diff
changeset
|
14 |
} |
50c0c74ca266
rust-pyo3: add a transaction module
Rapha?l Gom?s <rgomes@octobus.net>
parents:
diff
changeset
|
15 |
|
50c0c74ca266
rust-pyo3: add a transaction module
Rapha?l Gom?s <rgomes@octobus.net>
parents:
diff
changeset
|
16 |
impl Clone for PyTransaction { |
50c0c74ca266
rust-pyo3: add a transaction module
Rapha?l Gom?s <rgomes@octobus.net>
parents:
diff
changeset
|
17 |
fn clone(&self) -> Self { |
50c0c74ca266
rust-pyo3: add a transaction module
Rapha?l Gom?s <rgomes@octobus.net>
parents:
diff
changeset
|
18 |
Python::with_gil(|py| Self { |
50c0c74ca266
rust-pyo3: add a transaction module
Rapha?l Gom?s <rgomes@octobus.net>
parents:
diff
changeset
|
19 |
inner: self.inner.clone_ref(py), |
50c0c74ca266
rust-pyo3: add a transaction module
Rapha?l Gom?s <rgomes@octobus.net>
parents:
diff
changeset
|
20 |
}) |
50c0c74ca266
rust-pyo3: add a transaction module
Rapha?l Gom?s <rgomes@octobus.net>
parents:
diff
changeset
|
21 |
} |
50c0c74ca266
rust-pyo3: add a transaction module
Rapha?l Gom?s <rgomes@octobus.net>
parents:
diff
changeset
|
22 |
} |
50c0c74ca266
rust-pyo3: add a transaction module
Rapha?l Gom?s <rgomes@octobus.net>
parents:
diff
changeset
|
23 |
|
50c0c74ca266
rust-pyo3: add a transaction module
Rapha?l Gom?s <rgomes@octobus.net>
parents:
diff
changeset
|
24 |
impl Transaction for PyTransaction { |
50c0c74ca266
rust-pyo3: add a transaction module
Rapha?l Gom?s <rgomes@octobus.net>
parents:
diff
changeset
|
25 |
fn add(&mut self, file: impl AsRef<std::path::Path>, offset: usize) { |
50c0c74ca266
rust-pyo3: add a transaction module
Rapha?l Gom?s <rgomes@octobus.net>
parents:
diff
changeset
|
26 |
Python::with_gil(|py| { |
50c0c74ca266
rust-pyo3: add a transaction module
Rapha?l Gom?s <rgomes@octobus.net>
parents:
diff
changeset
|
27 |
let file = PyBytes::new(py, &get_bytes_from_path(file.as_ref())); |
50c0c74ca266
rust-pyo3: add a transaction module
Rapha?l Gom?s <rgomes@octobus.net>
parents:
diff
changeset
|
28 |
self.inner |
50c0c74ca266
rust-pyo3: add a transaction module
Rapha?l Gom?s <rgomes@octobus.net>
parents:
diff
changeset
|
29 |
.call_method(py, intern!(py, "add"), (file, offset), None) |
50c0c74ca266
rust-pyo3: add a transaction module
Rapha?l Gom?s <rgomes@octobus.net>
parents:
diff
changeset
|
30 |
.expect("transaction add failed"); |
50c0c74ca266
rust-pyo3: add a transaction module
Rapha?l Gom?s <rgomes@octobus.net>
parents:
diff
changeset
|
31 |
}) |
50c0c74ca266
rust-pyo3: add a transaction module
Rapha?l Gom?s <rgomes@octobus.net>
parents:
diff
changeset
|
32 |
} |
50c0c74ca266
rust-pyo3: add a transaction module
Rapha?l Gom?s <rgomes@octobus.net>
parents:
diff
changeset
|
33 |
} |