Skip to content

Commit e070568

Browse files
committed
[stdlib] Add documentation for pointer operators
1 parent 7494e20 commit e070568

File tree

2 files changed

+119
-8
lines changed

2 files changed

+119
-8
lines changed

stdlib/public/core/UnsafePointer.swift.gyb

Lines changed: 97 additions & 5 deletions
Original file line numberDiff line numberDiff line change
@@ -873,6 +873,13 @@ extension ${Self} : CustomPlaygroundQuickLookable {
873873
extension ${Self} {
874874
// - Note: Strideable's implementation is potentially less efficient and cannot
875875
// handle misaligned pointers.
876+
/// Returns a Boolean value indicating whether two pointers are equal.
877+
///
878+
/// - Parameters:
879+
/// - lhs: A pointer.
880+
/// - rhs: Another pointer.
881+
/// - Returns: `true` if `lhs` and `rhs` reference the same memory address;
882+
/// otherwise, `false`.
876883
@_transparent
877884
public static func == (lhs: ${Self}<Pointee>, rhs: ${Self}<Pointee>) -> Bool {
878885
return Bool(Builtin.cmp_eq_RawPointer(lhs._rawValue, rhs._rawValue))
@@ -882,6 +889,14 @@ extension ${Self} {
882889
// cannot handle misaligned pointers.
883890
//
884891
// - Note: This is an unsigned comparison unlike Strideable's implementation.
892+
/// Returns a Boolean value indicating whether the first pointer references
893+
/// an earlier memory location than the second pointer.
894+
///
895+
/// - Parameters:
896+
/// - lhs: A pointer.
897+
/// - rhs: Another pointer.
898+
/// - Returns: `true` if `lhs` references a memory address earlier than
899+
/// `rhs`; otherwise, `false`.
885900
@_transparent
886901
public static func < (lhs: ${Self}<Pointee>, rhs: ${Self}<Pointee>) -> Bool {
887902
return Bool(Builtin.cmp_ult_RawPointer(lhs._rawValue, rhs._rawValue))
@@ -891,25 +906,78 @@ extension ${Self} {
891906
// with Strideable. However, optimizer improvements are needed
892907
// before they can be removed without affecting performance.
893908

894-
/// - Precondition: The result is within bounds of the same allocation.
909+
/// Creates a new pointer, offset from a pointer by a specified number of
910+
/// instances of the pointer's `Pointee` type.
911+
///
912+
/// You use the addition operator (`+`) to advance a pointer by a number of
913+
/// contiguous instances. The resulting pointer must be within the bounds of
914+
/// the same allocation as `lhs`.
915+
///
916+
/// - Parameters:
917+
/// - lhs: A pointer.
918+
/// - rhs: The number of strides of the pointer's `Pointee` type to offset
919+
/// `lhs`. To access the stride, use `MemoryLayout<Pointee>.stride`.
920+
/// - Returns: A pointer offset from `lhs` by `rhs` instances of the
921+
/// `Pointee` type.
922+
///
923+
/// - SeeAlso: `MemoryLayout`
895924
@_transparent
896925
public static func + (lhs: ${Self}<Pointee>, rhs: Int) -> ${Self}<Pointee> {
897926
return ${Self}(Builtin.gep_Word(
898927
lhs._rawValue, rhs._builtinWordValue, Pointee.self))
899928
}
900929

901-
/// - Precondition: The result is within the bounds of the same allocation.
930+
/// Creates a new pointer, offset from a pointer by a specified number of
931+
/// instances of the pointer's `Pointee` type.
932+
///
933+
/// You use the addition operator (`+`) to advance a pointer by a number of
934+
/// contiguous instances. The resulting pointer must be within the bounds of
935+
/// the same allocation as `rhs`.
936+
///
937+
/// - Parameters:
938+
/// - lhs: The number of strides of the pointer's `Pointee` type to offset
939+
/// `rhs`. To access the stride, use `MemoryLayout<Pointee>.stride`.
940+
/// - rhs: A pointer.
941+
/// - Returns: A pointer offset from `rhs` by `lhs` instances of the
942+
/// `Pointee` type.
943+
///
944+
/// - SeeAlso: `MemoryLayout`
902945
@_transparent
903946
public static func + (lhs: Int, rhs: ${Self}<Pointee>) -> ${Self}<Pointee> {
904947
return rhs + lhs
905948
}
906949

907-
/// - Precondition: The result is within the bounds of the same allocation.
950+
/// Creates a new pointer, offset backward from a pointer by a specified
951+
/// number of instances of the pointer's `Pointee` type.
952+
///
953+
/// You use the subtraction operator (`-`) to shift a pointer backward by a
954+
/// number of contiguous instances. The resulting pointer must be within the
955+
/// bounds of the same allocation as `lhs`.
956+
///
957+
/// - Parameters:
958+
/// - lhs: A pointer.
959+
/// - rhs: The number of strides of the pointer's `Pointee` type to offset
960+
/// `lhs`. To access the stride, use `MemoryLayout<Pointee>.stride`.
961+
/// - Returns: A pointer offset backward from `lhs` by `rhs` instances of
962+
/// the `Pointee` type.
963+
///
964+
/// - SeeAlso: `MemoryLayout`
908965
@_transparent
909966
public static func - (lhs: ${Self}<Pointee>, rhs: Int) -> ${Self}<Pointee> {
910967
return lhs + -rhs
911968
}
912969

970+
/// Returns the distance between two pointers, counted as instances of the
971+
/// pointers' `Pointee` type.
972+
///
973+
/// - Parameters:
974+
/// - lhs: A pointer.
975+
/// - rhs: Another pointer.
976+
/// - Returns: The distance from `lhs` to `rhs`, in strides of the pointer's
977+
/// `Pointee` type. To access the stride, use
978+
/// `MemoryLayout<Pointee>.stride`.
979+
///
980+
/// - SeeAlso: `MemoryLayout`
913981
@_transparent
914982
public static func - (lhs: ${Self}<Pointee>, rhs: ${Self}<Pointee>) -> Int {
915983
return
@@ -918,13 +986,37 @@ extension ${Self} {
918986
/ MemoryLayout<Pointee>.stride
919987
}
920988

921-
/// - Precondition: The result is within the bounds of the same allocation.
989+
/// Advances a pointer by a specified number of instances of the pointer's
990+
/// `Pointee` type.
991+
///
992+
/// You use the addition assignment operator (`+=`) to advance a pointer by a
993+
/// number of contiguous instances. The resulting pointer must be within the
994+
/// bounds of the same allocation as `rhs`.
995+
///
996+
/// - Parameters:
997+
/// - lhs: A pointer to advance in place.
998+
/// - rhs: The number of strides of the pointer's `Pointee` type to offset
999+
/// `lhs`. To access the stride, use `MemoryLayout<Pointee>.stride`.
1000+
///
1001+
/// - SeeAlso: `MemoryLayout`
9221002
@_transparent
9231003
public static func += (lhs: inout ${Self}<Pointee>, rhs: Int) {
9241004
lhs = lhs + rhs
9251005
}
9261006

927-
/// - Precondition: The result is within the bounds of the same allocation.
1007+
/// Shifts a pointer backward by a specified number of instances of the
1008+
/// pointer's `Pointee` type.
1009+
///
1010+
/// You use the subtraction assignment operator (`-=`) to shift a pointer
1011+
/// backward by a number of contiguous instances. The resulting pointer must
1012+
/// be within the bounds of the same allocation as `rhs`.
1013+
///
1014+
/// - Parameters:
1015+
/// - lhs: A pointer to advance in place.
1016+
/// - rhs: The number of strides of the pointer's `Pointee` type to offset
1017+
/// `lhs`. To access the stride, use `MemoryLayout<Pointee>.stride`.
1018+
///
1019+
/// - SeeAlso: `MemoryLayout`
9281020
@_transparent
9291021
public static func -= (lhs: inout ${Self}<Pointee>, rhs: Int) {
9301022
lhs = lhs - rhs

stdlib/public/core/UnsafeRawPointer.swift.gyb

Lines changed: 22 additions & 3 deletions
Original file line numberDiff line numberDiff line change
@@ -884,6 +884,9 @@ public struct Unsafe${Mutable}RawPointer : Strideable, Hashable, _Pointer {
884884
/// With pointer `p` and distance `n`, the result of `p.advanced(by: n)` is
885885
/// equivalent to `p + n`.
886886
///
887+
/// The resulting pointer must be within the bounds of the same allocation as
888+
/// this pointer.
889+
///
887890
/// - Parameter n: The number of bytes to offset this pointer. `n` may be
888891
/// positive, negative, or zero.
889892
/// - Returns: A pointer offset from this pointer by `n` bytes.
@@ -893,14 +896,30 @@ public struct Unsafe${Mutable}RawPointer : Strideable, Hashable, _Pointer {
893896
}
894897

895898
extension ${Self} {
896-
/// - Note: This may be more efficient than Strideable's implementation
897-
/// calling ${Self}.distance().
899+
// - Note: This may be more efficient than Strideable's implementation
900+
// calling ${Self}.distance().
901+
/// Returns a Boolean value indicating whether two pointers are equal.
902+
///
903+
/// - Parameters:
904+
/// - lhs: A pointer.
905+
/// - rhs: Another pointer.
906+
/// - Returns: `true` if `lhs` and `rhs` reference the same memory address;
907+
/// otherwise, `false`.
898908
@_transparent
899909
public static func == (lhs: ${Self}, rhs: ${Self}) -> Bool {
900910
return Bool(Builtin.cmp_eq_RawPointer(lhs._rawValue, rhs._rawValue))
901911
}
902912

903-
/// - Note: This is an unsigned comparison unlike Strideable's implementation.
913+
// - Note: This is an unsigned comparison unlike Strideable's
914+
// implementation.
915+
/// Returns a Boolean value indicating whether the first pointer references
916+
/// an earlier memory location than the second pointer.
917+
///
918+
/// - Parameters:
919+
/// - lhs: A pointer.
920+
/// - rhs: Another pointer.
921+
/// - Returns: `true` if `lhs` references a memory address earlier than
922+
/// `rhs`; otherwise, `false`.
904923
@_transparent
905924
public static func < (lhs: ${Self}, rhs: ${Self}) -> Bool {
906925
return Bool(Builtin.cmp_ult_RawPointer(lhs._rawValue, rhs._rawValue))

0 commit comments

Comments
 (0)