Skip to content
New issue

Have a question about this project? Sign up for a free GitHub account to open an issue and contact its maintainers and the community.

By clicking “Sign up for GitHub”, you agree to our terms of service and privacy statement. We’ll occasionally send you account related emails.

Already on GitHub? Sign in to your account

Use const generics in ArrayVec #172

Merged
merged 12 commits into from
Mar 23, 2021
Merged

Use const generics in ArrayVec #172

merged 12 commits into from
Mar 23, 2021

Conversation

bluss
Copy link
Owner

@bluss bluss commented Dec 17, 2020

Use const generics for ArrayVec and ArrayString - now it can support arbitrary capacity/backing array.

Syntax changes like this:

  • old: ArrayVec<[T; CAP]>
  • new: ArrayVec<T, CAP>
  • old: ArrayString<[u8; CAP]>
  • new: ArrayString<CAP>

The ArrayVec<T, const CAP: usize> for now always uses an usize for its length, and so uses more space than the old ArrayVec<[T; CAP]>.

Requires Rust 1.51 (March 2021)

In for example Vim one can use the following substitution to update syntax:

:%s/ArrayVec\(:\?:\?\)<\[\(.*\);\(.*\)\]>/ArrayVec\1<\2,\3>/g

Fixes #125
Closes #100

src/const.rs Outdated Show resolved Hide resolved
src/const.rs Outdated Show resolved Hide resolved
@bluss bluss added this to the 0.6.0 milestone Feb 18, 2021
@bluss bluss changed the title Const generics version of ArrayVec Use const generics in ArrayVec Mar 23, 2021
@bluss bluss marked this pull request as ready for review March 23, 2021 16:59
bluss added 3 commits March 23, 2021 18:08
Just search/replace for syntax [T; N] -> T, N and it works.
With const generics we can't avoid the usize (or other type) length
field.
@bluss
Copy link
Owner Author

bluss commented Mar 23, 2021

CI passes on beta and nightly - will pass fully when Rust 1.51 is released this week.

@bluss bluss merged commit a58e1a5 into master Mar 23, 2021
@bluss bluss deleted the min-const-gen branch March 23, 2021 18:21
This was referenced Mar 23, 2021
@WiSaGaN
Copy link

WiSaGaN commented Apr 15, 2021

The ArrayVec<T, const CAP: usize> for now always uses an usize for its length, and so uses more space than the old ArrayVec<[T; CAP]>.

I am wondering if rust-lang/rust#76560 can help with the size regression introduced here in future version of ArrayVec?

@bluss
Copy link
Owner Author

bluss commented Apr 15, 2021

Later PR updated the length to always be u32 (for a smaller size). I can't answer your question, maybe you know? 🙂

@WiSaGaN
Copy link

WiSaGaN commented Apr 15, 2021

It seems so to me, but I am not terribly familiar with Rust constant generics. Will try push a pull request when it is stable!

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.

Support for constant generics Re-add generic-array integration
3 participants