Skip to main content

pyo3_ffi/
setobject.rs

1use crate::object::*;
2#[cfg(not(any(Py_LIMITED_API, PyPy, GraalPy)))]
3use crate::pyport::Py_hash_t;
4use crate::pyport::Py_ssize_t;
5use core::ffi::c_int;
6
7pub const PySet_MINSIZE: usize = 8;
8
9#[cfg(not(any(Py_LIMITED_API, PyPy, GraalPy)))]
10#[repr(C)]
11#[derive(Debug)]
12pub struct setentry {
13    pub key: *mut PyObject,
14    pub hash: Py_hash_t,
15}
16
17#[cfg(not(any(Py_LIMITED_API, PyPy, GraalPy)))]
18#[repr(C)]
19#[derive(Debug)]
20pub struct PySetObject {
21    pub ob_base: PyObject,
22    pub fill: Py_ssize_t,
23    pub used: Py_ssize_t,
24    pub mask: Py_ssize_t,
25    pub table: *mut setentry,
26    pub hash: Py_hash_t,
27    pub finger: Py_ssize_t,
28    pub smalltable: [setentry; PySet_MINSIZE],
29    pub weakreflist: *mut PyObject,
30}
31
32// skipped
33#[inline]
34#[cfg(all(not(any(PyPy, GraalPy)), not(Py_LIMITED_API)))]
35pub unsafe fn PySet_GET_SIZE(so: *mut PyObject) -> Py_ssize_t {
36    debug_assert_eq!(PyAnySet_Check(so), 1);
37    let so = so.cast::<PySetObject>();
38    (*so).used
39}
40
41// skipped _PySet_Dummy
42
43extern_libpython! {
44    // skipped non-limited _PySet_NextEntry
45    // skipped non-limited _PySet_Update
46}
47
48#[cfg(not(RustPython))]
49extern_libpython! {
50    #[cfg_attr(PyPy, link_name = "PyPySet_Type")]
51    pub static mut PySet_Type: PyTypeObject;
52    #[cfg_attr(PyPy, link_name = "PyPyFrozenSet_Type")]
53    pub static mut PyFrozenSet_Type: PyTypeObject;
54    pub static mut PySetIter_Type: PyTypeObject;
55}
56
57extern_libpython! {
58    #[cfg_attr(PyPy, link_name = "PyPySet_New")]
59    pub fn PySet_New(arg1: *mut PyObject) -> *mut PyObject;
60    #[cfg_attr(PyPy, link_name = "PyPyFrozenSet_New")]
61    pub fn PyFrozenSet_New(arg1: *mut PyObject) -> *mut PyObject;
62
63    #[cfg_attr(PyPy, link_name = "PyPySet_Add")]
64    pub fn PySet_Add(set: *mut PyObject, key: *mut PyObject) -> c_int;
65    #[cfg_attr(PyPy, link_name = "PyPySet_Clear")]
66    pub fn PySet_Clear(set: *mut PyObject) -> c_int;
67    #[cfg_attr(PyPy, link_name = "PyPySet_Contains")]
68    pub fn PySet_Contains(anyset: *mut PyObject, key: *mut PyObject) -> c_int;
69    #[cfg_attr(PyPy, link_name = "PyPySet_Discard")]
70    pub fn PySet_Discard(set: *mut PyObject, key: *mut PyObject) -> c_int;
71    #[cfg_attr(PyPy, link_name = "PyPySet_Pop")]
72    pub fn PySet_Pop(set: *mut PyObject) -> *mut PyObject;
73    #[cfg_attr(PyPy, link_name = "PyPySet_Size")]
74    pub fn PySet_Size(anyset: *mut PyObject) -> Py_ssize_t;
75
76    #[cfg(any(PyPy, RustPython))]
77    #[cfg_attr(PyPy, link_name = "PyPyFrozenSet_CheckExact")]
78    pub fn PyFrozenSet_CheckExact(ob: *mut PyObject) -> c_int;
79
80    #[cfg(any(PyPy, RustPython))]
81    #[cfg_attr(PyPy, link_name = "PyPyFrozenSet_Check")]
82    pub fn PyFrozenSet_Check(ob: *mut PyObject) -> c_int;
83
84    #[cfg(any(PyPy, RustPython))]
85    #[cfg_attr(PyPy, link_name = "PyPyAnySet_CheckExact")]
86    pub fn PyAnySet_CheckExact(ob: *mut PyObject) -> c_int;
87
88    #[cfg(RustPython)]
89    pub fn PyAnySet_Check(ob: *mut PyObject) -> c_int;
90
91    #[cfg(RustPython)]
92    pub fn PySet_CheckExact(op: *mut PyObject) -> c_int;
93
94    #[cfg(any(PyPy, RustPython))]
95    #[cfg_attr(PyPy, link_name = "PyPySet_Check")]
96    pub fn PySet_Check(ob: *mut PyObject) -> c_int;
97}
98
99#[inline]
100#[cfg(not(any(PyPy, GraalPy, RustPython)))]
101pub unsafe fn PyFrozenSet_CheckExact(ob: *mut PyObject) -> c_int {
102    (Py_TYPE(ob) == &raw mut PyFrozenSet_Type) as c_int
103}
104
105#[inline]
106#[cfg(not(any(PyPy, RustPython)))]
107pub unsafe fn PyFrozenSet_Check(ob: *mut PyObject) -> c_int {
108    (Py_TYPE(ob) == &raw mut PyFrozenSet_Type
109        || PyType_IsSubtype(Py_TYPE(ob), &raw mut PyFrozenSet_Type) != 0) as c_int
110}
111
112#[inline]
113#[cfg(not(any(PyPy, RustPython)))]
114pub unsafe fn PyAnySet_CheckExact(ob: *mut PyObject) -> c_int {
115    (Py_TYPE(ob) == &raw mut PySet_Type || Py_TYPE(ob) == &raw mut PyFrozenSet_Type) as c_int
116}
117
118#[inline]
119#[cfg(not(RustPython))]
120pub unsafe fn PyAnySet_Check(ob: *mut PyObject) -> c_int {
121    (PyAnySet_CheckExact(ob) != 0
122        || PyType_IsSubtype(Py_TYPE(ob), &raw mut PySet_Type) != 0
123        || PyType_IsSubtype(Py_TYPE(ob), &raw mut PyFrozenSet_Type) != 0) as c_int
124}
125
126#[inline]
127#[cfg(all(Py_3_10, not(RustPython)))]
128pub unsafe fn PySet_CheckExact(op: *mut PyObject) -> c_int {
129    crate::Py_IS_TYPE(op, &raw mut PySet_Type)
130}
131
132#[inline]
133#[cfg(not(any(PyPy, RustPython)))]
134pub unsafe fn PySet_Check(ob: *mut PyObject) -> c_int {
135    (Py_TYPE(ob) == &raw mut PySet_Type || PyType_IsSubtype(Py_TYPE(ob), &raw mut PySet_Type) != 0)
136        as c_int
137}