Skip to content

Commit 0d094bf

Browse files
committed
shape: Add trait ShapeArg
1 parent 31c71da commit 0d094bf

File tree

3 files changed

+33
-2
lines changed

3 files changed

+33
-2
lines changed

src/impl_methods.rs

Lines changed: 1 addition & 1 deletion
Original file line numberDiff line numberDiff line change
@@ -26,8 +26,8 @@ use crate::dimension::broadcast::co_broadcast;
2626
use crate::error::{self, ErrorKind, ShapeError, from_kind};
2727
use crate::math_cell::MathCell;
2828
use crate::itertools::zip;
29-
use crate::zip::{IntoNdProducer, Zip};
3029
use crate::AxisDescription;
30+
use crate::zip::{IntoNdProducer, Zip};
3131

3232
use crate::iter::{
3333
AxisChunksIter, AxisChunksIterMut, AxisIter, AxisIterMut, ExactChunks, ExactChunksMut,

src/lib.rs

Lines changed: 1 addition & 1 deletion
Original file line numberDiff line numberDiff line change
@@ -163,7 +163,7 @@ pub use crate::stacking::{concatenate, stack, stack_new_axis};
163163

164164
pub use crate::math_cell::MathCell;
165165
pub use crate::impl_views::IndexLonger;
166-
pub use crate::shape_builder::{Shape, ShapeBuilder, StrideShape};
166+
pub use crate::shape_builder::{Shape, ShapeBuilder, ShapeArg, StrideShape};
167167

168168
#[macro_use]
169169
mod macro_utils;

src/shape_builder.rs

Lines changed: 31 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -1,5 +1,6 @@
11
use crate::dimension::IntoDimension;
22
use crate::Dimension;
3+
use crate::order::Order;
34

45
/// A contiguous array shape of n dimensions.
56
///
@@ -184,3 +185,33 @@ where
184185
self.dim.size()
185186
}
186187
}
188+
189+
190+
/// Array shape argument with optional order parameter
191+
///
192+
/// Shape or array dimension argument, with optional [`Order`] parameter.
193+
///
194+
/// This is an argument conversion trait that is used to accept an array shape and
195+
/// (optionally) an ordering argument.
196+
///
197+
/// See for example [`.to_shape()`](crate::ArrayBase::to_shape).
198+
pub trait ShapeArg {
199+
type Dim: Dimension;
200+
fn into_shape_and_order(self) -> (Self::Dim, Option<Order>);
201+
}
202+
203+
impl<T> ShapeArg for T where T: IntoDimension {
204+
type Dim = T::Dim;
205+
206+
fn into_shape_and_order(self) -> (Self::Dim, Option<Order>) {
207+
(self.into_dimension(), None)
208+
}
209+
}
210+
211+
impl<T> ShapeArg for (T, Order) where T: IntoDimension {
212+
type Dim = T::Dim;
213+
214+
fn into_shape_and_order(self) -> (Self::Dim, Option<Order>) {
215+
(self.0.into_dimension(), Some(self.1))
216+
}
217+
}

0 commit comments

Comments
 (0)