Skip to content

Commit 97aad63

Browse files
committed
[se-0370] update documentation to track proposal
for the functions involving `fromContentsOf:`.
1 parent 3dcad8c commit 97aad63

File tree

3 files changed

+346
-243
lines changed

3 files changed

+346
-243
lines changed

stdlib/public/core/UnsafeBufferPointer.swift.gyb

Lines changed: 116 additions & 75 deletions
Original file line numberDiff line numberDiff line change
@@ -676,31 +676,32 @@ extension Unsafe${Mutable}BufferPointer {
676676
return source._copyContents(initializing: self)
677677
}
678678

679-
/// Initializes the buffer's memory with the given elements.
679+
/// Initializes the buffer's memory with every element of the source.
680680
///
681-
/// Prior to calling the `initialize(fromElements:)` method on a buffer,
682-
/// the memory it references must be uninitialized,
683-
/// or its `Element` type must be a trivial type. After the call,
681+
/// Prior to calling the `initialize(fromContentsOf:)` method on a buffer,
682+
/// the memory referenced by the buffer must be uninitialized,
683+
/// or the `Element` type must be a trivial type. After the call,
684684
/// the memory referenced by the buffer up to, but not including,
685685
/// the returned index is initialized.
686+
/// The buffer must reference enough memory to accommodate
687+
/// `source.count` elements.
686688
///
687689
/// The returned index is the position of the next uninitialized element
688-
/// in the buffer, which is one past the last element written.
689-
/// If `fromElements` contains no elements, the returned index is equal to
690-
/// the buffer's `startIndex`. If `fromElements` contains an equal or greater
691-
/// number of elements than the buffer can hold, the returned index is equal
692-
/// to the buffer's `endIndex`.
690+
/// in the buffer, one past the index of the last element written.
691+
/// If `source` contains no elements, the returned index is equal to the
692+
/// buffer's `startIndex`. If `source` contains as many elements as the buffer
693+
/// can hold, the returned index is equal to the buffer's `endIndex`.
694+
///
695+
/// - Precondition: `self.count` >= `source.count`
693696
///
694-
/// - Parameter fromElements: A collection of elements to be used to
697+
/// - Parameter source: A collection of elements to be used to
695698
/// initialize the buffer's storage.
696-
/// - Returns: An index to the next uninitialized element in the buffer,
697-
/// or `endIndex`.
698-
@discardableResult
699+
/// - Returns: An index to the next uninitialized element in the buffer.
699700
@inlinable
700701
@_alwaysEmitIntoClient
701-
public func initialize<C: Collection>(
702-
fromElements source: C
703-
) -> Index where C.Element == Element {
702+
public func initialize(
703+
fromContentsOf source: some Collection<Element>
704+
) -> Index {
704705
let count = Swift.min(self.count, source.count)
705706
guard count > 0, let base = baseAddress else { return startIndex }
706707

@@ -773,21 +774,29 @@ extension Unsafe${Mutable}BufferPointer {
773774
return (iterator, endIndex)
774775
}
775776

776-
/// Updates the buffer's initialized memory with the given elements.
777+
/// Updates the buffer's initialized memory with every element of the source.
777778
///
778-
/// The buffer’s memory must be initialized or its `Element` type
779-
/// must be a trivial type.
779+
/// Prior to calling the `update(fromContentsOf:)` method on a buffer,
780+
/// the first `source.count` elements of the buffer's memory must be
781+
/// initialized, or the buffer's `Element` type must be a trivial type.
782+
/// The buffer must reference enough initialized memory to accommodate
783+
/// `source.count` elements.
780784
///
781-
/// - Parameter fromElements: A collection of elements to be used to update
785+
/// The returned index is one past the index of the last element updated.
786+
/// If `source` contains no elements, the returned index is equal to the
787+
/// buffer's `startIndex`. If `source` contains as many elements as the buffer
788+
/// can hold, the returned index is equal to the buffer's `endIndex`.
789+
///
790+
/// - Precondition: `self.count` >= `source.count`
791+
///
792+
/// - Parameter source: A collection of elements to be used to update
782793
/// the buffer's contents.
783-
/// - Returns: An index one past the last updated element in the buffer,
784-
/// or `endIndex`.
785-
@discardableResult
794+
/// - Returns: An index one past the index of the last element updated.
786795
@inlinable
787796
@_alwaysEmitIntoClient
788-
public func update<C: Collection>(
789-
fromElements source: C
790-
) -> Index where C.Element == Element {
797+
public func update(
798+
fromContentsOf source: some Collection<Element>
799+
) -> Index {
791800
let count = Swift.min(source.count, self.count)
792801
guard count > 0, let base = baseAddress else { return startIndex }
793802

@@ -805,72 +814,98 @@ extension Unsafe${Mutable}BufferPointer {
805814
}
806815

807816
/// Moves every element of an initialized source buffer into the
808-
/// uninitialized memory referenced by this buffer, leaving the
809-
/// source memory uninitialized and this buffer's memory initialized.
817+
/// uninitialized memory referenced by this buffer, leaving the source memory
818+
/// uninitialized and this buffer's memory initialized.
819+
///
820+
/// Prior to calling the `moveInitialize(fromContentsOf:)` method on a buffer,
821+
/// the memory it references must be uninitialized,
822+
/// or its `Element` type must be a trivial type. After the call,
823+
/// the memory referenced by the buffer up to, but not including,
824+
/// the returned index is initialized. The memory referenced by
825+
/// `source` is uninitialized after the function returns.
826+
/// The buffer must reference enough memory to accommodate
827+
/// `source.count` elements.
828+
///
829+
/// The returned index is the position of the next uninitialized element
830+
/// in the buffer, one past the index of the last element written.
831+
/// If `source` contains no elements, the returned index is equal to the
832+
/// buffer's `startIndex`. If `source` contains as many elements as the buffer
833+
/// can hold, the returned index is equal to the buffer's `endIndex`.
810834
///
811-
/// The region of memory starting at the beginning of this buffer and
812-
/// covering `source.count` instances of its `Element` type must be
813-
/// uninitialized, or `Element` must be a trivial type. After calling
814-
/// `moveInitialize(fromElements:)`, the region is initialized and
815-
/// the region of memory underlying `source` is uninitialized.
835+
/// - Precondition: `self.count` >= `source.count`
816836
///
817837
/// - Parameter source: A buffer containing the values to copy. The memory
818-
/// region underlying `source` must be initialized. The memory regions
819-
/// referenced by `source` and this buffer may overlap.
838+
/// region underlying `source` must be initialized. The memory regions
839+
/// referenced by `source` and this buffer may overlap.
820840
/// - Returns: An index to the next uninitialized element in the buffer,
821-
/// or `endIndex`.
822-
@discardableResult
841+
/// or `endIndex`.
823842
@inlinable
824843
@_alwaysEmitIntoClient
825-
public func moveInitialize(fromElements source: Self) -> Index {
844+
public func moveInitialize(fromContentsOf source: Self) -> Index {
826845
guard let pointer = source.baseAddress, source.count > 0 else { return 0 }
827846
_debugPrecondition(count >= source.count)
828847
baseAddress._unsafelyUnwrappedUnchecked.moveInitialize(from: pointer,
829848
count: source.count)
830849
return startIndex.advanced(by: source.count)
831850
}
832851

833-
/// Moves every element of an initialized source buffer slice into the
834-
/// uninitialized memory referenced by this buffer, leaving the
835-
/// source memory uninitialized and this buffer's memory initialized.
852+
/// Moves every element of an initialized source buffer into the
853+
/// uninitialized memory referenced by this buffer, leaving the source memory
854+
/// uninitialized and this buffer's memory initialized.
855+
///
856+
/// Prior to calling the `moveInitialize(fromContentsOf:)` method on a buffer,
857+
/// the memory it references must be uninitialized,
858+
/// or its `Element` type must be a trivial type. After the call,
859+
/// the memory referenced by the buffer up to, but not including,
860+
/// the returned index is initialized. The memory referenced by
861+
/// `source` is uninitialized after the function returns.
862+
/// The buffer must reference enough memory to accommodate
863+
/// `source.count` elements.
864+
///
865+
/// The returned index is the position of the next uninitialized element
866+
/// in the buffer, one past the index of the last element written.
867+
/// If `source` contains no elements, the returned index is equal to the
868+
/// buffer's `startIndex`. If `source` contains as many elements as the buffer
869+
/// can hold, the returned index is equal to the buffer's `endIndex`.
836870
///
837-
/// The region of memory starting at the beginning of this buffer and
838-
/// covering `source.count` instances of its `Element` type must be
839-
/// uninitialized, or `Element` must be a trivial type. After calling
840-
/// `moveInitialize(fromElements:)`, the region is initialized and
841-
/// the region of memory underlying `source` is uninitialized.
871+
/// - Precondition: `self.count` >= `source.count`
842872
///
843873
/// - Parameter source: A buffer containing the values to copy. The memory
844-
/// region underlying `source` must be initialized. The memory regions
845-
/// referenced by `source` and this buffer may overlap.
846-
/// - Returns: An index one past the last replaced element in the buffer,
847-
/// or `endIndex`.
848-
@discardableResult
874+
/// region underlying `source` must be initialized. The memory regions
875+
/// referenced by `source` and this buffer may overlap.
876+
/// - Returns: An index to the next uninitialized element in the buffer,
877+
/// or `endIndex`.
849878
@inlinable
850879
@_alwaysEmitIntoClient
851-
public func moveInitialize(fromElements source: Slice<Self>) -> Index {
852-
return moveInitialize(fromElements: Self(rebasing: source))
880+
public func moveInitialize(fromContentsOf source: Slice<Self>) -> Index {
881+
return moveInitialize(fromContentsOf: Self(rebasing: source))
853882
}
854883

855884
/// Updates this buffer's initialized memory initialized memory by
856885
/// moving every element from the source buffer,
857886
/// leaving the source memory uninitialized.
858887
///
859-
/// The region of memory starting at the beginning of this buffer and
860-
/// covering `fromElements.count` instances of its `Element` type must be
861-
/// initialized, or `Element` must be a trivial type.
862-
/// After calling `moveUpdate(fromElements:)`,
863-
/// the region of memory underlying `source` is uninitialized.
888+
/// Prior to calling the `moveUpdate(fromContentsOf:)` method on a buffer,
889+
/// the first `source.count` elements of the buffer's memory must be
890+
/// initialized, or the buffer's `Element` type must be a trivial type.
891+
/// The memory referenced by `source` is uninitialized after the function
892+
/// returns. The buffer must reference enough initialized memory
893+
/// to accommodate `source.count` elements.
894+
///
895+
/// The returned index is one past the index of the last element updated.
896+
/// If `source` contains no elements, the returned index is equal to the
897+
/// buffer's `startIndex`. If `source` contains as many elements as the buffer
898+
/// can hold, the returned index is equal to the buffer's `endIndex`.
899+
///
900+
/// - Precondition: `self.count` >= `source.count`
864901
///
865902
/// - Parameter source: A buffer containing the values to move.
866-
/// The memory region underlying `source` must be initialized. The
867-
/// memory regions referenced by `source` and this pointer must not overlap.
868-
/// - Returns: An index one past the last updated element in the buffer,
869-
/// or `endIndex`.
870-
@discardableResult
903+
/// The memory region underlying `source` must be initialized. The memory
904+
/// regions referenced by `source` and this buffer must not overlap.
905+
/// - Returns: An index one past the index of the last element updated.
871906
@inlinable
872907
@_alwaysEmitIntoClient
873-
public func moveUpdate(fromElements source: Self) -> Index {
908+
public func moveUpdate(fromContentsOf source: Self) -> Index {
874909
guard let pointer = source.baseAddress, source.count > 0 else { return 0 }
875910
_debugPrecondition(count >= source.count)
876911
baseAddress._unsafelyUnwrappedUnchecked.moveUpdate(from: pointer,
@@ -882,22 +917,28 @@ extension Unsafe${Mutable}BufferPointer {
882917
/// moving every element from the source buffer slice,
883918
/// leaving the source memory uninitialized.
884919
///
885-
/// The region of memory starting at the beginning of this buffer and
886-
/// covering `fromElements.count` instances of its `Element` type must be
887-
/// initialized, or `Element` must be a trivial type.
888-
/// After calling `moveUpdate(fromElements:)`,
889-
/// the region of memory underlying `source` is uninitialized.
920+
/// Prior to calling the `moveUpdate(fromContentsOf:)` method on a buffer,
921+
/// the first `source.count` elements of the buffer's memory must be
922+
/// initialized, or the buffer's `Element` type must be a trivial type.
923+
/// The memory referenced by `source` is uninitialized after the function
924+
/// returns. The buffer must reference enough initialized memory
925+
/// to accommodate `source.count` elements.
890926
///
891-
/// - Parameter source: A buffer containing the values to move.
927+
/// The returned index is one past the index of the last element updated.
928+
/// If `source` contains no elements, the returned index is equal to the
929+
/// buffer's `startIndex`. If `source` contains as many elements as the buffer
930+
/// can hold, the returned index is equal to the buffer's `endIndex`.
931+
///
932+
/// - Precondition: `self.count` >= `source.count`
933+
///
934+
/// - Parameter source: A buffer slice containing the values to move.
892935
/// The memory region underlying `source` must be initialized. The
893936
/// memory regions referenced by `source` and this pointer must not overlap.
894-
/// - Returns: An index one past the last updated element in the buffer,
895-
/// or `endIndex`.
896-
@discardableResult
937+
/// - Returns: An index one past the index of the last element updated.
897938
@inlinable
898939
@_alwaysEmitIntoClient
899-
public func moveUpdate(fromElements source: Slice<Self>) -> Index {
900-
return moveUpdate(fromElements: Self(rebasing: source))
940+
public func moveUpdate(fromContentsOf source: Slice<Self>) -> Index {
941+
return moveUpdate(fromContentsOf: Self(rebasing: source))
901942
}
902943

903944
/// Deinitializes every instance in this buffer.

0 commit comments

Comments
 (0)