Skip to content

Commit 821f1ed

Browse files
committed
Update FFI associated types and constants
1 parent 986f75e commit 821f1ed

3 files changed

Lines changed: 81 additions & 171 deletions

File tree

src/npyffi/flags.rs

Lines changed: 17 additions & 17 deletions
Original file line numberDiff line numberDiff line change
@@ -1,4 +1,4 @@
1-
use super::npy_uint32;
1+
use super::{npy_uint32, npy_uint64};
22
use std::os::raw::c_int;
33

44
pub const NPY_ARRAY_C_CONTIGUOUS: c_int = 0x0001;
@@ -11,8 +11,8 @@ pub const NPY_ARRAY_ELEMENTSTRIDES: c_int = 0x0080;
1111
pub const NPY_ARRAY_ALIGNED: c_int = 0x0100;
1212
pub const NPY_ARRAY_NOTSWAPPED: c_int = 0x0200;
1313
pub const NPY_ARRAY_WRITEABLE: c_int = 0x0400;
14-
pub const NPY_ARRAY_UPDATEIFCOPY: c_int = 0x1000;
1514
pub const NPY_ARRAY_WRITEBACKIFCOPY: c_int = 0x2000;
15+
pub const NPY_ARRAY_ENSURENOCOPY: c_int = 0x4000;
1616
pub const NPY_ARRAY_BEHAVED: c_int = NPY_ARRAY_ALIGNED | NPY_ARRAY_WRITEABLE;
1717
pub const NPY_ARRAY_BEHAVED_NS: c_int = NPY_ARRAY_BEHAVED | NPY_ARRAY_NOTSWAPPED;
1818
pub const NPY_ARRAY_CARRAY: c_int = NPY_ARRAY_C_CONTIGUOUS | NPY_ARRAY_BEHAVED;
@@ -22,13 +22,14 @@ pub const NPY_ARRAY_FARRAY_RO: c_int = NPY_ARRAY_F_CONTIGUOUS | NPY_ARRAY_ALIGNE
2222
pub const NPY_ARRAY_DEFAULT: c_int = NPY_ARRAY_CARRAY;
2323
pub const NPY_ARRAY_IN_ARRAY: c_int = NPY_ARRAY_CARRAY_RO;
2424
pub const NPY_ARRAY_OUT_ARRAY: c_int = NPY_ARRAY_CARRAY;
25-
pub const NPY_ARRAY_INOUT_ARRAY: c_int = NPY_ARRAY_CARRAY | NPY_ARRAY_UPDATEIFCOPY;
25+
pub const NPY_ARRAY_INOUT_ARRAY: c_int = NPY_ARRAY_CARRAY;
2626
pub const NPY_ARRAY_INOUT_ARRAY2: c_int = NPY_ARRAY_CARRAY | NPY_ARRAY_WRITEBACKIFCOPY;
2727
pub const NPY_ARRAY_IN_FARRAY: c_int = NPY_ARRAY_FARRAY_RO;
2828
pub const NPY_ARRAY_OUT_FARRAY: c_int = NPY_ARRAY_FARRAY;
29-
pub const NPY_ARRAY_INOUT_FARRAY: c_int = NPY_ARRAY_FARRAY | NPY_ARRAY_UPDATEIFCOPY;
29+
pub const NPY_ARRAY_INOUT_FARRAY: c_int = NPY_ARRAY_FARRAY;
3030
pub const NPY_ARRAY_INOUT_FARRAY2: c_int = NPY_ARRAY_FARRAY | NPY_ARRAY_WRITEBACKIFCOPY;
31-
pub const NPY_ARRAY_UPDATE_ALL: c_int = NPY_ARRAY_C_CONTIGUOUS | NPY_ARRAY_F_CONTIGUOUS;
31+
pub const NPY_ARRAY_UPDATE_ALL: c_int =
32+
NPY_ARRAY_C_CONTIGUOUS | NPY_ARRAY_F_CONTIGUOUS | NPY_ARRAY_ALIGNED;
3233

