make Bound and Borrowed types public API

This commit is contained in:
David Hewitt 2023-12-21 12:20:33 +00:00
parent 704e9fc7b5
commit a09b9f8834
10 changed files with 23 additions and 24 deletions

View file

@ -0,0 +1 @@
Add `Bound<T>` and `Borrowed<T>` smart pointers as a new API for accessing Python objects.

View file

@ -45,7 +45,7 @@ pub unsafe trait PyNativeType: Sized {
/// A GIL-attached equivalent to `Py`. /// A GIL-attached equivalent to `Py`.
#[repr(transparent)] #[repr(transparent)]
pub(crate) struct Bound<'py, T>(Python<'py>, ManuallyDrop<Py<T>>); pub struct Bound<'py, T>(Python<'py>, ManuallyDrop<Py<T>>);
impl<'py> Bound<'py, PyAny> { impl<'py> Bound<'py, PyAny> {
/// Constructs a new Bound from a pointer. Panics if ptr is null. /// Constructs a new Bound from a pointer. Panics if ptr is null.
@ -224,7 +224,7 @@ unsafe impl<T> AsPyPointer for Bound<'_, T> {
/// ///
/// Similarly, this type is `Copy` and `Clone`, like a shared reference (`&T`). /// Similarly, this type is `Copy` and `Clone`, like a shared reference (`&T`).
#[repr(transparent)] #[repr(transparent)]
pub(crate) struct Borrowed<'a, 'py, T>(NonNull<ffi::PyObject>, PhantomData<&'a Py<T>>, Python<'py>); pub struct Borrowed<'a, 'py, T>(NonNull<ffi::PyObject>, PhantomData<&'a Py<T>>, Python<'py>);
impl<'py, T> Borrowed<'_, 'py, T> { impl<'py, T> Borrowed<'_, 'py, T> {
/// Creates a new owned `Bound` from this borrowed reference by increasing the reference count. /// Creates a new owned `Bound` from this borrowed reference by increasing the reference count.

View file

@ -12,7 +12,7 @@ pub use crate::conversion::{FromPyObject, IntoPy, ToPyObject};
#[allow(deprecated)] #[allow(deprecated)]
pub use crate::conversion::{PyTryFrom, PyTryInto}; pub use crate::conversion::{PyTryFrom, PyTryInto};
pub use crate::err::{PyErr, PyResult}; pub use crate::err::{PyErr, PyResult};
pub use crate::instance::{Py, PyObject}; pub use crate::instance::{Borrowed, Bound, Py, PyObject};
pub use crate::marker::Python; pub use crate::marker::Python;
pub use crate::pycell::{PyCell, PyRef, PyRefMut}; pub use crate::pycell::{PyCell, PyRef, PyRefMut};
pub use crate::pyclass_init::PyClassInitializer; pub use crate::pyclass_init::PyClassInitializer;
@ -24,15 +24,13 @@ pub use pyo3_macros::{pyclass, pyfunction, pymethods, pymodule, FromPyObject};
#[cfg(feature = "macros")] #[cfg(feature = "macros")]
pub use crate::wrap_pyfunction; pub use crate::wrap_pyfunction;
// Expected to become public API in 0.21 pub use crate::types::any::PyAnyMethods;
// pub(crate) use crate::instance::Bound; // Will be stabilized with a different name pub use crate::types::boolobject::PyBoolMethods;
// pub(crate) use crate::types::any::PyAnyMethods; pub use crate::types::bytearray::PyByteArrayMethods;
// pub(crate) use crate::types::boolobject::PyBoolMethods; pub use crate::types::bytes::PyBytesMethods;
// pub(crate) use crate::types::bytearray::PyByteArrayMethods; pub use crate::types::dict::PyDictMethods;
// pub(crate) use crate::types::bytes::PyBytesMethods; pub use crate::types::float::PyFloatMethods;
// pub(crate) use crate::types::dict::PyDictMethods; pub use crate::types::list::PyListMethods;
// pub(crate) use crate::types::float::PyFloatMethods; pub use crate::types::mapping::PyMappingMethods;
// pub(crate) use crate::types::list::PyListMethods; pub use crate::types::sequence::PySequenceMethods;
// pub(crate) use crate::types::mapping::PyMappingMethods; pub use crate::types::string::PyStringMethods;
// pub(crate) use crate::types::sequence::PySequenceMethods;
// pub(crate) use crate::types::string::PyStringMethods;

View file

@ -996,7 +996,7 @@ impl PyAny {
/// It is recommended you import this trait via `use pyo3::prelude::*` rather than /// It is recommended you import this trait via `use pyo3::prelude::*` rather than
/// by importing this trait directly. /// by importing this trait directly.
#[doc(alias = "PyAny")] #[doc(alias = "PyAny")]
pub(crate) trait PyAnyMethods<'py> { pub trait PyAnyMethods<'py> {
/// Returns whether `self` and `other` point to the same object. To compare /// Returns whether `self` and `other` point to the same object. To compare
/// the equality of two objects (the `==` operator), use [`eq`](PyAny::eq). /// the equality of two objects (the `==` operator), use [`eq`](PyAny::eq).
/// ///

View file

@ -99,7 +99,7 @@ impl PyBytes {
/// syntax these methods are separated into a trait, because stable Rust does not yet support /// syntax these methods are separated into a trait, because stable Rust does not yet support
/// `arbitrary_self_types`. /// `arbitrary_self_types`.
#[doc(alias = "PyBytes")] #[doc(alias = "PyBytes")]
pub(crate) trait PyBytesMethods<'py> { pub trait PyBytesMethods<'py> {
/// Gets the Python string as a byte slice. /// Gets the Python string as a byte slice.
fn as_bytes(&self) -> &[u8]; fn as_bytes(&self) -> &[u8];
} }

View file

@ -265,7 +265,7 @@ impl PyDict {
/// syntax these methods are separated into a trait, because stable Rust does not yet support /// syntax these methods are separated into a trait, because stable Rust does not yet support
/// `arbitrary_self_types`. /// `arbitrary_self_types`.
#[doc(alias = "PyDict")] #[doc(alias = "PyDict")]
pub(crate) trait PyDictMethods<'py> { pub trait PyDictMethods<'py> {
/// Returns a new dictionary that contains the same key-value pairs as self. /// Returns a new dictionary that contains the same key-value pairs as self.
/// ///
/// This is equivalent to the Python expression `self.copy()`. /// This is equivalent to the Python expression `self.copy()`.
@ -545,7 +545,7 @@ impl<'a> IntoIterator for &'a PyDict {
} }
/// PyO3 implementation of an iterator for a Python `dict` object. /// PyO3 implementation of an iterator for a Python `dict` object.
pub(crate) struct PyDictIterator2<'py> { pub struct PyDictIterator2<'py> {
dict: Bound<'py, PyDict>, dict: Bound<'py, PyDict>,
ppos: ffi::Py_ssize_t, ppos: ffi::Py_ssize_t,
di_used: ffi::Py_ssize_t, di_used: ffi::Py_ssize_t,

View file

@ -256,7 +256,7 @@ index_impls!(PyList, "list", PyList::len, PyList::get_slice);
/// syntax these methods are separated into a trait, because stable Rust does not yet support /// syntax these methods are separated into a trait, because stable Rust does not yet support
/// `arbitrary_self_types`. /// `arbitrary_self_types`.
#[doc(alias = "PyList")] #[doc(alias = "PyList")]
pub(crate) trait PyListMethods<'py> { pub trait PyListMethods<'py> {
/// Returns the length of the list. /// Returns the length of the list.
fn len(&self) -> usize; fn len(&self) -> usize;
@ -594,7 +594,7 @@ impl<'a> IntoIterator for &'a PyList {
} }
/// Used by `PyList::iter()`. /// Used by `PyList::iter()`.
pub(crate) struct PyListIterator2<'py> { pub struct PyListIterator2<'py> {
list: Bound<'py, PyList>, list: Bound<'py, PyList>,
index: usize, index: usize,
length: usize, length: usize,

View file

@ -117,7 +117,7 @@ impl PyMapping {
/// syntax these methods are separated into a trait, because stable Rust does not yet support /// syntax these methods are separated into a trait, because stable Rust does not yet support
/// `arbitrary_self_types`. /// `arbitrary_self_types`.
#[doc(alias = "PyMapping")] #[doc(alias = "PyMapping")]
pub(crate) trait PyMappingMethods<'py> { pub trait PyMappingMethods<'py> {
/// Returns the number of objects in the mapping. /// Returns the number of objects in the mapping.
/// ///
/// This is equivalent to the Python expression `len(self)`. /// This is equivalent to the Python expression `len(self)`.

View file

@ -200,7 +200,7 @@ impl PySequence {
/// syntax these methods are separated into a trait, because stable Rust does not yet support /// syntax these methods are separated into a trait, because stable Rust does not yet support
/// `arbitrary_self_types`. /// `arbitrary_self_types`.
#[doc(alias = "PySequence")] #[doc(alias = "PySequence")]
pub(crate) trait PySequenceMethods<'py> { pub trait PySequenceMethods<'py> {
/// Returns the number of objects in sequence. /// Returns the number of objects in sequence.
/// ///
/// This is equivalent to the Python expression `len(self)`. /// This is equivalent to the Python expression `len(self)`.

View file

@ -239,7 +239,7 @@ impl PyString {
/// syntax these methods are separated into a trait, because stable Rust does not yet support /// syntax these methods are separated into a trait, because stable Rust does not yet support
/// `arbitrary_self_types`. /// `arbitrary_self_types`.
#[doc(alias = "PyString")] #[doc(alias = "PyString")]
pub(crate) trait PyStringMethods<'py> { pub trait PyStringMethods<'py> {
/// Gets the Python string as a Rust UTF-8 string slice. /// Gets the Python string as a Rust UTF-8 string slice.
/// ///
/// Returns a `UnicodeEncodeError` if the input is not valid unicode /// Returns a `UnicodeEncodeError` if the input is not valid unicode