Skip to content

Add IntoPyObject & FromPyObject for Arc<T> #4987

New issue

Have a question about this project? Sign up for a free GitHub account to open an issue and contact its maintainers and the community.

By clicking “Sign up for GitHub”, you agree to our terms of service and privacy statement. We’ll occasionally send you account related emails.

Already on GitHub? Sign in to your account

Open
wants to merge 2 commits into
base: main
Choose a base branch
from
Open
Show file tree
Hide file tree
Changes from all commits
Commits
File filter

Filter by extension

Filter by extension

Conversations
Failed to load comments.
Loading
Jump to
Jump to file
Failed to load files.
Loading
Diff view
Diff view
1 change: 1 addition & 0 deletions newsfragments/4987.added.md
Original file line number Diff line number Diff line change
@@ -0,0 +1 @@
Add `IntoPyObject` & `FromPyObject` for `Arc<T>`
1 change: 1 addition & 0 deletions src/conversions/std/mod.rs
Original file line number Diff line number Diff line change
Expand Up @@ -9,5 +9,6 @@ mod path;
mod set;
mod slice;
mod string;
mod sync;
mod time;
mod vec;
85 changes: 85 additions & 0 deletions src/conversions/std/sync.rs
Original file line number Diff line number Diff line change
@@ -0,0 +1,85 @@
#[cfg(feature = "experimental-inspect")]
use crate::inspect::types::TypeInfo;
use crate::types::PyAnyMethods;
use crate::{Bound, BoundObject, FromPyObject, IntoPyObject, PyAny, PyErr, PyResult, Python};
use std::sync::Arc;

// TODO find a better way (without the extra type parameters) to name the associated types in the trait.
impl<'py, A, T, O, E> IntoPyObject<'py> for Arc<A>
where
for<'a> &'a A: IntoPyObject<'py, Target = T, Output = O, Error = E>,
O: BoundObject<'py, T>,
E: Into<PyErr>,
{
type Target = T;
type Output = O;
type Error = E;

#[inline]
fn into_pyobject(self, py: Python<'py>) -> Result<Self::Output, Self::Error> {
(&*self).into_pyobject(py)
}

#[cfg(feature = "experimental-inspect")]
fn type_output() -> TypeInfo {
<&A as IntoPyObject<'py>>::type_output()
}
}

impl<'a, 'py, T: 'a> IntoPyObject<'py> for &'a Arc<T>
where
&'a T: IntoPyObject<'py>,
{
type Target = <&'a T as IntoPyObject<'py>>::Target;
type Output = <&'a T as IntoPyObject<'py>>::Output;
type Error = <&'a T as IntoPyObject<'py>>::Error;

#[inline]
fn into_pyobject(self, py: Python<'py>) -> Result<Self::Output, Self::Error> {
(&**self).into_pyobject(py)
}

#[cfg(feature = "experimental-inspect")]
fn type_output() -> TypeInfo {
<&'a T as IntoPyObject<'py>>::type_output()
}
}

impl<'py, T> FromPyObject<'py> for Arc<T>
where
T: FromPyObject<'py>,
{
fn extract_bound(ob: &Bound<'py, PyAny>) -> PyResult<Self> {
ob.extract::<T>().map(Arc::new)
}

#[cfg(feature = "experimental-inspect")]
fn type_input() -> TypeInfo {
T::type_input()
}
}

#[cfg(test)]
mod tests {
use super::*;
use crate::types::PyInt;
use crate::Python;

#[test]
fn test_arc_into_pyobject() {
macro_rules! test_roundtrip {
($arc:expr) => {
Python::with_gil(|py| {
let arc = $arc;
let obj: Bound<'_, PyInt> = arc.into_pyobject(py).unwrap();
assert_eq!(obj.extract::<i32>().unwrap(), 42);
let roundtrip = obj.extract::<Arc<i32>>().unwrap();
assert_eq!(&42, roundtrip.as_ref());
});
};
}

test_roundtrip!(Arc::new(42));
test_roundtrip!(&Arc::new(42));
}
}
21 changes: 21 additions & 0 deletions tests/test_getter_setter.rs
Original file line number Diff line number Diff line change
Expand Up @@ -318,3 +318,24 @@ fn test_optional_setter() {
);
})
}

#[pyclass(get_all)]
struct ArcGetterSetter {
#[pyo3(set)]
foo: std::sync::Arc<i32>,
}

#[test]
fn test_arc_getter_setter() {
Python::with_gil(|py| {
let instance = Py::new(
py,
ArcGetterSetter {
foo: std::sync::Arc::new(42),
},
)
.unwrap();
py_run!(py, instance, "assert instance.foo == 42");
py_run!(py, instance, "instance.foo = 43; assert instance.foo == 43");
})
}
Loading