Skip to content
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
1 change: 1 addition & 0 deletions newsfragments/4593.changed.md
Original file line number Diff line number Diff line change
@@ -0,0 +1 @@
Use `DerefToPyAny` in blanket implementations of `From<Py<T>>` and `From<Bound<'py, T>>` for `PyObject`.
1 change: 1 addition & 0 deletions newsfragments/4593.removed.md
Original file line number Diff line number Diff line change
@@ -0,0 +1 @@
Remove implementations of `Deref` for `PyAny` and other "native" types.
2 changes: 2 additions & 0 deletions src/exceptions.rs
Original file line number Diff line number Diff line change
Expand Up @@ -274,6 +274,7 @@ macro_rules! create_exception_type_object {
macro_rules! impl_native_exception (
($name:ident, $exc_name:ident, $doc:expr, $layout:path $(, #checkfunction=$checkfunction:path)?) => (
#[doc = $doc]
#[repr(transparent)]
#[allow(clippy::upper_case_acronyms)]
pub struct $name($crate::PyAny);
Comment on lines +277 to 279
Copy link
Member Author

Choose a reason for hiding this comment

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

Without adding the #[repr] the types started raising dead code warnings for the contents never being read.

Copy link
Contributor

Choose a reason for hiding this comment

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

I guess an alternative would maybe be

#[non_exhaustive]
pub struct $name;

But I'm not sure if there are subtle semver differences between these.

Copy link
Member Author

Choose a reason for hiding this comment

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

Another alternative is to get rid of the create_exception! macro if we can come up with a complete error handling plan, which was started in #4186 and #4413 I guess...


Expand All @@ -291,6 +292,7 @@ macro_rules! impl_windows_native_exception (
($name:ident, $exc_name:ident, $doc:expr, $layout:path) => (
#[cfg(windows)]
#[doc = $doc]
#[repr(transparent)]
#[allow(clippy::upper_case_acronyms)]
pub struct $name($crate::PyAny);

Expand Down
4 changes: 2 additions & 2 deletions src/instance.rs
Original file line number Diff line number Diff line change
Expand Up @@ -1798,7 +1798,7 @@ unsafe impl<T> crate::AsPyPointer for Py<T> {

impl<T> std::convert::From<Py<T>> for PyObject
where
T: AsRef<PyAny>,
T: DerefToPyAny,
{
#[inline]
fn from(other: Py<T>) -> Self {
Expand All @@ -1808,7 +1808,7 @@ where

impl<T> std::convert::From<Bound<'_, T>> for PyObject
where
T: AsRef<PyAny>,
T: DerefToPyAny,
{
#[inline]
fn from(other: Bound<'_, T>) -> Self {
Expand Down
16 changes: 0 additions & 16 deletions src/types/mod.rs
Original file line number Diff line number Diff line change
Expand Up @@ -121,22 +121,6 @@ pub trait DerefToPyAny {
#[macro_export]
macro_rules! pyobject_native_type_named (
($name:ty $(;$generics:ident)*) => {
impl<$($generics,)*> ::std::convert::AsRef<$crate::PyAny> for $name {
#[inline]
fn as_ref(&self) -> &$crate::PyAny {
&self.0
}
}

impl<$($generics,)*> ::std::ops::Deref for $name {
type Target = $crate::PyAny;

#[inline]
fn deref(&self) -> &$crate::PyAny {
&self.0
}
}

impl $crate::types::DerefToPyAny for $name {}
};
);
Expand Down
Loading