From 5adef6c79598bc7dbfa3882739a6e508559cae42 Mon Sep 17 00:00:00 2001 From: Lucas Dumont Date: Tue, 7 Jun 2022 14:45:25 +0200 Subject: [PATCH] Add std::alloc::set_alloc_error_hook example --- library/std/src/alloc.rs | 14 ++++++++++++++ 1 file changed, 14 insertions(+) diff --git a/library/std/src/alloc.rs b/library/std/src/alloc.rs index 63c527b64da48..d3879273f5b03 100644 --- a/library/std/src/alloc.rs +++ b/library/std/src/alloc.rs @@ -296,6 +296,20 @@ static HOOK: AtomicPtr<()> = AtomicPtr::new(ptr::null_mut()); /// about the allocation that failed. /// /// The allocation error hook is a global resource. +/// +/// # Examples +/// +/// ``` +/// #![feature(alloc_error_hook)] +/// +/// use std::alloc::{Layout, set_alloc_error_hook}; +/// +/// fn custom_alloc_error_hook(layout: Layout) { +/// panic!("memory allocation of {} bytes failed", layout.size()); +/// } +/// +/// set_alloc_error_hook(custom_alloc_error_hook); +/// ``` #[unstable(feature = "alloc_error_hook", issue = "51245")] pub fn set_alloc_error_hook(hook: fn(Layout)) { HOOK.store(hook as *mut (), Ordering::SeqCst);