Skip to content
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

Remove the BoundObject impl for &Bound, use Borrowed instead #4487

Merged
Merged
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
8 changes: 4 additions & 4 deletions src/conversion.rs
Original file line number Diff line number Diff line change
Expand Up @@ -192,8 +192,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 @@ -273,11 +273,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 @@ -16,6 +16,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 @@ -32,11 +34,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 {}
Copy link
Member

Choose a reason for hiding this comment

The reason will be displayed to describe this comment to others. Learn more.

Yep I'm happy with the unsafe guarantee going here where users will never see it 😂


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 @@ -614,30 +620,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
Loading