Skip to content

Commit

Permalink
add blanket IntoPyObject implementation for &&T (#4680)
Browse files Browse the repository at this point in the history
  • Loading branch information
davidhewitt authored Nov 4, 2024
1 parent 00d84d8 commit 63f2189
Show file tree
Hide file tree
Showing 4 changed files with 17 additions and 3 deletions.
14 changes: 14 additions & 0 deletions src/conversion.rs
Original file line number Diff line number Diff line change
Expand Up @@ -333,6 +333,20 @@ impl<'a, 'py, T> IntoPyObject<'py> for &'a Py<T> {
}
}

impl<'a, 'py, T> IntoPyObject<'py> for &&'a 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)
}
}

/// Extract a type from a Python object.
///
///
Expand Down
2 changes: 1 addition & 1 deletion src/tests/hygiene/misc.rs
Original file line number Diff line number Diff line change
Expand Up @@ -71,7 +71,7 @@ struct IntoPyObject2<'a> {
#[pyo3(crate = "crate")]
struct IntoPyObject3<'py>(i32, crate::Bound<'py, crate::PyAny>); // tuple case

#[derive(crate::IntoPyObject)]
#[derive(crate::IntoPyObject, crate::IntoPyObjectRef)]
#[pyo3(crate = "crate")]
struct IntoPyObject4<'a, 'py> {
callable: &'a crate::Bound<'py, crate::PyAny>, // struct case
Expand Down
2 changes: 1 addition & 1 deletion tests/ui/invalid_property_args.stderr
Original file line number Diff line number Diff line change
Expand Up @@ -55,14 +55,14 @@ error[E0277]: `PhantomData<i32>` cannot be converted to a Python object
= help: the trait `IntoPyObject<'_>` is not implemented for `PhantomData<i32>`, which is required by `for<'py> PhantomData<i32>: PyO3GetField<'py>`
= note: implement `IntoPyObject` for `&PhantomData<i32>` or `IntoPyObject + Clone` for `PhantomData<i32>` to define the conversion
= help: the following other types implement trait `IntoPyObject<'py>`:
&&'a T
&&OsStr
&&Path
&&str
&'a (T0, T1)
&'a (T0, T1, T2)
&'a (T0, T1, T2, T3)
&'a (T0, T1, T2, T3, T4)
&'a (T0, T1, T2, T3, T4, T5)
and $N others
= note: required for `PhantomData<i32>` to implement `for<'py> PyO3GetField<'py>`
note: required by a bound in `PyClassGetterGenerator::<ClassT, FieldT, Offset, false, false, false, false, false>::generate`
Expand Down
2 changes: 1 addition & 1 deletion tests/ui/missing_intopy.stderr
Original file line number Diff line number Diff line change
Expand Up @@ -8,14 +8,14 @@ error[E0277]: `Blah` cannot be converted to a Python object
= note: if you do not wish to have a corresponding Python type, implement it manually
= note: if you do not own `Blah` you can perform a manual conversion to one of the types in `pyo3::types::*`
= help: the following other types implement trait `IntoPyObject<'py>`:
&&'a T
&&OsStr
&&Path
&&str
&'a (T0, T1)
&'a (T0, T1, T2)
&'a (T0, T1, T2, T3)
&'a (T0, T1, T2, T3, T4)
&'a (T0, T1, T2, T3, T4, T5)
and $N others
note: required by a bound in `UnknownReturnType::<T>::wrap`
--> src/impl_/wrap.rs
Expand Down

0 comments on commit 63f2189

Please sign in to comment.