2
2
//
3
3
// This source file is part of the Swift.org open source project
4
4
//
5
- // Copyright (c) 2014 - 2017 Apple Inc. and the Swift project authors
5
+ // Copyright (c) 2014 - 2024 Apple Inc. and the Swift project authors
6
6
// Licensed under Apache License v2.0 with Runtime Library Exception
7
7
//
8
8
// See https://swift.org/LICENSE.txt for license information
40
40
/// byteCount: count * MemoryLayout<Point>.stride,
41
41
/// alignment: MemoryLayout<Point>.alignment)
42
42
@frozen // namespace
43
- public enum MemoryLayout < T> {
43
+ public enum MemoryLayout < T: ~ Copyable> { }
44
+
45
+ extension MemoryLayout where T: ~ Copyable {
44
46
/// The contiguous memory footprint of `T`, in bytes.
45
47
///
46
48
/// A type's size does not include any dynamically allocated or out of line
@@ -50,6 +52,7 @@ public enum MemoryLayout<T> {
50
52
/// When allocating memory for multiple instances of `T` using an unsafe
51
53
/// pointer, use a multiple of the type's stride instead of its size.
52
54
@_transparent
55
+ @_alwaysEmitIntoClient
53
56
public static var size : Int {
54
57
return Int ( Builtin . sizeof ( T . self) )
55
58
}
@@ -62,6 +65,7 @@ public enum MemoryLayout<T> {
62
65
/// trades runtime performance for space efficiency. This value is always
63
66
/// positive.
64
67
@_transparent
68
+ @_alwaysEmitIntoClient
65
69
public static var stride : Int {
66
70
return Int ( Builtin . strideof ( T . self) )
67
71
}
@@ -71,6 +75,7 @@ public enum MemoryLayout<T> {
71
75
/// Use the `alignment` property for a type when allocating memory using an
72
76
/// unsafe pointer. This value is always positive.
73
77
@_transparent
78
+ @_alwaysEmitIntoClient
74
79
public static var alignment : Int {
75
80
return Int ( Builtin . alignof ( T . self) )
76
81
}
@@ -80,6 +85,29 @@ public enum MemoryLayout<T> {
80
85
extension MemoryLayout : _BitwiseCopyable { }
81
86
82
87
extension MemoryLayout {
88
+ // TODO: Merge this back into the noncopyable variant once we have @_preInverseGenerics
89
+ @_spi ( SwiftStdlibLegacyABI) @available ( swift, obsoleted: 2 )
90
+ @usableFromInline
91
+ internal static var size : Int {
92
+ return Int ( Builtin . sizeof ( T . self) )
93
+ }
94
+
95
+ // TODO: Merge this back into the noncopyable variant once we have @_preInverseGenerics
96
+ @_spi ( SwiftStdlibLegacyABI) @available ( swift, obsoleted: 2 )
97
+ @usableFromInline
98
+ internal static var stride : Int {
99
+ return Int ( Builtin . strideof ( T . self) )
100
+ }
101
+
102
+ // TODO: Merge this back into the noncopyable variant once we have @_preInverseGenerics
103
+ @_spi ( SwiftStdlibLegacyABI) @available ( swift, obsoleted: 2 )
104
+ @usableFromInline
105
+ internal static var alignment : Int {
106
+ return Int ( Builtin . alignof ( T . self) )
107
+ }
108
+ }
109
+
110
+ extension MemoryLayout where T: ~ Copyable {
83
111
/// Returns the contiguous memory footprint of the given instance.
84
112
///
85
113
/// The result does not include any dynamically allocated or out of line
@@ -103,7 +131,8 @@ extension MemoryLayout {
103
131
/// - Parameter value: A value representative of the type to describe.
104
132
/// - Returns: The size, in bytes, of the given value's type.
105
133
@_transparent
106
- public static func size( ofValue value: T ) -> Int {
134
+ @_alwaysEmitIntoClient
135
+ public static func size( ofValue value: borrowing T ) -> Int {
107
136
return MemoryLayout . size
108
137
}
109
138
@@ -131,7 +160,8 @@ extension MemoryLayout {
131
160
/// - Parameter value: A value representative of the type to describe.
132
161
/// - Returns: The stride, in bytes, of the given value's type.
133
162
@_transparent
134
- public static func stride( ofValue value: T ) -> Int {
163
+ @_alwaysEmitIntoClient
164
+ public static func stride( ofValue value: borrowing T ) -> Int {
135
165
return MemoryLayout . stride
136
166
}
137
167
@@ -156,10 +186,36 @@ extension MemoryLayout {
156
186
/// - Returns: The default memory alignment, in bytes, of the given value's
157
187
/// type. This value is always positive.
158
188
@_transparent
159
- public static func alignment( ofValue value: T ) -> Int {
189
+ @_alwaysEmitIntoClient
190
+ public static func alignment( ofValue value: borrowing T ) -> Int {
160
191
return MemoryLayout . alignment
161
192
}
193
+ }
162
194
195
+ extension MemoryLayout {
196
+ // TODO: Merge this back into the noncopyable variant once we have @_preInverseGenerics
197
+ @_spi ( SwiftStdlibLegacyABI) @available ( swift, obsoleted: 2 )
198
+ @usableFromInline
199
+ internal static func size( ofValue value: borrowing T ) -> Int {
200
+ return MemoryLayout . size
201
+ }
202
+
203
+ // TODO: Merge this back into the noncopyable variant once we have @_preInverseGenerics
204
+ @_spi ( SwiftStdlibLegacyABI) @available ( swift, obsoleted: 2 )
205
+ @usableFromInline
206
+ internal static func stride( ofValue value: borrowing T ) -> Int {
207
+ return MemoryLayout . stride
208
+ }
209
+
210
+ // TODO: Merge this back into the noncopyable variant once we have @_preInverseGenerics
211
+ @_spi ( SwiftStdlibLegacyABI) @available ( swift, obsoleted: 2 )
212
+ @usableFromInline
213
+ internal static func alignment( ofValue value: borrowing T ) -> Int {
214
+ return MemoryLayout . alignment
215
+ }
216
+ }
217
+
218
+ extension MemoryLayout {
163
219
/// Returns the offset of an inline stored property within a type's in-memory
164
220
/// representation.
165
221
///
@@ -229,12 +285,15 @@ extension MemoryLayout {
229
285
@_transparent
230
286
@_unavailableInEmbedded
231
287
public static func offset( of key: PartialKeyPath < T > ) -> Int ? {
288
+ // FIXME(noncopyableGenerics): The new (implicit) `where T: Copyable`
289
+ // extension constraint currently changes the mangling of this from a
290
+ // standalone function to an extension method.
232
291
return key. _storedInlineOffset
233
292
}
234
293
}
235
294
236
295
// Not-yet-public alignment conveniences
237
- extension MemoryLayout {
296
+ extension MemoryLayout where T : ~ Copyable {
238
297
internal static var _alignmentMask : Int { return alignment - 1 }
239
298
240
299
internal static func _roundingUpToAlignment( _ value: Int ) -> Int {
0 commit comments