memory_pager/
page.rs

1use std::ops::{Deref, DerefMut};
2
3/// Memory pages returned by [`Pager`].
4///
5/// [`Pager`]: struct.Pager.html
6#[derive(Debug, Clone)]
7pub struct Page {
8  /// Byte offset for the start of the `Page` relative to all other `Page`
9  /// instances.
10  offset: usize,
11  /// Buffer with capacity of size [`page_size`].
12  ///
13  /// [`page_size`]: struct.Pager.html#structfield.page_size
14  buffer: Vec<u8>,
15}
16
17impl Page {
18  /// Create a new instance.
19  pub fn new(index: usize, buf: Vec<u8>) -> Self {
20    Page {
21      offset: index * buf.capacity(),
22      buffer: buf,
23    }
24  }
25
26  /// Byte offset for the start of the `Page` relative to all other `Page`
27  /// instances.
28  pub fn offset(&self) -> usize {
29    self.offset
30  }
31}
32
33impl Deref for Page {
34  type Target = [u8];
35  fn deref(&self) -> &Self::Target {
36    &self.buffer
37  }
38}
39
40impl DerefMut for Page {
41  fn deref_mut(&mut self) -> &mut Self::Target {
42    &mut self.buffer
43  }
44}