|
12 | 12 |
|
13 | 13 | /// The memory layout of a type, describing its size, stride, and alignment.
|
14 | 14 | public enum MemoryLayout<T> {
|
15 |
| - |
16 |
| - /// The contiguous memory footprint of the type. |
| 15 | + /// The contiguous memory footprint of `T`. |
17 | 16 | ///
|
18 |
| - /// The `size` property for a type `T` does not include any |
19 |
| - /// dynamically-allocated or "remote" storage. In particular, |
20 |
| - /// `MemoryLayout<T>.size`, when `T` is a class type, is the same regardless |
21 |
| - /// of how many stored properties `T` has. |
| 17 | + /// Does not include any dynamically-allocated or "remote" storage. In |
| 18 | + /// particular, `MemoryLayout<T>.size`, when `T` is a class type, is the same |
| 19 | + /// regardless of how many stored properties `T` has. |
22 | 20 | @_transparent
|
23 | 21 | public static var size: Int {
|
24 | 22 | return Int(Builtin.sizeof(T.self))
|
25 | 23 | }
|
26 | 24 |
|
27 |
| - /// The number of bytes from the start of one instance to the start of the |
28 |
| - /// next, when stored in a contiguous array. |
| 25 | + /// The number of bytes from the start of one instance of `T` to the start of |
| 26 | + /// the next in an `Array<T>`. |
29 | 27 | ///
|
30 | 28 | /// This is the same as the number of bytes moved when an `UnsafePointer<T>`
|
31 |
| - /// is incremented. The type may have a lower minimal alignment that trades |
32 |
| - /// runtime performance for space efficiency. The result is always positive. |
| 29 | + /// is incremented. `T` may have a lower minimal alignment that trades runtime |
| 30 | + /// performance for space efficiency. The result is always positive. |
33 | 31 | @_transparent
|
34 | 32 | public static var stride: Int {
|
35 | 33 | return Int(Builtin.strideof_nonzero(T.self))
|
36 | 34 | }
|
37 | 35 |
|
38 |
| - /// The default memory alignment of the type. |
| 36 | + /// The default memory alignment of `T`. |
39 | 37 | @_transparent
|
40 | 38 | public static var alignment: Int {
|
41 | 39 | return Int(Builtin.alignof(T.self))
|
42 | 40 | }
|
43 | 41 | }
|
44 | 42 |
|
45 | 43 | extension MemoryLayout {
|
| 44 | + /// Returns the contiguous memory footprint of `T`. |
| 45 | + /// |
| 46 | + /// Does not include any dynamically-allocated or "remote" storage. In |
| 47 | + /// particular, `MemoryLayout.size(ofValue: x)`, when `x` is a class instance, |
| 48 | + /// is the same regardless of how many stored properties `T` has. |
| 49 | + @_transparent |
| 50 | + public static func size(ofValue _: T) -> Int { |
| 51 | + return MemoryLayout.size |
| 52 | + } |
| 53 | + |
| 54 | + /// Returns the number of bytes from the start of one instance of `T` to the |
| 55 | + /// start of the next in an `Array<T>`. |
| 56 | + /// |
| 57 | + /// This is the same as the number of bytes moved when an `UnsafePointer<T>` |
| 58 | + /// is incremented. `T` may have a lower minimal alignment that trades runtime |
| 59 | + /// performance for space efficiency. The result is always positive. |
| 60 | + @_transparent |
| 61 | + public static func stride(ofValue _: T) -> Int { |
| 62 | + return MemoryLayout.stride |
| 63 | + } |
| 64 | + |
| 65 | + /// Returns the default memory alignment of `T`. |
46 | 66 | @_transparent
|
47 |
| - public // @testable |
48 |
| - static func _ofInstance(_: @autoclosure () -> T) -> MemoryLayout<T>.Type { |
49 |
| - return MemoryLayout<T>.self |
| 67 | + public static func alignment(ofValue _: T) -> Int { |
| 68 | + return MemoryLayout.alignment |
50 | 69 | }
|
51 | 70 | }
|
0 commit comments