@@ -143,15 +143,15 @@ public struct AnyAttachable: AttachableContainer, Copyable, Sendable {
143143 attachableValue. estimatedAttachmentByteCount
144144 }
145145
146- public func withUnsafeBufferPointer < R> ( for attachment: borrowing Attachment < Self > , _ body: ( UnsafeRawBufferPointer ) throws -> R ) throws -> R {
146+ public func withUnsafeBytes < R> ( for attachment: borrowing Attachment < Self > , _ body: ( UnsafeRawBufferPointer ) throws -> R ) throws -> R {
147147 func open< T> ( _ attachableValue: T , for attachment: borrowing Attachment < Self > ) throws -> R where T: Attachable & Sendable & Copyable {
148148 let temporaryAttachment = Attachment < T > (
149149 _attachableValue: attachableValue,
150150 fileSystemPath: attachment. fileSystemPath,
151151 _preferredName: attachment. _preferredName,
152152 sourceLocation: attachment. sourceLocation
153153 )
154- return try temporaryAttachment. withUnsafeBufferPointer ( body)
154+ return try temporaryAttachment. withUnsafeBytes ( body)
155155 }
156156 return try open ( attachableValue, for: attachment)
157157 }
@@ -220,25 +220,61 @@ extension Attachment where AttachableValue: AttachableContainer & ~Copyable {
220220
221221#if !SWT_NO_LAZY_ATTACHMENTS
222222extension Attachment where AttachableValue: Sendable & Copyable {
223- /// Attach this instance to the current test.
223+ /// Attach an attachment to the current test.
224224 ///
225225 /// - Parameters:
226+ /// - attachment: The attachment to attach.
226227 /// - sourceLocation: The source location of the call to this function.
227228 ///
229+ /// When attaching a value of a type that does not conform to both
230+ /// [`Sendable`](https://developer.apple.com/documentation/swift/sendable) and
231+ /// [`Copyable`](https://developer.apple.com/documentation/swift/copyable),
232+ /// the testing library encodes it as data immediately. If the value cannot be
233+ /// encoded and an error is thrown, that error is recorded as an issue in the
234+ /// current test and the attachment is not written to the test report or to
235+ /// disk.
236+ ///
228237 /// An attachment can only be attached once.
229238 @_documentation ( visibility: private)
230- public consuming func attach ( sourceLocation: SourceLocation = #_sourceLocation) {
231- var attachmentCopy = Attachment < AnyAttachable > ( self )
239+ public static func record ( _ attachment : consuming Self , sourceLocation: SourceLocation = #_sourceLocation) {
240+ var attachmentCopy = Attachment < AnyAttachable > ( attachment )
232241 attachmentCopy. sourceLocation = sourceLocation
233242 Event . post ( . valueAttached( attachmentCopy) )
234243 }
244+
245+ /// Attach a value to the current test.
246+ ///
247+ /// - Parameters:
248+ /// - attachableValue: The value to attach.
249+ /// - preferredName: The preferred name of the attachment when writing it to
250+ /// a test report or to disk. If `nil`, the testing library attempts to
251+ /// derive a reasonable filename for the attached value.
252+ /// - sourceLocation: The source location of the call to this function.
253+ ///
254+ /// When attaching a value of a type that does not conform to both
255+ /// [`Sendable`](https://developer.apple.com/documentation/swift/sendable) and
256+ /// [`Copyable`](https://developer.apple.com/documentation/swift/copyable),
257+ /// the testing library encodes it as data immediately. If the value cannot be
258+ /// encoded and an error is thrown, that error is recorded as an issue in the
259+ /// current test and the attachment is not written to the test report or to
260+ /// disk.
261+ ///
262+ /// This function creates a new instance of ``Attachment`` and immediately
263+ /// attaches it to the current test.
264+ ///
265+ /// An attachment can only be attached once.
266+ @_documentation ( visibility: private)
267+ public static func record( _ attachableValue: consuming AttachableValue , named preferredName: String ? = nil , sourceLocation: SourceLocation = #_sourceLocation) {
268+ record ( Self ( attachableValue, named: preferredName) , sourceLocation: sourceLocation)
269+ }
235270}
236271#endif
237272
238273extension Attachment where AttachableValue: ~ Copyable {
239- /// Attach this instance to the current test.
274+ /// Attach an attachment to the current test.
240275 ///
241276 /// - Parameters:
277+ /// - attachment: The attachment to attach.
242278 /// - sourceLocation: The source location of the call to this function.
243279 ///
244280 /// When attaching a value of a type that does not conform to both
@@ -250,14 +286,14 @@ extension Attachment where AttachableValue: ~Copyable {
250286 /// disk.
251287 ///
252288 /// An attachment can only be attached once.
253- public consuming func attach ( sourceLocation: SourceLocation = #_sourceLocation) {
289+ public static func record ( _ attachment : consuming Self , sourceLocation: SourceLocation = #_sourceLocation) {
254290 do {
255- let attachmentCopy = try withUnsafeBufferPointer { buffer in
291+ let attachmentCopy = try attachment . withUnsafeBytes { buffer in
256292 let attachableContainer = AnyAttachable ( attachableValue: Array ( buffer) )
257293 return Attachment < AnyAttachable > (
258294 _attachableValue: attachableContainer,
259- fileSystemPath: fileSystemPath,
260- _preferredName: preferredName, // invokes preferredName(for:basedOn:)
295+ fileSystemPath: attachment . fileSystemPath,
296+ _preferredName: attachment . preferredName, // invokes preferredName(for:basedOn:)
261297 sourceLocation: sourceLocation
262298 )
263299 }
@@ -267,6 +303,31 @@ extension Attachment where AttachableValue: ~Copyable {
267303 Issue ( kind: . valueAttachmentFailed( error) , comments: [ ] , sourceContext: sourceContext) . record ( )
268304 }
269305 }
306+
307+ /// Attach a value to the current test.
308+ ///
309+ /// - Parameters:
310+ /// - attachableValue: The value to attach.
311+ /// - preferredName: The preferred name of the attachment when writing it to
312+ /// a test report or to disk. If `nil`, the testing library attempts to
313+ /// derive a reasonable filename for the attached value.
314+ /// - sourceLocation: The source location of the call to this function.
315+ ///
316+ /// When attaching a value of a type that does not conform to both
317+ /// [`Sendable`](https://developer.apple.com/documentation/swift/sendable) and
318+ /// [`Copyable`](https://developer.apple.com/documentation/swift/copyable),
319+ /// the testing library encodes it as data immediately. If the value cannot be
320+ /// encoded and an error is thrown, that error is recorded as an issue in the
321+ /// current test and the attachment is not written to the test report or to
322+ /// disk.
323+ ///
324+ /// This function creates a new instance of ``Attachment`` and immediately
325+ /// attaches it to the current test.
326+ ///
327+ /// An attachment can only be attached once.
328+ public static func record( _ attachableValue: consuming AttachableValue , named preferredName: String ? = nil , sourceLocation: SourceLocation = #_sourceLocation) {
329+ record ( Self ( attachableValue, named: preferredName) , sourceLocation: sourceLocation)
330+ }
270331}
271332
272333// MARK: - Getting the serialized form of an attachable value (generically)
@@ -286,10 +347,10 @@ extension Attachment where AttachableValue: ~Copyable {
286347 ///
287348 /// The testing library uses this function when writing an attachment to a
288349 /// test report or to a file on disk. This function calls the
289- /// ``Attachable/withUnsafeBufferPointer (for:_:)`` function on this
290- /// attachment's ``attachableValue-2tnj5`` property.
291- @inlinable public borrowing func withUnsafeBufferPointer < R> ( _ body: ( UnsafeRawBufferPointer ) throws -> R ) throws -> R {
292- try attachableValue. withUnsafeBufferPointer ( for: self , body)
350+ /// ``Attachable/withUnsafeBytes (for:_:)`` function on this attachment's
351+ /// ``attachableValue-2tnj5`` property.
352+ @inlinable public borrowing func withUnsafeBytes < R> ( _ body: ( UnsafeRawBufferPointer ) throws -> R ) throws -> R {
353+ try attachableValue. withUnsafeBytes ( for: self , body)
293354 }
294355}
295356
@@ -391,7 +452,7 @@ extension Attachment where AttachableValue: ~Copyable {
391452
392453 // There should be no code path that leads to this call where the attachable
393454 // value is nil.
394- try withUnsafeBufferPointer { buffer in
455+ try withUnsafeBytes { buffer in
395456 try file!. write ( buffer)
396457 }
397458
0 commit comments