Skip to content

make automatic length detection feature-gated #3

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

Open
wants to merge 1 commit into
base: generic-length-type-over-new-layout
Choose a base branch
from

Conversation

mysteriouslyseeing
Copy link

I think that the automatic length detection is cool, but I'm not sure how actually useful it is. As it is, the following code doesn't compile:

let a: ArrayVec<u8, 17> = ArrayVec::new();

Worse, the error message is confusing:

   Compiling t v0.1.0 (/home/wschroeder/Projects/t)
error[E0277]: the trait bound `arrayvec::len_type::ConstGenericSmuggler<17>: arrayvec::len_type::CapToDefaultLenType` is not satisfied
 --> src/main.rs:4:12
  |
4 |     let a: ArrayVec<u8, 17> = ArrayVec::new();
  |            ^^^^^^^^^^^^^^^^ the trait `arrayvec::len_type::CapToDefaultLenType` is not implemented for `arrayvec::len_type::ConstGenericSmuggler<17>`
  |
  = help: the following other types implement trait `arrayvec::len_type::CapToDefaultLenType`:
            arrayvec::len_type::ConstGenericSmuggler<0>
            arrayvec::len_type::ConstGenericSmuggler<1000000>
            arrayvec::len_type::ConstGenericSmuggler<1000>
            arrayvec::len_type::ConstGenericSmuggler<100>
            arrayvec::len_type::ConstGenericSmuggler<1024>
            arrayvec::len_type::ConstGenericSmuggler<10>
            arrayvec::len_type::ConstGenericSmuggler<11>
            arrayvec::len_type::ConstGenericSmuggler<128>
          and 30 others

For more information about this error, try `rustc --explain E0277`.
error: could not compile `t` (bin "t") due to 1 previous error

I just feel like the automatic length detection is reinventing the wheel whilst making code which used to compile break. Personally, I'd rather it just default to a u32, and you can specify a smaller type if your space requirements are particularly confining (or if u32::MAX is too small), rather than having code that used to compile not compile in a vast majority of circumstances.
There are defaults for 23/256 possible u8 lengths. The whole point of the automatic length detection is to save the user having to write

let a: ArrayVec<u8, 17, u8> = ArrayVec::new();
//                    ^^^^ this

when in a vast majority of circumstances (assuming even distribution of lengths, thats ~91% of u8s, ~99.98% of u16s, ~99.999999993% of u32s, and some ridiculous number for u64 that my calculator won't display) the user will have to write it anyway. It just seems like there's not much point, and we are better off leaving performance to be opt-in.

In nightly, we can use #![feature(specialization)] to allow a default implementation with u32, solving the problem and giving us the best of both worlds - but it should be gated behind a nightly feature, which is what I've done here.

@mysteriouslyseeing mysteriouslyseeing changed the title make automatic length detection complete make automatic length detection feature-gated Jul 20, 2024
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