Skip to content

Commit b2e2543

Browse files
authored
Rollup merge of #82917 - cuviper:iter-zip, r=m-ou-se
Add function core::iter::zip This makes it a little easier to `zip` iterators: ```rust for (x, y) in zip(xs, ys) {} // vs. for (x, y) in xs.into_iter().zip(ys) {} ``` You can `zip(&mut xs, &ys)` for the conventional `iter_mut()` and `iter()`, respectively. This can also support arbitrary nesting, where it's easier to see the item layout than with arbitrary `zip` chains: ```rust for ((x, y), z) in zip(zip(xs, ys), zs) {} for (x, (y, z)) in zip(xs, zip(ys, zs)) {} // vs. for ((x, y), z) in xs.into_iter().zip(ys).zip(xz) {} for (x, (y, z)) in xs.into_iter().zip((ys.into_iter().zip(xz)) {} ``` It may also format more nicely, especially when the first iterator is a longer chain of methods -- for example: ```rust iter::zip( trait_ref.substs.types().skip(1), impl_trait_ref.substs.types().skip(1), ) // vs. trait_ref .substs .types() .skip(1) .zip(impl_trait_ref.substs.types().skip(1)) ``` This replaces the tuple-pair `IntoIterator` in #78204. There is prior art for the utility of this in [`itertools::zip`]. [`itertools::zip`]: https://docs.rs/itertools/0.10.0/itertools/fn.zip.html
2 parents ebea9d9 + f0a6052 commit b2e2543

File tree

111 files changed

+310
-256
lines changed

Some content is hidden

Large Commits have some content hidden by default. Use the searchbox below for content that may be hidden.

111 files changed

+310
-256
lines changed

compiler/rustc_apfloat/src/ieee.rs

