Skip to content

Commit ee66c21

Browse files
Make IntoPyObject for Bound & Borrowed more generic (#5831)
1 parent 341b1e7 commit ee66c21

File tree

4 files changed

+22
-12
lines changed

4 files changed

+22
-12
lines changed

newsfragments/5831.added.md

Lines changed: 1 addition & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -0,0 +1 @@
1+
Add `Borrowed::as_unbound`

newsfragments/5831.changed.md

Lines changed: 1 addition & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -0,0 +1 @@
1+
Make `IntoPyObject` for `Bound` & `Borrowed` more generic

src/conversion.rs

Lines changed: 12 additions & 12 deletions
Original file line numberDiff line numberDiff line change
@@ -127,55 +127,55 @@ pub(crate) mod private {
127127
}
128128
}
129129

130-
impl<'py, T: PyTypeCheck> IntoPyObject<'py> for Bound<'py, T> {
130+
impl<'py, T: PyTypeCheck> IntoPyObject<'py> for Bound<'_, T> {
131131
type Target = T;
132132
type Output = Bound<'py, Self::Target>;
133133
type Error = Infallible;
134134

135135
#[cfg(feature = "experimental-inspect")]
136136
const OUTPUT_TYPE: PyStaticExpr = T::TYPE_HINT;
137137

138-
fn into_pyobject(self, _py: Python<'py>) -> Result<Self::Output, Self::Error> {
139-
Ok(self)
138+
fn into_pyobject(self, py: Python<'py>) -> Result<Self::Output, Self::Error> {
139+
Ok(self.unbind().into_bound(py))
140140
}
141141
}
142142

143-
impl<'a, 'py, T: PyTypeCheck> IntoPyObject<'py> for &'a Bound<'py, T> {
143+
impl<'a, 'py, T: PyTypeCheck> IntoPyObject<'py> for &'a Bound<'_, T> {
144144
type Target = T;
145145
type Output = Borrowed<'a, 'py, Self::Target>;
146146
type Error = Infallible;
147147

148148
#[cfg(feature = "experimental-inspect")]
149149
const OUTPUT_TYPE: PyStaticExpr = T::TYPE_HINT;
150150

151-
fn into_pyobject(self, _py: Python<'py>) -> Result<Self::Output, Self::Error> {
152-
Ok(self.as_borrowed())
151+
fn into_pyobject(self, py: Python<'py>) -> Result<Self::Output, Self::Error> {
152+
Ok(self.as_unbound().bind_borrowed(py))
153153
}
154154
}
155155

156-
impl<'a, 'py, T: PyTypeCheck> IntoPyObject<'py> for Borrowed<'a, 'py, T> {
156+
impl<'a, 'py, T: PyTypeCheck> IntoPyObject<'py> for Borrowed<'a, '_, T> {
157157
type Target = T;
158158
type Output = Borrowed<'a, 'py, Self::Target>;
159159
type Error = Infallible;
160160

161161
#[cfg(feature = "experimental-inspect")]
162162
const OUTPUT_TYPE: PyStaticExpr = T::TYPE_HINT;
163163

164-
fn into_pyobject(self, _py: Python<'py>) -> Result<Self::Output, Self::Error> {
165-
Ok(self)
164+
fn into_pyobject(self, py: Python<'py>) -> Result<Self::Output, Self::Error> {
165+
Ok(self.as_unbound().bind_borrowed(py))
166166
}
167167
}
168168

169-
impl<'a, 'py, T: PyTypeCheck> IntoPyObject<'py> for &Borrowed<'a, 'py, T> {
169+
impl<'a, 'py, T: PyTypeCheck> IntoPyObject<'py> for &Borrowed<'a, '_, T> {
170170
type Target = T;
171171
type Output = Borrowed<'a, 'py, Self::Target>;
172172
type Error = Infallible;
173173

174174
#[cfg(feature = "experimental-inspect")]
175175
const OUTPUT_TYPE: PyStaticExpr = T::TYPE_HINT;
176176

177-
fn into_pyobject(self, _py: Python<'py>) -> Result<Self::Output, Self::Error> {
178-
Ok(*self)
177+
fn into_pyobject(self, py: Python<'py>) -> Result<Self::Output, Self::Error> {
178+
Ok(self.as_unbound().bind_borrowed(py))
179179
}
180180
}
181181

src/instance.rs

Lines changed: 8 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -1075,6 +1075,14 @@ impl<'a, 'py, T> Borrowed<'a, 'py, T> {
10751075
pub unsafe fn cast_unchecked<U>(self) -> Borrowed<'a, 'py, U> {
10761076
Borrowed(self.0, PhantomData, self.2)
10771077
}
1078+
1079+
/// Removes the connection for this `Borrowed<T>` from the [`Python<'py>`] token,
1080+
/// allowing it to cross thread boundaries, without transferring ownership.
1081+
#[inline]
1082+
pub fn as_unbound(&self) -> &'a Py<T> {
1083+
// Safety: NonNull<ffi::PyObject> is layout-compatible with Py<T>
1084+
unsafe { NonNull::from(&self.0).cast().as_ref() }
1085+
}
10781086
}
10791087

10801088
impl<'a, T: PyClass> Borrowed<'a, '_, T> {

0 commit comments

Comments
 (0)