Skip to content

Commit

Permalink
Remove the BoundObject impl for &Bound, use Borrowed instead (#…
Browse files Browse the repository at this point in the history
…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]>
  • Loading branch information
ChayimFriedman2 and davidhewitt committed Aug 25, 2024
1 parent 3d46a3a commit c26bc3d
Show file tree
Hide file tree
Showing 3 changed files with 19 additions and 37 deletions.
8 changes: 4 additions & 4 deletions src/conversion.rs
Original file line number Diff line number Diff line change
Expand Up @@ -280,8 +280,8 @@ pub trait IntoPyObject<'py>: Sized {
type Target;
/// The smart pointer type to use.
///
/// This will usually be [`Bound<'py, Target>`], but can special cases `&'a Bound<'py, Target>`
/// or [`Borrowed<'a, 'py, Target>`] can be used to minimize reference counting overhead.
/// This will usually be [`Bound<'py, Target>`], but in special cases [`Borrowed<'a, 'py, Target>`] can be
/// used to minimize reference counting overhead.
type Output: BoundObject<'py, Self::Target>;
/// The type returned in the event of a conversion error.
type Error;
Expand Down Expand Up @@ -361,11 +361,11 @@ impl<'py, T> IntoPyObject<'py> for Bound<'py, T> {

impl<'a, 'py, T> IntoPyObject<'py> for &'a Bound<'py, T> {
type Target = T;
type Output = &'a Bound<'py, Self::Target>;
type Output = Borrowed<'a, 'py, Self::Target>;
type Error = Infallible;

fn into_pyobject(self, _py: Python<'py>) -> Result<Self::Output, Self::Error> {
Ok(self)
Ok(self.as_borrowed())
}
}

Expand Down
38 changes: 10 additions & 28 deletions src/instance.rs
Original file line number Diff line number Diff line change
Expand Up @@ -17,6 +17,8 @@ use std::ops::Deref;
use std::ptr::NonNull;

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

mod bound_object_sealed {
pub trait Sealed {}
/// # Safety
///
/// Type must be layout-compatible with `*mut ffi::PyObject`.
pub unsafe trait Sealed {}

impl<'py, T> Sealed for super::Bound<'py, T> {}
impl<'a, 'py, T> Sealed for &'a super::Bound<'py, T> {}
impl<'a, 'py, T> Sealed for super::Borrowed<'a, 'py, T> {}
// SAFETY: `Bound` is layout-compatible with `*mut ffi::PyObject`.
unsafe impl<'py, T> Sealed for super::Bound<'py, T> {}
// SAFETY: `Borrowed` is layout-compatible with `*mut ffi::PyObject`.
unsafe impl<'a, 'py, T> Sealed for super::Borrowed<'a, 'py, T> {}
}

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

impl<'a, 'py, T> BoundObject<'py, T> for &'a Bound<'py, T> {
type Any = &'a Bound<'py, PyAny>;

fn as_borrowed(&self) -> Borrowed<'a, 'py, T> {
Bound::as_borrowed(self)
}

fn into_bound(self) -> Bound<'py, T> {
self.clone()
}

fn into_any(self) -> Self::Any {
self.as_any()
}

fn into_ptr(self) -> *mut ffi::PyObject {
self.clone().into_ptr()
}

fn unbind(self) -> Py<T> {
self.clone().unbind()
}
}

/// A borrowed equivalent to `Bound`.
///
/// The advantage of this over `&Bound` is that it avoids the need to have a pointer-to-pointer, as Bound
Expand Down
10 changes: 5 additions & 5 deletions src/pycell.rs
Original file line number Diff line number Diff line change
Expand Up @@ -199,7 +199,7 @@ use crate::ffi_ptr_ext::FfiPtrExt;
use crate::internal_tricks::{ptr_from_mut, ptr_from_ref};
use crate::pyclass::{boolean_struct::False, PyClass};
use crate::types::any::PyAnyMethods;
use crate::{ffi, Bound, IntoPy, PyErr, PyObject, Python};
use crate::{ffi, Borrowed, Bound, IntoPy, PyErr, PyObject, Python};
use std::convert::Infallible;
use std::fmt;
use std::mem::ManuallyDrop;
Expand Down Expand Up @@ -479,11 +479,11 @@ impl<'py, T: PyClass> IntoPyObject<'py> for PyRef<'py, T> {

impl<'a, 'py, T: PyClass> IntoPyObject<'py> for &'a PyRef<'py, T> {
type Target = T;
type Output = &'a Bound<'py, T>;
type Output = Borrowed<'a, 'py, T>;
type Error = Infallible;

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

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

impl<'a, 'py, T: PyClass<Frozen = False>> IntoPyObject<'py> for &'a PyRefMut<'py, T> {
type Target = T;
type Output = &'a Bound<'py, T>;
type Output = Borrowed<'a, 'py, T>;
type Error = Infallible;

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

Expand Down

0 comments on commit c26bc3d

Please sign in to comment.