Skip to content

Instantly share code, notes, and snippets.

@Robbepop
Last active March 1, 2016 15:48
Show Gist options
  • Star 0 You must be signed in to star a gist
  • Fork 0 You must be signed in to fork a gist
  • Save Robbepop/da73db721c22492616f6 to your computer and use it in GitHub Desktop.
Save Robbepop/da73db721c22492616f6 to your computer and use it in GitHub Desktop.
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