1
- use std:: ops:: { AddAssign , Deref } ;
2
-
3
1
macro_rules! declare_types {
4
2
( $( <$lt: lifetime>) ?
5
3
$( derive( $( $Der: ident) ,* ) , ) ?
@@ -51,7 +49,18 @@ macro_rules! declare_types {
51
49
#[ derive( Debug , Copy , Clone ) ]
52
50
pub struct ModifierSet < S > ( S ) ;
53
51
54
- impl < S : Deref < Target = str > > ModifierSet < S > {
52
+ impl < S : Default > Default for ModifierSet < S > {
53
+ /// Construct the default modifier set.
54
+ ///
55
+ /// This is typically the empty set,
56
+ /// though the remark from [`Self::new_unchecked`] applies
57
+ /// since `S::default()` could technically be anything.
58
+ fn default ( ) -> Self {
59
+ Self ( S :: default ( ) )
60
+ }
61
+ }
62
+
63
+ impl < S : std:: ops:: Deref < Target = str > > ModifierSet < S > {
55
64
/// Convert the underlying string to a slice.
56
65
pub fn as_deref ( & self ) -> ModifierSet < & str > {
57
66
ModifierSet ( & self . 0 )
@@ -68,14 +77,6 @@ impl<S: Deref<Target = str>> ModifierSet<S> {
68
77
Self ( s)
69
78
}
70
79
71
- /// Construct an empty modifier set.
72
- pub fn empty ( ) -> Self
73
- where
74
- S : Default ,
75
- {
76
- Self ( S :: default ( ) )
77
- }
78
-
79
80
/// Whether `self` is empty.
80
81
pub fn is_empty ( & self ) -> bool {
81
82
self . 0 . is_empty ( )
@@ -88,7 +89,7 @@ impl<S: Deref<Target = str>> ModifierSet<S> {
88
89
/// `modifier` is not empty and doesn't contain the character `.`.
89
90
pub fn add_unchecked ( & mut self , m : & str )
90
91
where
91
- S : for < ' a > AddAssign < & ' a str > ,
92
+ S : for < ' a > std :: ops :: AddAssign < & ' a str > ,
92
93
{
93
94
if !self . 0 . is_empty ( ) {
94
95
self . 0 += "." ;
@@ -136,8 +137,8 @@ impl<S: Deref<Target = str>> ModifierSet<S> {
136
137
total += 1 ;
137
138
}
138
139
139
- let score = ( matching, core :: cmp:: Reverse ( total) ) ;
140
- if best_score. map_or ( true , |b| score > b) {
140
+ let score = ( matching, std :: cmp:: Reverse ( total) ) ;
141
+ if best_score. is_none_or ( |b| score > b) {
141
142
best = Some ( candidate. 1 ) ;
142
143
best_score = Some ( score) ;
143
144
}
@@ -146,3 +147,10 @@ impl<S: Deref<Target = str>> ModifierSet<S> {
146
147
best
147
148
}
148
149
}
150
+
151
+ impl < ' a > ModifierSet < & ' a str > {
152
+ /// Iterate over the list of modifiers with the original lifetime.
153
+ pub fn to_iter ( self ) -> impl Iterator < Item = & ' a str > {
154
+ self . 0 . split ( '.' ) . filter ( |s| !s. is_empty ( ) )
155
+ }
156
+ }
0 commit comments