use pref::PRef;
use byteorder::{ByteOrder, BigEndian};
pub const PAGE_SIZE: usize = 4096;
pub const PAGE_PAYLOAD_SIZE: usize = 4090;
#[derive(Clone)]
pub struct Page {
content: [u8; PAGE_SIZE],
}
impl Page {
pub fn new (pref: PRef) -> Page {
let mut page = Page{ content: [0u8; PAGE_SIZE] };
page.write_pref(PAGE_PAYLOAD_SIZE, pref);
page
}
pub fn from_buf (content: [u8; PAGE_SIZE]) -> Page {
Page{ content }
}
pub fn pref (&self) -> PRef {
self.read_pref(PAGE_PAYLOAD_SIZE)
}
pub fn write(&mut self, pos: usize, slice: &[u8]) {
self.content[pos .. pos + slice.len()].copy_from_slice(slice)
}
pub fn read (&self, pos: usize, buf: &mut [u8]) {
let len = buf.len();
buf.copy_from_slice(&self.content[pos .. pos+len])
}
pub fn write_pref(&mut self, pos: usize, pref: PRef) {
let mut buf = [0u8; 6];
BigEndian::write_u48(&mut buf, pref.as_u64());
self.content[pos..pos+6].copy_from_slice(&buf[..]);
}
pub fn read_pref(&self, pos: usize) -> PRef {
PRef::from(BigEndian::read_u48(&self.content[pos..pos+6]))
}
pub fn write_u64 (&mut self, pos: usize, n: u64) {
let mut buf = [0u8; 8];
BigEndian::write_u64(&mut buf, n);
self.content[pos..pos+8].copy_from_slice(&buf[..]);
}
pub fn read_u64(&self, pos: usize) -> u64 {
BigEndian::read_u64(&self.content[pos..pos+8])
}
pub fn into_buf (self) -> [u8; PAGE_SIZE] {
self.content
}
}