Skip to content

Commit

Permalink
move, seal and rename AnyBound to BoundObject
Browse files Browse the repository at this point in the history
  • Loading branch information
Icxolu committed Jul 12, 2024
1 parent 615e800 commit 8d7e0c0
Show file tree
Hide file tree
Showing 13 changed files with 137 additions and 129 deletions.
94 changes: 4 additions & 90 deletions src/conversion.rs
Original file line number Diff line number Diff line change
Expand Up @@ -5,7 +5,9 @@ use crate::inspect::types::TypeInfo;
use crate::pyclass::boolean_struct::False;
use crate::types::any::PyAnyMethods;
use crate::types::PyTuple;
use crate::{ffi, Borrowed, Bound, Py, PyAny, PyClass, PyObject, PyRef, PyRefMut, Python};
use crate::{
ffi, Borrowed, Bound, BoundObject, Py, PyAny, PyClass, PyObject, PyRef, PyRefMut, Python,
};
use std::convert::Infallible;
#[cfg(feature = "gil-refs")]
use {
Expand Down Expand Up @@ -176,94 +178,6 @@ pub trait IntoPy<T>: Sized {
}
}

/// Owned or borrowed gil-bound Python smart pointer
pub trait AnyBound<'py, T> {
/// Any
type Any: AnyBound<'py, PyAny>;
/// Borrow this smart pointer.
fn as_borrowed(&self) -> Borrowed<'_, 'py, T>;
/// Turns this smart pointer into an owned [`Bound<'py, T>`]
fn into_bound(self) -> Bound<'py, T>;
/// Upcast the target type of this smart pointer
fn into_any(self) -> Self::Any;
/// Turn this smart pointer into a strong reference pointer
fn into_ptr(self) -> *mut ffi::PyObject;
/// Turn this smart pointer into an owned [`Py<T>`]
fn unbind(self) -> Py<T>;
}

impl<'py, T> AnyBound<'py, T> for Bound<'py, T> {
type Any = Bound<'py, PyAny>;

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

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

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

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

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

impl<'a, 'py, T> AnyBound<'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()
}
}

impl<'a, 'py, T> AnyBound<'py, T> for Borrowed<'a, 'py, T> {
type Any = Borrowed<'a, 'py, PyAny>;

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

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

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

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

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

/// Defines a conversion from a Rust type to a Python object, which may fail.
///
/// It functions similarly to std's [`TryInto`] trait, but requires a [GIL token](Python)
Expand All @@ -275,7 +189,7 @@ pub trait IntoPyObject<'py>: Sized {
///
/// 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.
type Output: AnyBound<'py, Self::Target>;
type Output: BoundObject<'py, Self::Target>;
/// The type returned in the event of a conversion error.
type Error;

Expand Down
11 changes: 5 additions & 6 deletions src/conversions/either.rs
Original file line number Diff line number Diff line change
Expand Up @@ -43,12 +43,11 @@
//!
//! [either](https://docs.rs/either/ "A library for easy idiomatic error handling and reporting in Rust applications")’s

use crate::conversion::AnyBound;
#[cfg(feature = "experimental-inspect")]
use crate::inspect::types::TypeInfo;
use crate::{
conversion::IntoPyObject, exceptions::PyTypeError, types::any::PyAnyMethods, Bound,
FromPyObject, IntoPy, PyAny, PyErr, PyObject, PyResult, Python, ToPyObject,
BoundObject, FromPyObject, IntoPy, PyAny, PyErr, PyObject, PyResult, Python, ToPyObject,
};
use either::Either;

Expand Down Expand Up @@ -83,13 +82,13 @@ where
match self {
Either::Left(l) => l
.into_pyobject(py)
.map(AnyBound::into_any)
.map(AnyBound::into_bound)
.map(BoundObject::into_any)
.map(BoundObject::into_bound)
.map_err(Into::into),
Either::Right(r) => r
.into_pyobject(py)
.map(AnyBound::into_any)
.map(AnyBound::into_bound)
.map(BoundObject::into_any)
.map(BoundObject::into_bound)
.map_err(Into::into),
}
}
Expand Down
8 changes: 4 additions & 4 deletions src/conversions/hashbrown.rs
Original file line number Diff line number Diff line change
Expand Up @@ -17,15 +17,15 @@
//! Note that you must use compatible versions of hashbrown and PyO3.
//! The required hashbrown version may vary based on the version of PyO3.
use crate::{
conversion::{AnyBound, IntoPyObject},
conversion::IntoPyObject,
types::{
any::PyAnyMethods,
dict::PyDictMethods,
frozenset::PyFrozenSetMethods,
set::{new_from_iter, try_new_from_iter, PySetMethods},
IntoPyDict, PyDict, PyFrozenSet, PySet,
},
Bound, FromPyObject, IntoPy, PyAny, PyErr, PyObject, PyResult, Python, ToPyObject,
Bound, BoundObject, FromPyObject, IntoPy, PyAny, PyErr, PyObject, PyResult, Python, ToPyObject,
};
use std::{cmp, hash};

