ACP: Vec::push_mut
and Vec::insert_mut
#579
Labels
ACP-accepted
API Change Proposal is accepted (seconded with no objections)
api-change-proposal
A proposal to add or alter unstable APIs in the standard libraries
T-libs-api
Proposal
Problem statement
Currently, there is no safe, infallible way to obtain an particular element in a
Vec
: This is reasonable because there may not be any elements in aVec
. However, an occasionally useful operation is "Insert an Element and get that Element" which is infallible (modulo allocation failure). Currently, performing this ad-hoc requires inserting an element, and doing some form ofunwrap
on.last()
which is excessively verbose for what could reasonably be a one-liner.Motivating examples or use cases
In the visitor pattern, elements are built from another input format in groups. The two common designs are either to return some visitor that contains internal state or pass that internal state to an
Fn
of some kind - in either case, the state will almost always borrow (probably mutably) from the internal state of the current visitor. Where a subset of elements can be visited multiple times, this may involve borrowing new elements of aVec
or other extensible data structure. As an example, a simple visitor that accepts rust arrays might look likeSolution sketch
insert_mut
is included for completeness withVec::push_mut
andVec::index
, and is less important to this proposal than the baseVec::push_mut
(The
A: Allocator
in the trait bound is to indicate that, albeit likely obviously, the particular choice of allocator does not interfere with the API and doesn't depend on stabilizing allocator_api as-is)Alternatives
A few alternatives could be panicking and/or unchecked versions of
<[T]>::{first,last}
. While these remove the need tounwrap()
an option, they still require the same justification of infallibility as the.unwrap()
and also haven't reduced a two-line operation to a clean one-liner where the justification is encapsilated by the library.This can also be an extension trait or free function in another using a canonical implementation, like
However a direct implementation would be more readily able to take advantage of new support in
Vec
.Links and Related Work
Option
has.insert
,.get_or_insert
, and.get_or_insert_with
, and via theEntry
apistd::collections::HashMap
has.insert
,.or_insert
, and.or_insert_with
. These methods also see use in visitor pattern implementations when you may have a single element or have associated elements.In C++,
std::vector
returns either aniterator
(insert
andemplace
) or areference
(emplace_back
) oninsert
,emplace
, andemplace_back
(though as I've discovered, not onpush_back
). For background, aniterator
in C++ is a handle to a particular element or sequence of elements that can be dereferenced (in the case of a vector, other thanstd::vector<bool>
, this is a simple pointer or a thin wrapper arround a pointer).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):
Second, if there's a concrete solution:
The text was updated successfully, but these errors were encountered: