-
Notifications
You must be signed in to change notification settings - Fork 13.4k
Add array
lang item and [T; N]::map(f: FnMut(T) -> S)
#75212
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
Changes from all commits
d871818
f6411e4
56a651c
54b821e
412417d
af32db2
File filter
Filter by extension
Conversations
Jump to
Diff view
Diff view
There are no files selected for viewing
Original file line number | Diff line number | Diff line change |
---|---|---|
|
@@ -364,3 +364,66 @@ macro_rules! array_impl_default { | |
} | ||
|
||
array_impl_default! {32, T T T T T T T T T T T T T T T T T T T T T T T T T T T T T T T T} | ||
|
||
#[cfg(not(bootstrap))] | ||
#[lang = "array"] | ||
impl<T, const N: usize> [T; N] { | ||
/// Returns an array of the same size as `self`, with function `f` applied to each element | ||
/// in order. | ||
/// | ||
/// # Examples | ||
/// | ||
/// ``` | ||
/// #![feature(array_map)] | ||
JulianKnodt marked this conversation as resolved.
Show resolved
Hide resolved
|
||
/// let x = [1, 2, 3]; | ||
/// let y = x.map(|v| v + 1); | ||
/// assert_eq!(y, [2, 3, 4]); | ||
/// | ||
/// let x = [1, 2, 3]; | ||
/// let mut temp = 0; | ||
/// let y = x.map(|v| { temp += 1; v * temp }); | ||
/// assert_eq!(y, [1, 4, 9]); | ||
JulianKnodt marked this conversation as resolved.
Show resolved
Hide resolved
|
||
/// | ||
/// let x = ["Ferris", "Bueller's", "Day", "Off"]; | ||
/// let y = x.map(|v| v.len()); | ||
/// assert_eq!(y, [6, 9, 3, 3]); | ||
/// ``` | ||
#[unstable(feature = "array_map", issue = "75243")] | ||
pub fn map<F, U>(self, mut f: F) -> [U; N] | ||
where | ||
F: FnMut(T) -> U, | ||
{ | ||
use crate::mem::MaybeUninit; | ||
struct Guard<T, const N: usize> { | ||
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. This type is functionally equivalent to the one used in array's 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. ah sorry which in specific are you referring to? Is this one of the PR's open for implementing 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. There's no trait implementation (sadly), but the iterator type itself does exist: https://doc.rust-lang.org/std/array/struct.IntoIter.html 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. While they are similar, this one does only iterate in one direction so unifying it might take some work. Would you think it's important to do so? 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. I put it as a question since I'm not sure. My default answer is to reduce the amount of nuanced unsafe code, but if it's awkward to merge then you can probably just leave it -- unless a real libs-impl member says otherwise, of course. 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. Mm I do feel the use case is sufficiently different from the iterator, so I'm going to leave it for now then, unless there's a reason otherwise. 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. I think merging that code is too much hassle to be worth it. The iterator also uses a range (to be a doubled ended iterator) which is not required here. |
||
dst: *mut T, | ||
initialized: usize, | ||
} | ||
|
||
impl<T, const N: usize> Drop for Guard<T, N> { | ||
fn drop(&mut self) { | ||
debug_assert!(self.initialized <= N); | ||
|
||
let initialized_part = | ||
crate::ptr::slice_from_raw_parts_mut(self.dst, self.initialized); | ||
// SAFETY: this raw slice will contain only initialized objects | ||
// that's why, it is allowed to drop it. | ||
unsafe { | ||
crate::ptr::drop_in_place(initialized_part); | ||
} | ||
} | ||
} | ||
let mut dst = MaybeUninit::uninit_array::<N>(); | ||
let mut guard: Guard<U, N> = | ||
Guard { dst: MaybeUninit::first_ptr_mut(&mut dst), initialized: 0 }; | ||
for (src, dst) in IntoIter::new(self).zip(&mut dst) { | ||
dst.write(f(src)); | ||
guard.initialized += 1; | ||
} | ||
// FIXME: Convert to crate::mem::transmute once it works with generics. | ||
// unsafe { crate::mem::transmute::<[MaybeUninit<U>; N], [U; N]>(dst) } | ||
crate::mem::forget(guard); | ||
// SAFETY: At this point we've properly initialized the whole array | ||
// and we just need to cast it to the correct type. | ||
unsafe { crate::mem::transmute_copy::<_, [U; N]>(&dst) } | ||
} | ||
} |
Uh oh!
There was an error while loading. Please reload this page.