From 14a2bdc06232066c4be06825b8894a22666ef1ca Mon Sep 17 00:00:00 2001 From: Laurent Mazare Date: Sun, 26 Nov 2023 16:30:59 +0000 Subject: [PATCH] Small tweak: remove the macro usage for the range indexing trait. (#1376) --- candle-core/src/indexer.rs | 50 +++++++++++++++++--------------------- 1 file changed, 22 insertions(+), 28 deletions(-) diff --git a/candle-core/src/indexer.rs b/candle-core/src/indexer.rs index 7b84d316..df106b73 100644 --- a/candle-core/src/indexer.rs +++ b/candle-core/src/indexer.rs @@ -104,37 +104,31 @@ impl From<&Tensor> for TensorIndexer { } } -macro_rules! impl_from_range { - ($range_type:ty) => { - impl From<$range_type> for TensorIndexer { - fn from(range: $range_type) -> Self { - use std::ops::Bound::*; +trait RB: RangeBounds {} +impl RB for Range {} +impl RB for RangeFrom {} +impl RB for RangeFull {} +impl RB for RangeInclusive {} +impl RB for RangeTo {} +impl RB for RangeToInclusive {} - let start = match range.start_bound() { - Included(idx) => Included(*idx), - Excluded(idx) => Excluded(*idx), - Unbounded => Unbounded, - }; - - let end = match range.end_bound() { - Included(idx) => Included(*idx), - Excluded(idx) => Excluded(*idx), - Unbounded => Unbounded, - }; - - TensorIndexer::Narrow(start, end) - } - } - }; +impl From for TensorIndexer { + fn from(range: T) -> Self { + use std::ops::Bound::*; + let start = match range.start_bound() { + Included(idx) => Included(*idx), + Excluded(idx) => Excluded(*idx), + Unbounded => Unbounded, + }; + let end = match range.end_bound() { + Included(idx) => Included(*idx), + Excluded(idx) => Excluded(*idx), + Unbounded => Unbounded, + }; + TensorIndexer::Narrow(start, end) + } } -impl_from_range!(Range); -impl_from_range!(RangeFrom); -impl_from_range!(RangeFull); -impl_from_range!(RangeInclusive); -impl_from_range!(RangeTo); -impl_from_range!(RangeToInclusive); - /// Trait used to implement multiple signatures for ease of use of the slicing /// of a tensor pub trait IndexOp {