David Tolnay | 2b687b0 | 2018-08-24 13:36:36 -0400 | [diff] [blame] | 1 | use proc_macro2::Span; |
| 2 | |
David Tolnay | 544a90f | 2018-08-24 20:34:03 -0400 | [diff] [blame] | 3 | use super::lookahead::TokenMarker; |
David Tolnay | 2b687b0 | 2018-08-24 13:36:36 -0400 | [diff] [blame] | 4 | |
| 5 | pub trait IntoSpans<S> { |
| 6 | // Not public API. |
| 7 | #[doc(hidden)] |
| 8 | fn into_spans(self) -> S; |
| 9 | } |
| 10 | |
| 11 | impl<S> IntoSpans<S> for TokenMarker { |
| 12 | fn into_spans(self) -> S { |
| 13 | match self {} |
| 14 | } |
| 15 | } |
| 16 | |
| 17 | impl IntoSpans<[Span; 1]> for Span { |
| 18 | fn into_spans(self) -> [Span; 1] { |
| 19 | [self] |
| 20 | } |
| 21 | } |
| 22 | |
| 23 | impl IntoSpans<[Span; 2]> for Span { |
| 24 | fn into_spans(self) -> [Span; 2] { |
| 25 | [self, self] |
| 26 | } |
| 27 | } |
| 28 | |
| 29 | impl IntoSpans<[Span; 3]> for Span { |
| 30 | fn into_spans(self) -> [Span; 3] { |
| 31 | [self, self, self] |
| 32 | } |
| 33 | } |
| 34 | |
| 35 | impl IntoSpans<Self> for [Span; 1] { |
| 36 | fn into_spans(self) -> Self { |
| 37 | self |
| 38 | } |
| 39 | } |
| 40 | |
| 41 | impl IntoSpans<Self> for [Span; 2] { |
| 42 | fn into_spans(self) -> Self { |
| 43 | self |
| 44 | } |
| 45 | } |
| 46 | |
| 47 | impl IntoSpans<Self> for [Span; 3] { |
| 48 | fn into_spans(self) -> Self { |
| 49 | self |
| 50 | } |
| 51 | } |
| 52 | |
| 53 | pub trait FromSpans: Sized { |
| 54 | fn from_spans(spans: &[Span]) -> Self; |
| 55 | } |
| 56 | |
| 57 | impl FromSpans for [Span; 1] { |
| 58 | fn from_spans(spans: &[Span]) -> Self { |
| 59 | [spans[0]] |
| 60 | } |
| 61 | } |
| 62 | |
| 63 | impl FromSpans for [Span; 2] { |
| 64 | fn from_spans(spans: &[Span]) -> Self { |
| 65 | [spans[0], spans[1]] |
| 66 | } |
| 67 | } |
| 68 | |
| 69 | impl FromSpans for [Span; 3] { |
| 70 | fn from_spans(spans: &[Span]) -> Self { |
| 71 | [spans[0], spans[1], spans[2]] |
| 72 | } |
| 73 | } |