@@ -4,8 +4,8 @@ use std::cell::OnceCell;
44use std:: fmt;
55use std:: fmt:: Debug ;
66use std:: ops:: Deref ;
7+ use std:: slice:: { Iter , IterMut } ;
78
8- use itertools:: Either :: { Left , Right } ;
99use itertools:: Itertools ;
1010
1111use ruff_text_size:: { Ranged , TextRange , TextSize } ;
@@ -1051,23 +1051,33 @@ impl FStringValue {
10511051 matches ! ( self . inner, FStringValueInner :: Concatenated ( _) )
10521052 }
10531053
1054- /// Returns an iterator over all the [`FStringPart`]s contained in this value.
1055- pub fn parts ( & self ) -> impl Iterator < Item = & FStringPart > {
1054+ /// Returns a slice of all the [`FStringPart`]s contained in this value.
1055+ pub fn as_slice ( & self ) -> & [ FStringPart ] {
10561056 match & self . inner {
1057- FStringValueInner :: Single ( part) => Left ( std:: iter :: once ( part) ) ,
1058- FStringValueInner :: Concatenated ( parts) => Right ( parts. iter ( ) ) ,
1057+ FStringValueInner :: Single ( part) => std:: slice :: from_ref ( part) ,
1058+ FStringValueInner :: Concatenated ( parts) => parts,
10591059 }
10601060 }
10611061
1062- /// Returns an iterator over all the [`FStringPart`]s contained in this value
1063- /// that allows modification.
1064- pub ( crate ) fn parts_mut ( & mut self ) -> impl Iterator < Item = & mut FStringPart > {
1062+ /// Returns a mutable slice of all the [`FStringPart`]s contained in this value.
1063+ fn as_mut_slice ( & mut self ) -> & mut [ FStringPart ] {
10651064 match & mut self . inner {
1066- FStringValueInner :: Single ( part) => Left ( std:: iter :: once ( part) ) ,
1067- FStringValueInner :: Concatenated ( parts) => Right ( parts. iter_mut ( ) ) ,
1065+ FStringValueInner :: Single ( part) => std:: slice :: from_mut ( part) ,
1066+ FStringValueInner :: Concatenated ( parts) => parts,
10681067 }
10691068 }
10701069
1070+ /// Returns an iterator over all the [`FStringPart`]s contained in this value.
1071+ pub fn iter ( & self ) -> Iter < FStringPart > {
1072+ self . as_slice ( ) . iter ( )
1073+ }
1074+
1075+ /// Returns an iterator over all the [`FStringPart`]s contained in this value
1076+ /// that allows modification.
1077+ pub ( crate ) fn iter_mut ( & mut self ) -> IterMut < FStringPart > {
1078+ self . as_mut_slice ( ) . iter_mut ( )
1079+ }
1080+
10711081 /// Returns an iterator over the [`StringLiteral`] parts contained in this value.
10721082 ///
10731083 /// Note that this doesn't nest into the f-string parts. For example,
@@ -1078,7 +1088,7 @@ impl FStringValue {
10781088 ///
10791089 /// Here, the string literal parts returned would be `"foo"` and `"baz"`.
10801090 pub fn literals ( & self ) -> impl Iterator < Item = & StringLiteral > {
1081- self . parts ( ) . filter_map ( |part| part. as_literal ( ) )
1091+ self . iter ( ) . filter_map ( |part| part. as_literal ( ) )
10821092 }
10831093
10841094 /// Returns an iterator over the [`FString`] parts contained in this value.
@@ -1091,7 +1101,7 @@ impl FStringValue {
10911101 ///
10921102 /// Here, the f-string parts returned would be `f"bar {x}"` and `f"qux"`.
10931103 pub fn f_strings ( & self ) -> impl Iterator < Item = & FString > {
1094- self . parts ( ) . filter_map ( |part| part. as_f_string ( ) )
1104+ self . iter ( ) . filter_map ( |part| part. as_f_string ( ) )
10951105 }
10961106
10971107 /// Returns an iterator over all the [`FStringElement`] contained in this value.
@@ -1110,6 +1120,15 @@ impl FStringValue {
11101120 }
11111121}
11121122
1123+ impl < ' a > IntoIterator for & ' a FStringValue {
1124+ type Item = & ' a FStringPart ;
1125+ type IntoIter = Iter < ' a , FStringPart > ;
1126+
1127+ fn into_iter ( self ) -> Self :: IntoIter {
1128+ self . iter ( )
1129+ }
1130+ }
1131+
11131132/// An internal representation of [`FStringValue`].
11141133#[ derive( Clone , Debug , PartialEq ) ]
11151134enum FStringValueInner {
@@ -1238,26 +1257,36 @@ impl StringLiteralValue {
12381257 /// For an implicitly concatenated string, it returns `true` only if the first
12391258 /// string literal is a unicode string.
12401259 pub fn is_unicode ( & self ) -> bool {
1241- self . parts ( ) . next ( ) . map_or ( false , |part| part. unicode )
1260+ self . iter ( ) . next ( ) . map_or ( false , |part| part. unicode )
12421261 }
12431262
1244- /// Returns an iterator over all the [`StringLiteral`] parts contained in this value.
1245- pub fn parts ( & self ) -> impl Iterator < Item = & StringLiteral > {
1263+ /// Returns a slice of all the [`StringLiteral`] parts contained in this value.
1264+ pub fn as_slice ( & self ) -> & [ StringLiteral ] {
12461265 match & self . inner {
1247- StringLiteralValueInner :: Single ( value) => Left ( std:: iter :: once ( value) ) ,
1248- StringLiteralValueInner :: Concatenated ( value) => Right ( value. strings . iter ( ) ) ,
1266+ StringLiteralValueInner :: Single ( value) => std:: slice :: from_ref ( value) ,
1267+ StringLiteralValueInner :: Concatenated ( value) => value. strings . as_slice ( ) ,
12491268 }
12501269 }
12511270
1252- /// Returns an iterator over all the [`StringLiteral`] parts contained in this value
1253- /// that allows modification.
1254- pub ( crate ) fn parts_mut ( & mut self ) -> impl Iterator < Item = & mut StringLiteral > {
1271+ /// Returns a mutable slice of all the [`StringLiteral`] parts contained in this value.
1272+ fn as_mut_slice ( & mut self ) -> & mut [ StringLiteral ] {
12551273 match & mut self . inner {
1256- StringLiteralValueInner :: Single ( value) => Left ( std:: iter :: once ( value) ) ,
1257- StringLiteralValueInner :: Concatenated ( value) => Right ( value. strings . iter_mut ( ) ) ,
1274+ StringLiteralValueInner :: Single ( value) => std:: slice :: from_mut ( value) ,
1275+ StringLiteralValueInner :: Concatenated ( value) => value. strings . as_mut_slice ( ) ,
12581276 }
12591277 }
12601278
1279+ /// Returns an iterator over all the [`StringLiteral`] parts contained in this value.
1280+ pub fn iter ( & self ) -> Iter < StringLiteral > {
1281+ self . as_slice ( ) . iter ( )
1282+ }
1283+
1284+ /// Returns an iterator over all the [`StringLiteral`] parts contained in this value
1285+ /// that allows modification.
1286+ pub ( crate ) fn iter_mut ( & mut self ) -> IterMut < StringLiteral > {
1287+ self . as_mut_slice ( ) . iter_mut ( )
1288+ }
1289+
12611290 /// Returns `true` if the string literal value is empty.
12621291 pub fn is_empty ( & self ) -> bool {
12631292 self . len ( ) == 0
@@ -1266,12 +1295,12 @@ impl StringLiteralValue {
12661295 /// Returns the total length of the string literal value, in bytes, not
12671296 /// [`char`]s or graphemes.
12681297 pub fn len ( & self ) -> usize {
1269- self . parts ( ) . fold ( 0 , |acc, part| acc + part. value . len ( ) )
1298+ self . iter ( ) . fold ( 0 , |acc, part| acc + part. value . len ( ) )
12701299 }
12711300
12721301 /// Returns an iterator over the [`char`]s of each string literal part.
12731302 pub fn chars ( & self ) -> impl Iterator < Item = char > + ' _ {
1274- self . parts ( ) . flat_map ( |part| part. value . chars ( ) )
1303+ self . iter ( ) . flat_map ( |part| part. value . chars ( ) )
12751304 }
12761305
12771306 /// Returns the concatenated string value as a [`str`].
@@ -1286,6 +1315,15 @@ impl StringLiteralValue {
12861315 }
12871316}
12881317
1318+ impl < ' a > IntoIterator for & ' a StringLiteralValue {
1319+ type Item = & ' a StringLiteral ;
1320+ type IntoIter = Iter < ' a , StringLiteral > ;
1321+
1322+ fn into_iter ( self ) -> Self :: IntoIter {
1323+ self . iter ( )
1324+ }
1325+ }
1326+
12891327impl PartialEq < str > for StringLiteralValue {
12901328 fn eq ( & self , other : & str ) -> bool {
12911329 if self . len ( ) != other. len ( ) {
@@ -1457,37 +1495,55 @@ impl BytesLiteralValue {
14571495 matches ! ( self . inner, BytesLiteralValueInner :: Concatenated ( _) )
14581496 }
14591497
1460- /// Returns an iterator over all the [`BytesLiteral`] parts contained in this value.
1461- pub fn parts ( & self ) -> impl Iterator < Item = & BytesLiteral > {
1498+ /// Returns a slice of all the [`BytesLiteral`] parts contained in this value.
1499+ pub fn as_slice ( & self ) -> & [ BytesLiteral ] {
14621500 match & self . inner {
1463- BytesLiteralValueInner :: Single ( value) => Left ( std:: iter :: once ( value) ) ,
1464- BytesLiteralValueInner :: Concatenated ( values ) => Right ( values . iter ( ) ) ,
1501+ BytesLiteralValueInner :: Single ( value) => std:: slice :: from_ref ( value) ,
1502+ BytesLiteralValueInner :: Concatenated ( value ) => value . as_slice ( ) ,
14651503 }
14661504 }
14671505
1468- /// Returns an iterator over all the [`BytesLiteral`] parts contained in this value
1469- /// that allows modification.
1470- pub ( crate ) fn parts_mut ( & mut self ) -> impl Iterator < Item = & mut BytesLiteral > {
1506+ /// Returns a mutable slice of all the [`BytesLiteral`] parts contained in this value.
1507+ fn as_mut_slice ( & mut self ) -> & mut [ BytesLiteral ] {
14711508 match & mut self . inner {
1472- BytesLiteralValueInner :: Single ( value) => Left ( std:: iter :: once ( value) ) ,
1473- BytesLiteralValueInner :: Concatenated ( values ) => Right ( values . iter_mut ( ) ) ,
1509+ BytesLiteralValueInner :: Single ( value) => std:: slice :: from_mut ( value) ,
1510+ BytesLiteralValueInner :: Concatenated ( value ) => value . as_mut_slice ( ) ,
14741511 }
14751512 }
14761513
1514+ /// Returns an iterator over all the [`BytesLiteral`] parts contained in this value.
1515+ pub fn iter ( & self ) -> Iter < BytesLiteral > {
1516+ self . as_slice ( ) . iter ( )
1517+ }
1518+
1519+ /// Returns an iterator over all the [`BytesLiteral`] parts contained in this value
1520+ /// that allows modification.
1521+ pub ( crate ) fn iter_mut ( & mut self ) -> IterMut < BytesLiteral > {
1522+ self . as_mut_slice ( ) . iter_mut ( )
1523+ }
1524+
14771525 /// Returns `true` if the concatenated bytes has a length of zero.
14781526 pub fn is_empty ( & self ) -> bool {
1479- self . parts ( ) . all ( |part| part. is_empty ( ) )
1527+ self . iter ( ) . all ( |part| part. is_empty ( ) )
14801528 }
14811529
14821530 /// Returns the length of the concatenated bytes.
14831531 pub fn len ( & self ) -> usize {
1484- self . parts ( ) . map ( |part| part. len ( ) ) . sum ( )
1532+ self . iter ( ) . map ( |part| part. len ( ) ) . sum ( )
14851533 }
14861534
14871535 /// Returns an iterator over the bytes of the concatenated bytes.
14881536 fn bytes ( & self ) -> impl Iterator < Item = u8 > + ' _ {
1489- self . parts ( )
1490- . flat_map ( |part| part. as_slice ( ) . iter ( ) . copied ( ) )
1537+ self . iter ( ) . flat_map ( |part| part. as_slice ( ) . iter ( ) . copied ( ) )
1538+ }
1539+ }
1540+
1541+ impl < ' a > IntoIterator for & ' a BytesLiteralValue {
1542+ type Item = & ' a BytesLiteral ;
1543+ type IntoIter = Iter < ' a , BytesLiteral > ;
1544+
1545+ fn into_iter ( self ) -> Self :: IntoIter {
1546+ self . iter ( )
14911547 }
14921548}
14931549
0 commit comments