-
Notifications
You must be signed in to change notification settings - Fork 12.8k
New issue
Have a question about this project? Sign up for a free GitHub account to open an issue and contact its maintainers and the community.
By clicking “Sign up for GitHub”, you agree to our terms of service and privacy statement. We’ll occasionally send you account related emails.
Already on GitHub? Sign in to your account
Add [T; N]::each_ref
and [T; N]::each_mut
#75490
Merged
bors
merged 1 commit into
rust-lang:master
from
LukasKalbertodt:add-basic-array-methods
Jan 11, 2021
Merged
Changes from all commits
Commits
File filter
Filter by extension
Conversations
Failed to load comments.
Loading
Jump to
Jump to file
Failed to load files.
Loading
Diff view
Diff view
There are no files selected for viewing
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
Original file line number | Diff line number | Diff line change |
---|---|---|
|
@@ -12,6 +12,7 @@ use crate::convert::{Infallible, TryFrom}; | |
use crate::fmt; | ||
use crate::hash::{self, Hash}; | ||
use crate::marker::Unsize; | ||
use crate::mem::MaybeUninit; | ||
use crate::ops::{Index, IndexMut}; | ||
use crate::slice::{Iter, IterMut}; | ||
|
||
|
@@ -429,7 +430,6 @@ impl<T, const N: usize> [T; N] { | |
where | ||
F: FnMut(T) -> U, | ||
{ | ||
use crate::mem::MaybeUninit; | ||
struct Guard<T, const N: usize> { | ||
dst: *mut T, | ||
initialized: usize, | ||
|
@@ -481,8 +481,6 @@ impl<T, const N: usize> [T; N] { | |
/// ``` | ||
#[unstable(feature = "array_zip", issue = "80094")] | ||
pub fn zip<U>(self, rhs: [U; N]) -> [(T, U); N] { | ||
use crate::mem::MaybeUninit; | ||
|
||
let mut dst = MaybeUninit::uninit_array::<N>(); | ||
for (i, (lhs, rhs)) in IntoIter::new(self).zip(IntoIter::new(rhs)).enumerate() { | ||
dst[i].write((lhs, rhs)); | ||
|
@@ -506,4 +504,75 @@ impl<T, const N: usize> [T; N] { | |
pub fn as_mut_slice(&mut self) -> &mut [T] { | ||
self | ||
} | ||
|
||
/// Borrows each element and returns an array of references with the same | ||
/// size as `self`. | ||
/// | ||
/// | ||
/// # Example | ||
/// | ||
/// ``` | ||
/// #![feature(array_methods)] | ||
/// | ||
/// let floats = [3.1, 2.7, -1.0]; | ||
/// let float_refs: [&f64; 3] = floats.each_ref(); | ||
/// assert_eq!(float_refs, [&3.1, &2.7, &-1.0]); | ||
/// ``` | ||
/// | ||
/// This method is particularly useful if combined with other methods, like | ||
/// [`map`](#method.map). This way, you can can avoid moving the original | ||
There was a problem hiding this comment. Choose a reason for hiding this commentThe reason will be displayed to describe this comment to others. Learn more. Does anyone know how to use intra-doc links for this? |
||
/// array if its elements are not `Copy`. | ||
/// | ||
/// ``` | ||
/// #![feature(array_methods, array_map)] | ||
/// | ||
/// let strings = ["Ferris".to_string(), "♥".to_string(), "Rust".to_string()]; | ||
/// let is_ascii = strings.each_ref().map(|s| s.is_ascii()); | ||
/// assert_eq!(is_ascii, [true, false, true]); | ||
/// | ||
/// // We can still access the original array: it has not been moved. | ||
/// assert_eq!(strings.len(), 3); | ||
/// ``` | ||
#[unstable(feature = "array_methods", issue = "76118")] | ||
pub fn each_ref(&self) -> [&T; N] { | ||
// Unlike in `map`, we don't need a guard here, as dropping a reference | ||
// is a noop. | ||
let mut out = MaybeUninit::uninit_array::<N>(); | ||
for (src, dst) in self.iter().zip(&mut out) { | ||
dst.write(src); | ||
} | ||
|
||
// SAFETY: All elements of `dst` are properly initialized and | ||
// `MaybeUninit<T>` has the same layout as `T`, so this cast is valid. | ||
unsafe { (&mut out as *mut _ as *mut [&T; N]).read() } | ||
} | ||
|
||
/// Borrows each element mutably and returns an array of mutable references | ||
/// with the same size as `self`. | ||
/// | ||
/// | ||
/// # Example | ||
/// | ||
/// ``` | ||
/// #![feature(array_methods)] | ||
/// | ||
/// let mut floats = [3.1, 2.7, -1.0]; | ||
/// let float_refs: [&mut f64; 3] = floats.each_mut(); | ||
/// *float_refs[0] = 0.0; | ||
/// assert_eq!(float_refs, [&mut 0.0, &mut 2.7, &mut -1.0]); | ||
/// assert_eq!(floats, [0.0, 2.7, -1.0]); | ||
/// ``` | ||
#[unstable(feature = "array_methods", issue = "76118")] | ||
pub fn each_mut(&mut self) -> [&mut T; N] { | ||
// Unlike in `map`, we don't need a guard here, as dropping a reference | ||
// is a noop. | ||
let mut out = MaybeUninit::uninit_array::<N>(); | ||
for (src, dst) in self.iter_mut().zip(&mut out) { | ||
dst.write(src); | ||
} | ||
|
||
// SAFETY: All elements of `dst` are properly initialized and | ||
// `MaybeUninit<T>` has the same layout as `T`, so this cast is valid. | ||
unsafe { (&mut out as *mut _ as *mut [&mut T; N]).read() } | ||
} | ||
} |
Add this suggestion to a batch that can be applied as a single commit.
This suggestion is invalid because no changes were made to the code.
Suggestions cannot be applied while the pull request is closed.
Suggestions cannot be applied while viewing a subset of changes.
Only one suggestion per line can be applied in a batch.
Add this suggestion to a batch that can be applied as a single commit.
Applying suggestions on deleted lines is not supported.
You must change the existing code in this line in order to create a valid suggestion.
Outdated suggestions cannot be applied.
This suggestion has been applied or marked resolved.
Suggestions cannot be applied from pending reviews.
Suggestions cannot be applied on multi-line comments.
Suggestions cannot be applied while the pull request is queued to merge.
Suggestion cannot be applied right now. Please check back later.
There was a problem hiding this comment.
Choose a reason for hiding this comment
The reason will be displayed to describe this comment to others. Learn more.
I would say
length
here, since the overall size (as computed bystd::mem::size_of_val
) will likely be different.