Struct pages::PageRef[][src]

#[repr(transparent)]
pub struct PageRef<H, T> { /* fields omitted */ }
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

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)

The capacity of this page’s data array.

Safety

You must synchronise all reads and writes.

Access to this page’s header by reference.

Safety

You must synchronise all reads and writes.

Access to this page’s header by mut reference.

Safety

You must synchronise all reads and writes.

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

Safety

You must synchronise all reads and writes.

Returns this page’s layout information as a PageLayout.

Safety

You must synchronise all reads and writes.

Drops the page pointed to by the provided PageRef

Safety

You must no longer access this page via other PageRefs.

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

Returns a copy of the value. Read more

Performs copy-assignment from source. Read more

Formats the value using the given formatter. Read more

This method tests for self and other values to be equal, and is used by ==. Read more

This method tests for !=.

Auto Trait Implementations

Blanket Implementations

Gets the TypeId of self. Read more

Immutably borrows from an owned value. Read more

Mutably borrows from an owned value. Read more

Performs the conversion.

Performs the conversion.

The resulting type after obtaining ownership.

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

🔬 This is a nightly-only experimental API. (toowned_clone_into)

recently added

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

The type returned in the event of a conversion error.

Performs the conversion.

The type returned in the event of a conversion error.

Performs the conversion.