1
Fork 0

doc(ptr): add example for {read,write}_unaligned

Signed-off-by: Freyskeyd <simon.paitrault@gmail.com>
This commit is contained in:
Freyskeyd 2019-07-08 16:03:29 +02:00
parent 10840b8ae2
commit bc322af444
No known key found for this signature in database
GPG key ID: 5A645DEDF790F113

View file

@ -669,6 +669,22 @@ pub unsafe fn read<T>(src: *const T) -> T {
/// ///
/// Accessing unaligned fields directly with e.g. `packed.unaligned` is safe however. /// Accessing unaligned fields directly with e.g. `packed.unaligned` is safe however.
// FIXME: Update docs based on outcome of RFC #2582 and friends. // FIXME: Update docs based on outcome of RFC #2582 and friends.
///
/// # Examples
///
/// Read an usize value from a byte buffer:
///
/// ```
/// use std::mem;
///
/// fn read_usize(x: &[u8]) -> usize {
/// assert!(x.len() >= mem::size_of::<usize>());
///
/// let ptr = x.as_ptr() as *const usize;
///
/// unsafe { ptr.read_unaligned() }
/// }
/// ```
#[inline] #[inline]
#[stable(feature = "ptr_unaligned", since = "1.17.0")] #[stable(feature = "ptr_unaligned", since = "1.17.0")]
pub unsafe fn read_unaligned<T>(src: *const T) -> T { pub unsafe fn read_unaligned<T>(src: *const T) -> T {
@ -839,6 +855,22 @@ pub unsafe fn write<T>(dst: *mut T, src: T) {
/// ///
/// Accessing unaligned fields directly with e.g. `packed.unaligned` is safe however. /// Accessing unaligned fields directly with e.g. `packed.unaligned` is safe however.
// FIXME: Update docs based on outcome of RFC #2582 and friends. // FIXME: Update docs based on outcome of RFC #2582 and friends.
///
/// # Examples
///
/// Write an usize value to a byte buffer:
///
/// ```
/// use std::mem;
///
/// fn write_usize(x: &mut [u8], val: usize) {
/// assert!(x.len() >= mem::size_of::<usize>());
///
/// let ptr = x.as_mut_ptr() as *mut usize;
///
/// unsafe { ptr.write_unaligned(val) }
/// }
/// ```
#[inline] #[inline]
#[stable(feature = "ptr_unaligned", since = "1.17.0")] #[stable(feature = "ptr_unaligned", since = "1.17.0")]
pub unsafe fn write_unaligned<T>(dst: *mut T, src: T) { pub unsafe fn write_unaligned<T>(dst: *mut T, src: T) {