Skip to content

add new_with_capacity constructor#84

Open
bbstilson wants to merge 3 commits intocloudhead:masterfrom
bbstilson:feature/new_with_capacity
Open

add new_with_capacity constructor#84
bbstilson wants to merge 3 commits intocloudhead:masterfrom
bbstilson:feature/new_with_capacity

Conversation

@bbstilson
Copy link

@bbstilson bbstilson commented Mar 17, 2026

This PR adds a new constructor new_with_capacity, which mimics the Vec::with_capacity API. I shamelessly stole almost all the documentation from Rust 🤓

Choices made:

  • Naming: I started with with_capacity, but because we must instantiate with an element I leaned towards a name that differentiated itself to avoid confusion and landed on new_with_capacity. No strong feelings on this and happy to change.
  • capacity refers to the size of the NonEmpty struct as though it were a Vec. This is sort of a weird one, but there are two motivations.
    • First, I didn't want to expose the inner workings of how the data are stored under the hood in NonEmpty. The mental model of NonEmpty is that it's a Vec but always has at least one element in it. Because we require a value when constructing a NonEmpty vec, the size must always be at least 1 even though internally the tail Vec will be empty.
    • Second, requiring capacity to always be at least one matches the existing behavior of NonEmpty::capacity, which returns (roughly) 1 + tail.len().

Sign up for free to join this conversation on GitHub. Already have an account? Sign in to comment

Labels

None yet

Projects

None yet

Development

Successfully merging this pull request may close these issues.

1 participant