-
-
Save Robbepop/da73db721c22492616f6 to your computer and use it in GitHub Desktop.
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
trait Constant<T: Number> { | |
const N: T; // error: associated constants are unstable | |
fn value() -> T { Self::N } // rust can't evaluate value() at compile-time! | |
} | |
struct Constant128; | |
struct Constant256; | |
struct Constant512; | |
impl<T> Constant<T> for Constant128 { const N: T = 128; } | |
impl<T> Constant<T> for Constant256 { const N: T = 256; } | |
impl<T> Constant<T> for Constant512 { const N: T = 512; } | |
/// An Integer with a size known during compile-time for further optimization. | |
/// This can be used to instantiate it with any power-of-two number defined within | |
/// a struct implementing the Constant trait, e.g. Constant128, Constant256, etc. | |
struct BigInt<C> where C: Constant<i32> { | |
buffer: [i32; C::N / 4] // 4-byte per i32, C::N works since it can be evaluated at compile-time! | |
} |
Sign up for free
to join this conversation on GitHub.
Already have an account?
Sign in to comment