numpy/npyffi/
types.rs

1use pyo3::ffi::{Py_hash_t, Py_intptr_t, Py_uintptr_t};
2use std::os::raw::*;
3
4pub type npy_intp = Py_intptr_t;
5pub type npy_uintp = Py_uintptr_t;
6pub type npy_longlong = c_longlong;
7pub type npy_ulonglong = c_ulonglong;
8pub type npy_bool = c_uchar;
9pub type npy_longdouble = f64;
10pub type npy_byte = c_char;
11pub type npy_ubyte = c_uchar;
12pub type npy_ushort = c_ushort;
13pub type npy_uint = c_uint;
14pub type npy_ulong = c_ulong;
15pub type npy_char = c_char;
16pub type npy_short = c_short;
17pub type npy_int = c_int;
18pub type npy_long = c_long;
19pub type npy_float = f32;
20pub type npy_double = f64;
21pub type npy_hash_t = Py_hash_t;
22pub type npy_int64 = i64;
23pub type npy_uint64 = u64;
24pub type npy_int32 = i32;
25pub type npy_uint32 = u32;
26pub type npy_ucs4 = c_uint;
27pub type npy_int16 = i16;
28pub type npy_uint16 = u16;
29pub type npy_int8 = i8;
30pub type npy_uint8 = u8;
31pub type npy_float64 = f64;
32pub type npy_complex128 = npy_cdouble;
33pub type npy_float32 = f32;
34pub type npy_complex64 = npy_cfloat;
35pub type npy_half = npy_uint16;
36pub type npy_float16 = npy_half;
37pub type npy_float128 = npy_longdouble;
38pub type npy_complex256 = npy_clongdouble;
39pub type npy_timedelta = npy_int64;
40pub type npy_datetime = npy_int64;
41
42#[repr(C)]
43#[derive(Debug, Clone, Copy)]
44pub struct npy_cdouble {
45    pub real: f64,
46    pub imag: f64,
47}
48#[repr(C)]
49#[derive(Debug, Clone, Copy)]
50pub struct npy_cfloat {
51    pub real: f32,
52    pub imag: f32,
53}
54
55#[repr(C)]
56#[derive(Debug, Clone, Copy)]
57pub struct npy_clongdouble {
58    pub real: npy_longdouble,
59    pub imag: npy_longdouble,
60}
61
62#[repr(i32)]
63#[derive(Debug, Copy, Clone, PartialEq, Eq, Hash)]
64pub enum NPY_ORDER {
65    NPY_ANYORDER = -1,
66    NPY_CORDER = 0,
67    NPY_FORTRANORDER = 1,
68    NPY_KEEPORDER = 2,
69}
70
71#[repr(i32)]
72#[derive(Debug, Copy, Clone, PartialEq, Eq, Hash)]
73pub enum NPY_SCALARKIND {
74    NPY_NOSCALAR = -1,
75    NPY_BOOL_SCALAR = 0,
76    NPY_INTPOS_SCALAR = 1,
77    NPY_INTNEG_SCALAR = 2,
78    NPY_FLOAT_SCALAR = 3,
79    NPY_COMPLEX_SCALAR = 4,
80    NPY_OBJECT_SCALAR = 5,
81}
82
83#[repr(u32)]
84#[derive(Debug, Copy, Clone, PartialEq, Eq, Hash)]
85pub enum NPY_SORTKIND {
86    NPY_QUICKSORT = 0,
87    NPY_HEAPSORT = 1,
88    NPY_MERGESORT = 2,
89}
90
91#[repr(u32)]
92#[derive(Debug, Copy, Clone, PartialEq, Eq, Hash)]
93pub enum NPY_SEARCHSIDE {
94    NPY_SEARCHLEFT = 0,
95    NPY_SEARCHRIGHT = 1,
96}
97
98#[repr(u32)]
99#[derive(Debug, Copy, Clone, PartialEq, Eq, Hash)]
100pub enum NPY_DATETIMEUNIT {
101    NPY_FR_Y = 0,
102    NPY_FR_M = 1,
103    NPY_FR_W = 2,
104    NPY_FR_D = 4,
105    NPY_FR_h = 5,
106    NPY_FR_m = 6,
107    NPY_FR_s = 7,
108    NPY_FR_ms = 8,
109    NPY_FR_us = 9,
110    NPY_FR_ns = 10,
111    NPY_FR_ps = 11,
112    NPY_FR_fs = 12,
113    NPY_FR_as = 13,
114    NPY_FR_GENERIC = 14,
115}
116
117#[repr(u32)]
118#[derive(Debug, Copy, Clone, PartialEq, Eq, Hash, PartialOrd, Ord)]
119pub enum NPY_TYPES {
120    NPY_BOOL = 0,
121    NPY_BYTE = 1,
122    NPY_UBYTE = 2,
123    NPY_SHORT = 3,
124    NPY_USHORT = 4,
125    NPY_INT = 5,
126    NPY_UINT = 6,
127    NPY_LONG = 7,
128    NPY_ULONG = 8,
129    NPY_LONGLONG = 9,
130    NPY_ULONGLONG = 10,
131    NPY_FLOAT = 11,
132    NPY_DOUBLE = 12,
133    NPY_LONGDOUBLE = 13,
134    NPY_CFLOAT = 14,
135    NPY_CDOUBLE = 15,
136    NPY_CLONGDOUBLE = 16,
137    NPY_OBJECT = 17,
138    NPY_STRING = 18,
139    NPY_UNICODE = 19,
140    NPY_VOID = 20,
141    NPY_DATETIME = 21,
142    NPY_TIMEDELTA = 22,
143    NPY_HALF = 23,
144    NPY_NTYPES_LEGACY = 24,
145    NPY_NOTYPE = 25,
146    NPY_CHAR = 26,
147    NPY_USERDEF = 256,
148    NPY_VSTRING = 2056,
149}
150
151#[repr(u32)]
152#[derive(Debug, Copy, Clone, PartialEq, Eq, Hash)]
153pub enum NPY_SELECTKIND {
154    NPY_INTROSELECT = 0,
155}
156
157#[repr(u32)]
158#[derive(Debug, Copy, Clone, PartialEq, Eq, Hash)]
159pub enum NPY_CASTING {
160    NPY_NO_CASTING = 0,
161    NPY_EQUIV_CASTING = 1,
162    NPY_SAFE_CASTING = 2,
163    NPY_SAME_KIND_CASTING = 3,
164    NPY_UNSAFE_CASTING = 4,
165}
166
167#[repr(u32)]
168#[derive(Copy, Clone, PartialEq, Eq, Hash)]
169pub enum NPY_CLIPMODE {
170    NPY_CLIP = 0,
171    NPY_WRAP = 1,
172    NPY_RAISE = 2,
173}
174
175#[repr(C)]
176#[derive(Debug, Clone, Copy)]
177pub struct npy_datetimestruct {
178    pub year: npy_int64,
179    pub month: npy_int32,
180    pub day: npy_int32,
181    pub hour: npy_int32,
182    pub min: npy_int32,
183    pub sec: npy_int32,
184    pub us: npy_int32,
185    pub ps: npy_int32,
186    pub as_: npy_int32,
187}
188
189#[repr(C)]
190#[derive(Debug, Clone, Copy)]
191pub struct npy_timedeltastruct {
192    pub day: npy_int64,
193    pub sec: npy_int32,
194    pub us: npy_int32,
195    pub ps: npy_int32,
196    pub as_: npy_int32,
197}
198
199#[repr(C)]
200#[derive(Debug, Clone, Copy)]
201pub struct npy_stride_sort_item {
202    pub perm: npy_intp,
203    pub stride: npy_intp,
204}
205
206#[repr(u8)]
207#[derive(Debug, Clone, Copy)]
208pub enum NPY_TYPECHAR {
209    NPY_BOOLLTR = b'?',
210    NPY_BYTELTR = b'b',
211    NPY_UBYTELTR = b'B',
212    NPY_SHORTLTR = b'h',
213    NPY_USHORTLTR = b'H',
214    NPY_INTLTR = b'i',
215    NPY_UINTLTR = b'I',
216    NPY_LONGLTR = b'l',
217    NPY_ULONGLTR = b'L',
218    NPY_LONGLONGLTR = b'q',
219    NPY_ULONGLONGLTR = b'Q',
220    NPY_HALFLTR = b'e',
221    NPY_FLOATLTR = b'f',
222    NPY_DOUBLELTR = b'd',
223    NPY_LONGDOUBLELTR = b'g',
224    NPY_CFLOATLTR = b'F',
225    NPY_CDOUBLELTR = b'D',
226    NPY_CLONGDOUBLELTR = b'G',
227    NPY_OBJECTLTR = b'O',
228    NPY_STRINGLTR = b'S',
229    NPY_STRINGLTR2 = b'a',
230    NPY_UNICODELTR = b'U',
231    NPY_VOIDLTR = b'V',
232    NPY_DATETIMELTR = b'M',
233    NPY_TIMEDELTALTR = b'm',
234    NPY_CHARLTR = b'c',
235    NPY_INTPLTR = b'p',
236    NPY_UINTPLTR = b'P',
237}
238
239// Note: NPY_TYPEKINDCHAR doesn't exist in the header and has been created here artificially
240// because the original C enum contained duplicate values - namely, those related to type kinds.
241// There's also a comment in the C code preceding this block of values and stating that they are
242// related to type kind chars and not type codes.
243#[repr(u8)]
244#[derive(Debug, Clone, Copy)]
245pub enum NPY_TYPEKINDCHAR {
246    NPY_GENBOOLLTR = b'b',
247    NPY_SIGNEDLTR = b'i',
248    NPY_UNSIGNEDLTR = b'u',
249    NPY_FLOATINGLTR = b'f',
250    NPY_COMPLEXLTR = b'c',
251}
252
253#[repr(u8)]
254#[derive(Debug, Copy, Clone, PartialEq, Eq, Hash)]
255pub enum NPY_BYTEORDER_CHAR {
256    NPY_LITTLE = b'<',
257    NPY_BIG = b'>',
258    NPY_NATIVE = b'=',
259    NPY_SWAP = b's',
260    NPY_IGNORE = b'|',
261}
262
263impl NPY_BYTEORDER_CHAR {
264    #[cfg(target_endian = "little")]
265    pub const NPY_NATBYTE: Self = Self::NPY_LITTLE;
266    #[cfg(target_endian = "little")]
267    pub const NPY_OPPBYTE: Self = Self::NPY_BIG;
268
269    #[cfg(target_endian = "big")]
270    pub const NPY_NATBYTE: Self = Self::NPY_BIG;
271    #[cfg(target_endian = "big")]
272    pub const NPY_OPPBYTE: Self = Self::NPY_LITTLE;
273}
274
275#[repr(i32)]
276#[derive(Debug, Copy, Clone, PartialEq, Eq, Hash)]
277pub enum NPY_ARRAYMETHOD_FLAGS {
278    NPY_METH_REQUIRES_PYAPI = 1 << 0,
279    NPY_METH_NO_FLOATINGPOINT_ERRORS = 1 << 1,
280    NPY_METH_SUPPORTS_UNALIGNED = 1 << 2,
281    NPY_METH_IS_REORDERABLE = 1 << 3,
282    _NPY_METH_FORCE_CAST_INPUTS = 1 << 17,
283    NPY_METH_RUNTIME_FLAGS = (1 << 0) | (1 << 1), // NPY_METH_REQUIRES_PYAPI | NPY_METH_NO_FLOATINGPOINT_ERRORS
284}