Skip to content

Commit 033caa8

Browse files
authored
split more impl blocks (#4175)
1 parent 444be3b commit 033caa8

File tree

10 files changed

+235
-202
lines changed

10 files changed

+235
-202
lines changed

src/types/boolobject.rs

Lines changed: 20 additions & 16 deletions
Original file line numberDiff line numberDiff line change
@@ -1,9 +1,11 @@
11
#[cfg(feature = "experimental-inspect")]
22
use crate::inspect::types::TypeInfo;
3+
#[cfg(feature = "gil-refs")]
4+
use crate::PyNativeType;
35
use crate::{
46
exceptions::PyTypeError, ffi, ffi_ptr_ext::FfiPtrExt, instance::Bound,
5-
types::typeobject::PyTypeMethods, Borrowed, FromPyObject, IntoPy, PyAny, PyNativeType,
6-
PyObject, PyResult, Python, ToPyObject,
7+
types::typeobject::PyTypeMethods, Borrowed, FromPyObject, IntoPy, PyAny, PyObject, PyResult,
8+
Python, ToPyObject,
79
};
810

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

1719
impl PyBool {
18-
/// Deprecated form of [`PyBool::new_bound`]
19-
#[cfg(feature = "gil-refs")]
20-
#[deprecated(
21-
since = "0.21.0",
22-
note = "`PyBool::new` will be replaced by `PyBool::new_bound` in a future PyO3 version"
23-
)]
24-
#[inline]
25-
pub fn new(py: Python<'_>, val: bool) -> &PyBool {
26-
#[allow(deprecated)]
27-
unsafe {
28-
py.from_borrowed_ptr(if val { ffi::Py_True() } else { ffi::Py_False() })
29-
}
30-
}
31-
3220
/// Depending on `val`, returns `true` or `false`.
3321
///
3422
/// # Note
@@ -42,6 +30,22 @@ impl PyBool {
4230
.downcast_unchecked()
4331
}
4432
}
33+
}
34+
35+
#[cfg(feature = "gil-refs")]
36+
impl PyBool {
37+
/// Deprecated form of [`PyBool::new_bound`]
38+
#[deprecated(
39+
since = "0.21.0",
40+
note = "`PyBool::new` will be replaced by `PyBool::new_bound` in a future PyO3 version"
41+
)]
42+
#[inline]
43+
pub fn new(py: Python<'_>, val: bool) -> &PyBool {
44+
#[allow(deprecated)]
45+
unsafe {
46+
py.from_borrowed_ptr(if val { ffi::Py_True() } else { ffi::Py_False() })
47+
}
48+
}
4549

4650
/// Gets whether this boolean is `true`.
4751
#[inline]

src/types/bytearray.rs

Lines changed: 40 additions & 40 deletions
Original file line numberDiff line numberDiff line change
@@ -3,9 +3,9 @@ use crate::ffi_ptr_ext::FfiPtrExt;
33
use crate::instance::{Borrowed, Bound};
44
use crate::py_result_ext::PyResultExt;
55
use crate::types::any::PyAnyMethods;
6+
use crate::{ffi, PyAny, Python};
67
#[cfg(feature = "gil-refs")]
7-
use crate::AsPyPointer;
8-
use crate::{ffi, PyAny, PyNativeType, Python};
8+
use crate::{AsPyPointer, PyNativeType};
99
use std::os::raw::c_char;
1010
use std::slice;
1111

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

1818
impl PyByteArray {
19-
/// Deprecated form of [`PyByteArray::new_bound`]
20-
#[cfg(feature = "gil-refs")]
21-
#[deprecated(
22-
since = "0.21.0",
23-
note = "`PyByteArray::new` will be replaced by `PyByteArray::new_bound` in a future PyO3 version"
24-
)]
25-
pub fn new<'py>(py: Python<'py>, src: &[u8]) -> &'py PyByteArray {
26-
Self::new_bound(py, src).into_gil_ref()
27-
}
28-
2919
/// Creates a new Python bytearray object.
3020
///
3121
/// The byte string is initialized by copying the data from the `&[u8]`.
@@ -39,19 +29,6 @@ impl PyByteArray {
3929
}
4030
}
4131