Expand Down Expand Up @@ -131,8 +131,8 @@ where
py,
self.into_iter().map(|item| {
item.into_pyobject(py)
.map(AnyBound::into_any)
.map(AnyBound::unbind)
.map(BoundObject::into_any)
.map(BoundObject::unbind)
.map_err(Into::into)
}),
)
Expand Down
4 changes: 2 additions & 2 deletions src/conversions/indexmap.rs
Original file line number Diff line number Diff line change
Expand Up @@ -87,9 +87,9 @@
//! # if another hash table was used, the order could be random
//! ```

use crate::conversion::{AnyBound, IntoPyObject};
use crate::conversion::IntoPyObject;
use crate::types::*;
use crate::{Bound, FromPyObject, IntoPy, PyErr, PyObject, Python, ToPyObject};
use crate::{Bound, BoundObject, FromPyObject, IntoPy, PyErr, PyObject, Python, ToPyObject};
use std::{cmp, hash};

impl<K, V, H> ToPyObject for indexmap::IndexMap<K, V, H>
Expand Down
10 changes: 5 additions & 5 deletions src/conversions/smallvec.rs
Original file line number Diff line number Diff line change
Expand Up @@ -15,7 +15,7 @@
//!
//! Note that you must use compatible versions of smallvec and PyO3.
//! The required smallvec version may vary based on the version of PyO3.
use crate::conversion::{AnyBound, IntoPyObject};
use crate::conversion::IntoPyObject;
use crate::exceptions::PyTypeError;
#[cfg(feature = "experimental-inspect")]
use crate::inspect::types::TypeInfo;
Expand All @@ -24,8 +24,8 @@ use crate::types::list::{new_from_iter, try_new_from_iter};
use crate::types::{PyList, PySequence, PyString};
use crate::PyErr;
use crate::{
err::DowncastError, ffi, Bound, FromPyObject, IntoPy, PyAny, PyObject, PyResult, Python,
ToPyObject,
err::DowncastError, ffi, Bound, BoundObject, FromPyObject, IntoPy, PyAny, PyObject, PyResult,
Python, ToPyObject,
};
use smallvec::{Array, SmallVec};

