[][src]Struct alloc_compose::Fallback

pub struct Fallback<Primary, Secondary> {
    pub primary: Primary,
    pub secondary: Secondary,
}

An allocator equivalent of an "or" operator in algebra.

An allocation request is first attempted with the Primary allocator. If that fails, the request is forwarded to the Fallback allocator. All other requests are dispatched appropriately to one of the two allocators.

A Fallback is useful for fast, special-purpose allocators backed up by general-purpose allocators like Global or System.

Example

#![feature(allocator_api, slice_ptr_get)]

use alloc_compose::{region::Region, Fallback, Owns};
use std::{
    alloc::{AllocRef, Layout, System},
    mem::MaybeUninit,
};

let mut data = [MaybeUninit::new(0); 32];
let mut alloc = Fallback {
    primary: Region::new(&mut data),
    secondary: System,
};

let small_memory = alloc.alloc(Layout::new::<u32>())?;
let big_memory = alloc.alloc(Layout::new::<[u32; 64]>())?;

assert!(alloc.primary.owns(small_memory));
assert!(!alloc.primary.owns(big_memory));

unsafe {
    // `big_memory` was allocated from `System`, we can dealloc it directly
    System.dealloc(big_memory.as_non_null_ptr(), Layout::new::<[u32; 64]>());
    alloc.dealloc(small_memory.as_non_null_ptr(), Layout::new::<u32>());
};

Fields

primary: Primary

The primary allocator

secondary: Secondary

The fallback allocator

Trait Implementations

impl<Primary, Secondary> AllocRef for Fallback<Primary, Secondary> where
    Primary: AllocRef + Owns,
    Secondary: AllocRef
[src]

impl<Primary: Clone, Secondary: Clone> Clone for Fallback<Primary, Secondary>[src]

impl<Primary: Copy, Secondary: Copy> Copy for Fallback<Primary, Secondary>[src]

impl<Primary: Debug, Secondary: Debug> Debug for Fallback<Primary, Secondary>[src]

impl<Primary, Secondary> Owns for Fallback<Primary, Secondary> where
    Primary: Owns,
    Secondary: Owns
[src]

Auto Trait Implementations

impl<Primary, Secondary> Send for Fallback<Primary, Secondary> where
    Primary: Send,
    Secondary: Send

impl<Primary, Secondary> Sync for Fallback<Primary, Secondary> where
    Primary: Sync,
    Secondary: Sync

impl<Primary, Secondary> Unpin for Fallback<Primary, Secondary> where
    Primary: Unpin,
    Secondary: Unpin

Blanket Implementations

impl<T> Any for T where
    T: 'static + ?Sized
[src]

impl<T> Borrow<T> for T where
    T: ?Sized
[src]

impl<T> BorrowMut<T> for T where
    T: ?Sized
[src]

impl<T> From<T> for T[src]

impl<T, U> Into<U> for T where
    U: From<T>, 
[src]

impl<T> ToOwned for T where
    T: Clone
[src]

type Owned = T

The resulting type after obtaining ownership.

impl<T, U> TryFrom<U> for T where
    U: Into<T>, 
[src]

type Error = Infallible

The type returned in the event of a conversion error.

impl<T, U> TryInto<U> for T where
    U: TryFrom<T>, 
[src]

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

The type returned in the event of a conversion error.