Skip to content

Commit 8e48c5d

Browse files
committed
Add implementation of Rand for Division and add zoom_in/zoom_out methods
1 parent 3868eba commit 8e48c5d

File tree

5 files changed

+34
-10
lines changed

5 files changed

+34
-10
lines changed

.travis.yml

+1-5
Original file line numberDiff line numberDiff line change
@@ -2,11 +2,7 @@ language: rust
22

33
rust:
44
- nightly
5-
- 1.0.0-beta
6-
7-
os:
8-
- osx
9-
- linux
5+
- stable
106

117
script:
128
- cargo build --verbose

Cargo.toml

+1
Original file line numberDiff line numberDiff line change
@@ -12,4 +12,5 @@ homepage = "https://github.com/RustAudio/time_calc"
1212

1313
[dependencies]
1414
num = "*"
15+
rand = "*"
1516
rustc-serialize = "*"

README.md

+2-2
Original file line numberDiff line numberDiff line change
@@ -22,8 +22,8 @@ Usage
2222
Add time_calc to your cargo dependencies like this:
2323

2424
```
25-
[dependencies.time_calc]
26-
git = "https://github.com/RustAudio/time_calc.git"
25+
[dependencies]
26+
time_calc = "*"
2727
```
2828

2929
See [the example](https://github.com/RustAudio/time_calc/blob/master/examples/test.rs) for a better demo.

src/division.rs

+29-3
Original file line numberDiff line numberDiff line change
@@ -27,6 +27,10 @@ pub enum Division {
2727
OneThousandTwentyFourth,
2828
}
2929

30+
const HIGHEST_ZOOM_STEP: u8 = 10;
31+
32+
33+
3034
impl Division {
3135

3236
pub fn from_isize<T: ToPrimitive>(num: T) -> Division {
@@ -50,6 +54,21 @@ impl Division {
5054
}
5155
}
5256

57+
/// Zoom into a higher resolution division by the number of steps given.
58+
pub fn zoom_in(&self, steps: u8) -> Division {
59+
let zoom_step = self.to_u8().unwrap() + steps;
60+
assert!(zoom_step <= HIGHEST_ZOOM_STEP);
61+
NumCast::from(zoom_step).unwrap()
62+
}
63+
64+
/// Zoom into a higher resolution division by the number of steps given.
65+
pub fn zoom_out(&self, steps: u8) -> Division {
66+
let current_zoom_step = self.to_u8().unwrap();
67+
assert!(steps <= current_zoom_step);
68+
let zoom_step = current_zoom_step - steps;
69+
NumCast::from(zoom_step).unwrap()
70+
}
71+
5372
}
5473

5574
impl NumCast for Division {
@@ -112,11 +131,11 @@ impl Sub<isize> for Division {
112131
}
113132

114133
/// The 'Division Type'. Used for handling 'Thirds'.
115-
/// Whole represents a Whole division, while TwoThirds
116-
/// represents two thirds of a division.
134+
/// Whole represents a Whole division, while TwoThirds represents two thirds of a division.
117135
#[derive(Debug, Copy, Clone, RustcEncodable, RustcDecodable, PartialEq, Eq)]
118136
pub enum DivType {
119-
Whole, TwoThirds
137+
Whole,
138+
TwoThirds,
120139
}
121140

122141

@@ -132,6 +151,13 @@ impl NumCast for DivType {
132151
}
133152
}
134153

154+
impl ::rand::Rand for DivType {
155+
fn rand<R: ::rand::Rng>(rng: &mut R) -> DivType {
156+
let rand: bool = rng.gen();
157+
if rand { DivType::Whole } else { DivType::TwoThirds }
158+
}
159+
}
160+
135161
impl FromPrimitive for DivType {
136162
fn from_i64(n: i64) -> Option<Self> { Self::from_u64(n as u64) }
137163
fn from_u64(n: u64) -> Option<Self> {

src/lib.rs

+1
Original file line numberDiff line numberDiff line change
@@ -6,6 +6,7 @@
66
//!
77
88
extern crate num;
9+
extern crate rand;
910
extern crate rustc_serialize;
1011

1112
pub use bars::Bars;

0 commit comments

Comments
 (0)