Struct pages::Page[][src]

#[repr(transparent)]
pub struct Page<H, T> { /* fields omitted */ }
Expand description

A dynamically-sized heap-backed data page. Comprises a user-chosen header and data array packed into a single allocation. It is an owned object and the internal representation is a NonNull.

Example

use pages::Page;
// 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
        let item: *mut usize = self.0.data_mut()[0].as_mut_ptr();
        unsafe { item.write(value); }
    }
    fn get(&mut self) -> Option<usize> {
        if !(*self.0.header()) { return None; }
        let item: *mut usize = self.0.data_mut()[0].as_mut_ptr();
        *self.0.header_mut() = false; // free
        Some(unsafe { *item })
    }
}

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 slice 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 Page with the capacity for the provided number of items on the heap and a header.

Notes

Will panic if the header plus padding is extremely large (around 2^32 bytes)

The capacity of our data array.

Access to the header by reference.

Access to the header by mut reference.

Access to the raw data as a slice.

Access to the raw data as a mut slice.

Trait Implementations

Formats the value using the given formatter. Read more

Executes the destructor for this type. Read more

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 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.