Add 'core::array::from_fn' and 'core::array::try_from_fn'#75644
Add 'core::array::from_fn' and 'core::array::try_from_fn'#75644bors merged 7 commits intorust-lang:masterfrom
Conversation
|
(rust_highfive has picked a reviewer for you, use r? to override) |
|
I use similar functions since some months. I'm glad to see some love for arrays in stdlib, once we have some const generics :-) |
|
I personally do not think that this method has enough advantages over
Considering that |
|
These functions should not be seen as Take, for example, With the above reasoning:
It is also worth mentioning that the ClippyAnother use-case is the #![forbid(clippy::expect, clippy::unwrap)]
fn main() {
let _arr: [i32; 10] = (0..).map(f).collect().unwrap(); // Error!
}
Like I said, these functions should be seen as complementary companions to |
But
Yeah, this is why I used
This is true, but I do not see how
That one is a downside I did not consider. While I personally do think these lints are rarely appropriate, there are situations where not using any In general I can think of 2 solutions which I personally prefer:
trait UnwrapExt {
type Inner;
fn guaranteed_unwrap(self) -> Self::Inner;
}
impl<T, E> UnwrapExt for Result<T, E> {
type Inner = T;
fn guaranteed_unwrap(self) -> T {
extern "C" {
fn failed_to_optimize_unwrap_branch_check();
}
// This causes a linker error if the unwrap isn't optimized away, should probably
// only be used if your crate does not have any dependencies.
self.unwrap_or_else(|_| failed_to_optimize_unwrap_branch_check())
}
}
|
|
As previously commented, these functions deal with a different type of problem and shouldn't interact with the If someone wants to use iterators to create arrays, then
|
|
A few thoughts here:
|
|
+1 for |
|
☔ The latest upstream changes (presumably #76217) made this pull request unmergeable. Please resolve the merge conflicts. |
|
@c410-f3r Could you resolve the merge conflicts? |
6ff44b8 to
0acad64
Compare
|
Seems like there's indeed a That's probably preferable. |
|
@bors retry |
|
https://bors.rust-lang.org/queue/rust Looks like something is not right |
|
I guess, retry doesn’t work when there’s new commits. @yaahc you’ll need to r+ again. |
|
📌 Commit 91ad91e has been approved by |
|
In the name of Hades! ...this commit breaks the build when checked against all targets. #89656 Code used only in a @bors r- |
@bors r- |
Thanks. An unfortunate oversight |
|
lets try this again... In the name of Hades I approve this commit! |
|
📌 Commit 85c4a52 has been approved by |
|
I've tried to use this from_fn, but its usefulness is limited by the index that's always usize (and when possible I prefer to avoid type casts). Is it possible and a good idea to make the index type (input of F) generic (defaulting to usize)? |
|
@leonardo-m Like it or not, the index type is always |
|
@leonardo-m Note that default type parameters on functions aren't a thing, so I don't think it could even work. |
|
jhpratt in my codebase I use various ways to reduce the frequency of casting for indexes. It isn't a fact of life like Death or Taxes. I've seen that a from_fn like this is usable in more places in my codebase: pub fn from_fn<Ti, To, F, const N: usize>(mut cb: F) -> [To; N]
where
Ti: ZeroSucc + Copy,
F: FnMut(Ti) -> To,
{
let mut idx = Ti::ZERO;
[(); N].map(|_| {
let res = cb(idx);
idx.succ();
res
})
}The disadvantage is some kind of trait for Zero and successive, and in a subset of from_fn usages you need to specify the type: (Another factor that reduces the usefulness of |
|
Supposing that each iteration increases the counter, an |
|
I think a better place for discussing this than this merged PR would be the tracking issue: #89379 |
These auxiliary methods fill uninitialized arrays in a safe way and are particularly useful for elements that don't implement
Clone,CopyorDefault.Different from
FromIterator, it is guaranteed that the array will be fully filled and no error regarding uninitialized state will be throw. In certain scenarios, however, the creation of an element can fail and that is why thetry_from_fnfunction is also provided.