Small tweak: remove the macro usage for the range indexing trait. (#1376)

This commit is contained in:
Laurent Mazare
2023-11-26 16:30:59 +00:00
committed by GitHub
parent bfa7c8fc01
commit 14a2bdc062

View File

@ -104,37 +104,31 @@ impl From<&Tensor> for TensorIndexer {
} }
} }
macro_rules! impl_from_range { trait RB: RangeBounds<usize> {}
($range_type:ty) => { impl RB for Range<usize> {}
impl From<$range_type> for TensorIndexer { impl RB for RangeFrom<usize> {}
fn from(range: $range_type) -> Self { impl RB for RangeFull {}
use std::ops::Bound::*; impl RB for RangeInclusive<usize> {}
impl RB for RangeTo<usize> {}
impl RB for RangeToInclusive<usize> {}
let start = match range.start_bound() { impl<T: RB> From<T> for TensorIndexer {
Included(idx) => Included(*idx), fn from(range: T) -> Self {
Excluded(idx) => Excluded(*idx), use std::ops::Bound::*;
Unbounded => Unbounded, let start = match range.start_bound() {
}; Included(idx) => Included(*idx),
Excluded(idx) => Excluded(*idx),
let end = match range.end_bound() { Unbounded => Unbounded,
Included(idx) => Included(*idx), };
Excluded(idx) => Excluded(*idx), let end = match range.end_bound() {
Unbounded => Unbounded, Included(idx) => Included(*idx),
}; Excluded(idx) => Excluded(*idx),
Unbounded => Unbounded,
TensorIndexer::Narrow(start, end) };
} TensorIndexer::Narrow(start, end)
} }
};
} }
impl_from_range!(Range<usize>);
impl_from_range!(RangeFrom<usize>);
impl_from_range!(RangeFull);
impl_from_range!(RangeInclusive<usize>);
impl_from_range!(RangeTo<usize>);
impl_from_range!(RangeToInclusive<usize>);
/// Trait used to implement multiple signatures for ease of use of the slicing /// Trait used to implement multiple signatures for ease of use of the slicing
/// of a tensor /// of a tensor
pub trait IndexOp<T> { pub trait IndexOp<T> {