42-
/// Deprecated form of [`PyByteArray::new_bound_with`]
43-
#[cfg(feature = "gil-refs")]
44-
#[deprecated(
45-
since = "0.21.0",
46-
note = "`PyByteArray::new_with` will be replaced by `PyByteArray::new_bound_with` in a future PyO3 version"
47-
)]
48-
pub fn new_with<F>(py: Python<'_>, len: usize, init: F) -> PyResult<&PyByteArray>
49-
where
50-
F: FnOnce(&mut [u8]) -> PyResult<()>,
51-
{
52-
Self::new_bound_with(py, len, init).map(Bound::into_gil_ref)
53-
}
54-
5532
/// Creates a new Python `bytearray` object with an `init` closure to write its contents.
5633
/// Before calling `init` the bytearray is zero-initialised.
5734
/// * If Python raises a MemoryError on the allocation, `new_with` will return
@@ -101,16 +78,6 @@ impl PyByteArray {
10178
}
10279
}
10380

104-
/// Deprecated form of [`PyByteArray::from_bound`]
105-
#[cfg(feature = "gil-refs")]
106-
#[deprecated(
107-
since = "0.21.0",
108-
note = "`PyByteArray::from` will be replaced by `PyByteArray::from_bound` in a future PyO3 version"
109-
)]
110-
pub fn from(src: &PyAny) -> PyResult<&PyByteArray> {
111-
PyByteArray::from_bound(&src.as_borrowed()).map(Bound::into_gil_ref)
112-
}
113-
11481
/// Creates a new Python `bytearray` object from another Python object that
11582
/// implements the buffer protocol.
11683
pub fn from_bound<'py>(src: &Bound<'py, PyAny>) -> PyResult<Bound<'py, PyByteArray>> {
@@ -120,6 +87,39 @@ impl PyByteArray {
12087
.downcast_into_unchecked()
12188
}
12289
}
90+
}
91+
92+
#[cfg(feature = "gil-refs")]
93+
impl PyByteArray {
94+
/// Deprecated form of [`PyByteArray::new_bound`]
95+
#[deprecated(
96+
since = "0.21.0",
97+
note = "`PyByteArray::new` will be replaced by `PyByteArray::new_bound` in a future PyO3 version"
98+
)]
99+
pub fn new<'py>(py: Python<'py>, src: &[u8]) -> &'py PyByteArray {
100+
Self::new_bound(py, src).into_gil_ref()
101+
}
102+
103+
/// Deprecated form of [`PyByteArray::new_bound_with`]
104+
#[deprecated(
105+
since = "0.21.0",
106+
note = "`PyByteArray::new_with` will be replaced by `PyByteArray::new_bound_with` in a future PyO3 version"
107+
)]
108+
pub fn new_with<F>(py: Python<'_>, len: usize, init: F) -> PyResult<&PyByteArray>
109+
where
110+
F: FnOnce(&mut [u8]) -> PyResult<()>,
111+
{
112+
Self::new_bound_with(py, len, init).map(Bound::into_gil_ref)
113+
}
114+
115+
/// Deprecated form of [`PyByteArray::from_bound`]
116+
#[deprecated(
117+
since = "0.21.0",
118+
note = "`PyByteArray::from` will be replaced by `PyByteArray::from_bound` in a future PyO3 version"
119+
)]
120+
pub fn from(src: &PyAny) -> PyResult<&PyByteArray> {
121+
PyByteArray::from_bound(&src.as_borrowed()).map(Bound::into_gil_ref)
122+
}
123123

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

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

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

src/types/bytes.rs

Lines changed: 40 additions & 37 deletions
Original file line numberDiff line numberDiff line change
@@ -1,7 +1,9 @@
11
use crate::ffi_ptr_ext::FfiPtrExt;
22
use crate::instance::{Borrowed, Bound};
33
use crate::types::any::PyAnyMethods;
4-
use crate::{ffi, Py, PyAny, PyNativeType, PyResult, Python};
4+
#[cfg(feature = "gil-refs")]
5+
use crate::PyNativeType;
6+
use crate::{ffi, Py, PyAny, PyResult, Python};
57
use std::ops::Index;
68
use std::os::raw::c_char;
79
use std::slice::SliceIndex;
@@ -16,16 +18,6 @@ pub struct PyBytes(PyAny);
1618
pyobject_native_type_core!(PyBytes, pyobject_native_static_type_object!(ffi::PyBytes_Type), #checkfunction=ffi::PyBytes_Check);
1719

1820
impl PyBytes {
19-
/// Deprecated form of [`PyBytes::new_bound`].
20-
#[cfg(feature = "gil-refs")]
21-
#[deprecated(
22-
since = "0.21.0",
23-
note = "`PyBytes::new` will be replaced by `PyBytes::new_bound` in a future PyO3 version"
24-
)]
25-
pub fn new<'p>(py: Python<'p>, s: &[u8]) -> &'p PyBytes {
26-
Self::new_bound(py, s).into_gil_ref()
27-
}
28-
2921
/// Creates a new Python bytestring object.
3022
/// The bytestring is initialized by copying the data from the `&[u8]`.
3123
///
@@ -40,19 +32,6 @@ impl PyBytes {
4032
}
4133
}
4234

