1use crate::{
3 callback::IntoPyCallbackOutput, ffi, impl_::pyclass::PyClassImpl, IntoPy, PyObject, PyResult,
4 PyTypeInfo, Python,
5};
6use std::{cmp::Ordering, os::raw::c_int};
7
8mod create_type_object;
9mod gc;
10
11pub(crate) use self::create_type_object::{create_type_object, PyClassTypeObject};
12
13pub use self::gc::{PyTraverseError, PyVisit};
14
15#[allow(deprecated)]
20#[cfg(feature = "gil-refs")]
21pub trait PyClass: PyTypeInfo<AsRefTarget = crate::PyCell<Self>> + PyClassImpl {
22 type Frozen: Frozen;
26}
27
28#[cfg(not(feature = "gil-refs"))]
33pub trait PyClass: PyTypeInfo + PyClassImpl {
34 type Frozen: Frozen;
38}
39
40#[derive(Debug, Clone, Copy)]
42pub enum CompareOp {
43 Lt = ffi::Py_LT as isize,
45 Le = ffi::Py_LE as isize,
47 Eq = ffi::Py_EQ as isize,
49 Ne = ffi::Py_NE as isize,
51 Gt = ffi::Py_GT as isize,
53 Ge = ffi::Py_GE as isize,
55}
56
57impl CompareOp {
58 pub fn from_raw(op: c_int) -> Option<Self> {
60 match op {
61 ffi::Py_LT => Some(CompareOp::Lt),
62 ffi::Py_LE => Some(CompareOp::Le),
63 ffi::Py_EQ => Some(CompareOp::Eq),
64 ffi::Py_NE => Some(CompareOp::Ne),
65 ffi::Py_GT => Some(CompareOp::Gt),
66 ffi::Py_GE => Some(CompareOp::Ge),
67 _ => None,
68 }
69 }
70
71 pub fn matches(&self, result: Ordering) -> bool {
92 match self {
93 CompareOp::Eq => result == Ordering::Equal,
94 CompareOp::Ne => result != Ordering::Equal,
95 CompareOp::Lt => result == Ordering::Less,
96 CompareOp::Le => result != Ordering::Greater,
97 CompareOp::Gt => result == Ordering::Greater,
98 CompareOp::Ge => result != Ordering::Less,
99 }
100 }
101}
102
103#[deprecated(since = "0.21.0", note = "Use `Option` or `PyStopIteration` instead.")]
142pub enum IterNextOutput<T, U> {
143 Yield(T),
145 Return(U),
147}
148
149#[deprecated(since = "0.21.0", note = "Use `Option` or `PyStopIteration` instead.")]
151#[allow(deprecated)]
152pub type PyIterNextOutput = IterNextOutput<PyObject, PyObject>;
153
154#[allow(deprecated)]
155impl<T, U> IntoPyCallbackOutput<*mut ffi::PyObject> for IterNextOutput<T, U>
156where
157 T: IntoPy<PyObject>,
158 U: IntoPy<PyObject>,
159{
160 fn convert(self, py: Python<'_>) -> PyResult<*mut ffi::PyObject> {
161 match self {
162 IterNextOutput::Yield(o) => Ok(o.into_py(py).into_ptr()),
163 IterNextOutput::Return(o) => {
164 Err(crate::exceptions::PyStopIteration::new_err(o.into_py(py)))
165 }
166 }
167 }
168}
169
170#[deprecated(
174 since = "0.21.0",
175 note = "Use `Option` or `PyStopAsyncIteration` instead."
176)]
177pub enum IterANextOutput<T, U> {
178 Yield(T),
180 Return(U),
182}
183
184#[deprecated(
186 since = "0.21.0",
187 note = "Use `Option` or `PyStopAsyncIteration` instead."
188)]
189#[allow(deprecated)]
190pub type PyIterANextOutput = IterANextOutput<PyObject, PyObject>;
191
192#[allow(deprecated)]
193impl<T, U> IntoPyCallbackOutput<*mut ffi::PyObject> for IterANextOutput<T, U>
194where
195 T: IntoPy<PyObject>,
196 U: IntoPy<PyObject>,
197{
198 fn convert(self, py: Python<'_>) -> PyResult<*mut ffi::PyObject> {
199 match self {
200 IterANextOutput::Yield(o) => Ok(o.into_py(py).into_ptr()),
201 IterANextOutput::Return(o) => Err(crate::exceptions::PyStopAsyncIteration::new_err(
202 o.into_py(py),
203 )),
204 }
205 }
206}
207
208#[doc(hidden)]
212pub mod boolean_struct {
213 pub(crate) mod private {
214 use super::*;
215
216 pub trait Boolean {
218 const VALUE: bool;
219 }
220
221 impl Boolean for True {
222 const VALUE: bool = true;
223 }
224 impl Boolean for False {
225 const VALUE: bool = false;
226 }
227 }
228
229 pub struct True(());
230 pub struct False(());
231}
232
233#[doc(hidden)]
235pub trait Frozen: boolean_struct::private::Boolean {}
236
237impl Frozen for boolean_struct::True {}
238impl Frozen for boolean_struct::False {}
239
240mod tests {
241 #[test]
242 fn test_compare_op_matches() {
243 use super::CompareOp;
244 use std::cmp::Ordering;
245
246 assert!(CompareOp::Eq.matches(Ordering::Equal));
247 assert!(CompareOp::Ne.matches(Ordering::Less));
248 assert!(CompareOp::Ge.matches(Ordering::Greater));
249 assert!(CompareOp::Gt.matches(Ordering::Greater));
250 assert!(CompareOp::Le.matches(Ordering::Equal));
251 assert!(CompareOp::Lt.matches(Ordering::Less));
252 }
253}