--- id: "6ab7598b0c39e0a2a38f6efa" created: "2026-09-26T14:35:10+09:00" updated: "2026-11-21T22:33:53+09:00" --- allocator_api https://github.com/rust-lang/rust/pull/156882 [Rust]の[nightly feature] https://doc.rust-lang.org/beta/unstable-book/library-features/allocator-api.html https://github.com/rust-lang/rust/issues/32838 ガチ10年来の悲願...! [Rust Project Goals 2026]に含まれていた 2026-09-26 stabilizeされた!🎉 [allocator_ext]に切り出されたみたい [Rust 1.100]でリリースされる!うおおおおおおおおおおおおおおお 今はStableが[Rust 1.98]、Betaが[Rust 1.99]、Nightlyで使える https://doc.rust-lang.org/std/alloc/trait.Allocator.html code:allocator.rs unsafe trait Allocator { fn allocate( &self, layout: Layout, ) -> Result, AllocError>; unsafe fn deallocate( &self, ptr: NonNull, layout: Layout, ); fn allocate_zeroed(...); unsafe fn grow(...); unsafe fn grow_zeroed(...); unsafe fn shrink(...); } うおおおおおおおおおおおおおおおおおおおお [GlobalAlloc] / [global_allocator]と違って、`Box`とか`Vec`ごとにアロケーターを書ける話! プログラム全体を決めるからね code:global_allocator.rs unsafe trait GlobalAlloc { unsafe fn alloc(&self, layout: Layout) -> *mut u8; unsafe fn dealloc(&self, ptr: *mut u8, layout: Layout); unsafe fn alloc_zeroed(...) -> *mut u8; unsafe fn realloc(...) -> *mut u8; } `Result, AllocError>`なのいいね GlobalAllocは`*mut u8`、OOMはnull pointerで表す感じになる