Skip to content

Commit 73ffe90

Browse files
committed
API: Move Layout consts into the impl
Layout is fully pub (but hidden) for legacy reasons - used from NdProducer trait. It's not a stable feature.
1 parent a5a283d commit 73ffe90

File tree

2 files changed

+43
-41
lines changed

2 files changed

+43
-41
lines changed

src/layout/mod.rs

+37-35
Original file line numberDiff line numberDiff line change
@@ -9,6 +9,11 @@ mod layoutfmt;
99
pub struct Layout(u32);
1010

1111
impl Layout {
12+
pub(crate) const CORDER: u32 = 0b01;
13+
pub(crate) const FORDER: u32 = 0b10;
14+
pub(crate) const CPREFER: u32 = 0b0100;
15+
pub(crate) const FPREFER: u32 = 0b1000;
16+
1217
#[inline(always)]
1318
pub(crate) fn is(self, flag: u32) -> bool {
1419
self.0 & flag != 0
@@ -33,22 +38,22 @@ impl Layout {
3338

3439
#[inline(always)]
3540
pub(crate) fn c() -> Layout {
36-
Layout(CORDER | CPREFER)
41+
Layout(Layout::CORDER | Layout::CPREFER)
3742
}
3843

3944
#[inline(always)]
4045
pub(crate) fn f() -> Layout {
41-
Layout(FORDER | FPREFER)
46+
Layout(Layout::FORDER | Layout::FPREFER)
4247
}
4348

4449
#[inline(always)]
4550
pub(crate) fn cpref() -> Layout {
46-
Layout(CPREFER)
51+
Layout(Layout::CPREFER)
4752
}
4853

4954
#[inline(always)]
5055
pub(crate) fn fpref() -> Layout {
51-
Layout(FPREFER)
56+
Layout(Layout::FPREFER)
5257
}
5358

5459
#[inline(always)]
@@ -60,17 +65,12 @@ impl Layout {
6065
/// Subject to change when we can describe other layouts
6166
#[inline]
6267
pub(crate) fn tendency(self) -> i32 {
63-
(self.is(CORDER) as i32 - self.is(FORDER) as i32) +
64-
(self.is(CPREFER) as i32 - self.is(FPREFER) as i32)
68+
(self.is(Layout::CORDER) as i32 - self.is(Layout::FORDER) as i32) +
69+
(self.is(Layout::CPREFER) as i32 - self.is(Layout::FPREFER) as i32)
6570

6671
}
6772
}
6873

69-
pub const CORDER: u32 = 0b01;
70-
pub const FORDER: u32 = 0b10;
71-
pub const CPREFER: u32 = 0b0100;
72-
pub const FPREFER: u32 = 0b1000;
73-
7474

7575
#[cfg(test)]
7676
mod tests {
@@ -83,21 +83,23 @@ mod tests {
8383
type M0 = Array0<f32>;
8484

8585
macro_rules! assert_layouts {
86-
($mat:expr, $($layout:expr),*) => {{
86+
($mat:expr, $($layout:ident),*) => {{
8787
let layout = $mat.view().layout();
8888
$(
89-
assert!(layout.is($layout), "Assertion failed: array {:?} is not layout {}",
89+
assert!(layout.is(Layout::$layout),
90+
"Assertion failed: array {:?} is not layout {}",
9091
$mat,
9192
stringify!($layout));
9293
)*
9394
}}
9495
}
9596

9697
macro_rules! assert_not_layouts {
97-
($mat:expr, $($layout:expr),*) => {{
98+
($mat:expr, $($layout:ident),*) => {{
9899
let layout = $mat.view().layout();
99100
$(
100-
assert!(!layout.is($layout), "Assertion failed: array {:?} show not have layout {}",
101+
assert!(!layout.is(Layout::$layout),
102+
"Assertion failed: array {:?} show not have layout {}",
101103
$mat,
102104
stringify!($layout));
103105
)*
@@ -110,10 +112,10 @@ mod tests {
110112
let b = M::zeros((5, 5).f());
111113
let ac = a.view().layout();
112114
let af = b.view().layout();
113-
assert!(ac.is(CORDER) && ac.is(CPREFER));
114-
assert!(!ac.is(FORDER) && !ac.is(FPREFER));
115-
assert!(!af.is(CORDER) && !af.is(CPREFER));
116-
assert!(af.is(FORDER) && af.is(FPREFER));
115+
assert!(ac.is(Layout::CORDER) && ac.is(Layout::CPREFER));
116+
assert!(!ac.is(Layout::FORDER) && !ac.is(Layout::FPREFER));
117+
assert!(!af.is(Layout::CORDER) && !af.is(Layout::CPREFER));
118+
assert!(af.is(Layout::FORDER) && af.is(Layout::FPREFER));
117119
}
118120

119121
#[test]
@@ -152,10 +154,10 @@ mod tests {
152154
let v1 = a.slice(s![1.., ..]).layout();
153155
let v2 = a.slice(s![.., 1..]).layout();
154156

155-
assert!(v1.is(CORDER) && v1.is(CPREFER));
156-
assert!(!v1.is(FORDER) && !v1.is(FPREFER));
157-
assert!(!v2.is(CORDER) && v2.is(CPREFER));
158-
assert!(!v2.is(FORDER) && !v2.is(FPREFER));
157+
assert!(v1.is(Layout::CORDER) && v1.is(Layout::CPREFER));
158+
assert!(!v1.is(Layout::FORDER) && !v1.is(Layout::FPREFER));
159+
assert!(!v2.is(Layout::CORDER) && v2.is(Layout::CPREFER));
160+
assert!(!v2.is(Layout::FORDER) && !v2.is(Layout::FPREFER));
159161
}
160162

161163
let b = M::zeros((5, 5).f());
@@ -164,10 +166,10 @@ mod tests {
164166
let v1 = b.slice(s![1.., ..]).layout();
165167
let v2 = b.slice(s![.., 1..]).layout();
166168

167-
assert!(!v1.is(CORDER) && !v1.is(CPREFER));
168-
assert!(!v1.is(FORDER) && v1.is(FPREFER));
169-
assert!(!v2.is(CORDER) && !v2.is(CPREFER));
170-
assert!(v2.is(FORDER) && v2.is(FPREFER));
169+
assert!(!v1.is(Layout::CORDER) && !v1.is(Layout::CPREFER));
170+
assert!(!v1.is(Layout::FORDER) && v1.is(Layout::FPREFER));
171+
assert!(!v2.is(Layout::CORDER) && !v2.is(Layout::CPREFER));
172+
assert!(v2.is(Layout::FORDER) && v2.is(Layout::FPREFER));
171173
}
172174
}
173175

@@ -206,21 +208,21 @@ mod tests {
206208
let v1 = a.slice(s![..;2, ..]).layout();
207209
let v2 = a.slice(s![.., ..;2]).layout();
208210

209-
assert!(!v1.is(CORDER) && v1.is(CPREFER));
210-
assert!(!v1.is(FORDER) && !v1.is(FPREFER));
211-
assert!(!v2.is(CORDER) && !v2.is(CPREFER));
212-
assert!(!v2.is(FORDER) && !v2.is(FPREFER));
211+
assert!(!v1.is(Layout::CORDER) && v1.is(Layout::CPREFER));
212+
assert!(!v1.is(Layout::FORDER) && !v1.is(Layout::FPREFER));
213+
assert!(!v2.is(Layout::CORDER) && !v2.is(Layout::CPREFER));
214+
assert!(!v2.is(Layout::FORDER) && !v2.is(Layout::FPREFER));
213215
}
214216

215217
let b = M::zeros((5, 5).f());
216218
{
217219
let v1 = b.slice(s![..;2, ..]).layout();
218220
let v2 = b.slice(s![.., ..;2]).layout();
219221

220-
assert!(!v1.is(CORDER) && !v1.is(CPREFER));
221-
assert!(!v1.is(FORDER) && !v1.is(FPREFER));
222-
assert!(!v2.is(CORDER) && !v2.is(CPREFER));
223-
assert!(!v2.is(FORDER) && v2.is(FPREFER));
222+
assert!(!v1.is(Layout::CORDER) && !v1.is(Layout::CPREFER));
223+
assert!(!v1.is(Layout::FORDER) && !v1.is(Layout::FPREFER));
224+
assert!(!v2.is(Layout::CORDER) && !v2.is(Layout::CPREFER));
225+
assert!(!v2.is(Layout::FORDER) && v2.is(Layout::FPREFER));
224226
}
225227
}
226228
}

src/zip/mod.rs

+6-6
Original file line numberDiff line numberDiff line change
@@ -20,7 +20,6 @@ use crate::Layout;
2020
use crate::partial::Partial;
2121

2222
use crate::indexes::{indices, Indices};
23-
use crate::layout::{CORDER, FORDER};
2423
use crate::split_at::{SplitPreference, SplitAt};
2524

2625
pub use self::ndproducer::{NdProducer, IntoNdProducer, Offset};
@@ -272,7 +271,8 @@ where
272271
}
273272

274273
fn prefer_f(&self) -> bool {
275-
!self.layout.is(CORDER) && (self.layout.is(FORDER) || self.layout_tendency < 0)
274+
!self.layout.is(Layout::CORDER) &&
275+
(self.layout.is(Layout::FORDER) || self.layout_tendency < 0)
276276
}
277277

278278
/// Return an *approximation* to the max stride axis; if
@@ -310,7 +310,7 @@ where
310310
{
311311
if self.dimension.ndim() == 0 {
312312
function(acc, unsafe { self.parts.as_ref(self.parts.as_ptr()) })
313-
} else if self.layout.is(CORDER | FORDER) {
313+
} else if self.layout.is(Layout::CORDER | Layout::FORDER) {
314314
self.for_each_core_contiguous(acc, function)
315315
} else {
316316
self.for_each_core_strided(acc, function)
@@ -322,7 +322,7 @@ where
322322
F: FnMut(Acc, P::Item) -> FoldWhile<Acc>,
323323
P: ZippableTuple<Dim = D>,
324324
{
325-
debug_assert!(self.layout.is(CORDER | FORDER));
325+
debug_assert!(self.layout.is(Layout::CORDER | Layout::FORDER));
326326
let size = self.dimension.size();
327327
let ptrs = self.parts.as_ptr();
328328
let inner_strides = self.parts.contiguous_stride();
@@ -440,7 +440,7 @@ where
440440
// Method placement: only used for binary Zip at the moment.
441441
#[inline]
442442
pub(crate) fn debug_assert_c_order(self) -> Self {
443-
debug_assert!(self.layout.is(CORDER) || self.layout_tendency >= 0 ||
443+
debug_assert!(self.layout.is(Layout::CORDER) || self.layout_tendency >= 0 ||
444444
self.dimension.slice().iter().filter(|&&d| d > 1).count() <= 1,
445445
"Assertion failed: traversal is not c-order or 1D for \
446446
layout {:?}, tendency {}, dimension {:?}",
@@ -839,7 +839,7 @@ macro_rules! map_impl {
839839
// debug assert that the output is contiguous in the memory layout we need
840840
if cfg!(debug_assertions) {
841841
let out_layout = output.layout();
842-
assert!(out_layout.is(CORDER | FORDER));
842+
assert!(out_layout.is(Layout::CORDER | Layout::FORDER));
843843
assert!(
844844
(self.layout_tendency <= 0 && out_layout.tendency() <= 0) ||
845845
(self.layout_tendency >= 0 && out_layout.tendency() >= 0),

0 commit comments

Comments
 (0)