Skip to content

docs: Wording regarding guarantees about Vec's unused capacity is unclear #37746

Closed
@antrik

Description

@antrik

The "Guarantees" section in the documentation for Vec has some unclear wording, especially in the second-to-last paragraph:

Vec will not specifically overwrite any data that is removed from it, but also won't specifically preserve it. Its uninitialized memory is scratch space that it may use however it wants. It will generally just do whatever is most efficient or otherwise easy to implement.

In my reading, that means we are explicitly not allowed to store anything (e.g. via FFI calls) in the Vec's unused (but allocated) capacity, beyond its current valid length (size) -- but some others are disagreeing with my interpretation.

Specifically, the question is whether doing something like this is invalid:

let buf_size: usize = 666;
unsafe {
    let buf: Vec<u8> = Vec::with_capacity(buf_size);
    let data_size = libc::read(fd, buf.as_mut_ptr() as *mut c_void, buf_size as size_t) as usize;
    assert(data_size >= 0);
    buf.set_len(data_size);
}

As opposed to this variant, which should be valid even in my reading:

let buf_size: usize = 666;
unsafe {
    let buf: Vec<u8> = Vec::with_capacity(buf_size);
    buf.set_len(buf_size);
    let data_size = libc::read(fd, buf.as_mut_ptr() as *mut c_void, buf_size as size_t) as usize;
    assert(data_size >= 0);
    buf.set_len(data_size);
}

Metadata

Metadata

Assignees

No one assigned

    Labels

    A-docsArea: Documentation for any part of the project, including the compiler, standard library, and toolsT-libs-apiRelevant to the library API team, which will review and decide on the PR/issue.

    Type

    No type

    Projects

    No projects

    Milestone

    No milestone

    Relationships

    None yet

    Development

    No branches or pull requests

    Issue actions