#![doc(hidden)]
use rand::random;
use std::sync::{Arc, Mutex};
use super::Storage;
pub fn random_bytes(length: usize) -> Vec<u8> {
let mut bytes: Vec<u8> = Vec::with_capacity(length);
for _ in 0..length {
bytes.push(random::<u8>());
}
bytes
}
struct Entry {
name: Vec<u8>,
data: Vec<u8>,
}
pub struct SimpleStorage {
entries: Arc<Mutex<Vec<Entry>>>,
}
impl SimpleStorage {
pub fn new() -> SimpleStorage {
SimpleStorage { entries: Arc::new(Mutex::new(Vec::new())) }
}
pub fn has_chunk(&self, name: &[u8]) -> bool {
let lock = unwrap_result!(self.entries.lock());
for entry in lock.iter() {
if entry.name == name {
return true;
}
}
false
}
pub fn num_entries(&self) -> usize {
let lock = unwrap_result!(self.entries.lock());
lock.len()
}
}
impl Storage for SimpleStorage {
fn get(&self, name: &[u8]) -> Vec<u8> {
let lock = unwrap_result!(self.entries.lock());
for entry in lock.iter() {
if entry.name == name {
return entry.data.to_vec();
}
}
vec![]
}
fn put(&self, name: Vec<u8>, data: Vec<u8>) {
let mut lock = unwrap_result!(self.entries.lock());
lock.push(Entry {
name: name,
data: data,
})
}
}