-
Notifications
You must be signed in to change notification settings - Fork 12.9k
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
Stabilize array_from_fn
#94119
Stabilize array_from_fn
#94119
Conversation
(rust-highfive has picked a reviewer for you, use r? to override) |
library/core/src/array/mod.rs
Outdated
/// let array = core::array::from_fn(|i| i); | ||
/// assert_eq!(array, [0, 1, 2, 3, 4]); | ||
/// ``` | ||
#[inline] | ||
#[unstable(feature = "array_from_fn", issue = "89379")] | ||
#[stable(feature = "array_from_fn", since = "1.61.0")] | ||
pub fn from_fn<F, T, const N: usize>(mut cb: F) -> [T; N] |
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 think we should change the generic arguments to
pub fn from_fn<F, T, const N: usize>(mut cb: F) -> [T; N] | |
pub fn from_fn<T, const N: usize, F>(mut cb: F) -> [T; N] |
this makes it a lot more similar to other such functions with the function being last.
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.
We might even consider going to
pub fn from_fn<F, T, const N: usize>(mut cb: F) -> [T; N] | |
pub fn from_fn<T, const N: usize>(mut cb: impl FnMut(usize) -> T) -> [T; N] |
Because if the rules change to "you can turbofish the explicit generic parameters but not the APITs", then that would be nicer -- from_fn::<i32, N>(...)
instead of ::<i32, N, _>
.
That would, of course, mean that they'd need to be provided by context in the mean time, however. But TBH I prefer that anyway, as I find from_fn::<_, 4, _>
distasteful. (The closure almost always provides the T
.)
library/core/src/array/mod.rs
Outdated
@@ -80,7 +76,7 @@ where | |||
/// assert_eq!(array, None); | |||
/// ``` | |||
#[inline] | |||
#[unstable(feature = "array_from_fn", issue = "89379")] | |||
#[stable(feature = "array_from_fn", since = "1.61.0")] | |||
pub fn try_from_fn<F, R, const N: usize>(cb: F) -> ChangeOutputType<R, [R::Output; N]> |
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.
Note for libs-api: This would be the first stable usage of the https://doc.rust-lang.org/nightly/std/ops/trait.Residual.html mechanism (the other things like Iterator::try_find
are still unstable) so it probably deserves extra careful consideration.
To hopefully help team discussion, I'll link my rationale in favour of
|
We discussed this in today's libs-api meeting. Out of an abundance of caution, since this needs the Try trait and the residual mechanism, we decided to not stabilize Separately from that, we'd like to see the arguments reordered to put the function type last, which will allow us to in the future omit that argument (with further language enhancements). |
The fact that
With all that said, I ask the responsible party to reconsider the stabilization of |
That's a compelling argument, to me. |
Those stable examples don't use the (This was noted before in #94119 (comment)) |
The fact that Regardless, you indeed got a point so I withdraw my request for reconsidering @joshtriplett The PR will be updated according to #94119 (comment) |
@cuviper Thanks for catching that distinction. |
665d934
to
0f67c25
Compare
The order of the type parameters was modified and the feature of |
@rfcbot merge |
Team member @joshtriplett has proposed to merge this. The next step is review by the rest of the tagged team members: No concerns currently listed. Once a majority of reviewers approve (and at most 2 approvals are outstanding), this will enter its final comment period. If you spot a major issue that hasn't been raised at any point in this process, please speak up! See this document for info about what commands tagged team members can give me. |
🔔 This is now entering its final comment period, as per the review above. 🔔 |
0f67c25
to
5c67153
Compare
5c67153
to
d917112
Compare
The final comment period, with a disposition to merge, as per the review above, is now complete. As the automated representative of the governance process, I would like to thank the author for their work and everyone else who contributed. This will be merged soon. |
Someone has to r+ |
Thanks! The updated generic order update is in, and the version matches the current one (after the bump last week). @bors r+ |
📌 Commit d917112 has been approved by |
☀️ Test successful - checks-actions |
Finished benchmarking commit (09ea213): comparison url. Instruction countThis benchmark run did not return any relevant results for this metric. Max RSS (memory usage)Results
CyclesResults
If you disagree with this performance assessment, please file an issue in rust-lang/rustc-perf. @rustbot label: -perf-regression Footnotes |
Overall
Stabilizes
core::array::from_fn
andto allow the creation of custom infalliblecore::array::try_from_fn
and falliblearrays.Signature proposed for stabilization here, tweaked as requested in the meeting:
Examples in https://doc.rust-lang.org/nightly/std/array/fn.from_fn.html
History
try_from_fn
was changed.Considerations
try_from_fn
returns an unstable typeR: Try
does not prevent stabilization. Although I'm honestly not sure about it.These considerations are not ways of saying what is better or what is worse. In reality, they are an attempt to move things forward, anything really.
cc #89379