1
2
3
4
5
6
7
8
9
10
11
12
13
14
15
16
17
18
19
20
21
22
23
24
25
26
27
28
29
30
31
32
33
34
35
36
37
38
39
40
41
42
43
44
45
46
47
48
49
50
51
52
53
54
55
56
57
58
59
60
use interface::*;
use errors::*;
use std::cmp;
pub fn copy<T: MemoryBlock>(src: &T, dst: &mut T) -> Result<Addr, Error> {
let src_sz = src.get_size();
let dst_sz = dst.get_size();
if src_sz > dst_sz {
bail!(ErrorKind::TooBig(src_sz, dst_sz));
}
for i in 0..src_sz {
let status = match src.get(i) {
Ok(byte) => dst.set(i, byte),
Err(e) => Err(e),
};
status.chain_err(|| "in copy helper")?;
}
Ok(src_sz)
}
pub fn copy_at<T: MemoryBlock>(src: &T, dst: &mut T, from: Addr, to: Addr, pos: Addr) -> Result<Addr, Error> {
let src_sz = src.get_size();
let dst_sz = dst.get_size();
let lowest = cmp::min(to, from);
let numbytes = cmp::max(to, from) - lowest;
if (lowest + numbytes) > src_sz {
bail!(ErrorKind::TooBig(lowest + numbytes, src_sz));
};
if (pos + numbytes) > dst_sz {
bail!(ErrorKind::TooBig(pos + numbytes, dst_sz));
};
for i in from..(to+1) {
let status = match src.get(i) {
Ok(byte) => {
let dstpos = pos + (i - lowest);
dst.set(dstpos, byte)
},
Err(e) => Err(e),
};
status.chain_err(|| "in copy_at helper")?;
}
Ok(numbytes)
}