@@ -669,7 +669,7 @@ impl<T: ?Sized> *const T {
669
669
/// `mem::size_of::<T>()` then the result of the division is rounded towards
670
670
/// zero.
671
671
///
672
- /// This function returns `None` if `T` is a zero-sized typed .
672
+ /// This function returns `None` if `T` is a zero-sized type .
673
673
///
674
674
/// # Examples
675
675
///
@@ -719,7 +719,7 @@ impl<T: ?Sized> *const T {
719
719
/// * The distance between the pointers, **in bytes**, cannot overflow an `isize`.
720
720
///
721
721
/// * The distance between the pointers, in bytes, must be an exact multiple
722
- /// of the size of `T` and `T` must not be a Zero-Sized Type ("ZST") .
722
+ /// of the size of `T`.
723
723
///
724
724
/// * The distance being in bounds cannot rely on "wrapping around" the address space.
725
725
///
@@ -740,6 +740,10 @@ impl<T: ?Sized> *const T {
740
740
/// difficult to satisfy. The only advantage of this method is that it
741
741
/// enables more aggressive compiler optimizations.
742
742
///
743
+ /// # Panics
744
+ ///
745
+ /// This function panics if `T` is a Zero-Sized Type ("ZST").
746
+ ///
743
747
/// # Examples
744
748
///
745
749
/// Basic usage:
@@ -759,12 +763,14 @@ impl<T: ?Sized> *const T {
759
763
/// ```
760
764
#[ unstable( feature = "ptr_offset_from" , issue = "41079" ) ]
761
765
#[ inline]
762
- pub unsafe fn offset_from ( self , other : * const T ) -> isize where T : Sized {
766
+ pub unsafe fn offset_from ( self , origin : * const T ) -> isize where T : Sized {
763
767
let pointee_size = mem:: size_of :: < T > ( ) ;
764
768
assert ! ( 0 < pointee_size && pointee_size <= isize :: max_value( ) as usize ) ;
765
769
766
- // FIXME: can this be nuw/nsw?
767
- let d = isize:: wrapping_sub ( self as _ , other as _ ) ;
770
+ // This is the same sequence that Clang emits for pointer subtraction.
771
+ // It can be neither `nsw` nor `nuw` because the input is treated as
772
+ // unsigned but then the output is treated as signed, so neither works.
773
+ let d = isize:: wrapping_sub ( self as _ , origin as _ ) ;
768
774
intrinsics:: exact_div ( d, pointee_size as _ )
769
775
}
770
776
@@ -775,9 +781,13 @@ impl<T: ?Sized> *const T {
775
781
/// `mem::size_of::<T>()` then the result of the division is rounded towards
776
782
/// zero.
777
783
///
784
+ /// Though this method is safe for any two pointers, note that its result
785
+ /// will be mostly useless if the two pointers aren't into the same allocated
786
+ /// object, for example if they point to two different local variables.
787
+ ///
778
788
/// # Panics
779
789
///
780
- /// This function panics if `T` is a zero-sized typed .
790
+ /// This function panics if `T` is a zero-sized type .
781
791
///
782
792
/// # Examples
783
793
///
@@ -800,11 +810,11 @@ impl<T: ?Sized> *const T {
800
810
/// ```
801
811
#[ unstable( feature = "ptr_wrapping_offset_from" , issue = "41079" ) ]
802
812
#[ inline]
803
- pub fn wrapping_offset_from ( self , other : * const T ) -> isize where T : Sized {
813
+ pub fn wrapping_offset_from ( self , origin : * const T ) -> isize where T : Sized {
804
814
let pointee_size = mem:: size_of :: < T > ( ) ;
805
815
assert ! ( 0 < pointee_size && pointee_size <= isize :: max_value( ) as usize ) ;
806
816
807
- let d = isize:: wrapping_sub ( self as _ , other as _ ) ;
817
+ let d = isize:: wrapping_sub ( self as _ , origin as _ ) ;
808
818
d. wrapping_div ( pointee_size as _ )
809
819
}
810
820
@@ -1424,7 +1434,7 @@ impl<T: ?Sized> *mut T {
1424
1434
/// `mem::size_of::<T>()` then the result of the division is rounded towards
1425
1435
/// zero.
1426
1436
///
1427
- /// This function returns `None` if `T` is a zero-sized typed .
1437
+ /// This function returns `None` if `T` is a zero-sized type .
1428
1438
///
1429
1439
/// # Examples
1430
1440
///
@@ -1474,7 +1484,7 @@ impl<T: ?Sized> *mut T {
1474
1484
/// * The distance between the pointers, **in bytes**, cannot overflow an `isize`.
1475
1485
///
1476
1486
/// * The distance between the pointers, in bytes, must be an exact multiple
1477
- /// of the size of `T` and `T` must not be a Zero-Sized Type ("ZST") .
1487
+ /// of the size of `T`.
1478
1488
///
1479
1489
/// * The distance being in bounds cannot rely on "wrapping around" the address space.
1480
1490
///
@@ -1495,6 +1505,10 @@ impl<T: ?Sized> *mut T {
1495
1505
/// difficult to satisfy. The only advantage of this method is that it
1496
1506
/// enables more aggressive compiler optimizations.
1497
1507
///
1508
+ /// # Panics
1509
+ ///
1510
+ /// This function panics if `T` is a Zero-Sized Type ("ZST").
1511
+ ///
1498
1512
/// # Examples
1499
1513
///
1500
1514
/// Basic usage:
@@ -1514,8 +1528,8 @@ impl<T: ?Sized> *mut T {
1514
1528
/// ```
1515
1529
#[ unstable( feature = "ptr_offset_from" , issue = "41079" ) ]
1516
1530
#[ inline]
1517
- pub unsafe fn offset_from ( self , other : * const T ) -> isize where T : Sized {
1518
- ( self as * const T ) . offset_from ( other )
1531
+ pub unsafe fn offset_from ( self , origin : * const T ) -> isize where T : Sized {
1532
+ ( self as * const T ) . offset_from ( origin )
1519
1533
}
1520
1534
1521
1535
/// Calculates the distance between two pointers. The returned value is in
@@ -1525,9 +1539,13 @@ impl<T: ?Sized> *mut T {
1525
1539
/// `mem::size_of::<T>()` then the result of the division is rounded towards
1526
1540
/// zero.
1527
1541
///
1542
+ /// Though this method is safe for any two pointers, note that its result
1543
+ /// will be mostly useless if the two pointers aren't into the same allocated
1544
+ /// object, for example if they point to two different local variables.
1545
+ ///
1528
1546
/// # Panics
1529
1547
///
1530
- /// This function panics if `T` is a zero-sized typed .
1548
+ /// This function panics if `T` is a zero-sized type .
1531
1549
///
1532
1550
/// # Examples
1533
1551
///
@@ -1550,8 +1568,8 @@ impl<T: ?Sized> *mut T {
1550
1568
/// ```
1551
1569
#[ unstable( feature = "ptr_wrapping_offset_from" , issue = "41079" ) ]
1552
1570
#[ inline]
1553
- pub fn wrapping_offset_from ( self , other : * const T ) -> isize where T : Sized {
1554
- ( self as * const T ) . wrapping_offset_from ( other )
1571
+ pub fn wrapping_offset_from ( self , origin : * const T ) -> isize where T : Sized {
1572
+ ( self as * const T ) . wrapping_offset_from ( origin )
1555
1573
}
1556
1574
1557
1575
/// Computes the byte offset that needs to be applied in order to
0 commit comments