Skip to content

Commit 1f6b31a

Browse files
committed
Format
1 parent 7d37cfc commit 1f6b31a

File tree

11 files changed

+276
-258
lines changed

11 files changed

+276
-258
lines changed

src/array.rs

Lines changed: 64 additions & 44 deletions
Original file line numberDiff line numberDiff line change
@@ -1,15 +1,15 @@
11
//! Untyped safe interface for NumPy ndarray
22
3-
use npyffi;
4-
use pyffi;
53
use cpython::*;
64
use ndarray::*;
5+
use npyffi;
6+
use pyffi;
77

88
use std::os::raw::c_void;
99
use std::ptr::null_mut;
1010

11-
use super::*;
1211
use super::error::ArrayCastError;
12+
use super::*;
1313

1414
/// Untyped safe interface for NumPy ndarray.
1515
pub struct PyArray(PyObject);
@@ -85,8 +85,10 @@ impl PyArray {
8585
fn ndarray_shape<A>(&self) -> StrideShape<IxDyn> {
8686
// FIXME may be done more simply
8787
let shape: Shape<_> = Dim(self.shape()).into();
88-
let st: Vec<usize> =
89-
self.strides().iter().map(|&x| x as usize / ::std::mem::size_of::<A>()).collect();
88+
let st: Vec<usize> = self.strides()
89+
.iter()
90+
.map(|&x| x as usize / ::std::mem::size_of::<A>())
91+
.collect();
9092
shape.strides(Dim(st))
9193
}
9294

@@ -110,13 +112,23 @@ impl PyArray {
110112
/// Get data as a ndarray::ArrayView
111113
pub fn as_array<A: types::TypeNum>(&self) -> Result<ArrayViewD<A>, ArrayCastError> {
112114
self.type_check::<A>()?;
113-
unsafe { Ok(ArrayView::from_shape_ptr(self.ndarray_shape::<A>(), self.data())) }
115+
unsafe {
116+
Ok(ArrayView::from_shape_ptr(
117+
self.ndarray_shape::<A>(),
118+
self.data(),
119+
))
120+
}
114121
}
115122

116123
/// Get data as a ndarray::ArrayViewMut
117124
pub fn as_array_mut<A: types::TypeNum>(&self) -> Result<ArrayViewMutD<A>, ArrayCastError> {
118125
self.type_check::<A>()?;
119-
unsafe { Ok(ArrayViewMut::from_shape_ptr(self.ndarray_shape::<A>(), self.data())) }
126+
unsafe {
127+
Ok(ArrayViewMut::from_shape_ptr(
128+
self.ndarray_shape::<A>(),
129+
self.data(),
130+
))
131+
}
120132
}
121133

122134
/// Get data as a Rust immutable slice
@@ -131,23 +143,25 @@ impl PyArray {
131143
unsafe { Ok(::std::slice::from_raw_parts_mut(self.data(), self.len())) }
132144
}
133145

134-
pub unsafe fn new_<T: TypeNum>(py: Python,
135-
np: &PyArrayModule,
136-
dims: &[usize],
137-
strides: *mut npy_intp,
138-
data: *mut c_void)
139-
-> Self {
146+
pub unsafe fn new_<T: TypeNum>(
147+
py: Python,
148+
np: &PyArrayModule,
149+
dims: &[usize],
150+
strides: *mut npy_intp,
151+
data: *mut c_void,
152+
) -> Self {
140153
let dims: Vec<_> = dims.iter().map(|d| *d as npy_intp).collect();
141-
let ptr = np.PyArray_New(np.get_type_object(npyffi::ArrayType::PyArray_Type),
142-
dims.len() as i32,
143-
dims.as_ptr() as *mut npy_intp,
144-
T::typenum(),
145-
strides,
146-
data,
147-
0, // itemsize
148-
0, // flag
149-
::std::ptr::null_mut() //obj
150-
);
154+
let ptr = np.PyArray_New(
155+
np.get_type_object(npyffi::ArrayType::PyArray_Type),
156+
dims.len() as i32,
157+
dims.as_ptr() as *mut npy_intp,
158+
T::typenum(),
159+
strides,
160+
data,
161+
0, // itemsize
162+
0, // flag
163+
::std::ptr::null_mut(), //obj
164+
);
151165
Self::from_owned_ptr(py, ptr)
152166
}
153167

@@ -157,29 +171,33 @@ impl PyArray {
157171
}
158172

159173
/// a wrapper of [PyArray_ZEROS](https://docs.scipy.org/doc/numpy/reference/c-api.array.html#c.PyArray_ZEROS)
160-
pub fn zeros<T: TypeNum>(py: Python,
161-
np: &PyArrayModule,
162-
dims: &[usize],
163-
order: NPY_ORDER)
164-
-> Self {
174+
pub fn zeros<T: TypeNum>(
175+
py: Python,
176+
np: &PyArrayModule,
177+
dims: &[usize],
178+
order: NPY_ORDER,
179+
) -> Self {
165180
let dims: Vec<npy_intp> = dims.iter().map(|d| *d as npy_intp).collect();
166181
unsafe {
167182
let descr = np.PyArray_DescrFromType(T::typenum());
168-
let ptr = np.PyArray_Zeros(dims.len() as i32,
169-
dims.as_ptr() as *mut npy_intp,
170-
descr,
171-
order as i32);
183+
let ptr = np.PyArray_Zeros(
184+
dims.len() as i32,
185+
dims.as_ptr() as *mut npy_intp,
186+
descr,
187+
order as i32,
188+
);
172189
Self::from_owned_ptr(py, ptr)
173190
}
174191
}
175192

176193
/// a wrapper of [PyArray_Arange](https://docs.scipy.org/doc/numpy/reference/c-api.array.html#c.PyArray_Arange)
177-
pub fn arange<T: TypeNum>(py: Python,
178-
np: &PyArrayModule,
179-
start: f64,
180-
stop: f64,
181-
step: f64)
182-
-> Self {
194+
pub fn arange<T: TypeNum>(
195+
py: Python,
196+
np: &PyArrayModule,
197+
start: f64,
198+
stop: f64,
199+
step: f64,
200+
) -> Self {
183201
unsafe {
184202
let ptr = np.PyArray_Arange(start, stop, step, T::typenum());
185203
Self::from_owned_ptr(py, ptr)
@@ -230,9 +248,10 @@ impl PythonObject for PyArray {
230248
}
231249

232250
impl PythonObjectWithCheckedDowncast for PyArray {
233-
fn downcast_from<'p>(py: Python<'p>,
234-
obj: PyObject)
235-
-> Result<PyArray, PythonObjectDowncastError<'p>> {
251+
fn downcast_from<'p>(
252+
py: Python<'p>,
253+
obj: PyObject,
254+
) -> Result<PyArray, PythonObjectDowncastError<'p>> {
236255
let np = PyArrayModule::import(py).unwrap();
237256
unsafe {
238257
if npyffi::PyArray_Check(&np, obj.as_ptr()) != 0 {
@@ -243,9 +262,10 @@ impl PythonObjectWithCheckedDowncast for PyArray {
243262
}
244263
}
245264

246-
fn downcast_borrow_from<'a, 'p>(py: Python<'p>,
247-
obj: &'a PyObject)
248-
-> Result<&'a PyArray, PythonObjectDowncastError<'p>> {
265+
fn downcast_borrow_from<'a, 'p>(
266+
py: Python<'p>,
267+
obj: &'a PyObject,
268+
) -> Result<&'a PyArray, PythonObjectDowncastError<'p>> {
249269
let np = PyArrayModule::import(py).unwrap();
250270
unsafe {
251271
if npyffi::PyArray_Check(&np, obj.as_ptr()) != 0 {

src/convert.rs

Lines changed: 4 additions & 4 deletions
Original file line numberDiff line numberDiff line change
@@ -1,11 +1,10 @@
1-
21
use cpython::Python;
32
use ndarray::*;
43

5-
use std::os::raw::c_void;
6-
use std::ptr::null_mut;
74
use std::iter::Iterator;
85
use std::mem::size_of;
6+
use std::os::raw::c_void;
7+
use std::ptr::null_mut;
98

109
use super::*;
1110

@@ -44,7 +43,8 @@ pub trait ToPyArray {
4443
}
4544

4645
impl<Iter, T: TypeNum> ToPyArray for Iter
47-
where Iter: Iterator<Item = T> + Sized
46+
where
47+
Iter: Iterator<Item = T> + Sized,
4848
{
4949
fn to_pyarray(self, py: Python, np: &PyArrayModule) -> PyArray {
5050
let vec: Vec<T> = self.collect();

src/error.rs

Lines changed: 1 addition & 1 deletion
Original file line numberDiff line numberDiff line change
@@ -1,8 +1,8 @@
11
//! Define Errors
22
3+
use cpython::*;
34
use std::error;
45
use std::fmt;
5-
use cpython::*;
66

77
pub trait IntoPyErr {
88
fn into_pyerr(self, py: Python, msg: &str) -> PyErr;

src/lib.rs

Lines changed: 7 additions & 8 deletions
Original file line numberDiff line numberDiff line change
@@ -1,18 +1,17 @@
1-
2-
extern crate python3_sys as pyffi;
31
extern crate cpython;
42
extern crate libc;
5-
extern crate num_complex;
63
extern crate ndarray;
4+
extern crate num_complex;
5+
extern crate python3_sys as pyffi;
76

8-
pub mod types;
97
pub mod array;
10-
pub mod npyffi;
11-
pub mod error;
128
pub mod convert;
9+
pub mod error;
10+
pub mod npyffi;
11+
pub mod types;
1312

1413
pub use array::PyArray;
15-
pub use types::*;
16-
pub use error::*;
1714
pub use convert::{IntoPyArray, ToPyArray};
15+
pub use error::*;
1816
pub use npyffi::{PyArrayModule, PyUFuncModule};
17+
pub use types::*;

src/npyffi/array.rs

Lines changed: 46 additions & 44 deletions
Original file line numberDiff line numberDiff line change
@@ -2,14 +2,14 @@
22
//!
33
//! https://docs.scipy.org/doc/numpy/reference/c-api.array.html
44
5-
use std::os::raw::*;
6-
use std::ptr::null_mut;
75
use libc::FILE;
86
use std::ops::Deref;
7+
use std::os::raw::*;
8+
use std::ptr::null_mut;
99

10+
use cpython::{ObjectProtocol, PyModule, PyResult, Python, PythonObject};
1011
use pyffi;
1112
use pyffi::{PyObject, PyTypeObject};
12-
use cpython::{Python, PythonObject, ObjectProtocol, PyResult, PyModule};
1313

1414
use npyffi::*;
1515

@@ -48,8 +48,8 @@ impl PyArrayModule {
4848
let numpy = py.import("numpy.core.multiarray")?;
4949
let c_api = numpy.as_object().getattr(py, "_ARRAY_API")?;
5050
let api = unsafe {
51-
pyffi::PyCapsule_GetPointer(c_api.as_object().as_ptr(), null_mut()) as
52-
*const *const c_void
51+
pyffi::PyCapsule_GetPointer(c_api.as_object().as_ptr(), null_mut())
52+
as *const *const c_void
5353
};
5454
Ok(Self {
5555
numpy: numpy,
@@ -331,45 +331,47 @@ impl PyArrayModule {
331331
}
332332
}} // impl_array_type!;
333333

334-
impl_array_type!((1, PyBigArray_Type),
335-
(2, PyArray_Type),
336-
(3, PyArrayDescr_Type),
337-
(4, PyArrayFlags_Type),
338-
(5, PyArrayIter_Type),
339-
(6, PyArrayMultiIter_Type),
340-
(7, NPY_NUMUSERTYPES),
341-
(8, PyBoolArrType_Type),
342-
(9, _PyArrayScalar_BoolValues),
343-
(10, PyGenericArrType_Type),
344-
(11, PyNumberArrType_Type),
345-
(12, PyIntegerArrType_Type),
346-
(13, PySignedIntegerArrType_Type),
347-
(14, PyUnsignedIntegerArrType_Type),
348-
(15, PyInexactArrType_Type),
349-
(16, PyFloatingArrType_Type),
350-
(17, PyComplexFloatingArrType_Type),
351-
(18, PyFlexibleArrType_Type),
352-
(19, PyCharacterArrType_Type),
353-
(20, PyByteArrType_Type),
354-
(21, PyShortArrType_Type),
355-
(22, PyIntArrType_Type),
356-
(23, PyLongArrType_Type),
357-
(24, PyLongLongArrType_Type),
358-
(25, PyUByteArrType_Type),
359-
(26, PyUShortArrType_Type),
360-
(27, PyUIntArrType_Type),
361-
(28, PyULongArrType_Type),
362-
(29, PyULongLongArrType_Type),
363-
(30, PyFloatArrType_Type),
364-
(31, PyDoubleArrType_Type),
365-
(32, PyLongDoubleArrType_Type),
366-
(33, PyCFloatArrType_Type),
367-
(34, PyCDoubleArrType_Type),
368-
(35, PyCLongDoubleArrType_Type),
369-
(36, PyObjectArrType_Type),
370-
(37, PyStringArrType_Type),
371-
(38, PyUnicodeArrType_Type),
372-
(39, PyVoidArrType_Type));
334+
impl_array_type!(
335+
(1, PyBigArray_Type),
336+
(2, PyArray_Type),
337+
(3, PyArrayDescr_Type),
338+
(4, PyArrayFlags_Type),
339+
(5, PyArrayIter_Type),
340+
(6, PyArrayMultiIter_Type),
341+
(7, NPY_NUMUSERTYPES),
342+
(8, PyBoolArrType_Type),
343+
(9, _PyArrayScalar_BoolValues),
344+
(10, PyGenericArrType_Type),
345+
(11, PyNumberArrType_Type),
346+
(12, PyIntegerArrType_Type),
347+
(13, PySignedIntegerArrType_Type),
348+
(14, PyUnsignedIntegerArrType_Type),
349+
(15, PyInexactArrType_Type),
350+
(16, PyFloatingArrType_Type),
351+
(17, PyComplexFloatingArrType_Type),
352+
(18, PyFlexibleArrType_Type),
353+
(19, PyCharacterArrType_Type),
354+
(20, PyByteArrType_Type),
355+
(21, PyShortArrType_Type),
356+
(22, PyIntArrType_Type),
357+
(23, PyLongArrType_Type),
358+
(24, PyLongLongArrType_Type),
359+
(25, PyUByteArrType_Type),
360+
(26, PyUShortArrType_Type),
361+
(27, PyUIntArrType_Type),
362+
(28, PyULongArrType_Type),
363+
(29, PyULongLongArrType_Type),
364+
(30, PyFloatArrType_Type),
365+
(31, PyDoubleArrType_Type),
366+
(32, PyLongDoubleArrType_Type),
367+
(33, PyCFloatArrType_Type),
368+
(34, PyCDoubleArrType_Type),
369+
(35, PyCLongDoubleArrType_Type),
370+
(36, PyObjectArrType_Type),
371+
(37, PyStringArrType_Type),
372+
(38, PyUnicodeArrType_Type),
373+
(39, PyVoidArrType_Type)
374+
);
373375

374376
#[allow(non_snake_case)]
375377
pub unsafe fn PyArray_Check(np: &PyArrayModule, op: *mut PyObject) -> c_int {

src/npyffi/mod.rs

Lines changed: 4 additions & 4 deletions
Original file line numberDiff line numberDiff line change
@@ -9,12 +9,12 @@
99
//! - http://docs.python.jp/3/c-api/
1010
//! - http://dgrunwald.github.io/rust-cpython/doc/cpython/
1111
12-
pub mod types;
13-
pub mod objects;
1412
pub mod array;
13+
pub mod objects;
14+
pub mod types;
1515
pub mod ufunc;
1616

17-
pub use self::types::*;
18-
pub use self::objects::*;
1917
pub use self::array::*;
18+
pub use self::objects::*;
19+
pub use self::types::*;
2020
pub use self::ufunc::*;

0 commit comments

Comments
 (0)