Skip to content

Commit

Permalink
split more impl blocks (#4175)
Browse files Browse the repository at this point in the history
  • Loading branch information
Icxolu authored May 11, 2024
1 parent 444be3b commit 033caa8
Show file tree
Hide file tree
Showing 10 changed files with 235 additions and 202 deletions.
36 changes: 20 additions & 16 deletions src/types/boolobject.rs
Original file line number Diff line number Diff line change
@@ -1,9 +1,11 @@
#[cfg(feature = "experimental-inspect")]
use crate::inspect::types::TypeInfo;
#[cfg(feature = "gil-refs")]
use crate::PyNativeType;
use crate::{
exceptions::PyTypeError, ffi, ffi_ptr_ext::FfiPtrExt, instance::Bound,
types::typeobject::PyTypeMethods, Borrowed, FromPyObject, IntoPy, PyAny, PyNativeType,
PyObject, PyResult, Python, ToPyObject,
types::typeobject::PyTypeMethods, Borrowed, FromPyObject, IntoPy, PyAny, PyObject, PyResult,
Python, ToPyObject,
};

use super::any::PyAnyMethods;
Expand All @@ -15,20 +17,6 @@ pub struct PyBool(PyAny);
pyobject_native_type!(PyBool, ffi::PyObject, pyobject_native_static_type_object!(ffi::PyBool_Type), #checkfunction=ffi::PyBool_Check);

impl PyBool {
/// Deprecated form of [`PyBool::new_bound`]
#[cfg(feature = "gil-refs")]
#[deprecated(
since = "0.21.0",
note = "`PyBool::new` will be replaced by `PyBool::new_bound` in a future PyO3 version"
)]
#[inline]
pub fn new(py: Python<'_>, val: bool) -> &PyBool {
#[allow(deprecated)]
unsafe {
py.from_borrowed_ptr(if val { ffi::Py_True() } else { ffi::Py_False() })
}
}

/// Depending on `val`, returns `true` or `false`.
///
/// # Note
Expand All @@ -42,6 +30,22 @@ impl PyBool {
.downcast_unchecked()
}
}
}

#[cfg(feature = "gil-refs")]
impl PyBool {
/// Deprecated form of [`PyBool::new_bound`]
#[deprecated(
since = "0.21.0",
note = "`PyBool::new` will be replaced by `PyBool::new_bound` in a future PyO3 version"
)]
#[inline]
pub fn new(py: Python<'_>, val: bool) -> &PyBool {
#[allow(deprecated)]
unsafe {
py.from_borrowed_ptr(if val { ffi::Py_True() } else { ffi::Py_False() })
}
}

/// Gets whether this boolean is `true`.
#[inline]
Expand Down
80 changes: 40 additions & 40 deletions src/types/bytearray.rs
Original file line number Diff line number Diff line change
Expand Up @@ -3,9 +3,9 @@ use crate::ffi_ptr_ext::FfiPtrExt;
use crate::instance::{Borrowed, Bound};
use crate::py_result_ext::PyResultExt;
use crate::types::any::PyAnyMethods;
use crate::{ffi, PyAny, Python};
#[cfg(feature = "gil-refs")]
use crate::AsPyPointer;
use crate::{ffi, PyAny, PyNativeType, Python};
use crate::{AsPyPointer, PyNativeType};
use std::os::raw::c_char;
use std::slice;

Expand All @@ -16,16 +16,6 @@ pub struct PyByteArray(PyAny);
pyobject_native_type_core!(PyByteArray, pyobject_native_static_type_object!(ffi::PyByteArray_Type), #checkfunction=ffi::PyByteArray_Check);

impl PyByteArray {
/// Deprecated form of [`PyByteArray::new_bound`]
#[cfg(feature = "gil-refs")]
#[deprecated(
since = "0.21.0",
note = "`PyByteArray::new` will be replaced by `PyByteArray::new_bound` in a future PyO3 version"
)]
pub fn new<'py>(py: Python<'py>, src: &[u8]) -> &'py PyByteArray {
Self::new_bound(py, src).into_gil_ref()
}

/// Creates a new Python bytearray object.
///
/// The byte string is initialized by copying the data from the `&[u8]`.
Expand All @@ -39,19 +29,6 @@ impl PyByteArray {
}
}

/// Deprecated form of [`PyByteArray::new_bound_with`]
#[cfg(feature = "gil-refs")]
#[deprecated(
since = "0.21.0",
note = "`PyByteArray::new_with` will be replaced by `PyByteArray::new_bound_with` in a future PyO3 version"
)]
pub fn new_with<F>(py: Python<'_>, len: usize, init: F) -> PyResult<&PyByteArray>
where
F: FnOnce(&mut [u8]) -> PyResult<()>,
{
Self::new_bound_with(py, len, init).map(Bound::into_gil_ref)
}

/// Creates a new Python `bytearray` object with an `init` closure to write its contents.
/// Before calling `init` the bytearray is zero-initialised.
/// * If Python raises a MemoryError on the allocation, `new_with` will return
Expand Down Expand Up @@ -101,16 +78,6 @@ impl PyByteArray {
}
}

