rust-lang / rust-lang/rfcs

Proposal: const boolean restrictions in `where` clause

Open
#3,162 9 comments 3 reactions 0 assignees View on GitHub

Nobody has claimed this yet.

Dominant language
Markdown
Stars
6.6k
Forks
1.7k
Avg merge
16h 14m
Merged PRs (30d)
1

Description

Currently the where clause supports only type: trait restrictions and works fine for the lifetime and type parameters. But for const generics, there is few ability to restrict those const parameters.

For example, if I want a small vec where the maximum on-stack size can be specified by a const generic parameter:

union Buf<T, const N: usize> {
    Stack([MaybeUninit<T>; N]),
    Heap(RawVec<T>),
}
pub struct SmallVec<T, const N: usize> {
    buf: Buf<T, N>,
    len: usize,
}

But, I need to keep N tiny to avoid too large stack occupying. How about introduce such where clause like this?

const MAX_ON_STACK_LEN: usize = 8;
union Buf<T, const N: usize>
    where const N < MAX_ON_STACK_LEN,
{ /* ... */ }

It might not violate the parser because const is not in the FIRST set of <ty>, and , is in the FOLLOW set of <expr>.

The only requirement of the <expr> is:

  1. it must be const (compile-time computable),
  2. it must have type bool.

Contributor guide

No contributing guide indexed for this repository

First steps

  1. Read the whole issue, then the project's contributing guide.
  2. Comment on the issue to say you are picking it up — it saves two people doing the same work.
  3. Fork the repository and make your change on a branch.
  4. Open a pull request that references the issue number.

Research direction

No implementation files, tests, or entry points are named. Start by reviewing the proposed const-generic where syntax and the two stated requirements, then determine the Rust language-design and RFC work needed; done means the syntax and compile-time boolean and type rules are specified.

Written by the indexing model from the issue text.

Assessment

Tech stack
rust
Domain
compilers
Issue type
Feature
Difficulty
5/5
Estimated time
Over a week
Activity status
Stale
Clarity
Mostly clear
Newbie friendliness
30/100

Get new issues in your inbox

A short digest of beginner-friendly GitHub issues.