Skip to content

Commit c26bc3d

Browse files
Remove the BoundObject impl for &Bound, use Borrowed instead (#4487)
* Remove the `BoundObject` impl for `&Bound`, use `Borrowed` instead For rationale see #4467. I chose to make `bound_object_sealed::Sealed` unsafe instead of `BoundObject` so that users won't see the `# Safety` section, as it's not relevant for them. * delete newsfragment --------- Co-authored-by: David Hewitt <[email protected]>
1 parent 3d46a3a commit c26bc3d

File tree

3 files changed

+19
-37
lines changed

3 files changed

+19
-37
lines changed

src/conversion.rs

Lines changed: 4 additions & 4 deletions
Original file line numberDiff line numberDiff line change
@@ -280,8 +280,8 @@ pub trait IntoPyObject<'py>: Sized {
280280
type Target;
281281
/// The smart pointer type to use.
282282
///
283-
/// This will usually be [`Bound<'py, Target>`], but can special cases `&'a Bound<'py, Target>`
284-
/// or [`Borrowed<'a, 'py, Target>`] can be used to minimize reference counting overhead.
283+
/// This will usually be [`Bound<'py, Target>`], but in special cases [`Borrowed<'a, 'py, Target>`] can be
284+
/// used to minimize reference counting overhead.
285285
type Output: BoundObject<'py, Self::Target>;
286286
/// The type returned in the event of a conversion error.
287287
type Error;
@@ -361,11 +361,11 @@ impl<'py, T> IntoPyObject<'py> for Bound<'py, T> {
361361

362362
impl<'a, 'py, T> IntoPyObject<'py> for &'a Bound<'py, T> {
363363
type Target = T;
364-
type Output = &'a Bound<'py, Self::Target>;
364+
type Output = Borrowed<'a, 'py, Self::Target>;
365365
type Error = Infallible;
366366

367367
fn into_pyobject(self, _py: Python<'py>) -> Result<Self::Output, Self::Error> {
368-
Ok(self)
368+
Ok(self.as_borrowed())
369369
}
370370
}
371371

src/instance.rs

Lines changed: 10 additions & 28 deletions
Original file line numberDiff line numberDiff line change
@@ -17,6 +17,8 @@ use std::ops::Deref;
1717
use std::ptr::NonNull;
1818

1919
/// Owned or borrowed gil-bound Python smart pointer
20+
///
21+
/// This is implemented for [`Bound`] and [`Borrowed`].
2022
pub trait BoundObject<'py, T>: bound_object_sealed::Sealed {
2123
/// Type erased version of `Self`
2224
type Any: BoundObject<'py, PyAny>;
@@ -33,11 +35,15 @@ pub trait BoundObject<'py, T>: bound_object_sealed::Sealed {
3335
}
3436

3537
mod bound_object_sealed {
36-
pub trait Sealed {}
38+
/// # Safety
39+
///
40+
/// Type must be layout-compatible with `*mut ffi::PyObject`.
41+
pub unsafe trait Sealed {}
3742

38-
impl<'py, T> Sealed for super::Bound<'py, T> {}
39-
impl<'a, 'py, T> Sealed for &'a super::Bound<'py, T> {}
40-
impl<'a, 'py, T> Sealed for super::Borrowed<'a, 'py, T> {}
43+
// SAFETY: `Bound` is layout-compatible with `*mut ffi::PyObject`.
44+
unsafe impl<'py, T> Sealed for super::Bound<'py, T> {}
45+
// SAFETY: `Borrowed` is layout-compatible with `*mut ffi::PyObject`.
46+
unsafe impl<'a, 'py, T> Sealed for super::Borrowed<'a, 'py, T> {}
4147
}
4248

4349
/// A GIL-attached equivalent to [`Py<T>`].
@@ -615,30 +621,6 @@ impl<'py, T> BoundObject<'py, T> for Bound<'py, T> {
615621
}
616622
}
617623

618-
impl<'a, 'py, T> BoundObject<'py, T> for &'a Bound<'py, T> {
619-
type Any = &'a Bound<'py, PyAny>;
620-
621-
fn as_borrowed(&self) -> Borrowed<'a, 'py, T> {
622-
Bound::as_borrowed(self)
623-
}
624-
625-
fn into_bound(self) -> Bound<'py, T> {
626-
self.clone()
627-
}
628-
629-
fn into_any(self) -> Self::Any {
630-
self.as_any()
631-
}
632-
633-
fn into_ptr(self) -> *mut ffi::PyObject {
634-
self.clone().into_ptr()
635-
}
636-
637-
fn unbind(self) -> Py<T> {
638-
self.clone().unbind()
639-
}
640-
}
641-
642624
/// A borrowed equivalent to `Bound`.
643625
///
644626
/// The advantage of this over `&Bound` is that it avoids the need to have a pointer-to-pointer, as Bound

src/pycell.rs

Lines changed: 5 additions & 5 deletions
Original file line numberDiff line numberDiff line change
@@ -199,7 +199,7 @@ use crate::ffi_ptr_ext::FfiPtrExt;
199199
use crate::internal_tricks::{ptr_from_mut, ptr_from_ref};
200200
use crate::pyclass::{boolean_struct::False, PyClass};
201201
use crate::types::any::PyAnyMethods;
202-
use crate::{ffi, Bound, IntoPy, PyErr, PyObject, Python};
202+
use crate::{ffi, Borrowed, Bound, IntoPy, PyErr, PyObject, Python};
203203
use std::convert::Infallible;
204204
use std::fmt;
205205
use std::mem::ManuallyDrop;
@@ -479,11 +479,11 @@ impl<'py, T: PyClass> IntoPyObject<'py> for PyRef<'py, T> {
479479

480480
impl<'a, 'py, T: PyClass> IntoPyObject<'py> for &'a PyRef<'py, T> {
481481
type Target = T;
482-
type Output = &'a Bound<'py, T>;
482+
type Output = Borrowed<'a, 'py, T>;
483483
type Error = Infallible;
484484

485485
fn into_pyobject(self, _py: Python<'py>) -> Result<Self::Output, Self::Error> {
486-
Ok(&self.inner)
486+
Ok(self.inner.as_borrowed())
487487
}
488488
}
489489

@@ -668,11 +668,11 @@ impl<'py, T: PyClass<Frozen = False>> IntoPyObject<'py> for PyRefMut<'py, T> {
668668

669669
impl<'a, 'py, T: PyClass<Frozen = False>> IntoPyObject<'py> for &'a PyRefMut<'py, T> {
670670
type Target = T;
671-
type Output = &'a Bound<'py, T>;
671+
type Output = Borrowed<'a, 'py, T>;
672672
type Error = Infallible;
673673

674674
fn into_pyobject(self, _py: Python<'py>) -> Result<Self::Output, Self::Error> {
675-
Ok(&self.inner)
675+
Ok(self.inner.as_borrowed())
676676
}
677677
}
678678

0 commit comments

Comments
 (0)