Skip to content

Commit acd2526

Browse files
committed
various updates based on review
1 parent 3c00ffa commit acd2526

File tree

2 files changed

+38
-24
lines changed

2 files changed

+38
-24
lines changed

core/src/ptr/const_ptr.rs

+19-12
Original file line numberDiff line numberDiff line change
@@ -355,8 +355,8 @@ impl<T: ?Sized> *const T {
355355
///
356356
/// If any of the following conditions are violated, the result is Undefined Behavior:
357357
///
358-
/// * The computed offset, `count * size_of::<T>()` bytes (using unbounded arithmetic),
359-
/// must fit in an `isize`.
358+
/// * The offset in bytes, `count * size_of::<T>()`, computed on mathematical integers (without
359+
/// "wrapping around"), must fit in an `isize`.
360360
///
361361
/// * If the computed offset is non-zero, then `self` must be derived from a pointer to some
362362
/// [allocated object], and the entire memory range between `self` and the result must be in
@@ -399,7 +399,7 @@ impl<T: ?Sized> *const T {
399399
unsafe { intrinsics::offset(self, count) }
400400
}
401401

402-
/// Calculates the offset from a pointer in bytes.
402+
/// Adds a signed offset in bytes to a pointer.
403403
///
404404
/// `count` is in units of **bytes**.
405405
///
@@ -808,7 +808,11 @@ impl<T: ?Sized> *const T {
808808
}
809809
}
810810

