rust-lang / rust-lang/libs-team

add `Vec::try_from_fn`

Open
#880 0 comments 0 reactions 0 assignees View on GitHub

Nobody has claimed this yet.

api-change-proposal
Dominant language
Rust
Stars
178
Forks
28
Avg merge
15m
Merged PRs (30d)
1

Description

Proposal

Problem

I think #484 left out an important part- that being Vec::try_from_fn. It seems unnatural to me to have one without the other, as things such as (0..n).map(..).collect::<Result<Vec<_>, _>>() are relatively common, and I think could be better represented.

Motivating examples or use cases

(0..buff.read_u16()? as usize)
    .map(|_| buff.read_i32())
    .collect::<Result<Vec<_>, _>>()?,

https://github.com/cyphar/paperback/blob/aed348b90c92d59391877bc502a5f08de7a309c4/src/raw.rs#L335
https://github.com/Rik-de-Kort/minithesis-rust/blob/c13ff2309d266f766643495b1b01b982da3a05a6/src/main.rs#L907
https://github.com/joaoviictorti/coffeeldr/blob/45fcea8c7145e4d4afa2fe59950cbb491f80dbc3/src/coff.rs#L109
https://github.com/alibaba/font-toolkit/blob/f773a43da732a1e51d6275ab07a24b5e11fa3e09/src/font.rs#L201

Solution sketch

impl<T> Vec<T> {
    pub fn try_from_fn<R, F>(length: usize, mut f: F) -> ChangeOutputType<R, Vec<T>>
    where
        R: Try<Residual: Residual<Vec<T>>, Output = T>,
        F: FnMut(usize) -> R,
    {
        let mut me = Vec::with_capacity(length);
        for i in 0..length {
            me.push(f(i)?);
        }
        <_>::from_output(me)
    }
}

Alternatives

(0..n).map(|i| f(i)).collect()

Links and related work

#484

What happens now?

This issue contains an API change proposal (or ACP) and is part of the libs-api team feature lifecycle. Once this issue is filed, the libs-api team will review open proposals as capability becomes available. Current response times do not have a clear estimate, but may be up to several months.

Possible responses

The libs team may respond in various different ways. First, the team will consider the problem (this doesn't require any concrete solution or alternatives to have been proposed):

  • We think this problem seems worth solving, and the standard library might be the right place to solve it.
  • We think that this probably doesn't belong in the standard library.

Second, if there's a concrete solution:

  • We think this specific solution looks roughly right, approved, you or someone else should implement this. (Further review will still happen on the subsequent implementation PR.)
  • We're not sure this is the right solution, and the alternatives or other materials don't give us enough information to be sure about that. Here are some questions we have that aren't answered, or rough ideas about alternatives we'd want to see discussed.

Contributor guide

No contributing guide indexed for this repository

First steps

  1. Read the whole issue, then the project's contributing guide.
  2. Comment on the issue to say you are picking it up — it saves two people doing the same work.
  3. Fork the repository and make your change on a branch.
  4. Open a pull request that references the issue number.

Research direction

Start by reading related issue #484 and the Rust standard library feature lifecycle. Review the proposed full signature, motivating examples, and alternatives, then follow the libs-api discussion. Done means the proposal receives a decision on whether the problem and API belong in the standard library, rather than an implementation being assumed.

Written by the indexing model from the issue text.

Assessment

Tech stack
rust
Domain
backend-api-design
Issue type
Feature
Difficulty
5/5
Estimated time
Over a week
Activity status
Active
Clarity
Mostly clear
Newbie friendliness
35/100

Get new issues in your inbox

A short digest of beginner-friendly GitHub issues.