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" name = "pumpkin"
version = "0.1.0" version = "0.1.0"
dependencies = [ 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)", "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)", "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)", "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]] [[package]]
name = "gcc" name = "gcc"
version = "0.3.17" version = "0.3.17"
@ -29,6 +36,11 @@ name = "libc"
version = "0.1.10" version = "0.1.10"
source = "registry+https://github.com/rust-lang/crates.io-index" 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]] [[package]]
name = "ramp" name = "ramp"
version = "0.1.9" version = "0.1.9"

View file

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

View file

@ -34,6 +34,8 @@
/// ``` /// ```
extern crate core; extern crate core;
#[macro_use] extern crate custom_derive;
#[macro_use] extern crate newtype_derive;
extern crate ramp; extern crate ramp;
extern crate rand; 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 /// 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, /// as well. `Prime`s simply guarantee that the number you're dealing with is,
/// a prime number. /// a prime number.
#[derive(Debug)] custom_derive! {
pub struct Prime { #[derive(Debug)]
bit_length: usize, #[derive(NewtypeAdd, NewtypeSub, NewtypeMul, NewtypeDiv)]
num: Int pub struct Prime(Int);
} }
impl Prime { impl Prime {
@ -125,16 +125,14 @@ impl Prime {
candidate += &two; candidate += &two;
} }
Prime { Prime(candidate)
bit_length: bit_length,
num: candidate
}
} }
} }
impl fmt::Display for Prime { impl fmt::Display for Prime {
fn fmt(&self, f: &mut fmt::Formatter) -> fmt::Result { 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)); b.iter(|| Prime::new(2048));
} }
} }