diff options
| author | Lucas Dumont <contact@lucasdumont.com> | 2022-06-07 14:45:25 +0200 | 
|---|---|---|
| committer | Lucas Dumont <contact@lucasdumont.com> | 2022-06-07 15:06:18 +0200 | 
| commit | 5adef6c79598bc7dbfa3882739a6e508559cae42 (patch) | |
| tree | c34085391604d203bfcb8acd12e3a8058c2d0133 /library/std/src/alloc.rs | |
| parent | 91cacb3faf987805675e39aca41859ec1fcabef3 (diff) | |
| download | rust-5adef6c79598bc7dbfa3882739a6e508559cae42.tar.gz rust-5adef6c79598bc7dbfa3882739a6e508559cae42.zip | |
Add std::alloc::set_alloc_error_hook example
Diffstat (limited to 'library/std/src/alloc.rs')
| -rw-r--r-- | library/std/src/alloc.rs | 14 | 
1 files changed, 14 insertions, 0 deletions
| diff --git a/library/std/src/alloc.rs b/library/std/src/alloc.rs index 63c527b64da..d3879273f5b 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); | 
