diff --git a/Cargo.lock b/Cargo.lock index fe739b9..c4545df 100644 --- a/Cargo.lock +++ b/Cargo.lock @@ -2,6 +2,8 @@ name = "pumpkin" version = "0.1.0" dependencies = [ + "custom_derive 0.1.2 (registry+https://github.com/rust-lang/crates.io-index)", + "newtype_derive 0.1.2 (registry+https://github.com/rust-lang/crates.io-index)", "ramp 0.1.9 (registry+https://github.com/rust-lang/crates.io-index)", "rand 0.3.11 (registry+https://github.com/rust-lang/crates.io-index)", ] @@ -15,6 +17,11 @@ dependencies = [ "winapi-build 0.1.1 (registry+https://github.com/rust-lang/crates.io-index)", ] +[[package]] +name = "custom_derive" +version = "0.1.2" +source = "registry+https://github.com/rust-lang/crates.io-index" + [[package]] name = "gcc" version = "0.3.17" @@ -29,6 +36,11 @@ name = "libc" version = "0.1.10" source = "registry+https://github.com/rust-lang/crates.io-index" +[[package]] +name = "newtype_derive" +version = "0.1.2" +source = "registry+https://github.com/rust-lang/crates.io-index" + [[package]] name = "ramp" version = "0.1.9" diff --git a/Cargo.toml b/Cargo.toml index 8fe1d17..3eda19c 100644 --- a/Cargo.toml +++ b/Cargo.toml @@ -4,5 +4,7 @@ version = "0.1.0" authors = ["Zach Dziura "] [dependencies] +custom_derive = "0.1.2" +newtype_derive = "0.1.2" ramp = "0.1.9" rand = "0.3.11" diff --git a/src/lib.rs b/src/lib.rs index 3b61826..4943f43 100644 --- a/src/lib.rs +++ b/src/lib.rs @@ -34,6 +34,8 @@ /// ``` extern crate core; +#[macro_use] extern crate custom_derive; +#[macro_use] extern crate newtype_derive; extern crate ramp; extern crate rand; diff --git a/src/prime.rs b/src/prime.rs index 4c25ca4..150269c 100644 --- a/src/prime.rs +++ b/src/prime.rs @@ -96,10 +96,10 @@ static SMALL_PRIMES: [u32; 999] = [3, 5, 7, 11, 13, 17, 19, 23, 29, 31, 37, 41, /// fact, all operations that you can do with `Int`s, you can do with `Prime`s /// as well. `Prime`s simply guarantee that the number you're dealing with is, /// a prime number. -#[derive(Debug)] -pub struct Prime { - bit_length: usize, - num: Int +custom_derive! { + #[derive(Debug)] + #[derive(NewtypeAdd, NewtypeSub, NewtypeMul, NewtypeDiv)] + pub struct Prime(Int); } impl Prime { @@ -125,16 +125,14 @@ impl Prime { candidate += &two; } - Prime { - bit_length: bit_length, - num: candidate - } + Prime(candidate) } } impl fmt::Display for Prime { fn fmt(&self, f: &mut fmt::Formatter) -> fmt::Result { - write!(f, "{}", self.num) + let &Prime(ref num) = self; + write!(f, "{}", num) } } @@ -260,3 +258,4 @@ mod tests { b.iter(|| Prime::new(2048)); } } +