rust-lang / libs-team

The home of the library team
Apache License 2.0
128 stars 19 forks source link

Docs - type guarantees update #482

Open WiktorPrzetacznik opened 2 weeks ago

WiktorPrzetacznik commented 2 weeks ago

Proposal

Problem statement

Some type guarantees in docs are not precise enough.

Motivating examples or use cases

The lack of precision can be misleading and introduces unnecessary complexity, like Pin::map_unchecked

This function is unsafe. You must guarantee that the data you return will not move so long as the argument value does not move (for example, because it is one of the fields of that value), and also that you do not move out of the argument you receive to the interior function.

There's no way to do so, I'm pretty sure this is just a copy-paste from Pin::map_unchecked_mut

Another worth noting example is String::reserve (and other types based on Vec):

Panics if the new capacity overflows usize.

The more accurate would be informing that it panics if exceeds [isize::MAX] bytes.

Solution sketch

Update documentation of:

Precise information about what I'm proposing to change can be found in the related open PR #129822

Alternatives

  1. Leaving the docs as they are now.
  2. Moving Vec's related docs about panicing when exceeding usize into some other place, so it doesn't have to be changed in every related type.

Links and related work

PR #129822 - docs changes proposal forum discussion about Pin::map_unchecked forum discussion - Is Saturating guaranteed to have the same layout and ABI as T?