Skip to content

[Use Case]: const-time if/else choosing entirely different impls and not compiling the not-taken ones #52

Open
@the8472

Description

@the8472

Code description

Basically

if const { condition } {
   // impl A
} else {
   // impl B
}

but with compile-time optimization so that compiler doesn't try to monomorphize / do type-checking of the bodies (and their callees!) of the not-taken branches

#![feature(generic_const_exprs)]

use core::marker::PhantomData;

struct Bar<T> {
    _x: PhantomData<T>
}

enum Condition<const COND: bool> {}
trait Dispatch<T> {
    fn something(arg: &T);
}

impl<T> Dispatch<T> for Condition<true> {
    fn something(arg: &T) {
        // impl A
    }
}
impl<T> Dispatch<T> for Condition<false> {
    fn something(arg: &T) {
       // impl B
    }
}


fn always_covered<T>(a: Bar<T>) {
    <Condition<{std::mem::size_of::<T>() > 16}> as Dispatch<_>>::something(&a);
}

What worked well

That the pattern can be expressed at all

What worked less well

Currently it does not detect that COND is exhaustively covered and thus Dispatch is always implemented by Condition.

error[[E0277]](https://doc.rust-lang.org/nightly/error_codes/E0277.html): the trait bound `Condition<{std::mem::size_of::<T>() > 16}>: Dispatch<_>` is not satisfied
  --> src/lib.rs:23:76
   |
23 |     <Condition<{std::mem::size_of::<T>() > 16}> as Dispatch<_>>::something(&a);
   |     ---------------------------------------------------------------------- ^^ the trait `Dispatch<_>` is not implemented for `Condition<{std::mem::size_of::<T>() > 16}>`
   |     |
   |     required by a bound introduced by this call
   |
   = help: the following other types implement trait `Dispatch<T>`:
             Condition<false>
             Condition<true>

Metadata

Metadata

Assignees

No one assigned

    Labels

    A-generic-exprsGeneric const expressionsC-use-caseCategory: This is a use case for a feature

    Type

    No type

    Projects

    No projects

    Milestone

    No milestone

    Relationships

    None yet

    Development

    No branches or pull requests

    Issue actions