Skip to content

LenType causes regression regarding supported Vec sizes and usage in libraries. #568

Open
@sosthene-nitrokey

Description

@sosthene-nitrokey

The addition of LenType to vecs and the DefaultLenType means that arbitrary length of Vec are much less usable.

Not all lengths "just work" with Vec like they did previously

For example, the following: Vec<u8, 587> does not compile anymore, and needs to be worked around with Vec<u8, u16, 587>.

This itself is I think a significant downgrade in readability (the benefits of LenType are in most cases I think not worth making code less readable, since the overhead of the Vec itself will often be negligible compared to the buffer of the Vec).

In a library setting it forces the library writer to be explicit of the LenType:

fn some_func<const N: usize>() -> Vec<u8, N> {
    todo!()
}

Fails to compile with

error[E0277]: Length `N` does not have a default `LenType` mapping
   --> src/lib.rs:261:35
    |
261 | fn some_func<const N: usize>() -> Vec<u8, N> {
    |                                   ^^^^^^^^^^ the trait `SmallestLenType` is not implemented for `Const<N>`
    |
    = note: Provide the `LenType` explicitly, such as `usize`
    = help: the following other types implement trait `SmallestLenType`:
              Const<0>
              Const<1000>
              Const<100>
              Const<101>
              Const<1024>
              Const<102>
              Const<103>
              Const<1048576>
            and 287 others

It makes compatibility with View types much harder.

Because of the LenType the following fails to compile:

let vec: &mut VecView<u8> = Vec::<u8, 8>::new();
error[E0308]: mismatched types
   --> src/lib.rs:262:33
    |
262 |     let vec: &mut VecView<u8> = Vec::<u8, 8>::new();
    |              ----------------   ^^^^^^^^^^^^^^^^^^^ expected `&mut VecInner<u8, usize, ...>`, found `VecInner<u8, u8, ...>`
    |              |
    |              expected due to this
    |
    = note: expected mutable reference `&mut VecInner<_, usize, VecStorageInner<[MaybeUninit<u8>]>>`
                          found struct `VecInner<_, u8, VecStorageInner<[MaybeUninit<u8>; 8]>>

It can be worked around by specifying the LenType, but this will mean that the VecView will not coerce from most Vecs since most will not use usize as the LenType.

Metadata

Metadata

Assignees

No one assigned

    Labels

    Type

    No type

    Projects

    No projects

    Milestone

    No milestone

    Relationships

    None yet

    Development

    No branches or pull requests

    Issue actions