Skip to content

Commit 4c94698

Browse files
committed
Add wrapper functions for the statically_allocated field
1 parent 7791885 commit 4c94698

File tree

2 files changed

+47
-2
lines changed

2 files changed

+47
-2
lines changed

pyo3-ffi/src/cpython/unicodeobject.rs

+42-2
Original file line numberDiff line numberDiff line change
@@ -1,10 +1,10 @@
11
#[cfg(not(PyPy))]
22
use crate::Py_hash_t;
33
use crate::{PyObject, Py_UCS1, Py_UCS2, Py_UCS4, Py_ssize_t};
4-
use libc::wchar_t;
5-
use std::os::raw::{c_char, c_int, c_uint, c_void};
4+
use libc::{c_ushort, wchar_t};
65
#[cfg(Py_3_14)]
76
use std::os::raw::c_ushort;
7+
use std::os::raw::{c_char, c_int, c_uint, c_void};
88

99
// skipped Py_UNICODE_ISSPACE()
1010
// skipped Py_UNICODE_ISLOWER()
@@ -587,6 +587,46 @@ impl PyASCIIObject {
587587
state.set_ready(val);
588588
self.state = u32::from(state);
589589
}
590+
591+
/// Get the `statically_allocated` field of the [`PyASCIIObject`] state bitfield.
592+
///
593+
/// Returns either `0` or `1`.
594+
#[inline]
595+
#[cfg(all(Py_3_12), not(Py_3_14))]
596+
pub unsafe fn statically_allocated(&self) -> c_uint {
597+
PyASCIIObjectState::from(self.state).statically_allocated()
598+
}
599+
600+
/// Set the `statically_allocated` flag of the [`PyASCIIObject`] state bitfield.
601+
///
602+
/// Calling this function with an argument that is neither `0` nor `1` is invalid.
603+
#[inline]
604+
#[cfg(all(Py_3_12), not(Py_3_14))]
605+
pub unsafe fn set_statically_allocated(&mut self, val: c_uint) {
606+
let mut state = PyASCIIObjectState::from(self.state);
607+
state.set_statically_allocated(val);
608+
self.state = u32::from(state);
609+
}
610+
611+
/// Get the `statically_allocated` field of the [`PyASCIIObject`] state bitfield.
612+
///
613+
/// Returns either `0` or `1`.
614+
#[inline]
615+
#[cfg(Py_3_14)]
616+
pub unsafe fn statically_allocated(&self) -> c_ushort {
617+
PyASCIIObjectState::from(self.state).statically_allocated()
618+
}
619+
620+
/// Set the `statically_allocated` flag of the [`PyASCIIObject`] state bitfield.
621+
///
622+
/// Calling this function with an argument that is neither `0` nor `1` is invalid.
623+
#[inline]
624+
#[cfg(Py_3_14)]
625+
pub unsafe fn set_statically_allocated(&mut self, val: c_ushort) {
626+
let mut state = PyASCIIObjectState::from(self.state);
627+
state.set_statically_allocated(val);
628+
self.state = u32::from(state);
629+
}
590630
}
591631

592632
#[repr(C)]

src/ffi/tests.rs

+5
Original file line numberDiff line numberDiff line change
@@ -158,6 +158,11 @@ fn ascii_object_bitfield() {
158158
o.set_ready(1);
159159
#[cfg(not(Py_3_12))]
160160
assert_eq!(o.ready(), 1);
161+
162+
#[cfg(Py_3_12)]
163+
o.set_statically_allocated(1);
164+
#[cfg(Py_3_12)]
165+
assert_eq!(o.statically_allocated(), 1);
161166
}
162167
}
163168

0 commit comments

Comments
 (0)