Skip to content

Commit

Permalink
rustdoc: Add some basic Safety sections to unsafe functions (#31639)
Browse files Browse the repository at this point in the history
  • Loading branch information
mrobinson committed Mar 13, 2024
1 parent 0860deb commit 38db1a5
Show file tree
Hide file tree
Showing 3 changed files with 22 additions and 0 deletions.
12 changes: 12 additions & 0 deletions components/allocator/lib.rs
Original file line number Diff line number Diff line change
Expand Up @@ -16,6 +16,10 @@ mod platform {
pub use jemallocator::Jemalloc as Allocator;

/// Get the size of a heap block.
///
/// # Safety
///
/// Passing a non-heap allocated pointer to this function results in undefined behavior.
pub unsafe extern "C" fn usable_size(ptr: *const c_void) -> usize {
jemallocator::usable_size(ptr)
}
Expand All @@ -35,6 +39,10 @@ mod platform {
use std::os::raw::c_void;

/// Get the size of a heap block.
///
/// # Safety
///
/// Passing a non-heap allocated pointer to this function results in undefined behavior.
pub unsafe extern "C" fn usable_size(ptr: *const c_void) -> usize {
#[cfg(target_vendor = "apple")]
return libc::malloc_size(ptr);
Expand All @@ -56,6 +64,10 @@ mod platform {
use winapi::um::heapapi::{GetProcessHeap, HeapSize, HeapValidate};

/// Get the size of a heap block.
///
/// # Safety
///
/// Passing a non-heap allocated pointer to this function results in undefined behavior.
pub unsafe extern "C" fn usable_size(mut ptr: *const c_void) -> usize {
let heap = GetProcessHeap();

Expand Down
5 changes: 5 additions & 0 deletions components/gfx/text/shaping/harfbuzz.rs
Original file line number Diff line number Diff line change
Expand Up @@ -47,6 +47,11 @@ pub struct ShapedGlyphEntry {
}

impl ShapedGlyphData {
/// Create a new [`SharedGlyphData`] from the given HarfBuzz buffer.
///
/// # Safety
///
/// Passing an invalid buffer pointer to this function results in undefined behavior.
pub unsafe fn new(buffer: *mut hb_buffer_t) -> ShapedGlyphData {
let mut glyph_count = 0;
let glyph_infos = hb_buffer_get_glyph_infos(buffer, &mut glyph_count);
Expand Down
5 changes: 5 additions & 0 deletions components/shared/canvas/webgl.rs
Original file line number Diff line number Diff line change
Expand Up @@ -546,6 +546,11 @@ macro_rules! define_resource_id {
impl $name {
#[allow(unsafe_code)]
#[inline]
/// Create a new $name.
///
/// # Safety
///
/// Using an invalid OpenGL id may result in undefined behavior.
pub unsafe fn new(id: $type) -> Self {
$name(<nonzero_type!($type)>::new_unchecked(id))
}
Expand Down

0 comments on commit 38db1a5

Please sign in to comment.