@@ -4,8 +4,8 @@ use std::cell::OnceCell;
4
4
use std:: fmt;
5
5
use std:: fmt:: Debug ;
6
6
use std:: ops:: Deref ;
7
+ use std:: slice:: { Iter , IterMut } ;
7
8
8
- use itertools:: Either :: { Left , Right } ;
9
9
use itertools:: Itertools ;
10
10
11
11
use ruff_text_size:: { Ranged , TextRange , TextSize } ;
@@ -1051,23 +1051,33 @@ impl FStringValue {
1051
1051
matches ! ( self . inner, FStringValueInner :: Concatenated ( _) )
1052
1052
}
1053
1053
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 ] {
1056
1056
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,
1059
1059
}
1060
1060
}
1061
1061
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 ] {
1065
1064
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,
1068
1067
}
1069
1068
}
1070
1069
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
+
1071
1081
/// Returns an iterator over the [`StringLiteral`] parts contained in this value.
1072
1082
///
1073
1083
/// Note that this doesn't nest into the f-string parts. For example,
@@ -1078,7 +1088,7 @@ impl FStringValue {
1078
1088
///
1079
1089
/// Here, the string literal parts returned would be `"foo"` and `"baz"`.
1080
1090
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 ( ) )
1082
1092
}
1083
1093
1084
1094
/// Returns an iterator over the [`FString`] parts contained in this value.
@@ -1091,7 +1101,7 @@ impl FStringValue {
1091
1101
///
1092
1102
/// Here, the f-string parts returned would be `f"bar {x}"` and `f"qux"`.
1093
1103
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 ( ) )
1095
1105
}
1096
1106
1097
1107
/// Returns an iterator over all the [`FStringElement`] contained in this value.
@@ -1110,6 +1120,15 @@ impl FStringValue {
1110
1120
}
1111
1121
}
1112
1122
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
+
1113
1132
/// An internal representation of [`FStringValue`].
1114
1133
#[ derive( Clone , Debug , PartialEq ) ]
1115
1134
enum FStringValueInner {
@@ -1238,26 +1257,36 @@ impl StringLiteralValue {
1238
1257
/// For an implicitly concatenated string, it returns `true` only if the first
1239
1258
/// string literal is a unicode string.
1240
1259
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 )
1242
1261
}
1243
1262
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 ] {
1246
1265
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 ( ) ,
1249
1268
}
1250
1269
}
1251
1270
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 ] {
1255
1273
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 ( ) ,
1258
1276
}
1259
1277
}
1260
1278
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
+
1261
1290
/// Returns `true` if the string literal value is empty.
1262
1291
pub fn is_empty ( & self ) -> bool {
1263
1292
self . len ( ) == 0
@@ -1266,12 +1295,12 @@ impl StringLiteralValue {
1266
1295
/// Returns the total length of the string literal value, in bytes, not
1267
1296
/// [`char`]s or graphemes.
1268
1297
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 ( ) )
1270
1299
}
1271
1300
1272
1301
/// Returns an iterator over the [`char`]s of each string literal part.
1273
1302
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 ( ) )
1275
1304
}
1276
1305
1277
1306
/// Returns the concatenated string value as a [`str`].
@@ -1286,6 +1315,15 @@ impl StringLiteralValue {
1286
1315
}
1287
1316
}
1288
1317
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
+
1289
1327
impl PartialEq < str > for StringLiteralValue {
1290
1328
fn eq ( & self , other : & str ) -> bool {
1291
1329
if self . len ( ) != other. len ( ) {
@@ -1457,37 +1495,55 @@ impl BytesLiteralValue {
1457
1495
matches ! ( self . inner, BytesLiteralValueInner :: Concatenated ( _) )
1458
1496
}
1459
1497
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 ] {
1462
1500
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 ( ) ,
1465
1503
}
1466
1504
}
1467
1505
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 ] {
1471
1508
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 ( ) ,
1474
1511
}
1475
1512
}
1476
1513
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
+
1477
1525
/// Returns `true` if the concatenated bytes has a length of zero.
1478
1526
pub fn is_empty ( & self ) -> bool {
1479
- self . parts ( ) . all ( |part| part. is_empty ( ) )
1527
+ self . iter ( ) . all ( |part| part. is_empty ( ) )
1480
1528
}
1481
1529
1482
1530
/// Returns the length of the concatenated bytes.
1483
1531
pub fn len ( & self ) -> usize {
1484
- self . parts ( ) . map ( |part| part. len ( ) ) . sum ( )
1532
+ self . iter ( ) . map ( |part| part. len ( ) ) . sum ( )
1485
1533
}
1486
1534
1487
1535
/// Returns an iterator over the bytes of the concatenated bytes.
1488
1536
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 ( )
1491
1547
}
1492
1548
}
1493
1549
0 commit comments