Struct PageRef

Source
pub struct PageRef<H, T> { /* private fields */ }
Expand description

A mutable pointer to a dynamically-sized heap-backed data page comprising a user-chosen header and data array packed into a single allocation. The internal representation is a NonNull.

§Example

use pages::Page;
use core::mem::MaybeUninit;
// A really crappy replacement for Box<Option<usize>>
struct Maybe(Page::<bool, usize>);
impl Maybe {
    fn new() -> Self { Maybe(Page::new(false, 1)) }
    fn put(&mut self, value: usize) {
        *self.0.header_mut() = true; // occupied
        unsafe { self.0.data().write(MaybeUninit::new(value)) };
    }
    fn get(&mut self) -> Option<usize> {
        if !(*self.0.header()) { return None; }
        *self.0.header_mut() = false; // free
        Some(unsafe { self.0.data().read().assume_init() })
    }
}

let mut maybe = Maybe::new();
assert_eq!(maybe.get(), None);
maybe.put(42);
assert_eq!(maybe.get(), Some(42));

§Notes

Data is exposed as a MaybeUninit pointer for maximum flexibility. Unfortunately this means we’re unable to automatically drop the data for you in our destructor. You could cause a memory leak if you don’t.

Implementations§

Source§

impl<H, T> PageRef<H, T>

Source

pub fn new(header: H, items: u32) -> Self

Creates a new PageRef on the heap with the provided header and capacity for items items.

§Notes

Will panic if items is 0 or the header plus padding is extremely large (u32::MAX - 8 bytes)

Source

pub unsafe fn capacity(self) -> u32

The capacity of this page’s data array.

§Safety

You must synchronise all reads and writes.

Source

pub unsafe fn header(&self) -> &H

Access to this page’s header by reference.

§Safety

You must synchronise all reads and writes.

Source

pub unsafe fn header_mut(&mut self) -> &mut H

Access to this page’s header by mut reference.

§Safety

You must synchronise all reads and writes.

Source

pub unsafe fn data(self) -> *mut MaybeUninit<T>

Access to the start of the data array for this page as a mut pointer.

§Safety

You must synchronise all reads and writes.

Source

pub unsafe fn layout(self) -> PageLayout<H, T>

Returns this page’s layout information as a PageLayout.

§Safety

You must synchronise all reads and writes.

Source

pub unsafe fn drop(page: Self)

Drops the page pointed to by the provided PageRef

§Safety

You must no longer access this page via other PageRefs.

Source

pub unsafe fn from_uninit( raw_ptr: *mut u8, header: H, layout: PageLayout<H, T>, ) -> Self

Creates a new PageRef from a pointer to uninitialised memory, a header and a PageLayout.

§Safety

The pointer must have been allocated according to the provided PageLayout.

Trait Implementations§

Source§

impl<H, T> Clone for PageRef<H, T>

Source§

fn clone(&self) -> Self

Returns a duplicate of the value. Read more
1.0.0 · Source§

fn clone_from(&mut self, source: &Self)

Performs copy-assignment from source. Read more
Source§

impl<H, T> Debug for PageRef<H, T>

Source§

fn fmt(&self, fmt: &mut Formatter<'_>) -> Result

Formats the value using the given formatter. Read more
Source§

impl<H, T> PartialEq for PageRef<H, T>

Source§

fn eq(&self, other: &Self) -> bool

Tests for self and other values to be equal, and is used by ==.
1.0.0 · Source§

fn ne(&self, other: &Rhs) -> bool

Tests for !=. The default implementation is almost always sufficient, and should not be overridden without very good reason.
Source§

impl<H, T> Copy for PageRef<H, T>

Source§

impl<H, T> Eq for PageRef<H, T>

Auto Trait Implementations§

§

impl<H, T> Freeze for PageRef<H, T>

§

impl<H, T> RefUnwindSafe for PageRef<H, T>

§

impl<H, T> !Send for PageRef<H, T>

§

impl<H, T> !Sync for PageRef<H, T>

§

impl<H, T> Unpin for PageRef<H, T>
where H: Unpin, T: Unpin,

§

impl<H, T> UnwindSafe for PageRef<H, T>
where H: UnwindSafe, T: UnwindSafe,

Blanket Implementations§

Source§

impl<T> Any for T
where T: 'static + ?Sized,

Source§

fn type_id(&self) -> TypeId

Gets the TypeId of self. Read more
Source§

impl<T> Borrow<T> for T
where T: ?Sized,

Source§

fn borrow(&self) -> &T

Immutably borrows from an owned value. Read more
Source§

impl<T> BorrowMut<T> for T
where T: ?Sized,

Source§

fn borrow_mut(&mut self) -> &mut T

Mutably borrows from an owned value. Read more
Source§

impl<T> CloneToUninit for T
where T: Clone,

Source§

unsafe fn clone_to_uninit(&self, dest: *mut u8)

🔬This is a nightly-only experimental API. (clone_to_uninit)
Performs copy-assignment from self to dest. Read more
Source§

impl<T> From<T> for T

Source§

fn from(t: T) -> T

Returns the argument unchanged.

Source§

impl<T, U> Into<U> for T
where U: From<T>,

Source§

fn into(self) -> U

Calls U::from(self).

That is, this conversion is whatever the implementation of From<T> for U chooses to do.

Source§

impl<T> ToOwned for T
where T: Clone,

Source§

type Owned = T

The resulting type after obtaining ownership.
Source§

fn to_owned(&self) -> T

Creates owned data from borrowed data, usually by cloning. Read more
Source§

fn clone_into(&self, target: &mut T)

Uses borrowed data to replace owned data, usually by cloning. Read more
Source§

impl<T, U> TryFrom<U> for T
where U: Into<T>,

Source§

type Error = Infallible

The type returned in the event of a conversion error.
Source§

fn try_from(value: U) -> Result<T, <T as TryFrom<U>>::Error>

Performs the conversion.
Source§

impl<T, U> TryInto<U> for T
where U: TryFrom<T>,

Source§

type Error = <U as TryFrom<T>>::Error

The type returned in the event of a conversion error.
Source§

fn try_into(self) -> Result<U, <U as TryFrom<T>>::Error>

Performs the conversion.