@@ -13,45 +13,41 @@ use crate::tui::buffer::LineEnding;
1313
1414/// Trait that provides simple methods to get the last valid index of a collection or slice.
1515pub trait LastIndex {
16+ /// Returns the index of the last element in the collection.
17+ ///
18+ /// Returns `None` if the collection is empty.
19+ ///
20+ /// Under most cirumstances, this should be the only trait function you need to define.
21+ fn last_index_checked ( & self ) -> Option < usize > ;
1622 /// Returns `true` if the given index matches the index of the last element in the collection.
1723 ///
1824 /// Returns `false` if the index doesn't match, or if the collection is empty.
19- fn last_index_eq ( & self , index : usize ) -> bool ;
25+ fn last_index_eq ( & self , index : usize ) -> bool {
26+ if let Some ( last_index) = self . last_index_checked ( ) {
27+ last_index == index
28+ } else {
29+ false
30+ }
31+ }
2032 /// Returns `true` if the given index matches or is greater than the index of the last element in the collection.
2133 ///
2234 /// Returns `false` if the index doesn't fit either condition, or if the collection is empty.
23- fn last_index_eq_or_greater ( & self , index : usize ) -> bool ;
35+ fn last_index_eq_or_under ( & self , index : usize ) -> bool {
36+ if let Some ( last_index) = self . last_index_checked ( ) {
37+ index >= last_index
38+ } else {
39+ false
40+ }
41+ }
2442 /// Returns the index of the last element in the collection.
2543 ///
2644 /// **Panics** if the collection is empty.
2745 fn last_index ( & self ) -> usize {
2846 self . last_index_checked ( )
2947 . expect ( "empty collection; no final element exists" )
3048 }
31- /// Returns the index of the last element in the collection.
32- ///
33- /// Returns `None` if the collection is empty.
34- fn last_index_checked ( & self ) -> Option < usize > ;
3549}
3650impl < T > LastIndex for [ T ] {
37- fn last_index_eq ( & self , index : usize ) -> bool {
38- if self . is_empty ( ) {
39- false
40- } else if index == self . len ( ) - 1 {
41- true
42- } else {
43- false
44- }
45- }
46- fn last_index_eq_or_greater ( & self , index : usize ) -> bool {
47- if self . is_empty ( ) {
48- false
49- } else if index >= self . len ( ) - 1 {
50- true
51- } else {
52- false
53- }
54- }
5551 fn last_index_checked ( & self ) -> Option < usize > {
5652 if self . is_empty ( ) {
5753 None
0 commit comments