Change:
/// /// # Example /// <del> /// ```no_run /// use std::alloc::{GlobalAlloc, Layout, alloc}; </del> <ins> /// ``` /// use std::alloc::{GlobalAlloc, Layout}; /// use std::cell::UnsafeCell; </ins> /// use std::ptr::null_mut; <ins> /// use std::sync::atomic::{ ///     AtomicUsize, ///     Ordering::{Acquire, SeqCst}, /// }; </ins> /// <del> /// struct MyAllocator; /// /// unsafe impl GlobalAlloc for MyAllocator { ///     unsafe fn alloc(&self, _layout: Layout) -> *mut u8 { null_mut() } ///     unsafe fn dealloc(&self, _ptr: *mut u8, _layout: Layout) {} </del> <ins> /// const ARENA_SIZE: usize = 128 * 1024; /// const MAX_SUPPORTED_ALIGN: usize = 4096; /// #[repr(C, align(4096))] // 4096 == MAX_SUPPORTED_ALIGN /// struct SimpleAllocator { ///     arena: UnsafeCell<[u8; ARENA_SIZE]>, ///     remaining: AtomicUsize, // we allocate from the top, counting down </ins> /// } /// /// #[global_allocator] <del> /// static A: MyAllocator = MyAllocator; </del> <ins> /// static ALLOCATOR: SimpleAllocator = SimpleAllocator { ///     arena: UnsafeCell::new([0x55; ARENA_SIZE]), ///     remaining: AtomicUsize::new(ARENA_SIZE), /// }; </ins> /// <del> /// fn main() { ///     unsafe { ///         assert!(alloc(Layout::new::<u32>()).is_null()) </del> <ins> /// unsafe impl Sync for SimpleAllocator {} /// /// unsafe impl GlobalAlloc for SimpleAllocator { ///     unsafe fn alloc(&self, layout: Layout) -> *mut u8 { ///         let size = layout.size(); ///         let align = layout.align(); /// ///         // `Layout` contract forbids making a `Layout` with align=0, or align not power of 2. ///         // So we can safely use a mask to ensure alignment without worrying about UB. ///         let align_mask_to_round_down = !(align - 1); /// ///         if align > MAX_SUPPORTED_ALIGN { ///             return null_mut(); ///         } /// ///         let mut allocated = 0; ///         if self ///             .remaining ///             .fetch_update(SeqCst, SeqCst, |mut remaining| { ///                 if size > remaining { ///                     return None; ///                 } ///                 remaining -= size; ///                 remaining &= align_mask_to_round_down; ///                 allocated = remaining; ///                 Some(remaining) ///             }) ///             .is_err() ///         { ///             return null_mut(); ///         }; ///         (self.arena.get() as *mut u8).add(allocated) </ins> ///     } <ins> ///     unsafe fn dealloc(&self, _ptr: *mut u8, _layout: Layout) {} /// } /// /// fn main() { ///     let _s = format!("allocating a string!"); ///     let currently = ALLOCATOR.remaining.load(Acquire); ///     println!("allocated so far: {}", ARENA_SIZE - currently); </ins> /// } /// ``` ///

Discussion:
<!-- Thank you for filing a bug report! Please provide a short summary of the bug, along with any information you feel relevant to replicating the bug. --I cut and pasted from the stdlib docs into the playground. Contrary to the implications in the documentation, the program does not run to completion. Instead, it prints I think this is fine behaviour. The example is wrong and should be fixed somehow. <strikeI was doing this because I am trying to write a test case for the stdlib which involves a stunt allocator. So far my annotation in a file in does not seem to be effective. I suspect that whatever is getting in my way might also get in the way of adding a test case specifically for .</strikeLooks like is the right place and I should read the dev docs... The works correctly. <!-- TRIAGEBOTSTART --<!-- TRIAGEBOTASSIGNSTART --<!-- TRIAGEBOTASSIGNDATASTARTTRIAGEBOTASSIGNDATAEND --<!-- TRIAGEBOTASSIGNEND --<!-- TRIAGEBOTEND --
modify labels +T-libs
claim
My $.02 is that is a bit too involved for an example. The existing example can be fixed by delegating to the allocator up until runs, at which point the sample allocator can take effect. Something like .