/// Deprecated form of [`PyByteArray::from_bound`]
#[cfg(feature = "gil-refs")]
#[deprecated(
since = "0.21.0",
note = "`PyByteArray::from` will be replaced by `PyByteArray::from_bound` in a future PyO3 version"
)]
pub fn from(src: &PyAny) -> PyResult<&PyByteArray> {
PyByteArray::from_bound(&src.as_borrowed()).map(Bound::into_gil_ref)
}

/// Creates a new Python `bytearray` object from another Python object that
/// implements the buffer protocol.
pub fn from_bound<'py>(src: &Bound<'py, PyAny>) -> PyResult<Bound<'py, PyByteArray>> {
Expand All @@ -120,6 +87,39 @@ impl PyByteArray {
.downcast_into_unchecked()
}
}
}

#[cfg(feature = "gil-refs")]
impl PyByteArray {
/// Deprecated form of [`PyByteArray::new_bound`]
#[deprecated(
since = "0.21.0",
note = "`PyByteArray::new` will be replaced by `PyByteArray::new_bound` in a future PyO3 version"
)]
pub fn new<'py>(py: Python<'py>, src: &[u8]) -> &'py PyByteArray {
Self::new_bound(py, src).into_gil_ref()
}

/// Deprecated form of [`PyByteArray::new_bound_with`]
#[deprecated(
since = "0.21.0",
note = "`PyByteArray::new_with` will be replaced by `PyByteArray::new_bound_with` in a future PyO3 version"
)]
pub fn new_with<F>(py: Python<'_>, len: usize, init: F) -> PyResult<&PyByteArray>
where
F: FnOnce(&mut [u8]) -> PyResult<()>,
{
Self::new_bound_with(py, len, init).map(Bound::into_gil_ref)
}

/// Deprecated form of [`PyByteArray::from_bound`]
#[deprecated(
since = "0.21.0",
note = "`PyByteArray::from` will be replaced by `PyByteArray::from_bound` in a future PyO3 version"
)]
pub fn from(src: &PyAny) -> PyResult<&PyByteArray> {
PyByteArray::from_bound(&src.as_borrowed()).map(Bound::into_gil_ref)
}

/// Gets the length of the bytearray.
#[inline]
Expand Down Expand Up @@ -300,7 +300,7 @@ pub trait PyByteArrayMethods<'py>: crate::sealed::Sealed {
///
/// # Safety
///
/// See the safety requirements of [`PyByteArray::as_bytes`] and [`PyByteArray::as_bytes_mut`].
/// See the safety requirements of [`PyByteArrayMethods::as_bytes`] and [`PyByteArrayMethods::as_bytes_mut`].
fn data(&self) -> *mut u8;

/// Extracts a slice of the `ByteArray`'s entire buffer.
Expand All @@ -311,7 +311,7 @@ pub trait PyByteArrayMethods<'py>: crate::sealed::Sealed {
/// undefined.
///
/// These mutations may occur in Python code as well as from Rust:
/// - Calling methods like [`PyByteArray::as_bytes_mut`] and [`PyByteArray::resize`] will
/// - Calling methods like [`PyByteArrayMethods::as_bytes_mut`] and [`PyByteArrayMethods::resize`] will
/// invalidate the slice.
/// - Actions like dropping objects or raising exceptions can invoke `__del__`methods or signal
/// handlers, which may execute arbitrary Python code. This means that if Python code has a
Expand Down Expand Up @@ -405,7 +405,7 @@ pub trait PyByteArrayMethods<'py>: crate::sealed::Sealed {
/// # Safety
///
/// Any other accesses of the `bytearray`'s buffer invalidate the slice. If it is used
/// afterwards, the behavior is undefined. The safety requirements of [`PyByteArray::as_bytes`]
/// afterwards, the behavior is undefined. The safety requirements of [`PyByteArrayMethods::as_bytes`]
/// apply to this function as well.
#[allow(clippy::mut_from_ref)]
unsafe fn as_bytes_mut(&self) -> &mut [u8];
Expand All @@ -432,8 +432,8 @@ pub trait PyByteArrayMethods<'py>: crate::sealed::Sealed {

/// Resizes the bytearray object to the new length `len`.
///
/// Note that this will invalidate any pointers obtained by [PyByteArray::data], as well as
/// any (unsafe) slices obtained from [PyByteArray::as_bytes] and [PyByteArray::as_bytes_mut].
/// Note that this will invalidate any pointers obtained by [PyByteArrayMethods::data], as well as
/// any (unsafe) slices obtained from [PyByteArrayMethods::as_bytes] and [PyByteArrayMethods::as_bytes_mut].
fn resize(&self, len: usize) -> PyResult<()>;
}

