Added custom operator implementations

This commit is contained in:
Zach Dziura 2015-10-08 00:49:01 -04:00
parent b901d7962b
commit 81ed7f8c9a
4 changed files with 24 additions and 9 deletions

12
Cargo.lock generated
View file

@ -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"

View file

@ -4,5 +4,7 @@ version = "0.1.0"
authors = ["Zach Dziura <zcdziura@gmail.com>"]
[dependencies]
custom_derive = "0.1.2"
newtype_derive = "0.1.2"
ramp = "0.1.9"
rand = "0.3.11"

View file

@ -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;

View file

@ -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.
custom_derive! {
#[derive(Debug)]
pub struct Prime {
bit_length: usize,
num: Int
#[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));
}
}