std::intrinsics::align_offset - Rust
Description: API documentation for the Rust `align_offset` fn in crate `std`.
Keywords: rust, rustlang, rust-lang, align_offset

# Function std::intrinsics::align_offset [−] [src]

    pub unsafe extern "rust-intrinsic" fn align_offset(
        ptr: *const (),
        align: usize
    ) -> usize

🔬 This is a nightly-only experimental API. (`core_intrinsics`)

intrinsics are unlikely to ever be stabilized, instead they should be used through stabilized interfaces in the rest of the standard library

Computes the byte offset that needs to be applied to `ptr` in order to make it aligned to `align`. If it is not possible to align `ptr`, the implementation returns `usize::max_value()`.

There are no guarantees whatsover that offsetting the pointer will not overflow or go beyond the allocation that `ptr` points into. It is up to the caller to ensure that the returned offset is correct in all terms other than alignment.

# Examples

Accessing adjacent `u8` as `u16`

    let x = [5u8, 6u8, 7u8, 8u8, 9u8];
    let ptr = &x[n] as *const u8;
    let offset = align_offset(ptr as *const (), align_of::<u16>());
    if offset < x.len() - n - 1 {
        let u16_ptr = ptr.offset(offset as isize) as *const u16;
        assert_ne!(*u16_ptr, 500);
    } else {
        // while the pointer can be aligned via `offset`, it would point
        // outside the allocation
    }Run

# Help

## Keyboard Shortcuts

?
  Show this help dialog
S
  Focus the search field
↑
  Move up in search results
↓
  Move down in search results
↹
  Switch tab
⏎
  Go to active search result
+ / -
  Collapse/expand all sections

## Search Tricks

Prefix searches with a type followed by a colon (e.g. `fn:`) to restrict the search to a given type.

Accepted types are: `fn`, `mod`, `struct`, `enum`, `trait`, `type`, `macro`, and `const`.

Search functions by type signature (e.g. `vec -> usize` or `* -> vec`)