From 40423085733816e9708f6b15649f25a7b418c745 Mon Sep 17 00:00:00 2001 From: =?UTF-8?q?=E5=88=98=E7=A5=BA?= <3442853561@qq.com> Date: Thu, 22 Jun 2017 00:38:36 +0800 Subject: [PATCH] Update 0.0.1 --- .gitignore | 3 +++ .travis.yml | 5 +++++ Cargo.toml | 17 +++++++++++++++++ src/examples/main.rs | 7 +++++++ src/float.rs | 1 + src/lib.rs | 3 +++ src/math.rs | 32 ++++++++++++++++++++++++++++++++ 7 files changed, 68 insertions(+) create mode 100644 .travis.yml create mode 100644 Cargo.toml create mode 100644 src/examples/main.rs create mode 100644 src/float.rs create mode 100644 src/lib.rs create mode 100644 src/math.rs diff --git a/.gitignore b/.gitignore index cb14a42..795e64a 100644 --- a/.gitignore +++ b/.gitignore @@ -5,3 +5,6 @@ # Remove Cargo.lock from gitignore if creating an executable, leave it for libraries # More information here http://doc.crates.io/guide.html#cargotoml-vs-cargolock Cargo.lock + +# These are backup files generated by rustfmt +**/*.rs.bk \ No newline at end of file diff --git a/.travis.yml b/.travis.yml new file mode 100644 index 0000000..b4f863c --- /dev/null +++ b/.travis.yml @@ -0,0 +1,5 @@ +language: rust +rust: + - stable + - beta + - nightly diff --git a/Cargo.toml b/Cargo.toml new file mode 100644 index 0000000..b89d4bd --- /dev/null +++ b/Cargo.toml @@ -0,0 +1,17 @@ +[package] +name = "granny" +version = "0.0.1" +license = "MIT" +authors = ["3442853561<3442853561@qq.com>"] +keywords = ["computing", "scientific computing"] + +description = "Granny is the crate for scientific computing with Rust" +repository = "https://github.com/RustDream/Granny" + +[lib] +name = "granny" +path = "./src/lib.rs" + +[[bin]] +name = "main" +path = "./src/examples/main.rs" diff --git a/src/examples/main.rs b/src/examples/main.rs new file mode 100644 index 0000000..a01f18e --- /dev/null +++ b/src/examples/main.rs @@ -0,0 +1,7 @@ +extern crate granny; + +use granny::Euclidean; + +fn main() { + println!("{}", 10u8.gcd(8)); +} diff --git a/src/float.rs b/src/float.rs new file mode 100644 index 0000000..8b13789 --- /dev/null +++ b/src/float.rs @@ -0,0 +1 @@ + diff --git a/src/lib.rs b/src/lib.rs new file mode 100644 index 0000000..e38f52c --- /dev/null +++ b/src/lib.rs @@ -0,0 +1,3 @@ +pub mod float; +pub mod math; +pub use math::Euclidean; diff --git a/src/math.rs b/src/math.rs new file mode 100644 index 0000000..3403423 --- /dev/null +++ b/src/math.rs @@ -0,0 +1,32 @@ +use std::ops::{Div, Rem}; + +pub trait Euclidean + where T: Div + Rem +{ + /// Calculate the maximum common divisor + fn gcd(self, p: T) -> T; +} + +impl Euclidean for u8 { + fn gcd(self, p: u8) -> u8 { + if self % p != 0 { p.gcd(self % p) } else { p } + } +} + +impl Euclidean for u16 { + fn gcd(self, p: u16) -> u16 { + if self % p != 0 { p.gcd(self % p) } else { p } + } +} + +impl Euclidean for u32 { + fn gcd(self, p: u32) -> u32 { + if self % p != 0 { p.gcd(self % p) } else { p } + } +} + +impl Euclidean for u64 { + fn gcd(self, p: u64) -> u64 { + if self % p != 0 { p.gcd(self % p) } else { p } + } +}