'Rust: allocator API: expected struct `std::alloc::Global` when I provide my own allocator

I want to use my own allocator in alloc::vec::Vec::new_in. However, Rust tells me "expected struct std::alloc::Global". How can I use my own allocator? I'm on Rust nightly 1.60.

#![feature(allocator_api)]

use std::alloc::{Allocator, AllocError, Layout};
use std::ptr::NonNull;

struct PageAlignedAlloc;

unsafe impl Allocator for PageAlignedAlloc {
    fn allocate(&self, layout: Layout) -> Result<NonNull<[u8]>, AllocError> {
        std::alloc::System.allocate(layout.align_to(4096).unwrap())
    }

    unsafe fn deallocate(&self, ptr: NonNull<u8>, layout: Layout) {
        std::alloc::System.deallocate(ptr, layout)
    }
}

fn main() {
    let vec = Vec::<u8>::new_in(PageAlignedAlloc);
}


Sources

This article follows the attribution requirements of Stack Overflow and is licensed under CC BY-SA 3.0.

Source: Stack Overflow

Solution Source