@@ -417,8 +417,9 @@ impl<T: ?Sized> *const T {
417
417
/// If any of the following conditions are violated, the result is Undefined
418
418
/// Behavior:
419
419
///
420
- /// * Both the starting and resulting pointer must be either in bounds or one
421
- /// byte past the end of the same [allocated object].
420
+ /// * If the computed offset is non-zero, then both the starting and resulting pointer must be
421
+ /// either in bounds or one byte past the end of the same [allocated object].
422
+ /// (If it is zero, then the function is allays well-defined.)
422
423
///
423
424
/// * The computed offset, **in bytes**, cannot overflow an `isize`.
424
425
///
@@ -628,11 +629,11 @@ impl<T: ?Sized> *const T {
628
629
/// If any of the following conditions are violated, the result is Undefined
629
630
/// Behavior:
630
631
///
631
- /// * Both `self` and `origin` must be either in bounds or one
632
- /// byte past the end of the same [allocated object].
632
+ /// * `self` and `origin` must either
633
633
///
634
- /// * Both pointers must be *derived from* a pointer to the same object.
635
- /// (See below for an example.)
634
+ /// * both be *derived from* a pointer to the same [allocated object], and they must be either
635
+ /// in bounds or one byte past the end of that object. (See below for an example.)
636
+ /// * or both be derived from an integer literal/constant, and point to the same address.
636
637
///
637
638
/// * The distance between the pointers, in bytes, must be an exact multiple
638
639
/// of the size of `T`.
@@ -695,14 +696,14 @@ impl<T: ?Sized> *const T {
695
696
/// let ptr1 = Box::into_raw(Box::new(0u8)) as *const u8;
696
697
/// let ptr2 = Box::into_raw(Box::new(1u8)) as *const u8;
697
698
/// let diff = (ptr2 as isize).wrapping_sub(ptr1 as isize);
698
- /// // Make ptr2_other an "alias" of ptr2, but derived from ptr1.
699
- /// let ptr2_other = (ptr1 as *const u8).wrapping_offset(diff);
699
+ /// // Make ptr2_other an "alias" of ptr2.add(1) , but derived from ptr1.
700
+ /// let ptr2_other = (ptr1 as *const u8).wrapping_offset(diff).wrapping_offset(1) ;
700
701
/// assert_eq!(ptr2 as usize, ptr2_other as usize);
701
702
/// // Since ptr2_other and ptr2 are derived from pointers to different objects,
702
703
/// // computing their offset is undefined behavior, even though
703
- /// // they point to the same address !
704
+ /// // they point to addresses that are in-bounds of the same object !
704
705
/// unsafe {
705
- /// let zero = ptr2_other.offset_from(ptr2); // Undefined Behavior
706
+ /// let one = ptr2_other.offset_from(ptr2); // Undefined Behavior
706
707
/// }
707
708
/// ```
708
709
#[ stable( feature = "ptr_offset_from" , since = "1.47.0" ) ]
@@ -894,8 +895,9 @@ impl<T: ?Sized> *const T {
894
895
/// If any of the following conditions are violated, the result is Undefined
895
896
/// Behavior:
896
897
///
897
- /// * Both the starting and resulting pointer must be either in bounds or one
898
- /// byte past the end of the same [allocated object].
898
+ /// * If the computed offset is non-zero, then both the starting and resulting pointer must be
899
+ /// either in bounds or one byte past the end of the same [allocated object].
900
+ /// (If it is zero, then the function is allays well-defined.)
899
901
///
900
902
/// * The computed offset, **in bytes**, cannot overflow an `isize`.
901
903
///
@@ -978,8 +980,9 @@ impl<T: ?Sized> *const T {
978
980
/// If any of the following conditions are violated, the result is Undefined
979
981
/// Behavior:
980
982
///
981
- /// * Both the starting and resulting pointer must be either in bounds or one
982
- /// byte past the end of the same [allocated object].
983
+ /// * If the computed offset is non-zero, then both the starting and resulting pointer must be
984
+ /// either in bounds or one byte past the end of the same [allocated object].
985
+ /// (If it is zero, then the function is allays well-defined.)
983
986
///
984
987
/// * The computed offset cannot exceed `isize::MAX` **bytes**.
985
988
///
0 commit comments