Skip to content

Commit

Permalink
Add by value integer splitting
Browse files Browse the repository at this point in the history
  • Loading branch information
panicbit committed Mar 3, 2016
1 parent 8eaf573 commit f69f1b3
Showing 1 changed file with 36 additions and 14 deletions.
50 changes: 36 additions & 14 deletions src/incubation/split_int.rs
Original file line number Diff line number Diff line change
@@ -1,12 +1,19 @@
pub trait SplitInt {
pub trait SplitInt<'a> {
type Output;
type MutOutput;
fn split_mut(self) -> Self::MutOutput;
fn split(self) -> Self::Output;
fn split_mut(&'a mut self) -> Self::MutOutput;
}

impl <'a> SplitInt for &'a mut u16 {
impl <'a> SplitInt<'a> for u16 {
type Output = (u8, u8);
type MutOutput = (&'a mut u8, &'a mut u8);

fn split_mut(self) -> Self::MutOutput {
fn split(self) -> Self::Output {
((self >> 8) as u8, self as u8)
}

fn split_mut(&'a mut self) -> Self::MutOutput {
split_mut_u16(self)
}
}
Expand Down Expand Up @@ -42,33 +49,48 @@ pub fn split_mut_u16(n: &mut u16) -> (&mut u8, &mut u8) {
#[cfg(test)]
mod test {
use super::*;
#[test]
fn u16_low() {
let n: u16 = 0b11001100_10101010;
let (_, low) = n.split();

assert_eq!(low, 0b10101010);
}

#[test]
fn u16_get_low() {
let mut n = 0b11001100_10101010;
let (_, low) = split_mut_u16(&mut n);
fn u16_high() {
let n: u16 = 0b11001100_10101010;
let (high, _) = n.split();

assert_eq!(high, 0b11001100);
}

#[test]
fn mut_u16_get_low() {
let mut n: u16 = 0b11001100_10101010;
let (_, low) = n.split_mut();
assert_eq!(*low , 0b10101010);
}

#[test]
fn u16_get_high() {
let mut n = 0b11001100_10101010;
let (high, _) = split_mut_u16(&mut n);
fn mut_u16_get_high() {
let mut n: u16 = 0b11001100_10101010;
let (high, _) = n.split_mut();
assert_eq!(*high , 0b11001100);
}

#[test]
fn u16_set_low() {
let mut n = 0b11001100_10101010;
fn mut_u16_set_low() {
let mut n: u16 = 0b11001100_10101010;
{
let (_, low) = split_mut_u16(&mut n);
let (_, low) = n.split_mut();
*low = 0b01010101;
}
assert_eq!(n, 0b11001100_01010101);
}

#[test]
fn u16_set_high() {
fn mut_u16_set_high() {
let mut n = 0b11001100_10101010;
{
let (high, _) = split_mut_u16(&mut n);
Expand Down

0 comments on commit f69f1b3

Please sign in to comment.