-
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
Tracking issue for try_array_from_fn
#89379
Comments
I am not certain the currently implemented signature of |
My 2¢ on the
So personally I think I don't think |
Seconding this. I'm trying to collect an iterator into an array, for which I'd like to write: fn parse() -> Option<()> {
let arr = array::try_from_fn(|_| self.iter.next())?;
// ...
} But because |
@yoshuawuyts I think my PR handles that scenario: https://github.com/rust-lang/rust/pull/91286/files#diff-534d866e6adad8a1f7577c2f1b3eed0b05d5f001c0d5032ac226393736940381R78-R82 |
I hate to re-bikeshed, but I stumbled on a possibility which I didn't see mentioned in #75644 I'm making a PR for So I went to make So a potential new coat of paint for the constructors could be
I don't know if this makes me feel differently about the |
The two most important things about these functions are (1) Provide an unified way for creating custom arrays (it is common enough for That said, I will personally support any proposal that will move any function or method towards stabilization as soon as possible. So if desired feel free to create Although some recent development is happening in this field, sometimes I think that custom array constructors will take, if ever, as long as |
Since I've seen this, I've written |
…ttmcm Stabilize `array_from_fn` ## Overall Stabilizes `core::array::from_fn` ~~and `core::array::try_from_fn`~~ to allow the creation of custom infallible ~~and fallible~~ arrays. Signature proposed for stabilization here, tweaked as requested in the meeting: ```rust // in core::array pub fn from_fn<T, const N: usize, F>(_: F) -> [T; N]; ``` Examples in https://doc.rust-lang.org/nightly/std/array/fn.from_fn.html ## History * On 2020-08-17, implementation was [proposed](rust-lang#75644). * On 2021-09-29, tracking issue was [created](rust-lang#89379). * On 2021-10-09, the proposed implementation was [merged](bc8ad24). * On 2021-12-03, the return type of `try_from_fn` was [changed](rust-lang#91286 (comment)). ## Considerations * It is being assumed that indices are useful and shouldn't be removed from the callbacks * The fact that `try_from_fn` returns an unstable type `R: Try` does not prevent stabilization. Although I'm honestly not sure about it. * The addition or not of repeat-like variants is orthogonal to this PR. 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 rust-lang#89379
Now that #94119 has been merged to stabilize this, can we close this issue? |
Please, also add this functions: fn concat_arrs<T, const N: usize, const M: usize>(a: &mut ([T; N], [T; M])) -> &mut [T; N + M]; // Without coping, same for 3-tuples, 4-tuples, etc
fn concat_arr_of_arrs<T, const N: usize, const M: usize>(a: &mut [[T; N]; M]) -> &mut [T; N * M]; |
Can't you just use |
I was wondering if these functions could be const in the future when the pending issues with const fn are dealt with. These functions actually seem pretty useful for lookup tables. The new signature would probably be something like: const fn from_fn<F, T, const N: usize>(cb: F) -> [T; N]
where
F: ~const + FnMut(usize) -> T; |
array_from_fn
array_from_fn
and try_array_from_fn
`enum-iterator` is a dependency for `vergen`, but because `v1.2.0` is causing the `error[E0658]`, we are restricting its version by defining it as `~1.1.3` here. To give more context, until we've added tracing, vergen v1.1.3 had been chosen by cargo. With tracing, if we use any version for vergen equal or greater than v7.0.0, cargo jumps enum-iterator from v0.8.1 to v1.2.0, which will cause an error when building in the CI: error[E0658]: use of unstable library feature 'array_from_fn' --> /root/.cargo/registry/src/github.com-1285ae84e5963aae/enum-iterator-1.2.0/src/lib.rs:554:18 | 554 | Some(core::array::from_fn(|_| unreachable!())) | ^^^^^^^^^^^^^^^^^^^^ | = note: see issue #89379 <rust-lang/rust#89379> for more information By adding `enum-iterator = "~1.1.3"`, we are giving v1.1.3 preference over the latest version (v1.2.0), so the build is fixed.
enum-iterator 1.2.0 is using unstable features, causing projects using vergen to fail when building: ``` error[E0658]: use of unstable library feature 'array_from_fn' --> /root/.cargo/registry/src/github.com-1285ae84e5963aae/enum-iterator-1.2.0/src/lib.rs:554:18 | 554 | Some(core::array::from_fn(|_| unreachable!())) | ^^^^^^^^^^^^^^^^^^^^ | = note: see issue #89379 <rust-lang/rust#89379> for more information ```
`enum-iterator` is a dependency for `vergen`, but because `v1.2.0` is causing the `error[E0658]`, we are restricting its version by defining it as `~1.1.3` here. To give more context, until we've added tracing, vergen v1.1.3 had been chosen by cargo. With tracing, if we use any version for vergen equal or greater than v7.0.0, cargo jumps enum-iterator from v0.8.1 to v1.2.0, which will cause an error when building in the CI: error[E0658]: use of unstable library feature 'array_from_fn' --> /root/.cargo/registry/src/github.com-1285ae84e5963aae/enum-iterator-1.2.0/src/lib.rs:554:18 | 554 | Some(core::array::from_fn(|_| unreachable!())) | ^^^^^^^^^^^^^^^^^^^^ | = note: see issue #89379 <rust-lang/rust#89379> for more information By adding `enum-iterator = "~1.1.3"`, we are giving v1.1.3 preference over the latest version (v1.2.0), so the build is fixed.
* Prevent E0658 by limiting enum-iterator version. enum-iterator 1.2.0 is using unstable features, causing projects using vergen to fail when building: ``` error[E0658]: use of unstable library feature 'array_from_fn' --> /root/.cargo/registry/src/github.com-1285ae84e5963aae/enum-iterator-1.2.0/src/lib.rs:554:18 | 554 | Some(core::array::from_fn(|_| unreachable!())) | ^^^^^^^^^^^^^^^^^^^^ | = note: see issue #89379 <rust-lang/rust#89379> for more information ``` * allow for 'variant_size_differences' Co-authored-by: Jason Ozias <jason.g.ozias@gmail.com>
Stabilize `array_from_fn` ## Overall Stabilizes `core::array::from_fn` ~~and `core::array::try_from_fn`~~ to allow the creation of custom infallible ~~and fallible~~ arrays. Signature proposed for stabilization here, tweaked as requested in the meeting: ```rust // in core::array pub fn from_fn<T, const N: usize, F>(_: F) -> [T; N]; ``` Examples in https://doc.rust-lang.org/nightly/std/array/fn.from_fn.html ## History * On 2020-08-17, implementation was [proposed](rust-lang/rust#75644). * On 2021-09-29, tracking issue was [created](rust-lang/rust#89379). * On 2021-10-09, the proposed implementation was [merged](rust-lang-ci/rust@bc8ad24). * On 2021-12-03, the return type of `try_from_fn` was [changed](rust-lang/rust#91286 (comment)). ## Considerations * It is being assumed that indices are useful and shouldn't be removed from the callbacks * The fact that `try_from_fn` returns an unstable type `R: Try` does not prevent stabilization. Although I'm honestly not sure about it. * The addition or not of repeat-like variants is orthogonal to this PR. 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 rust-lang/rust#89379
Filed #89379 to try to get some extra traction on making the already-stabilized |
As @scottmcm said, it is necessary to have stable constant closures in order to "constify" I think it is already possible to at least "constify" the hidden underlying machinery but I am not totally sure. |
Is there any reason this isn't stable yet? |
Hello, let mut parts = "Mary had a small lamp".split_ascii_whitespace();
let parts: [&str; 5] = from_fn(|_i| parts.next().unwrap());
// produces: ["Mary", "had", "a", "small", "lamp"] This works, but the documentation doesn't state that the callback parameter will grow upwards, so I'm not confident to use this code. Is it possible that one day, we need the callback parameter to go in decreasing order? |
Are there any open questions about this? If not, could someone start an FCP? |
array_from_fn
and try_array_from_fn
try_array_from_fn
|
Why does this depend on try trait necessarily? |
Well, the original function returned
EDIT: Oh I think it is because of concerns around the |
@c410-f3r is right -- the distinction is the use of the See #94119 (comment) and #94119 (comment) Same questions as, for example, |
Provides the common use-case of creating custom fallible arrays in a reliable and unified way. Particularly useful for elements that don't implement Copy, Clone or Default.
Public API
Steps / History
Unresolved Questions
Should the callback return indices (Add 'core::array::from_fn' and 'core::array::try_from_fn' #75644 (comment)) ?from_fn
andtry_from_fn
are very similar torepeat_with
andtry_repeat_with
(Tracking Issue forcore::array::(try_)repeat(_with)
(featurearray_repeat
) #91613), just that they take unary instead of nullary functions. Anyone considering stabilizing these should think about those, and vice versa.The text was updated successfully, but these errors were encountered: