Skip to content

Commit 13853f1

Browse files
natecook1000airspeedswift
authored andcommitted
[stdlib] Various documentation improvements (#18013) (#18046)
- Revise Bool.toggle() discussion and fix attribute placement - Revise to Hasher abstracts and discussions - Correct the name of the remainder operator - Clean up deprecations and paste-os w/in UnsafePointer
1 parent a831110 commit 13853f1

File tree

5 files changed

+39
-21
lines changed

5 files changed

+39
-21
lines changed

stdlib/public/core/Bool.swift

Lines changed: 5 additions & 5 deletions
Original file line numberDiff line numberDiff line change
@@ -341,16 +341,16 @@ extension Bool {
341341
}
342342

343343
extension Bool {
344-
@inlinable
345-
/// Toggles the value of the Boolean.
344+
/// Toggles the Boolean variable's value.
346345
///
347-
/// Calling this method sets the variable to `true` if it was `false`,
348-
/// and sets it to `false` if it was `true`. For example:
346+
/// Use this method to toggle a Boolean value from `true` to `false` or from
347+
/// `false` to `true`.
349348
///
350349
/// var bools = [true, false]
351350
///
352351
/// bools[0].toggle()
353-
/// // bools now contains [false, false]
352+
/// // bools == [false, false]
353+
@inlinable
354354
public mutating func toggle() {
355355
self = !self
356356
}

stdlib/public/core/Hasher.swift

Lines changed: 15 additions & 7 deletions
Original file line numberDiff line numberDiff line change
@@ -255,8 +255,10 @@ public struct Hasher {
255255

256256
internal var _core: Core
257257

258-
/// Initialize a new hasher. The hasher uses a per-execution seed value that
259-
/// is set during process startup, usually from a high-quality random source.
258+
/// Creates a new hasher.
259+
///
260+
/// The hasher uses a per-execution seed value that is set during process
261+
/// startup, usually from a high-quality random source.
260262
@effects(releasenone)
261263
public init() {
262264
self._core = Core(seed: Hasher._seed)
@@ -299,8 +301,10 @@ public struct Hasher {
299301
}
300302
}
301303

302-
/// Feed `value` to this hasher, mixing its essential parts into
303-
/// the hasher state.
304+
/// Adds the given value to this hasher, mixing its essential parts into the
305+
/// hasher state.
306+
///
307+
/// - Parameter value: A value to add to the hasher.
304308
@inlinable
305309
@inline(__always)
306310
public mutating func combine<H: Hashable>(_ value: H) {
@@ -343,8 +347,10 @@ public struct Hasher {
343347
_core.combine(bytes: value, count: count)
344348
}
345349

346-
/// Feed the contents of `buffer` into this hasher, mixing it into the hasher
347-
/// state.
350+
/// Adds the contents of the given buffer to this hasher, mixing it into the
351+
/// hasher state.
352+
///
353+
/// - Parameter bytes: A raw memory buffer.
348354
@effects(releasenone)
349355
public mutating func combine(bytes: UnsafeRawBufferPointer) {
350356
_core.combine(bytes: bytes)
@@ -359,14 +365,16 @@ public struct Hasher {
359365
return Int(truncatingIfNeeded: _core.finalize())
360366
}
361367

362-
/// Finalize the hasher state and return the hash value.
368+
/// Finalizes the hasher state and returns the hash value.
363369
///
364370
/// Finalizing consumes the hasher: it is illegal to finalize a hasher you
365371
/// don't own, or to perform operations on a finalized hasher. (These may
366372
/// become compile-time errors in the future.)
367373
///
368374
/// Hash values are not guaranteed to be equal across different executions of
369375
/// your program. Do not save hash values to use during a future execution.
376+
///
377+
/// - Returns: The hash value calculated by the hasher.
370378
@effects(releasenone)
371379
public __consuming func finalize() -> Int {
372380
var core = _core

stdlib/public/core/Integers.swift.gyb

Lines changed: 2 additions & 2 deletions
Original file line numberDiff line numberDiff line change
@@ -223,8 +223,8 @@ def operatorComment(operator, fixedWidth):
223223
'%': """\
224224
/// Returns the remainder of dividing the first value by the second.
225225
///
226-
/// The result of the modulo operator (`%`) has the same sign as `lhs` and is
227-
/// less than `rhs.magnitude`.
226+
/// The result of the remainder operator (`%`) has the same sign as `lhs` and
227+
/// is less than `rhs.magnitude`.
228228
///
229229
/// let x = 22 % 5
230230
/// // x == 2

stdlib/public/core/SequenceAlgorithms.swift

Lines changed: 7 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -492,6 +492,13 @@ extension Sequence {
492492
/// Returns a Boolean value indicating whether every element of a sequence
493493
/// satisfies a given predicate.
494494
///
495+
/// The following code uses this method to test whether all the names in an
496+
/// array have at least five characters:
497+
///
498+
/// let names = ["Sofia", "Camilla", "Martina", "Mateo", "Nicolás"]
499+
/// let allHaveAtLeastFive = names.allSatisfy({ $0.count >= 5 })
500+
/// // allHaveAtLeastFive == true
501+
///
495502
/// - Parameter predicate: A closure that takes an element of the sequence
496503
/// as its argument and returns a Boolean value that indicates whether
497504
/// the passed element satisfies a condition.

stdlib/public/core/UnsafePointer.swift.gyb

Lines changed: 10 additions & 7 deletions
Original file line numberDiff line numberDiff line change
@@ -51,8 +51,8 @@
5151
/// initialized before it can be accessed for reading.
5252
///
5353
%if mutable:
54-
/// You can use methods like `initialize(to:count:)`, `initialize(from:)`, and
55-
/// `moveInitialize(from:count:)` to initialize the memory referenced by a
54+
/// You can use methods like `initialize(to:count:)`, `initialize(from:count:)`,
55+
/// and `moveInitialize(from:count:)` to initialize the memory referenced by a
5656
/// pointer with a value or series of values.
5757
///
5858
% end
@@ -81,8 +81,9 @@
8181
/// memory, `uint8Pointer`, will be used for the examples below.
8282
///
8383
% if mutable:
84+
/// var bytes: [UInt8] = [39, 77, 111, 111, 102, 33, 39, 0]
8485
/// let uint8Pointer = UnsafeMutablePointer<UInt8>.allocate(capacity: 8)
85-
/// uint8Pointer.initialize(from: [39, 77, 111, 111, 102, 33, 39, 0])
86+
/// uint8Pointer.initialize(from: &bytes, count: 8)
8687
% else:
8788
/// let uint8Pointer: UnsafePointer<UInt8> = fetchEightBytes()
8889
% end
@@ -416,9 +417,11 @@ public struct ${Self}<Pointee>: _Pointer {
416417
/// instances and then initializes that memory with the elements of a range.
417418
///
418419
/// let intPointer = UnsafeMutablePointer<Int>.allocate(capacity: 4)
419-
/// intPointer.initialize(from: 1...4)
420+
/// for i in 0..<4 {
421+
/// (intPointer + i).initialize(to: i)
422+
/// }
420423
/// print(intPointer.pointee)
421-
/// // Prints "1"
424+
/// // Prints "0"
422425
///
423426
/// When you allocate memory, always remember to deallocate once you're
424427
/// finished.
@@ -618,7 +621,7 @@ public struct ${Self}<Pointee>: _Pointer {
618621
/// The region of memory starting at this pointer and covering `count`
619622
/// instances of the pointer's `Pointee` type must be uninitialized or
620623
/// `Pointee` must be a trivial type. After calling
621-
/// `initialize(from:count:)`, the region is initialized and the memory
624+
/// `moveInitialize(from:count:)`, the region is initialized and the memory
622625
/// region `source..<(source + count)` is uninitialized.
623626
///
624627
/// - Parameters:
@@ -711,7 +714,7 @@ public struct ${Self}<Pointee>: _Pointer {
711714
/// The region of memory starting at this pointer and covering `count`
712715
/// instances of the pointer's `Pointee` type must be initialized or
713716
/// `Pointee` must be a trivial type. After calling
714-
/// `initialize(from:count:)`, the region is initialized and the memory
717+
/// `moveAssign(from:count:)`, the region is initialized and the memory
715718
/// region `source..<(source + count)` is uninitialized.
716719
///
717720
/// - Parameters:

0 commit comments

Comments
 (0)