Skip to content

Commit e4cda62

Browse files
committed
Refactor
1 parent 2f92994 commit e4cda62

File tree

1 file changed

+23
-17
lines changed

1 file changed

+23
-17
lines changed

src/types.rs

Lines changed: 23 additions & 17 deletions
Original file line numberDiff line numberDiff line change
@@ -35,24 +35,12 @@ impl NpyDataType {
3535
x if x == NPY_TYPES::NPY_BYTE as i32 => NpyDataType::Int8,
3636
x if x == NPY_TYPES::NPY_SHORT as i32 => NpyDataType::Int16,
3737
x if x == NPY_TYPES::NPY_INT as i32 => NpyDataType::Int32,
38-
x if x == NPY_TYPES::NPY_LONG as i32 => {
39-
if cfg!(any(windows, target_pointer_width = "32")) {
40-
NpyDataType::Int32
41-
} else {
42-
NpyDataType::Int64
43-
}
44-
}
38+
x if x == NPY_TYPES::NPY_LONG as i32 => NpyDataType::from_clong(false),
4539
x if x == NPY_TYPES::NPY_LONGLONG as i32 => NpyDataType::Int64,
4640
x if x == NPY_TYPES::NPY_UBYTE as i32 => NpyDataType::Uint8,
4741
x if x == NPY_TYPES::NPY_USHORT as i32 => NpyDataType::Uint16,
4842
x if x == NPY_TYPES::NPY_UINT as i32 => NpyDataType::Uint32,
49-
x if x == NPY_TYPES::NPY_ULONG as i32 => {
50-
if cfg!(any(windows, target_pointer_width = "32")) {
51-
NpyDataType::Uint32
52-
} else {
53-
NpyDataType::Uint64
54-
}
55-
}
43+
x if x == NPY_TYPES::NPY_ULONG as i32 => NpyDataType::from_clong(true),
5644
x if x == NPY_TYPES::NPY_ULONGLONG as i32 => NpyDataType::Uint64,
5745
x if x == NPY_TYPES::NPY_FLOAT as i32 => NpyDataType::Float32,
5846
x if x == NPY_TYPES::NPY_DOUBLE as i32 => NpyDataType::Float64,
@@ -61,6 +49,24 @@ impl NpyDataType {
6149
_ => NpyDataType::Unsupported,
6250
}
6351
}
52+
#[inline(always)]
53+
fn from_clong(is_usize: bool) -> NpyDataType {
54+
if cfg!(any(target_pointer_width = "32", windows)) {
55+
if is_usize {
56+
NpyDataType::Uint32
57+
} else {
58+
NpyDataType::Int32
59+
}
60+
} else if cfg!(all(target_pointer_width = "64", not(windows))) {
61+
if is_usize {
62+
NpyDataType::Uint64
63+
} else {
64+
NpyDataType::Int64
65+
}
66+
} else {
67+
NpyDataType::Unsupported
68+
}
69+
}
6470
}
6571

6672
pub trait TypeNum: Clone {
@@ -84,7 +90,7 @@ macro_rules! impl_type_num {
8490
}
8591
}
8692
};
87-
} // impl_type_num!
93+
}
8894

8995
impl_type_num!(bool, Bool, NPY_BOOL);
9096
impl_type_num!(i8, Int8, NPY_BYTE);
@@ -97,12 +103,12 @@ impl_type_num!(c32, Complex32, NPY_CFLOAT);
97103
impl_type_num!(c64, Complex64, NPY_CDOUBLE);
98104

99105
cfg_if! {
100-
if #[cfg(any(windows, target_pointer_width = "32"))] {
106+
if #[cfg(any(target_pointer_width = "32", windows))] {
101107
impl_type_num!(i32, Int32, NPY_INT, NPY_LONG);
102108
impl_type_num!(u32, Uint32, NPY_UINT, NPY_ULONG);
103109
impl_type_num!(i64, Int64, NPY_LONGLONG);
104110
impl_type_num!(u64, Uint64, NPY_ULONGLONG);
105-
} else {
111+
} else if #[cfg(all(target_pointer_width = "64", not(windows)))] {
106112
impl_type_num!(i32, Int32, NPY_INT);
107113
impl_type_num!(u32, Uint32, NPY_UINT);
108114
impl_type_num!(i64, Int64, NPY_LONG, NPY_LONGLONG);

0 commit comments

Comments
 (0)