Skip to content

Commit

Permalink
Use iter::zip in library/
Browse files Browse the repository at this point in the history
  • Loading branch information
cuviper committed Mar 26, 2021
1 parent b362958 commit 3b1f5e3
Show file tree
Hide file tree
Showing 8 changed files with 18 additions and 15 deletions.
1 change: 1 addition & 0 deletions library/alloc/src/lib.rs
Original file line number Diff line number Diff line change
Expand Up @@ -108,6 +108,7 @@
// that the feature-gate isn't enabled. Ideally, it wouldn't check for the feature gate for docs
// from other crates, but since this can only appear for lang items, it doesn't seem worth fixing.
#![feature(intra_doc_pointers)]
#![feature(iter_zip)]
#![feature(lang_items)]
#![feature(layout_for_ptr)]
#![feature(maybe_uninit_ref)]
Expand Down
9 changes: 3 additions & 6 deletions library/alloc/src/vec/mod.rs
Original file line number Diff line number Diff line change
Expand Up @@ -58,7 +58,7 @@ use core::convert::TryFrom;
use core::fmt;
use core::hash::{Hash, Hasher};
use core::intrinsics::{arith_offset, assume};
use core::iter::FromIterator;
use core::iter::{self, FromIterator};
use core::marker::PhantomData;
use core::mem::{self, ManuallyDrop, MaybeUninit};
use core::ops::{self, Index, IndexMut, Range, RangeBounds};
Expand Down Expand Up @@ -2268,11 +2268,8 @@ impl<T: Clone, A: Allocator> ExtendFromWithinSpec for Vec<T, A> {
// - caller guaratees that src is a valid index
let to_clone = unsafe { this.get_unchecked(src) };

to_clone
.iter()
.cloned()
.zip(spare.iter_mut())
.map(|(src, dst)| dst.write(src))
iter::zip(to_clone, spare)
.map(|(src, dst)| dst.write(src.clone()))
// Note:
// - Element was just initialized with `MaybeUninit::write`, so it's ok to increace len
// - len is increased after each element to prevent leaks (see issue #82533)
Expand Down
4 changes: 2 additions & 2 deletions library/core/src/array/iter.rs
Original file line number Diff line number Diff line change
Expand Up @@ -2,7 +2,7 @@
use crate::{
fmt,
iter::{ExactSizeIterator, FusedIterator, TrustedLen, TrustedRandomAccess},
iter::{self, ExactSizeIterator, FusedIterator, TrustedLen, TrustedRandomAccess},
mem::{self, MaybeUninit},
ops::Range,
ptr,
Expand Down Expand Up @@ -215,7 +215,7 @@ impl<T: Clone, const N: usize> Clone for IntoIter<T, N> {
let mut new = Self { data: MaybeUninit::uninit_array(), alive: 0..0 };

// Clone all alive elements.
for (src, dst) in self.as_slice().iter().zip(&mut new.data) {
for (src, dst) in iter::zip(self.as_slice(), &mut new.data) {
// Write a clone into the new array, then update its alive range.
// If cloning panics, we'll correctly drop the previous items.
dst.write(src.clone());
Expand Down
5 changes: 3 additions & 2 deletions library/core/src/fmt/mod.rs
Original file line number Diff line number Diff line change
Expand Up @@ -4,6 +4,7 @@

use crate::cell::{Cell, Ref, RefCell, RefMut, UnsafeCell};
use crate::char::EscapeDebugExtArgs;
use crate::iter;
use crate::marker::PhantomData;
use crate::mem;
use crate::num::flt2dec;
Expand Down Expand Up @@ -1088,7 +1089,7 @@ pub fn write(output: &mut dyn Write, args: Arguments<'_>) -> Result {
match args.fmt {
None => {
// We can use default formatting parameters for all arguments.
for (arg, piece) in args.args.iter().zip(args.pieces.iter()) {
for (arg, piece) in iter::zip(args.args, args.pieces) {
formatter.buf.write_str(*piece)?;
(arg.formatter)(arg.value, &mut formatter)?;
idx += 1;
Expand All @@ -1097,7 +1098,7 @@ pub fn write(output: &mut dyn Write, args: Arguments<'_>) -> Result {
Some(fmt) => {
// Every spec has a corresponding argument that is preceded by
// a string piece.
for (arg, piece) in fmt.iter().zip(args.pieces.iter()) {
for (arg, piece) in iter::zip(fmt, args.pieces) {
formatter.buf.write_str(*piece)?;
// SAFETY: arg and args.args come from the same Arguments,
// which guarantees the indexes are always within bounds.
Expand Down
6 changes: 4 additions & 2 deletions library/core/src/num/bignum.rs
Original file line number Diff line number Diff line change
Expand Up @@ -181,11 +181,12 @@ macro_rules! define_bignum {
/// Adds `other` to itself and returns its own mutable reference.
pub fn add<'a>(&'a mut self, other: &$name) -> &'a mut $name {
use crate::cmp;
use crate::iter;
use crate::num::bignum::FullOps;

let mut sz = cmp::max(self.size, other.size);
let mut carry = false;
for (a, b) in self.base[..sz].iter_mut().zip(&other.base[..sz]) {
for (a, b) in iter::zip(&mut self.base[..sz], &other.base[..sz]) {
let (c, v) = (*a).full_add(*b, carry);
*a = v;
carry = c;
Expand Down Expand Up @@ -219,11 +220,12 @@ macro_rules! define_bignum {
/// Subtracts `other` from itself and returns its own mutable reference.
pub fn sub<'a>(&'a mut self, other: &$name) -> &'a mut $name {
use crate::cmp;
use crate::iter;
use crate::num::bignum::FullOps;

let sz = cmp::max(self.size, other.size);
let mut noborrow = true;
for (a, b) in self.base[..sz].iter_mut().zip(&other.base[..sz]) {
for (a, b) in iter::zip(&mut self.base[..sz], &other.base[..sz]) {
let (c, v) = (*a).full_add(!*b, noborrow);
*a = v;
noborrow = c;
Expand Down
3 changes: 2 additions & 1 deletion library/core/src/slice/ascii.rs
Original file line number Diff line number Diff line change
@@ -1,5 +1,6 @@
//! Operations on ASCII `[u8]`.
use crate::iter;
use crate::mem;

#[lang = "slice_u8"]
Expand All @@ -19,7 +20,7 @@ impl [u8] {
#[stable(feature = "ascii_methods_on_intrinsics", since = "1.23.0")]
#[inline]
pub fn eq_ignore_ascii_case(&self, other: &[u8]) -> bool {
self.len() == other.len() && self.iter().zip(other).all(|(a, b)| a.eq_ignore_ascii_case(b))
self.len() == other.len() && iter::zip(self, other).all(|(a, b)| a.eq_ignore_ascii_case(b))
}

/// Converts this slice to its ASCII upper case equivalent in-place.
Expand Down
1 change: 1 addition & 0 deletions library/std/src/lib.rs
Original file line number Diff line number Diff line change
Expand Up @@ -278,6 +278,7 @@
#![feature(integer_atomics)]
#![feature(into_future)]
#![feature(intra_doc_pointers)]
#![feature(iter_zip)]
#![feature(lang_items)]
#![feature(link_args)]
#![feature(linkage)]
Expand Down
4 changes: 2 additions & 2 deletions library/std/src/sys/unix/ext/net/addr.rs
Original file line number Diff line number Diff line change
Expand Up @@ -2,7 +2,7 @@ use crate::ffi::OsStr;
use crate::os::unix::ffi::OsStrExt;
use crate::path::Path;
use crate::sys::cvt;
use crate::{ascii, fmt, io, mem};
use crate::{ascii, fmt, io, iter, mem};

// FIXME(#43348): Make libc adapt #[doc(cfg(...))] so we don't need these fake definitions here?
#[cfg(not(unix))]
Expand Down Expand Up @@ -41,7 +41,7 @@ pub(super) unsafe fn sockaddr_un(path: &Path) -> io::Result<(libc::sockaddr_un,
&"path must be shorter than SUN_LEN",
));
}
for (dst, src) in addr.sun_path.iter_mut().zip(bytes.iter()) {
for (dst, src) in iter::zip(&mut addr.sun_path, bytes) {
*dst = *src as libc::c_char;
}
// null byte for pathname addresses is already there because we zeroed the
Expand Down

0 comments on commit 3b1f5e3

Please sign in to comment.