Skip to content

Commit 25839dd

Browse files
Rollup merge of #53344 - frewsxcv:frewsxcv-doc-ptr, r=ollie27
Add doc examples for std::alloc::{alloc,alloc_zeroed}. None
2 parents 6bea743 + d1193bf commit 25839dd

File tree

1 file changed

+31
-0
lines changed

1 file changed

+31
-0
lines changed

src/liballoc/alloc.rs

+31
Original file line numberDiff line numberDiff line change
@@ -56,6 +56,22 @@ pub struct Global;
5656
/// # Safety
5757
///
5858
/// See [`GlobalAlloc::alloc`].
59+
///
60+
/// # Examples
61+
///
62+
/// ```
63+
/// use std::alloc::{alloc, dealloc, Layout};
64+
///
65+
/// unsafe {
66+
/// let layout = Layout::new::<u16>();
67+
/// let ptr = alloc(layout);
68+
///
69+
/// *(ptr as *mut u16) = 42;
70+
/// assert_eq!(*(ptr as *mut u16), 42);
71+
///
72+
/// dealloc(ptr, layout);
73+
/// }
74+
/// ```
5975
#[stable(feature = "global_alloc", since = "1.28.0")]
6076
#[inline]
6177
pub unsafe fn alloc(layout: Layout) -> *mut u8 {
@@ -110,6 +126,21 @@ pub unsafe fn realloc(ptr: *mut u8, layout: Layout, new_size: usize) -> *mut u8
110126
/// # Safety
111127
///
112128
/// See [`GlobalAlloc::alloc_zeroed`].
129+
///
130+
/// # Examples
131+
///
132+
/// ```
133+
/// use std::alloc::{alloc_zeroed, dealloc, Layout};
134+
///
135+
/// unsafe {
136+
/// let layout = Layout::new::<u16>();
137+
/// let ptr = alloc_zeroed(layout);
138+
///
139+
/// assert_eq!(*(ptr as *mut u16), 0);
140+
///
141+
/// dealloc(ptr, layout);
142+
/// }
143+
/// ```
113144
#[stable(feature = "global_alloc", since = "1.28.0")]
114145
#[inline]
115146
pub unsafe fn alloc_zeroed(layout: Layout) -> *mut u8 {

0 commit comments

Comments
 (0)