rust-lang / rust-lang/rust-clippy

Reword or even allow-by-default box_vec

Open
#2,404 3 comments 0 reactions 0 assignees View on GitHub

Nobody has claimed this yet.

A-documentation
Dominant language
Rust
Stars
13.5k
Forks
2.2k
Avg merge
2d 10h
Merged PRs (30d)
32

Description

In https://github.com/rust-lang-nursery/rust-clippy/issues/2394#issuecomment-360296105 (a typical "only 20%-on-topic comment" of mine), I wrote

But thinking about the box_vec lint I'm not sure I agree with its reasoning:

Vec already keeps its contents in a separate area on the heap. So if you Box it, you just add another level of indirection without any benefit whatsoever.

There is a benefit I can think of: Boxing a Vec or String reduces the size from 3 words to 1. I'm sure some this may be a good idea, and, more importantly, this is most likely done on purpose. But maybe this is just a good idea in my imagination. Or maybe there is a crate that gives you a Vec-like API but stores length an capacity as part of the heap data and we should suggest using that?

And indeed—there is a crate that does just that, and it is @Gankro's thin-vec. I'll allow the fact that it is not published to crates.io and has no readme to count for "this is an obscure use case".

Update: But it may be used in Gecko in the future (source).

So, I'm totally fine if nobody cares about this (I'll most likely never use this myself), but can I suggest changing the box_vec description to this? :)

Vec already keeps its contents in a separate area on the heap. So if you Box it, you add another level of indirection. This is often not the programmer's intention.

In the case you want to reduce the size of item on the stack, you should consider using a special Vec-like data structure that stores its length and capacity on the heap.

Contributor guide

Open the contributing guide

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

Review the box_vec lint description linked in the issue and compare it with the proposed wording. Check the referenced thin-vec source for context, then update the description so it explains the extra indirection and the stack-size tradeoff.

Written by the indexing model from the issue text.

Assessment

Tech stack
rust
Domain
documentation
Issue type
Documentation
Difficulty
1/5
Estimated time
Under an hour
Activity status
Stale
Clarity
Clearly specified
Newbie friendliness
50/100

Get new issues in your inbox

A short digest of beginner-friendly GitHub issues.