811-
/// Adds an offset to a pointer.
811+
/// Adds an unsigned offset to a pointer.
812+
///
813+
/// This can only move the pointer forward (or not move it). If you need to move forward or
814+
/// backward depending on the value, then you might want [`offset`](#method.offset) instead
815+
/// which takes a signed offset.
812816
///
813817
/// `count` is in units of T; e.g., a `count` of 3 represents a pointer
814818
/// offset of `3 * size_of::<T>()` bytes.
@@ -817,8 +821,8 @@ impl<T: ?Sized> *const T {
817821
///
818822
/// If any of the following conditions are violated, the result is Undefined Behavior:
819823
///
820-
/// * The computed offset, `count * size_of::<T>()` bytes (using unbounded arithmetic),
821-
/// must fit in an `isize`.
824+
/// * The offset in bytes, `count * size_of::<T>()`, computed on mathematical integers (without
825+
/// "wrapping around"), must fit in an `isize`.
822826
///
823827
/// * If the computed offset is non-zero, then `self` must be derived from a pointer to some
824828
/// [allocated object], and the entire memory range between `self` and the result must be in
@@ -861,7 +865,7 @@ impl<T: ?Sized> *const T {
861865
unsafe { intrinsics::offset(self, count) }
862866
}
863867

864-
/// Calculates the offset from a pointer in bytes (convenience for `.byte_offset(count as isize)`).
868+
/// Adds an unsigned offset in bytes to a pointer.
865869
///
866870
/// `count` is in units of bytes.
867871
///
@@ -882,7 +886,11 @@ impl<T: ?Sized> *const T {
882886
unsafe { self.cast::<u8>().add(count).with_metadata_of(self) }
883887
}
884888

885-
/// Subtracts an offset from a pointer.
889+
/// Subtracts an unsigned offset from a pointer.
890+
///
891+
/// This can only move the pointer backward (or not move it). If you need to move forward or
892+
/// backward depending on the value, then you might want [`offset`](#method.offset) instead
893+
/// which takes a signed offset.
886894
///
887895
/// `count` is in units of T; e.g., a `count` of 3 represents a pointer
888896
/// offset of `3 * size_of::<T>()` bytes.
@@ -891,8 +899,8 @@ impl<T: ?Sized> *const T {
891899
///
892900
/// If any of the following conditions are violated, the result is Undefined Behavior:
893901
///
894-
/// * The computed offset, `count * size_of::<T>()` bytes (using unbounded arithmetic),
895-
/// must fit in an `isize`.
902+
/// * The offset in bytes, `count * size_of::<T>()`, computed on mathematical integers (without
903+
/// "wrapping around"), must fit in an `isize`.
896904
///
897905
/// * If the computed offset is non-zero, then `self` must be derived from a pointer to some
898906
/// [allocated object], and the entire memory range between `self` and the result must be in
@@ -943,8 +951,7 @@ impl<T: ?Sized> *const T {
943951
}
944952
}
945953

946-
/// Calculates the offset from a pointer in bytes (convenience for
947-
/// `.byte_offset((count as isize).wrapping_neg())`).
954+
/// Subtracts an unsigned offset in bytes from a pointer.
948955
///
949956
/// `count` is in units of bytes.
950957
///

core/src/ptr/mut_ptr.rs

+19-12
Original file line numberDiff line numberDiff line change
@@ -353,8 +353,8 @@ impl<T: ?Sized> *mut T {
353353
///
354354
/// If any of the following conditions are violated, the result is Undefined Behavior:
355355
///
356-
/// * The computed offset, `count * size_of::<T>()` bytes (using unbounded arithmetic),
357-
/// must fit in an `isize`.
356+
/// * The offset in bytes, `count * size_of::<T>()`, computed on mathematical integers (without
357+
/// "wrapping around"), must fit in an `isize`.
358358
///
359359
/// * If the computed offset is non-zero, then `self` must be derived from a pointer to some
360360
/// [allocated object], and the entire memory range between `self` and the result must be in
@@ -399,7 +399,7 @@ impl<T: ?Sized> *mut T {
399399
unsafe { intrinsics::offset(self, count) }
400400
}
401401

402-
/// Calculates the offset from a pointer in bytes.
402+
/// Adds a signed offset in bytes to a pointer.
403403
///
404404
/// `count` is in units of **bytes**.
405405
///
@@ -889,7 +889,11 @@ impl<T: ?Sized> *mut T {
889889
unsafe { (self as *const T).sub_ptr(origin) }
890890
}
891891

892-
/// Adds an offset to a pointer.
892+
/// Adds an unsigned offset to a pointer.
893+
///
894+
/// This can only move the pointer forward (or not move it). If you need to move forward or
895+
/// backward depending on the value, then you might want [`offset`](#method.offset) instead
896+
/// which takes a signed offset.
893897
///
894898
/// `count` is in units of T; e.g., a `count` of 3 represents a pointer
895899
/// offset of `3 * size_of::<T>()` bytes.
@@ -898,8 +902,8 @@ impl<T: ?Sized> *mut T {
898902
///
899903
/// If any of the following conditions are violated, the result is Undefined Behavior:
900904
///
901-
/// * The computed offset, `count * size_of::<T>()` bytes (using unbounded arithmetic),
902-
/// must fit in an `isize`.
905+
/// * The offset in bytes, `count * size_of::<T>()`, computed on mathematical integers (without
906+
/// "wrapping around"), must fit in an `isize`.
903907
///
904908
/// * If the computed offset is non-zero, then `self` must be derived from a pointer to some
905909
/// [allocated object], and the entire memory range between `self` and the result must be in
@@ -942,7 +946,7 @@ impl<T: ?Sized> *mut T {
942946
unsafe { intrinsics::offset(self, count) }
943947
}
944948

945-
/// Calculates the offset from a pointer in bytes (convenience for `.byte_offset(count as isize)`).
949+
/// Adds an unsigned offset in bytes to a pointer.
946950
///
947951
/// `count` is in units of bytes.
948952
///
@@ -963,7 +967,11 @@ impl<T: ?Sized> *mut T {
963967
unsafe { self.cast::<u8>().add(count).with_metadata_of(self) }
964968
}
965969

966-
/// Subtracts an offset from a pointer.
970+
/// Subtracts an unsigned offset from a pointer.
971+
///
972+
/// This can only move the pointer backward (or not move it). If you need to move forward or
973+
/// backward depending on the value, then you might want [`offset`](#method.offset) instead
974+
/// which takes a signed offset.
967975
///
968976
/// `count` is in units of T; e.g., a `count` of 3 represents a pointer
969977
/// offset of `3 * size_of::<T>()` bytes.
@@ -972,8 +980,8 @@ impl<T: ?Sized> *mut T {
972980
///
973981
/// If any of the following conditions are violated, the result is Undefined Behavior:
974982
///
975-
/// * The computed offset, `count * size_of::<T>()` bytes (using unbounded arithmetic),
976-
/// must fit in an `isize`.
983+
/// * The offset in bytes, `count * size_of::<T>()`, computed on mathematical integers (without
984+
/// "wrapping around"), must fit in an `isize`.
977985
///
978986
/// * If the computed offset is non-zero, then `self` must be derived from a pointer to some
979987
/// [allocated object], and the entire memory range between `self` and the result must be in
@@ -1024,8 +1032,7 @@ impl<T: ?Sized> *mut T {
10241032
}
10251033
}
10261034

1027-
/// Calculates the offset from a pointer in bytes (convenience for
1028-
/// `.byte_offset((count as isize).wrapping_neg())`).
1035+
/// Subtracts an unsigned offset in bytes from a pointer.
10291036
///
10301037
/// `count` is in units of bytes.
10311038
///

0 commit comments

Comments
 (0)