use copy_from_slice instead of for loop (#5647)

This commit is contained in:
Guanqun Lu 2017-05-18 05:46:14 -05:00 committed by Gav Wood
parent b1eab698d2
commit 6d81b8a49a

View File

@ -86,10 +86,7 @@ impl Memory for Vec<u8> {
fn write_slice(&mut self, offset: U256, slice: &[u8]) {
let off = offset.low_u64() as usize;
// TODO [todr] Optimize?
for pos in off..off+slice.len() {
self[pos] = slice[pos - off];
}
self[off..off+slice.len()].copy_from_slice(slice);
}
fn write(&mut self, offset: U256, value: U256) {
@ -114,9 +111,13 @@ impl Memory for Vec<u8> {
}
}
#[cfg(test)]
mod tests {
use util::U256;
use super::Memory;
#[test]
fn test_memory_read_and_write() {
#[test]
fn test_memory_read_and_write() {
// given
let mem: &mut Memory = &mut vec![];
mem.resize(0x80 + 32);
@ -126,10 +127,10 @@ fn test_memory_read_and_write() {
// then
assert_eq!(mem.read(U256::from(0x80)), U256::from(0xabcdef));
}
}
#[test]
fn test_memory_read_and_write_byte() {
#[test]
fn test_memory_read_and_write_byte() {
// given
let mem: &mut Memory = &mut vec![];
mem.resize(32);
@ -141,4 +142,26 @@ fn test_memory_read_and_write_byte() {
// then
assert_eq!(mem.read(U256::from(0x00)), U256::from(0xabcdef));
}
#[test]
fn test_memory_read_slice_and_write_slice() {
let mem: &mut Memory = &mut vec![];
mem.resize(32);
{
let slice = "abcdefghijklmnopqrstuvwxyz012345".as_bytes();
mem.write_slice(U256::from(0), slice);
assert_eq!(mem.read_slice(U256::from(0), U256::from(32)), slice);
}
// write again
{
let slice = "67890".as_bytes();
mem.write_slice(U256::from(0x1), slice);
assert_eq!(mem.read_slice(U256::from(0), U256::from(7)), "a67890g".as_bytes());
}
}
}