Merge pull request #4 from gavofyork/insert_slice
rlp uses insert_slice
This commit is contained in:
commit
04abfae6b5
@ -5,6 +5,7 @@ pub mod hash;
|
|||||||
pub mod uint;
|
pub mod uint;
|
||||||
pub mod bytes;
|
pub mod bytes;
|
||||||
pub mod rlp;
|
pub mod rlp;
|
||||||
|
pub mod vector;
|
||||||
|
|
||||||
#[test]
|
#[test]
|
||||||
fn it_works() {
|
fn it_works() {
|
||||||
|
16
src/rlp.rs
16
src/rlp.rs
@ -48,6 +48,7 @@ use std::cell::Cell;
|
|||||||
use std::collections::LinkedList;
|
use std::collections::LinkedList;
|
||||||
use std::error::Error as StdError;
|
use std::error::Error as StdError;
|
||||||
use bytes::{ToBytes, FromBytes, FromBytesError};
|
use bytes::{ToBytes, FromBytes, FromBytesError};
|
||||||
|
use vector::InsertSlice;
|
||||||
|
|
||||||
/// rlp container
|
/// rlp container
|
||||||
#[derive(Debug)]
|
#[derive(Debug)]
|
||||||
@ -455,16 +456,9 @@ impl BasicEncoder {
|
|||||||
}
|
}
|
||||||
|
|
||||||
/// inserts list prefix at given position
|
/// inserts list prefix at given position
|
||||||
/// TODO: optimise it, so it does not copy an array
|
/// TODO: optimise it further?
|
||||||
fn insert_list_len_at_pos(&mut self, len: usize, pos: usize) -> () {
|
fn insert_list_len_at_pos(&mut self, len: usize, pos: usize) -> () {
|
||||||
// new bytes
|
let mut res = vec![];
|
||||||
let mut res: Vec<u8> = vec![];
|
|
||||||
// reserve a space equal at least current space + space for length
|
|
||||||
res.reserve(self.bytes.len() + 1);
|
|
||||||
{
|
|
||||||
let (before_slice, after_slice) = self.bytes.split_at(pos);
|
|
||||||
res.extend(before_slice);
|
|
||||||
|
|
||||||
match len {
|
match len {
|
||||||
0...55 => res.push(0xc0u8 + len as u8),
|
0...55 => res.push(0xc0u8 + len as u8),
|
||||||
_ => {
|
_ => {
|
||||||
@ -473,9 +467,7 @@ impl BasicEncoder {
|
|||||||
}
|
}
|
||||||
};
|
};
|
||||||
|
|
||||||
res.extend(after_slice);
|
self.bytes.insert_slice(pos, &res);
|
||||||
}
|
|
||||||
self.bytes = res;
|
|
||||||
}
|
}
|
||||||
|
|
||||||
/// get encoded value
|
/// get encoded value
|
||||||
|
34
src/vector.rs
Normal file
34
src/vector.rs
Normal file
@ -0,0 +1,34 @@
|
|||||||
|
|
||||||
|
use std::ptr;
|
||||||
|
|
||||||
|
pub trait InsertSlice<T> {
|
||||||
|
fn insert_slice(&mut self, index: usize, elements: &[T]);
|
||||||
|
}
|
||||||
|
|
||||||
|
/// based on `insert` function implementation from standard library
|
||||||
|
impl<T> InsertSlice<T> for Vec<T> {
|
||||||
|
fn insert_slice(&mut self, index: usize, elements: &[T]) {
|
||||||
|
let e_len = elements.len();
|
||||||
|
if e_len == 0 {
|
||||||
|
return;
|
||||||
|
}
|
||||||
|
|
||||||
|
let len = self.len();
|
||||||
|
assert!(index <= len);
|
||||||
|
|
||||||
|
// space for the new element
|
||||||
|
self.reserve(e_len);
|
||||||
|
|
||||||
|
unsafe {
|
||||||
|
{
|
||||||
|
let p = self.as_mut_ptr().offset(index as isize);
|
||||||
|
let ep = elements.as_ptr().offset(0);
|
||||||
|
// shift everything by e_len, to make space
|
||||||
|
ptr::copy(p, p.offset(e_len as isize), len - index);
|
||||||
|
// write new element
|
||||||
|
ptr::copy(ep, p, e_len);
|
||||||
|
}
|
||||||
|
self.set_len(len + e_len);
|
||||||
|
}
|
||||||
|
}
|
||||||
|
}
|
Loading…
Reference in New Issue
Block a user