numpy/npyffi/
types.rs

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