3334
pub const NPY_ITER_C_INDEX: npy_uint32 = 0x00000001;
3435
pub const NPY_ITER_F_INDEX: npy_uint32 = 0x00000002;
@@ -63,19 +64,18 @@ pub const NPY_ITER_OVERLAP_ASSUME_ELEMENTWISE: npy_uint32 = 0x40000000;
6364
pub const NPY_ITER_GLOBAL_FLAGS: npy_uint32 = 0x0000ffff;
6465
pub const NPY_ITER_PER_OP_FLAGS: npy_uint32 = 0xffff0000;
6566

66-
pub const NPY_ITEM_REFCOUNT: u64 = 0x01;
67-
pub const NPY_ITEM_HASOBJECT: u64 = 0x01;
68-
pub const NPY_LIST_PICKLE: u64 = 0x02;
69-
pub const NPY_ITEM_IS_POINTER: u64 = 0x04;
70-
pub const NPY_NEEDS_INIT: u64 = 0x08;
71-
pub const NPY_NEEDS_PYAPI: u64 = 0x10;
72-
pub const NPY_USE_GETITEM: u64 = 0x20;
73-
pub const NPY_USE_SETITEM: u64 = 0x40;
74-
#[allow(overflowing_literals)]
75-
pub const NPY_ALIGNED_STRUCT: u64 = 0x80;
76-
pub const NPY_FROM_FIELDS: u64 =
67+
pub const NPY_ITEM_REFCOUNT: npy_uint64 = 0x01;
68+
pub const NPY_ITEM_HASOBJECT: npy_uint64 = 0x01;
69+
pub const NPY_LIST_PICKLE: npy_uint64 = 0x02;
70+
pub const NPY_ITEM_IS_POINTER: npy_uint64 = 0x04;
71+
pub const NPY_NEEDS_INIT: npy_uint64 = 0x08;
72+
pub const NPY_NEEDS_PYAPI: npy_uint64 = 0x10;
73+
pub const NPY_USE_GETITEM: npy_uint64 = 0x20;
74+
pub const NPY_USE_SETITEM: npy_uint64 = 0x40;
75+
pub const NPY_ALIGNED_STRUCT: npy_uint64 = 0x80;
76+
pub const NPY_FROM_FIELDS: npy_uint64 =
7777
NPY_NEEDS_INIT | NPY_LIST_PICKLE | NPY_ITEM_REFCOUNT | NPY_NEEDS_PYAPI;
78-
pub const NPY_OBJECT_DTYPE_FLAGS: u64 = NPY_LIST_PICKLE
78+
pub const NPY_OBJECT_DTYPE_FLAGS: npy_uint64 = NPY_LIST_PICKLE
7979
| NPY_USE_GETITEM
8080
| NPY_ITEM_IS_POINTER
8181
| NPY_ITEM_REFCOUNT

src/npyffi/objects.rs

Lines changed: 38 additions & 131 deletions
Original file line numberDiff line numberDiff line change
@@ -10,6 +10,9 @@ use std::os::raw::*;
1010
use super::types::*;
1111
use crate::npyffi::*;
1212

