File tree 1 file changed +32
-0
lines changed
1 file changed +32
-0
lines changed Original file line number Diff line number Diff line change @@ -669,6 +669,22 @@ pub unsafe fn read<T>(src: *const T) -> T {
669
669
///
670
670
/// Accessing unaligned fields directly with e.g. `packed.unaligned` is safe however.
671
671
// FIXME: Update docs based on outcome of RFC #2582 and friends.
672
+ ///
673
+ /// # Examples
674
+ ///
675
+ /// Read an usize value from a byte buffer:
676
+ ///
677
+ /// ```
678
+ /// use std::mem;
679
+ ///
680
+ /// fn read_usize(x: &[u8]) -> usize {
681
+ /// assert!(x.len() >= mem::size_of::<usize>());
682
+ ///
683
+ /// let ptr = x.as_ptr() as *const usize;
684
+ ///
685
+ /// unsafe { ptr.read_unaligned() }
686
+ /// }
687
+ /// ```
672
688
#[ inline]
673
689
#[ stable( feature = "ptr_unaligned" , since = "1.17.0" ) ]
674
690
pub unsafe fn read_unaligned < T > ( src : * const T ) -> T {
@@ -839,6 +855,22 @@ pub unsafe fn write<T>(dst: *mut T, src: T) {
839
855
///
840
856
/// Accessing unaligned fields directly with e.g. `packed.unaligned` is safe however.
841
857
// FIXME: Update docs based on outcome of RFC #2582 and friends.
858
+ ///
859
+ /// # Examples
860
+ ///
861
+ /// Write an usize value to a byte buffer:
862
+ ///
863
+ /// ```
864
+ /// use std::mem;
865
+ ///
866
+ /// fn write_usize(x: &mut [u8], val: usize) {
867
+ /// assert!(x.len() >= mem::size_of::<usize>());
868
+ ///
869
+ /// let ptr = x.as_mut_ptr() as *mut usize;
870
+ ///
871
+ /// unsafe { ptr.write_unaligned(val) }
872
+ /// }
873
+ /// ```
842
874
#[ inline]
843
875
#[ stable( feature = "ptr_unaligned" , since = "1.17.0" ) ]
844
876
pub unsafe fn write_unaligned < T > ( dst : * mut T , src : T ) {
You can’t perform that action at this time.
0 commit comments