Expand Down Expand Up @@ -69,8 +69,8 @@ where
fn into_pyobject(self, py: Python<'py>) -> Result<Self::Output, Self::Error> {
let mut iter = self.into_iter().map(|e| {
e.into_pyobject(py)
.map(AnyBound::into_any)
.map(AnyBound::unbind)
.map(BoundObject::into_any)
.map(BoundObject::unbind)
.map_err(Into::into)
});
try_new_from_iter(py, &mut iter)
Expand Down
2 changes: 1 addition & 1 deletion src/conversions/std/array.rs
Original file line number Diff line number Diff line change
Expand Up @@ -47,7 +47,7 @@ where
type Error = T::Error;

fn into_pyobject(self, py: Python<'py>) -> Result<Self::Output, Self::Error> {
use crate::conversion::AnyBound;
use crate::BoundObject;
unsafe {
let len = N as ffi::Py_ssize_t;

Expand Down
4 changes: 2 additions & 2 deletions src/conversions/std/map.rs
Original file line number Diff line number Diff line change
Expand Up @@ -3,10 +3,10 @@ use std::{cmp, collections, hash};
#[cfg(feature = "experimental-inspect")]
use crate::inspect::types::TypeInfo;
use crate::{
conversion::{AnyBound, IntoPyObject},
conversion::IntoPyObject,
instance::Bound,
types::{any::PyAnyMethods, dict::PyDictMethods, IntoPyDict, PyDict},
FromPyObject, IntoPy, PyAny, PyErr, PyObject, Python, ToPyObject,
BoundObject, FromPyObject, IntoPy, PyAny, PyErr, PyObject, Python, ToPyObject,
};

impl<K, V, H> ToPyObject for collections::HashMap<K, V, H>
Expand Down
9 changes: 4 additions & 5 deletions src/conversions/std/option.rs
Original file line number Diff line number Diff line change
@@ -1,7 +1,6 @@
use crate::conversion::AnyBound;
use crate::{
conversion::IntoPyObject, ffi, types::any::PyAnyMethods, AsPyPointer, Bound, FromPyObject,
IntoPy, PyAny, PyObject, PyResult, Python, ToPyObject,
conversion::IntoPyObject, ffi, types::any::PyAnyMethods, AsPyPointer, Bound, BoundObject,
FromPyObject, IntoPy, PyAny, PyObject, PyResult, Python, ToPyObject,
};

/// `Option::Some<T>` is converted like `T`.
Expand Down Expand Up @@ -38,8 +37,8 @@ where
|| Ok(py.None().into_bound(py)),
|val| {
val.into_pyobject(py)
.map(AnyBound::into_any)
.map(AnyBound::into_bound)
.map(BoundObject::into_any)
.map(BoundObject::into_bound)
},
)
}
Expand Down
12 changes: 6 additions & 6 deletions src/conversions/std/set.rs
Original file line number Diff line number Diff line change
Expand Up @@ -3,15 +3,15 @@ use std::{cmp, collections, hash};
#[cfg(feature = "experimental-inspect")]
use crate::inspect::types::TypeInfo;
use crate::{
conversion::{AnyBound, IntoPyObject},
conversion::IntoPyObject,
instance::Bound,
types::{
any::PyAnyMethods,
frozenset::PyFrozenSetMethods,
set::{new_from_iter, try_new_from_iter, PySetMethods},
PyFrozenSet, PySet,
},
FromPyObject, IntoPy, PyAny, PyErr, PyObject, PyResult, Python, ToPyObject,
BoundObject, FromPyObject, IntoPy, PyAny, PyErr, PyObject, PyResult, Python, ToPyObject,
};

impl<T, S> ToPyObject for collections::HashSet<T, S>
Expand Down Expand Up @@ -69,8 +69,8 @@ where
py,
self.into_iter().map(|item| {
item.into_pyobject(py)
.map(AnyBound::into_any)
.map(AnyBound::unbind)
.map(BoundObject::into_any)
.map(BoundObject::unbind)
.map_err(Into::into)
}),
)
Expand Down Expand Up @@ -131,8 +131,8 @@ where
py,
self.into_iter().map(|item| {
item.into_pyobject(py)
.map(AnyBound::into_any)
.map(AnyBound::unbind)
.map(BoundObject::into_any)
.map(BoundObject::unbind)
.map_err(Into::into)
}),
)
Expand Down
8 changes: 4 additions & 4 deletions src/conversions/std/vec.rs
Original file line number Diff line number Diff line change
@@ -1,9 +1,9 @@
use crate::conversion::{AnyBound, IntoPyObject};
use crate::conversion::IntoPyObject;
#[cfg(feature = "experimental-inspect")]
use crate::inspect::types::TypeInfo;
use crate::types::list::{new_from_iter, try_new_from_iter};
use crate::types::PyList;
use crate::{Bound, IntoPy, PyErr, PyObject, Python, ToPyObject};
use crate::{Bound, BoundObject, IntoPy, PyErr, PyObject, Python, ToPyObject};

impl<T> ToPyObject for [T]
where
Expand Down Expand Up @@ -53,8 +53,8 @@ where
fn into_pyobject(self, py: Python<'py>) -> Result<Self::Output, Self::Error> {
let mut iter = self.into_iter().map(|e| {
e.into_pyobject(py)
.map(AnyBound::into_any)
.map(AnyBound::unbind)
.map(BoundObject::into_any)
.map(BoundObject::unbind)
.map_err(Into::into)
});

Expand Down
6 changes: 3 additions & 3 deletions src/impl_/wrap.rs
Original file line number Diff line number Diff line change
@@ -1,8 +1,8 @@
use std::convert::Infallible;

use crate::conversion::AnyBound;
use crate::{
conversion::IntoPyObject, ffi, types::PyNone, Bound, IntoPy, PyErr, PyObject, PyResult, Python,
conversion::IntoPyObject, ffi, types::PyNone, Bound, BoundObject, IntoPy, PyErr, PyObject,
PyResult, Python,
};

/// Used to wrap values in `Option<T>` for default arguments.
Expand Down Expand Up @@ -129,7 +129,7 @@ impl IntoPyObjectTag {
PyErr: From<T::Error>,
{
obj.and_then(|obj| obj.into_pyobject(py).map_err(Into::into))
.map(AnyBound::into_bound)
.map(BoundObject::into_bound)
.map(Bound::into_ptr)
}

Expand Down
Loading

0 comments on commit 8d7e0c0

Please sign in to comment.