13+
pub const NPY_NTYPES_ABI_COMPATIBLE: usize = 21;
14+
pub const NPY_MAXDIMS_LEGACY_ITERS: usize = 32;
15+
1316
#[repr(C)]
1417
pub struct PyArrayObject {
1518
pub ob_base: PyObject,
@@ -192,7 +195,7 @@ pub struct PyArray_ArrayDescr {
192195
#[repr(C)]
193196
#[derive(Copy, Clone)]
194197
pub struct PyArray_ArrFuncs {
195-
pub cast: [PyArray_VectorUnaryFunc; 21usize],
198+
pub cast: [PyArray_VectorUnaryFunc; NPY_NTYPES_ABI_COMPATIBLE],
196199
pub getitem: PyArray_GetItemFunc,
197200
pub setitem: PyArray_SetItemFunc,
198201
pub copyswapn: PyArray_CopySwapNFunc,
@@ -205,15 +208,15 @@ pub struct PyArray_ArrFuncs {
205208
pub nonzero: PyArray_NonzeroFunc,
206209
pub fill: PyArray_FillFunc,
207210
pub fillwithscalar: PyArray_FillWithScalarFunc,
208-
pub sort: [PyArray_SortFunc; 3usize],
209-
pub argsort: [PyArray_ArgSortFunc; 3usize],
211+
pub sort: [PyArray_SortFunc; NPY_NSORTS],
212+
pub argsort: [PyArray_ArgSortFunc; NPY_NSORTS],
210213
pub castdict: *mut PyObject,
211214
pub scalarkind: PyArray_ScalarKindFunc,
212215
pub cancastscalarkindto: *mut *mut c_int,
213216
pub cancastto: *mut c_int,
214-
pub fastclip: PyArray_FastClipFunc,
215-
pub fastputmask: PyArray_FastPutmaskFunc,
216-
pub fasttake: PyArray_FastTakeFunc,
217+
pub _unused1: *mut c_void,
218+
pub _unused2: *mut c_void,
219+
pub _unused3: *mut c_void,
217220
pub argmin: PyArray_ArgFunc,
218221
}
219222

@@ -263,53 +266,9 @@ pub type PyArray_SortFunc =
263266
Option<unsafe extern "C" fn(*mut c_void, npy_intp, *mut c_void) -> c_int>;
264267
pub type PyArray_ArgSortFunc =
265268
Option<unsafe extern "C" fn(*mut c_void, *mut npy_intp, npy_intp, *mut c_void) -> c_int>;
266-
pub type PyArray_PartitionFunc = Option<
267-
unsafe extern "C" fn(
268-
*mut c_void,
269-
npy_intp,
270-
npy_intp,
271-
*mut npy_intp,
272-
*mut npy_intp,
273-
*mut c_void,
274-
) -> c_int,
275-
>;
276-
pub type PyArray_ArgPartitionFunc = Option<
277-
unsafe extern "C" fn(
278-
*mut c_void,
279-
*mut npy_intp,
280-
npy_intp,
281-
npy_intp,
282-
*mut npy_intp,
283-
*mut npy_intp,
284-
*mut c_void,
285-
) -> c_int,
286-
>;
287269
pub type PyArray_FillWithScalarFunc =
288270
Option<unsafe extern "C" fn(*mut c_void, npy_intp, *mut c_void, *mut c_void) -> c_int>;
289271
pub type PyArray_ScalarKindFunc = Option<unsafe extern "C" fn(*mut c_void) -> c_int>;
290-
pub type PyArray_FastClipFunc =
291-
Option<unsafe extern "C" fn(*mut c_void, npy_intp, *mut c_void, *mut c_void, *mut c_void)>;
292-
pub type PyArray_FastPutmaskFunc =
293-
Option<unsafe extern "C" fn(*mut c_void, *mut c_void, npy_intp, *mut c_void, npy_intp)>;
294-
pub type PyArray_FastTakeFunc = Option<
295-
unsafe extern "C" fn(
296-
*mut c_void,
297-
*mut c_void,
298-
*mut npy_intp,
299-
npy_intp,
300-
npy_intp,
301-
npy_intp,
302-
npy_intp,
303-
NPY_CLIPMODE,
304-
) -> c_int,
305-
>;
306-
307-
#[repr(C)]
308-
pub struct PyArrayFlagsObject {
309-
pub ob_base: PyObject,
310-
pub arr: *mut PyObject,
311-
pub flags: c_int,
312-
}
313272

314273
#[repr(C)]
315274
#[derive(Clone, Copy)]
@@ -365,9 +324,12 @@ pub struct PyUFuncObject {
365324
pub core_offsets: *mut c_int,
366325
pub core_signature: *mut c_char,
367326
pub type_resolver: PyUFunc_TypeResolutionFunc,
368-
pub legacy_inner_loop_selector: PyUFunc_LegacyInnerLoopSelectionFunc,
369-
pub reserved2: *mut c_void,
370-
pub masked_inner_loop_selector: PyUFunc_MaskedInnerLoopSelectionFunc,
327+
pub reserved2: *mut c_void, // Was the legacy loop resolver
328+
#[cfg(all(Py_3_8, not(Py_LIMITED_API)))]
329+
pub vectorcall: Option<vectorcallfunc>,
330+
#[cfg(not(all(Py_3_8, not(Py_LIMITED_API))))]
331+
pub vectorcall: *mut c_void,
332+
pub reserved3: *mut c_void, // Was previously the `PyUFunc_MaskedInnerLoopSelectionFunc`
371333
pub op_flags: *mut npy_uint32,
372334
pub iter_flags: npy_uint32,
373335
}
@@ -393,27 +355,6 @@ pub type PyUFunc_TypeResolutionFunc = Option<
393355
*mut *mut PyArray_Descr,
394356
) -> c_int,
395357
>;
396-
pub type PyUFunc_LegacyInnerLoopSelectionFunc = Option<
397-
unsafe extern "C" fn(
398-
*mut PyUFuncObject,
399-
*mut *mut PyArray_Descr,
400-
*mut PyUFuncGenericFunction,
401-
*mut *mut c_void,
402-
*mut c_int,
403-
) -> c_int,
404-
>;
405-
pub type PyUFunc_MaskedInnerLoopSelectionFunc = Option<
406-
unsafe extern "C" fn(
407-
*mut PyUFuncObject,
408-
*mut *mut PyArray_Descr,
409-
*mut PyArray_Descr,
410-
*mut npy_intp,
411-
npy_intp,
412-
*mut PyUFunc_MaskedStridedInnerLoopFunc,
413-
*mut *mut NpyAuxData,
414-
*mut c_int,
415-
) -> c_int,
416-
>;
417358

418359
#[repr(C)]
419360
#[derive(Debug, Copy, Clone)]
@@ -425,17 +366,17 @@ pub struct PyArrayIterObject {
425366
pub nd_m1: c_int,
426367
pub index: npy_intp,
427368
pub size: npy_intp,
428-
pub coordinates: [npy_intp; 32usize],
429-
pub dims_m1: [npy_intp; 32usize],
430-
pub strides: [npy_intp; 32usize],
431-
pub backstrides: [npy_intp; 32usize],
432-
pub factors: [npy_intp; 32usize],
369+
pub coordinates: [npy_intp; NPY_MAXDIMS_LEGACY_ITERS],
370+
pub dims_m1: [npy_intp; NPY_MAXDIMS_LEGACY_ITERS],
371+
pub strides: [npy_intp; NPY_MAXDIMS_LEGACY_ITERS],
372+
pub backstrides: [npy_intp; NPY_MAXDIMS_LEGACY_ITERS],
373+
pub factors: [npy_intp; NPY_MAXDIMS_LEGACY_ITERS],
433374
pub ao: *mut PyArrayObject,
434375
pub dataptr: *mut c_char,
435376
pub contiguous: npy_bool,
436-
pub bounds: [[npy_intp; 2usize]; 32usize],
437-
pub limits: [[npy_intp; 2usize]; 32usize],
438-
pub limits_sizes: [npy_intp; 32usize],
377+
pub bounds: [[npy_intp; 2usize]; NPY_MAXDIMS_LEGACY_ITERS],
378+
pub limits: [[npy_intp; 2usize]; NPY_MAXDIMS_LEGACY_ITERS],
379+
pub limits_sizes: [npy_intp; NPY_MAXDIMS_LEGACY_ITERS],
439380
pub translate: npy_iter_get_dataptr_t,
440381
}
441382

@@ -446,7 +387,7 @@ pub struct PyArrayMultiIterObject {
446387
pub size: npy_intp,
447388
pub index: npy_intp,
448389
pub nd: c_int,
449-
pub dimensions: [npy_intp; 32usize],
390+
pub dimensions: [npy_intp; NPY_MAXDIMS_LEGACY_ITERS],
450391
pub iters: [*mut PyArrayIterObject; 32usize],
451392
}
452393

@@ -456,62 +397,25 @@ pub struct PyArrayNeighborhoodIterObject {
456397
pub nd_m1: c_int,
457398
pub index: npy_intp,
458399
pub size: npy_intp,
459-
pub coordinates: [npy_intp; 32usize],
460-
pub dims_m1: [npy_intp; 32usize],
461-
pub strides: [npy_intp; 32usize],
462-
pub backstrides: [npy_intp; 32usize],
463-
pub factors: [npy_intp; 32usize],
400+
pub coordinates: [npy_intp; NPY_MAXDIMS_LEGACY_ITERS],
401+
pub dims_m1: [npy_intp; NPY_MAXDIMS_LEGACY_ITERS],
402+
pub strides: [npy_intp; NPY_MAXDIMS_LEGACY_ITERS],
403+
pub backstrides: [npy_intp; NPY_MAXDIMS_LEGACY_ITERS],
404+
pub factors: [npy_intp; NPY_MAXDIMS_LEGACY_ITERS],
464405
pub ao: *mut PyArrayObject,
465406
pub dataptr: *mut c_char,
466407
pub contiguous: npy_bool,
467-
pub bounds: [[npy_intp; 2usize]; 32usize],
468-
pub limits: [[npy_intp; 2usize]; 32usize],
469-
pub limits_sizes: [npy_intp; 32usize],
408+
pub bounds: [[npy_intp; 2usize]; NPY_MAXDIMS_LEGACY_ITERS],
409+
pub limits: [[npy_intp; 2usize]; NPY_MAXDIMS_LEGACY_ITERS],
410+
pub limits_sizes: [npy_intp; NPY_MAXDIMS_LEGACY_ITERS],
470411
pub translate: npy_iter_get_dataptr_t,
471412
pub nd: npy_intp,
472-
pub dimensions: [npy_intp; 32usize],
413+
pub dimensions: [npy_intp; NPY_MAXDIMS_LEGACY_ITERS],
473414
pub _internal_iter: *mut PyArrayIterObject,
474415
pub constant: *mut c_char,
475416
pub mode: c_int,
476417
}
477418

478-
#[repr(C)]
479-
pub struct PyArrayMapIterObject {
480-
pub ob_base: PyObject,
481-
pub numiter: c_int,
482-
pub size: npy_intp,
483-
pub index: npy_intp,
484-
pub nd: c_int,
485-
pub dimensions: [npy_intp; 32usize],
486-
pub outer: *mut NpyIter,
487-
pub unused: [*mut c_void; 30usize],
488-
pub array: *mut PyArrayObject,
489-
pub ait: *mut PyArrayIterObject,
490-
pub subspace: *mut PyArrayObject,
491-
pub iteraxes: [c_int; 32usize],
492-
pub fancy_strides: [npy_intp; 32usize],
493-
pub baseoffset: *mut c_char,
494-
pub consec: c_int,
495-
pub dataptr: *mut c_char,
496-
pub nd_fancy: c_int,
497-
pub fancy_dims: [npy_intp; 32usize],
498-
pub needs_api: c_int,
499-
pub extra_op: *mut PyArrayObject,
500-
pub extra_op_dtype: *mut PyArray_Descr,
501-
pub extra_op_flags: *mut npy_uint32,
502-
pub extra_op_iter: *mut NpyIter,
503-
pub extra_op_next: NpyIter_IterNextFunc,
504-
pub extra_op_ptrs: *mut *mut c_char,
505-
pub outer_next: NpyIter_IterNextFunc,
506-
pub outer_ptrs: *mut *mut c_char,
507-
pub outer_strides: *mut npy_intp,
508-
pub subspace_iter: *mut NpyIter,
509-
pub subspace_next: NpyIter_IterNextFunc,
510-
pub subspace_ptrs: *mut *mut c_char,
511-
pub subspace_strides: *mut npy_intp,
512-
pub iter_count: npy_intp,
513-
}
514-
515419
pub type NpyIter_IterNextFunc = Option<unsafe extern "C" fn(*mut NpyIter) -> c_int>;
516420
pub type NpyIter_GetMultiIndexFunc = Option<unsafe extern "C" fn(*mut NpyIter, *mut npy_intp)>;
517421
pub type PyDataMem_EventHookFunc =
@@ -547,8 +451,11 @@ pub struct PyArray_DatetimeDTypeMetaData {
547451
// npy_packed_static_string and npy_string_allocator are opaque pointers.
548452
// FIXME(adamreichold): Consider extern types when they are stabilized.
549453
// https://github.com/rust-lang/rust/issues/43467
550-
pub type npy_packed_static_string = c_void;
551-
pub type npy_string_allocator = c_void;
454+
#[repr(C)]
455+
pub struct npy_packed_static_string([u8; 0]);
456+
457+
#[repr(C)]
458+
pub struct npy_string_allocator([u8; 0]);
552459

553460
#[cfg(not(Py_LIMITED_API))]
554461
#[repr(C)]

0 commit comments

Comments
 (0)