use std::ops::{Range, RangeFrom, RangeTo, RangeFull};
use std::fmt;
use super::Ixs;
#[derive(PartialEq, Eq, Hash)]
pub struct Si(pub Ixs, pub Option<Ixs>, pub Ixs);
impl fmt::Debug for Si {
fn fmt(&self, f: &mut fmt::Formatter) -> fmt::Result {
match *self {
Si(0, _, _) => { }
Si(i, _, _) => { try!(write!(f, "{}", i)); }
}
try!(write!(f, ".."));
match *self {
Si(_, None, _) => { }
Si(_, Some(i), _) => { try!(write!(f, "{}", i)); }
}
match *self {
Si(_, _, 1) => { }
Si(_, _, s) => { try!(write!(f, ";{}", s)); }
}
Ok(())
}
}
impl From<Range<Ixs>> for Si {
#[inline]
fn from(r: Range<Ixs>) -> Si {
Si(r.start, Some(r.end), 1)
}
}
impl From<RangeFrom<Ixs>> for Si {
#[inline]
fn from(r: RangeFrom<Ixs>) -> Si {
Si(r.start, None, 1)
}
}
impl From<RangeTo<Ixs>> for Si {
#[inline]
fn from(r: RangeTo<Ixs>) -> Si {
Si(0, Some(r.end), 1)
}
}
impl From<RangeFull> for Si {
#[inline]
fn from(_: RangeFull) -> Si {
S
}
}
impl Si {
#[inline]
pub fn step(self, step: Ixs) -> Self {
Si(self.0, self.1, self.2 * step)
}
}
copy_and_clone!{Si}
pub const S: Si = Si(0, None, 1);
#[macro_export]
macro_rules! s(
(@parse [$($stack:tt)*] $r:expr;$s:expr) => {
&[$($stack)* s!(@step $r, $s)]
};
(@parse [$($stack:tt)*] $r:expr) => {
&[$($stack)* s!(@step $r, 1)]
};
(@parse [$($stack:tt)*] $r:expr;$s:expr, $($t:tt)*) => {
s![@parse [$($stack)* s!(@step $r, $s),] $($t)*]
};
(@parse [$($stack:tt)*] $r:expr, $($t:tt)*) => {
s![@parse [$($stack)* s!(@step $r, 1),] $($t)*]
};
(@step $r:expr, $s:expr) => {
<$crate::Si as ::std::convert::From<_>>::from($r).step($s)
};
($($t:tt)*) => {
s![@parse [] $($t)*]
};
);