Skip to content

Commit fbe9203

Browse files
authored
Update rust re-hydration code to pyo3 0.21 (#264)
* Upgrade to 0.20 * Bump to pyo3 0.21
1 parent 35a4d1c commit fbe9203

File tree

3 files changed

+62
-31
lines changed

3 files changed

+62
-31
lines changed

src/pypgstac/Cargo.lock

Lines changed: 31 additions & 16 deletions
Some generated files are not rendered by default. Learn more about customizing how changed files appear on GitHub.

src/pypgstac/Cargo.toml

Lines changed: 5 additions & 1 deletion
Original file line numberDiff line numberDiff line change
@@ -11,4 +11,8 @@ crate-type = ["cdylib"]
1111

1212
[dependencies]
1313
anyhow = "1"
14-
pyo3 = { version = "0.19.1", features = ["abi3-py38", "extension-module", "anyhow"] }
14+
pyo3 = { version = "0.21", features = [
15+
"abi3-py38",
16+
"extension-module",
17+
"anyhow",
18+
] }

src/pypgstac/src/lib.rs

Lines changed: 26 additions & 14 deletions
Original file line numberDiff line numberDiff line change
@@ -22,30 +22,37 @@
2222
)]
2323

2424
use anyhow::anyhow;
25+
use pyo3::pybacked::PyBackedStr;
2526
use pyo3::{
2627
prelude::*,
27-
types::{PyDict, PyList, PyString},
28+
types::{PyDict, PyList},
2829
};
2930

3031
const MAGIC_MARKER: &str = "𒍟※";
3132

3233
#[pyfunction]
33-
fn hydrate<'a>(base: &PyAny, item: &'a PyAny) -> PyResult<&'a PyAny> {
34+
fn hydrate<'py>(
35+
base: &'py Bound<'py, PyAny>,
36+
item: &'py Bound<'py, PyAny>,
37+
) -> PyResult<&'py Bound<'py, PyAny>> {
3438
hydrate_any(base, item)
3539
}
3640

37-
fn hydrate_any<'a>(base: &PyAny, item: &'a PyAny) -> PyResult<&'a PyAny> {
41+
fn hydrate_any<'py>(
42+
base: &'py Bound<'py, PyAny>,
43+
item: &'py Bound<'py, PyAny>,
44+
) -> PyResult<&'py Bound<'py, PyAny>> {
3845
if let Ok(item) = item.downcast::<PyDict>() {
3946
if let Ok(base) = base.downcast::<PyDict>() {
40-
hydrate_dict(base, item).map(|item| item.into())
47+
Ok(hydrate_dict(base, item)?.as_any())
4148
} else if base.is_none() {
4249
Ok(item)
4350
} else {
4451
Err(anyhow!("type mismatch").into())
4552
}
4653
} else if let Ok(item) = item.downcast::<PyList>() {
4754
if let Ok(base) = base.downcast::<PyList>() {
48-
hydrate_list(base, item).map(|item| item.into())
55+
Ok(hydrate_list(base, item)?.as_any())
4956
} else if base.is_none() {
5057
Ok(item)
5158
} else {
@@ -56,30 +63,35 @@ fn hydrate_any<'a>(base: &PyAny, item: &'a PyAny) -> PyResult<&'a PyAny> {
5663
}
5764
}
5865

59-
fn hydrate_list<'a>(base: &PyList, item: &'a PyList) -> PyResult<&'a PyList> {
66+
fn hydrate_list<'py>(
67+
base: &'py Bound<'py, PyList>,
68+
item: &'py Bound<'py, PyList>,
69+
) -> PyResult<&'py Bound<'py, PyList>> {
6070
for i in 0..item.len() {
6171
if i >= base.len() {
6272
return Ok(item);
6373
} else {
64-
item.set_item(i, hydrate(&base[i], &item[i])?)?;
74+
item.set_item(i, hydrate(&base.get_item(i)?, &item.get_item(i)?)?)?;
6575
}
6676
}
6777
Ok(item)
6878
}
6979

70-
fn hydrate_dict<'a>(base: &PyDict, item: &'a PyDict) -> PyResult<&'a PyDict> {
80+
fn hydrate_dict<'py>(
81+
base: &'py Bound<'py, PyDict>,
82+
item: &'py Bound<'py, PyDict>,
83+
) -> PyResult<&'py Bound<'py, PyDict>> {
7184
for (key, base_value) in base {
72-
if let Some(item_value) = item.get_item(key) {
85+
if let Some(item_value) = item.get_item(&key)? {
7386
if item_value
74-
.downcast::<PyString>()
87+
.extract::<PyBackedStr>()
7588
.ok()
76-
.and_then(|value| value.to_str().ok())
77-
.map(|s| s == MAGIC_MARKER)
89+
.map(|s| <PyBackedStr as AsRef<str>>::as_ref(&s) == MAGIC_MARKER)
7890
.unwrap_or(false)
7991
{
8092
item.del_item(key)?;
8193
} else {
82-
item.set_item(key, hydrate(base_value, item_value)?)?;
94+
item.set_item(&key, hydrate(&base_value, &item_value)?)?;
8395
}
8496
} else {
8597
item.set_item(key, base_value)?;
@@ -89,7 +101,7 @@ fn hydrate_dict<'a>(base: &PyDict, item: &'a PyDict) -> PyResult<&'a PyDict> {
89101
}
90102

91103
#[pymodule]
92-
fn pgstacrs(_py: Python<'_>, m: &PyModule) -> PyResult<()> {
104+
fn pgstacrs(_py: Python<'_>, m: &Bound<'_, PyModule>) -> PyResult<()> {
93105
m.add_function(wrap_pyfunction!(crate::hydrate, m)?)?;
94106
Ok(())
95107
}

0 commit comments

Comments
 (0)