25
25
/// type cannot conform directly to this protocol (such as a non-final class or
26
26
/// a type declared in a third-party module), you can create a container type
27
27
/// that conforms to ``AttachableContainer`` to act as a proxy.
28
- @_spi ( Experimental)
28
+ ///
29
+ /// @Metadata {
30
+ /// @Available(Swift, introduced: 6.2)
31
+ /// }
29
32
public protocol Attachable : ~ Copyable {
30
33
/// An estimate of the number of bytes of memory needed to store this value as
31
34
/// an attachment.
@@ -41,6 +44,10 @@ public protocol Attachable: ~Copyable {
41
44
///
42
45
/// - Complexity: O(1) unless `Self` conforms to `Collection`, in which case
43
46
/// up to O(_n_) where _n_ is the length of the collection.
47
+ ///
48
+ /// @Metadata {
49
+ /// @Available(Swift, introduced: 6.2)
50
+ /// }
44
51
var estimatedAttachmentByteCount : Int ? { get }
45
52
46
53
/// Call a function and pass a buffer representing this instance to it.
@@ -63,7 +70,11 @@ public protocol Attachable: ~Copyable {
63
70
/// the buffer to contain an image in PNG format, JPEG format, etc., but it
64
71
/// would not be idiomatic for the buffer to contain a textual description of
65
72
/// the image.
66
- borrowing func withUnsafeBufferPointer< R> ( for attachment: borrowing Attachment < Self > , _ body: ( UnsafeRawBufferPointer ) throws -> R ) throws -> R
73
+ ///
74
+ /// @Metadata {
75
+ /// @Available(Swift, introduced: 6.2)
76
+ /// }
77
+ borrowing func withUnsafeBytes< R> ( for attachment: borrowing Attachment < Self > , _ body: ( UnsafeRawBufferPointer ) throws -> R ) throws -> R
67
78
68
79
/// Generate a preferred name for the given attachment.
69
80
///
@@ -79,6 +90,10 @@ public protocol Attachable: ~Copyable {
79
90
/// when adding `attachment` to a test report or persisting it to storage. The
80
91
/// default implementation of this function returns `suggestedName` without
81
92
/// any changes.
93
+ ///
94
+ /// @Metadata {
95
+ /// @Available(Swift, introduced: 6.2)
96
+ /// }
82
97
borrowing func preferredName( for attachment: borrowing Attachment < Self > , basedOn suggestedName: String ) -> String
83
98
}
84
99
@@ -99,7 +114,7 @@ extension Attachable where Self: Collection, Element == UInt8 {
99
114
count
100
115
}
101
116
102
- // We do not provide an implementation of withUnsafeBufferPointer (for:_:) here
117
+ // We do not provide an implementation of withUnsafeBytes (for:_:) here
103
118
// because there is no way in the standard library to statically detect if a
104
119
// collection can provide contiguous storage (_HasContiguousBytes is not API.)
105
120
// If withContiguousStorageIfAvailable(_:) fails, we don't want to make a
@@ -118,40 +133,35 @@ extension Attachable where Self: StringProtocol {
118
133
119
134
// Implement the protocol requirements for byte arrays and buffers so that
120
135
// developers can attach raw data when needed.
121
- @_spi ( Experimental)
122
136
extension Array < UInt8 > : Attachable {
123
- public func withUnsafeBufferPointer < R> ( for attachment: borrowing Attachment < Self > , _ body: ( UnsafeRawBufferPointer ) throws -> R ) throws -> R {
137
+ public func withUnsafeBytes < R> ( for attachment: borrowing Attachment < Self > , _ body: ( UnsafeRawBufferPointer ) throws -> R ) throws -> R {
124
138
try withUnsafeBytes ( body)
125
139
}
126
140
}
127
141
128
- @_spi ( Experimental)
129
142
extension ContiguousArray < UInt8 > : Attachable {
130
- public func withUnsafeBufferPointer < R> ( for attachment: borrowing Attachment < Self > , _ body: ( UnsafeRawBufferPointer ) throws -> R ) throws -> R {
143
+ public func withUnsafeBytes < R> ( for attachment: borrowing Attachment < Self > , _ body: ( UnsafeRawBufferPointer ) throws -> R ) throws -> R {
131
144
try withUnsafeBytes ( body)
132
145
}
133
146
}
134
147
135
- @_spi ( Experimental)
136
148
extension ArraySlice < UInt8 > : Attachable {
137
- public func withUnsafeBufferPointer < R> ( for attachment: borrowing Attachment < Self > , _ body: ( UnsafeRawBufferPointer ) throws -> R ) throws -> R {
149
+ public func withUnsafeBytes < R> ( for attachment: borrowing Attachment < Self > , _ body: ( UnsafeRawBufferPointer ) throws -> R ) throws -> R {
138
150
try withUnsafeBytes ( body)
139
151
}
140
152
}
141
153
142
- @_spi ( Experimental)
143
154
extension String : Attachable {
144
- public func withUnsafeBufferPointer < R> ( for attachment: borrowing Attachment < Self > , _ body: ( UnsafeRawBufferPointer ) throws -> R ) throws -> R {
155
+ public func withUnsafeBytes < R> ( for attachment: borrowing Attachment < Self > , _ body: ( UnsafeRawBufferPointer ) throws -> R ) throws -> R {
145
156
var selfCopy = self
146
157
return try selfCopy. withUTF8 { utf8 in
147
158
try body ( UnsafeRawBufferPointer ( utf8) )
148
159
}
149
160
}
150
161
}
151
162
152
- @_spi ( Experimental)
153
163
extension Substring : Attachable {
154
- public func withUnsafeBufferPointer < R> ( for attachment: borrowing Attachment < Self > , _ body: ( UnsafeRawBufferPointer ) throws -> R ) throws -> R {
164
+ public func withUnsafeBytes < R> ( for attachment: borrowing Attachment < Self > , _ body: ( UnsafeRawBufferPointer ) throws -> R ) throws -> R {
155
165
var selfCopy = self
156
166
return try selfCopy. withUTF8 { utf8 in
157
167
try body ( UnsafeRawBufferPointer ( utf8) )
0 commit comments