Expand Down
77 changes: 40 additions & 37 deletions src/types/bytes.rs
Original file line number Diff line number Diff line change
@@ -1,7 +1,9 @@
use crate::ffi_ptr_ext::FfiPtrExt;
use crate::instance::{Borrowed, Bound};
use crate::types::any::PyAnyMethods;
use crate::{ffi, Py, PyAny, PyNativeType, PyResult, Python};
#[cfg(feature = "gil-refs")]
use crate::PyNativeType;
use crate::{ffi, Py, PyAny, PyResult, Python};
use std::ops::Index;
use std::os::raw::c_char;
use std::slice::SliceIndex;
Expand All @@ -16,16 +18,6 @@ pub struct PyBytes(PyAny);
pyobject_native_type_core!(PyBytes, pyobject_native_static_type_object!(ffi::PyBytes_Type), #checkfunction=ffi::PyBytes_Check);

impl PyBytes {
/// Deprecated form of [`PyBytes::new_bound`].
#[cfg(feature = "gil-refs")]
#[deprecated(
since = "0.21.0",
note = "`PyBytes::new` will be replaced by `PyBytes::new_bound` in a future PyO3 version"
)]
pub fn new<'p>(py: Python<'p>, s: &[u8]) -> &'p PyBytes {
Self::new_bound(py, s).into_gil_ref()
}

/// Creates a new Python bytestring object.
/// The bytestring is initialized by copying the data from the `&[u8]`.
///
Expand All @@ -40,19 +32,6 @@ impl PyBytes {
}
}

/// Deprecated form of [`PyBytes::new_bound_with`].
#[cfg(feature = "gil-refs")]
#[deprecated(
since = "0.21.0",
note = "`PyBytes::new_with` will be replaced by `PyBytes::new_bound_with` in a future PyO3 version"
)]
pub fn new_with<F>(py: Python<'_>, len: usize, init: F) -> PyResult<&PyBytes>
where
F: FnOnce(&mut [u8]) -> PyResult<()>,
{
Self::new_bound_with(py, len, init).map(Bound::into_gil_ref)
}

/// Creates a new Python `bytes` object with an `init` closure to write its contents.
/// Before calling `init` the bytes' contents are zero-initialised.
/// * If Python raises a MemoryError on the allocation, `new_with` will return
Expand Down Expand Up @@ -95,19 +74,6 @@ impl PyBytes {
}
}

/// Deprecated form of [`PyBytes::bound_from_ptr`].
///
/// # Safety
/// See [`PyBytes::bound_from_ptr`].
#[cfg(feature = "gil-refs")]
#[deprecated(
since = "0.21.0",
note = "`PyBytes::from_ptr` will be replaced by `PyBytes::bound_from_ptr` in a future PyO3 version"
)]
pub unsafe fn from_ptr(py: Python<'_>, ptr: *const u8, len: usize) -> &PyBytes {
Self::bound_from_ptr(py, ptr, len).into_gil_ref()
}

/// Creates a new Python byte string object from a raw pointer and length.
///
/// Panics if out of memory.
Expand All @@ -123,6 +89,42 @@ impl PyBytes {
.assume_owned(py)
.downcast_into_unchecked()
}
}

#[cfg(feature = "gil-refs")]
impl PyBytes {
/// Deprecated form of [`PyBytes::new_bound`].
#[deprecated(
since = "0.21.0",
note = "`PyBytes::new` will be replaced by `PyBytes::new_bound` in a future PyO3 version"
)]
pub fn new<'p>(py: Python<'p>, s: &[u8]) -> &'p PyBytes {
Self::new_bound(py, s).into_gil_ref()
}

/// Deprecated form of [`PyBytes::new_bound_with`].
#[deprecated(
since = "0.21.0",
note = "`PyBytes::new_with` will be replaced by `PyBytes::new_bound_with` in a future PyO3 version"
)]
pub fn new_with<F>(py: Python<'_>, len: usize, init: F) -> PyResult<&PyBytes>
where
F: FnOnce(&mut [u8]) -> PyResult<()>,
{
Self::new_bound_with(py, len, init).map(Bound::into_gil_ref)
}

/// Deprecated form of [`PyBytes::bound_from_ptr`].
///
/// # Safety
/// See [`PyBytes::bound_from_ptr`].
#[deprecated(
since = "0.21.0",
note = "`PyBytes::from_ptr` will be replaced by `PyBytes::bound_from_ptr` in a future PyO3 version"
)]
pub unsafe fn from_ptr(py: Python<'_>, ptr: *const u8, len: usize) -> &PyBytes {
Self::bound_from_ptr(py, ptr, len).into_gil_ref()
}

/// Gets the Python string as a byte slice.
#[inline]
Expand Down Expand Up @@ -172,6 +174,7 @@ impl Py<PyBytes> {
}

/// This is the same way [Vec] is indexed.
#[cfg(feature = "gil-refs")]
impl<I: SliceIndex<[u8]>> Index<I> for PyBytes {
type Output = I::Output;

Expand Down
Loading

0 comments on commit 033caa8

Please sign in to comment.