|
1 | | -use std::{borrow::Cow, ops::Not as _}; |
| 1 | +use std::{borrow::Cow, cmp::Ordering, ops::Not as _}; |
2 | 2 |
|
3 | 3 | use darling::util::IdentString; |
4 | 4 | use indoc::formatdoc; |
@@ -580,7 +580,7 @@ struct TracingTokens { |
580 | 580 | try_convert_instrumentation: Option<TokenStream>, |
581 | 581 | } |
582 | 582 |
|
583 | | -fn conversion_path<'a, T>(elements: &'a [T]) -> Vec<(&'a T, Cow<'a, [T]>)> |
| 583 | +fn conversion_path<T>(elements: &[T]) -> Vec<(&T, Cow<'_, [T]>)> |
584 | 584 | where |
585 | 585 | T: Clone + Ord, |
586 | 586 | { |
@@ -611,26 +611,28 @@ where |
611 | 611 | elements.iter().position(|v| v == start), |
612 | 612 | elements.iter().position(|v| v == end), |
613 | 613 | ) { |
614 | | - let path = if start_index < end_index { |
615 | | - // If the start index is smaller than the end index (upgrade), we can return |
616 | | - // a slice pointing directly into the original slice. That's why Cow::Borrowed |
617 | | - // can be used here. |
618 | | - Cow::Borrowed(&elements[start_index + 1..=end_index]) |
619 | | - } else if start_index > end_index { |
620 | | - // If the start index is bigger than the end index (downgrade), we need to reverse |
621 | | - // the elements. With a slice, this is only possible to do in place, which is not |
622 | | - // what we want in this case. Instead, the data is reversed and cloned and collected |
623 | | - // into a Vec and Cow::Owned is used. |
624 | | - let path = elements[end_index..start_index] |
625 | | - .iter() |
626 | | - .rev() |
627 | | - .cloned() |
628 | | - .collect(); |
629 | | - Cow::Owned(path) |
630 | | - } else { |
631 | | - unreachable!( |
| 614 | + let path = match start_index.cmp(&end_index) { |
| 615 | + Ordering::Less => { |
| 616 | + // If the start index is smaller than the end index (upgrade), we can return |
| 617 | + // a slice pointing directly into the original slice. That's why Cow::Borrowed |
| 618 | + // can be used here. |
| 619 | + Cow::Borrowed(&elements[start_index + 1..=end_index]) |
| 620 | + } |
| 621 | + Ordering::Greater => { |
| 622 | + // If the start index is bigger than the end index (downgrade), we need to reverse |
| 623 | + // the elements. With a slice, this is only possible to do in place, which is not |
| 624 | + // what we want in this case. Instead, the data is reversed and cloned and collected |
| 625 | + // into a Vec and Cow::Owned is used. |
| 626 | + let path = elements[end_index..start_index] |
| 627 | + .iter() |
| 628 | + .rev() |
| 629 | + .cloned() |
| 630 | + .collect(); |
| 631 | + Cow::Owned(path) |
| 632 | + } |
| 633 | + Ordering::Equal => unreachable!( |
632 | 634 | "start and end index cannot be the same due to selecting permutations" |
633 | | - ); |
| 635 | + ), |
634 | 636 | }; |
635 | 637 |
|
636 | 638 | chain.push((start, path)); |
|
0 commit comments