43-
/// Deprecated form of [`PyBytes::new_bound_with`].
44-
#[cfg(feature = "gil-refs")]
45-
#[deprecated(
46-
since = "0.21.0",
47-
note = "`PyBytes::new_with` will be replaced by `PyBytes::new_bound_with` in a future PyO3 version"
48-
)]
49-
pub fn new_with<F>(py: Python<'_>, len: usize, init: F) -> PyResult<&PyBytes>
50-
where
51-
F: FnOnce(&mut [u8]) -> PyResult<()>,
52-
{
53-
Self::new_bound_with(py, len, init).map(Bound::into_gil_ref)
54-
}
55-
5635
/// Creates a new Python `bytes` object with an `init` closure to write its contents.
5736
/// Before calling `init` the bytes' contents are zero-initialised.
5837
/// * If Python raises a MemoryError on the allocation, `new_with` will return
@@ -95,19 +74,6 @@ impl PyBytes {
9574
}
9675
}
9776

98-
/// Deprecated form of [`PyBytes::bound_from_ptr`].
99-
///
100-
/// # Safety
101-
/// See [`PyBytes::bound_from_ptr`].
102-
#[cfg(feature = "gil-refs")]
103-
#[deprecated(
104-
since = "0.21.0",
105-
note = "`PyBytes::from_ptr` will be replaced by `PyBytes::bound_from_ptr` in a future PyO3 version"
106-
)]
107-
pub unsafe fn from_ptr(py: Python<'_>, ptr: *const u8, len: usize) -> &PyBytes {
108-
Self::bound_from_ptr(py, ptr, len).into_gil_ref()
109-
}
110-
11177
/// Creates a new Python byte string object from a raw pointer and length.
11278
///
11379
/// Panics if out of memory.
@@ -123,6 +89,42 @@ impl PyBytes {
12389
.assume_owned(py)
12490
.downcast_into_unchecked()
12591
}
92+
}
93+
94+
#[cfg(feature = "gil-refs")]
95+
impl PyBytes {
96+
/// Deprecated form of [`PyBytes::new_bound`].
97+
#[deprecated(
98+
since = "0.21.0",
99+
note = "`PyBytes::new` will be replaced by `PyBytes::new_bound` in a future PyO3 version"
100+
)]
101+
pub fn new<'p>(py: Python<'p>, s: &[u8]) -> &'p PyBytes {
102+
Self::new_bound(py, s).into_gil_ref()
103+
}
104+
105+
/// Deprecated form of [`PyBytes::new_bound_with`].
106+
#[deprecated(
107+
since = "0.21.0",
108+
note = "`PyBytes::new_with` will be replaced by `PyBytes::new_bound_with` in a future PyO3 version"
109+
)]
110+
pub fn new_with<F>(py: Python<'_>, len: usize, init: F) -> PyResult<&PyBytes>
111+
where
112+
F: FnOnce(&mut [u8]) -> PyResult<()>,
113+
{
114+
Self::new_bound_with(py, len, init).map(Bound::into_gil_ref)
115+
}
116+
117+
/// Deprecated form of [`PyBytes::bound_from_ptr`].
118+
///
119+
/// # Safety
120+
/// See [`PyBytes::bound_from_ptr`].
121+
#[deprecated(
122+
since = "0.21.0",
123+
note = "`PyBytes::from_ptr` will be replaced by `PyBytes::bound_from_ptr` in a future PyO3 version"
124+
)]
125+
pub unsafe fn from_ptr(py: Python<'_>, ptr: *const u8, len: usize) -> &PyBytes {
126+
Self::bound_from_ptr(py, ptr, len).into_gil_ref()
127+
}
126128

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

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

0 commit comments

Comments
 (0)