Created
March 1, 2014 20:30
-
-
Save mcpherrinm/9296786 to your computer and use it in GitHub Desktop.
How can I change this impl to work on &[u8] instead of ~[u8]
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
trait Bitbuf { | |
fn read_bytes(&self, start: uint, len: uint) -> u64; | |
fn write_bytes(&mut self, start: uint, len: uint, data: u64); | |
} | |
// An implementation on a bare buffer | |
//impl<'a> Bitbuf for &'a [u8] { | |
impl Bitbuf for ~[u8] { | |
fn read_bytes(&self, start: uint, len: uint) -> u64 { | |
let mut data = 0; | |
for i in range(0, len) { | |
data |= ( self[start+i] as u64 ) << ( 8 * i ); | |
} | |
data | |
} | |
fn write_bytes(&mut self, start: uint, len: uint, data: u64) { | |
for i in range(0, len) { | |
self[start+i] = ( data >> ( 8 * i ) ) as u8; | |
} | |
} | |
} | |
#[test] | |
fn test_read_bytes() { | |
let bytes: ~[u8] = ~[0,1,2,3,4,5,6,7,8,9,0xA,0xB,0xC,0xD,0xE,0xF]; | |
for i in range(0u,0xE) { | |
assert_eq!(bytes.read_bytes(i, 2), ((i as u64) | ((i+1 << 8) as u64))); | |
} | |
assert_eq!(bytes.read_bytes(0, 8), 0x0706050403020100u64); | |
assert_eq!(bytes.read_bytes(8, 8), 0x0F0E0D0C0B0A0908u64); | |
} | |
#[test] | |
fn test_write_bytes() { | |
let mut bytes: ~[u8] = ~[0, ..8]; | |
bytes.write_bytes(0, 8, 0x0706050403020100u64); | |
assert_eq!(bytes, ~[0,1,2,3,4,5,6,7]); | |
let mut bytes: ~[u8] = ~[0, ..16]; | |
bytes.write_bytes(8,8,0x0F0E0D0C0B0A0908u64); | |
assert_eq!(bytes, ~[0,0, 0, 0, 0, 0, 0, 0, | |
8,9,0xA,0xB,0xC,0xD,0xE,0xF]); | |
} |
Sign up for free
to join this conversation on GitHub.
Already have an account?
Sign in to comment
trait Bitbuf {
fn read_bytes(&self, start: uint, len: uint) -> u64;
fn write_bytes(&mut self, start: uint, len: uint, data: u64);
}
// An implementation on a bare buffer
impl<'a> Bitbuf for &'a mut [u8] {
fn read_bytes(&self, start: uint, len: uint) -> u64 {
let mut data = 0;
for i in range(0, len) {
data |= ( self[start+i] as u64 ) << ( 8 * i );
}
data
}
fn write_bytes(&mut self, start: uint, len: uint, data: u64) {
for i in range(0, len) {
self[start+i] = ( data >> ( 8 * i ) ) as u8;
}
}
}
impl<'a> Bitbuf for &'a [u8] {
fn read_bytes(&self, start: uint, len: uint) -> u64 {
let mut data = 0;
for i in range(0, len) {
data |= ( self[start+i] as u64 ) << ( 8 * i );
}
data
}
fn write_bytes(&mut self, _: uint, _: uint, _: u64) {
fail!("Can't write immutable buffer");
}
}
[test]
fn test_read_bytes() {
let bytes: ~[u8] = ~[0,1,2,3,4,5,6,7,8,9,0xA,0xB,0xC,0xD,0xE,0xF];
for i in range(0u,0xE) {
assert_eq!(bytes.read_bytes(i, 2), ((i as u64) | ((i+1 << 8) as u64)));
}
assert_eq!(bytes.read_bytes(0, 8), 0x0706050403020100u64);
assert_eq!(bytes.read_bytes(8, 8), 0x0F0E0D0C0B0A0908u64);
}
[test]
fn test_write_bytes() {
let mut bytes: ~[u8] = ~[0, ..8];
bytes.as_mut_slice().write_bytes(0, 8, 0x0706050403020100u64);
assert_eq!(bytes, ~[0,1,2,3,4,5,6,7]);
let mut bytes: ~[u8] = ~[0, ..16];
bytes.as_mut_slice().write_bytes(8,8,0x0F0E0D0C0B0A0908u64);
assert_eq!(bytes, ~[0,0, 0, 0, 0, 0, 0, 0,
8,9,0xA,0xB,0xC,0xD,0xE,0xF]);
}