Lines changed: 3 additions & 2 deletions
Original file line numberDiff line numberDiff line change
@@ -2273,6 +2273,7 @@ impl Loss {
22732273
mod sig {
22742274
use super::{limbs_for_bits, ExpInt, Limb, Loss, LIMB_BITS};
22752275
use core::cmp::Ordering;
2276+
use core::iter;
22762277
use core::mem;
22772278

22782279
pub(super) fn is_all_zeros(limbs: &[Limb]) -> bool {
@@ -2483,7 +2484,7 @@ mod sig {
24832484
pub(super) fn add(a: &mut [Limb], b: &[Limb], mut c: Limb) -> Limb {
24842485
assert!(c <= 1);
24852486

2486-
for (a, &b) in a.iter_mut().zip(b) {
2487+
for (a, &b) in iter::zip(a, b) {
24872488
let (r, overflow) = a.overflowing_add(b);
24882489
let (r, overflow2) = r.overflowing_add(c);
24892490
*a = r;
@@ -2497,7 +2498,7 @@ mod sig {
24972498
pub(super) fn sub(a: &mut [Limb], b: &[Limb], mut c: Limb) -> Limb {
24982499
assert!(c <= 1);
24992500

2500-
for (a, &b) in a.iter_mut().zip(b) {
2501+
for (a, &b) in iter::zip(a, b) {
25012502
let (r, overflow) = a.overflowing_sub(b);
25022503
let (r, overflow2) = r.overflowing_sub(c);
25032504
*a = r;

compiler/rustc_apfloat/src/lib.rs

Lines changed: 1 addition & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -33,6 +33,7 @@
3333
#![doc(html_root_url = "https://doc.rust-lang.org/nightly/nightly-rustc/")]
3434
#![no_std]
3535
#![forbid(unsafe_code)]
36+
#![feature(iter_zip)]
3637
#![feature(nll)]
3738
#![cfg_attr(bootstrap, feature(or_patterns))]
3839

compiler/rustc_ast/src/lib.rs

Lines changed: 1 addition & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -14,6 +14,7 @@
1414
#![feature(const_fn_transmute)]
1515
#![feature(const_panic)]
1616
#![feature(crate_visibility_modifier)]
17+
#![feature(iter_zip)]
1718
#![feature(label_break_value)]
1819
#![feature(nll)]
1920
#![cfg_attr(bootstrap, feature(or_patterns))]

compiler/rustc_ast/src/tokenstream.rs

Lines changed: 1 addition & 1 deletion
Original file line numberDiff line numberDiff line change
@@ -341,7 +341,7 @@ impl TokenStream {
341341
pub fn eq_unspanned(&self, other: &TokenStream) -> bool {
342342
let mut t1 = self.trees();
343343
let mut t2 = other.trees();
344-
for (t1, t2) in t1.by_ref().zip(t2.by_ref()) {
344+
for (t1, t2) in iter::zip(&mut t1, &mut t2) {
345345
if !t1.eq_unspanned(&t2) {
346346
return false;
347347
}

compiler/rustc_ast_lowering/src/item.rs

Lines changed: 3 additions & 2 deletions
Original file line numberDiff line numberDiff line change
@@ -18,6 +18,7 @@ use rustc_target::spec::abi;
1818
use smallvec::{smallvec, SmallVec};
1919
use tracing::debug;
2020

21+
use std::iter;
2122
use std::mem;
2223

2324
pub(super) struct ItemLowerer<'a, 'lowering, 'hir> {
@@ -206,7 +207,7 @@ impl<'hir> LoweringContext<'_, 'hir> {
206207
UseTreeKind::Glob => {}
207208
UseTreeKind::Simple(_, id1, id2) => {
208209
for (_, &id) in
209-
self.expect_full_res_from_use(base_id).skip(1).zip([id1, id2].iter())
210+
iter::zip(self.expect_full_res_from_use(base_id).skip(1), &[id1, id2])
210211
{
211212
vec.push(id);
212213
}
@@ -537,7 +538,7 @@ impl<'hir> LoweringContext<'_, 'hir> {
537538
// won't be dealing with macros in the rest of the compiler.
538539
// Essentially a single `use` which imports two names is desugared into
539540
// two imports.
540-
for (res, &new_node_id) in resolutions.zip([id1, id2].iter()) {
541+
for (res, &new_node_id) in iter::zip(resolutions, &[id1, id2]) {
541542
let ident = *ident;
542543
let mut path = path.clone();
543544
for seg in &mut path.segments {

compiler/rustc_ast_lowering/src/lib.rs

Lines changed: 1 addition & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -33,6 +33,7 @@
3333
#![feature(crate_visibility_modifier)]
3434
#![cfg_attr(bootstrap, feature(or_patterns))]
3535
#![feature(box_patterns)]
36+
#![feature(iter_zip)]
3637
#![recursion_limit = "256"]
3738

3839
use rustc_ast::node_id::NodeMap;

compiler/rustc_builtin_macros/src/deriving/generic/mod.rs

Lines changed: 3 additions & 5 deletions
Original file line numberDiff line numberDiff line change
@@ -1034,7 +1034,7 @@ impl<'a> MethodDef<'a> {
10341034
// make a series of nested matches, to destructure the
10351035
// structs. This is actually right-to-left, but it shouldn't
10361036
// matter.
1037-
for (arg_expr, pat) in self_args.iter().zip(patterns) {
1037+
for (arg_expr, pat) in iter::zip(self_args, patterns) {
10381038
body = cx.expr_match(
10391039
trait_.span,
10401040
arg_expr.clone(),
@@ -1351,7 +1351,7 @@ impl<'a> MethodDef<'a> {
13511351
let mut discriminant_test = cx.expr_bool(sp, true);
13521352

13531353
let mut first_ident = None;
1354-
for (&ident, self_arg) in vi_idents.iter().zip(&self_args) {
1354+
for (&ident, self_arg) in iter::zip(&vi_idents, &self_args) {
13551355
let self_addr = cx.expr_addr_of(sp, self_arg.clone());
13561356
let variant_value =
13571357
deriving::call_intrinsic(cx, sp, sym::discriminant_value, vec![self_addr]);
@@ -1571,9 +1571,7 @@ impl<'a> TraitDef<'a> {
15711571
let subpats = self.create_subpatterns(cx, paths, mutbl, use_temporaries);
15721572
let pattern = match *struct_def {
15731573
VariantData::Struct(..) => {
1574-
let field_pats = subpats
1575-
.into_iter()
1576-
.zip(&ident_exprs)
1574+
let field_pats = iter::zip(subpats, &ident_exprs)
15771575
.map(|(pat, &(sp, ident, ..))| {
15781576
if ident.is_none() {
15791577
cx.span_bug(sp, "a braced struct with unnamed fields in `derive`");

compiler/rustc_builtin_macros/src/lib.rs

Lines changed: 1 addition & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -7,6 +7,7 @@
77
#![feature(bool_to_option)]
88
#![feature(crate_visibility_modifier)]
99
#![feature(decl_macro)]
10+
#![feature(iter_zip)]
1011
#![feature(nll)]
1112
#![cfg_attr(bootstrap, feature(or_patterns))]
1213
#![feature(proc_macro_internals)]

compiler/rustc_codegen_llvm/src/back/lto.rs

Lines changed: 2 additions & 3 deletions
Original file line numberDiff line numberDiff line change
@@ -24,6 +24,7 @@ use tracing::{debug, info};
2424
use std::ffi::{CStr, CString};
2525
use std::fs::File;
2626
use std::io;
27+
use std::iter;
2728
use std::path::Path;
2829
use std::ptr;
2930
use std::slice;
@@ -916,9 +917,7 @@ impl ThinLTOKeysMap {
916917
modules: &[llvm::ThinLTOModule],
917918
names: &[CString],
918919
) -> Self {
919-
let keys = modules
920-
.iter()
921-
.zip(names.iter())
920+
let keys = iter::zip(modules, names)
922921
.map(|(module, name)| {
923922
let key = build_string(|rust_str| unsafe {
924923
llvm::LLVMRustComputeLTOCacheKey(rust_str, module.identifier, data.0);

compiler/rustc_codegen_llvm/src/builder.rs

Lines changed: 3 additions & 6 deletions
Original file line numberDiff line numberDiff line change
@@ -21,6 +21,7 @@ use rustc_target::abi::{self, Align, Size};
2121
use rustc_target::spec::{HasTargetSpec, Target};
2222
use std::borrow::Cow;
2323
use std::ffi::CStr;
24+
use std::iter;
2425
use std::ops::{Deref, Range};
2526
use std::ptr;
2627
use tracing::debug;
@@ -1352,18 +1353,14 @@ impl Builder<'a, 'll, 'tcx> {
13521353

13531354
let param_tys = self.cx.func_params_types(fn_ty);
13541355

1355-
let all_args_match = param_tys
1356-
.iter()
1357-
.zip(args.iter().map(|&v| self.val_ty(v)))
1356+
let all_args_match = iter::zip(&param_tys, args.iter().map(|&v| self.val_ty(v)))
13581357
.all(|(expected_ty, actual_ty)| *expected_ty == actual_ty);
13591358

13601359
if all_args_match {
13611360
return Cow::Borrowed(args);
13621361
}
13631362

1364-
let casted_args: Vec<_> = param_tys
1365-
.into_iter()
1366-
.zip(args.iter())
1363+
let casted_args: Vec<_> = iter::zip(param_tys, args)
13671364
.enumerate()
13681365
.map(|(i, (expected_ty, &actual_val))| {
13691366
let actual_ty = self.val_ty(actual_val);

0 commit comments

Comments
 (0)