-
Notifications
You must be signed in to change notification settings - Fork 10.5k
/
Copy pathbitwise_copyable.swift
59 lines (51 loc) · 1.69 KB
/
bitwise_copyable.swift
1
2
3
4
5
6
7
8
9
10
11
12
13
14
15
16
17
18
19
20
21
22
23
24
25
26
27
28
29
30
31
32
33
34
35
36
37
38
39
40
41
42
43
44
45
46
47
48
49
50
51
52
53
54
55
56
57
58
59
// RUN: %target-swift-frontend \
// RUN: %s \
// RUN: -emit-sil \
// RUN: -enable-experimental-feature ValueGenerics \
// RUN: -enable-builtin-module \
// RUN: -disable-availability-checking \
// RUN: -O
// REQUIRES: swift_feature_ValueGenerics
// Force verification of TypeLowering's isTrivial.
import Builtin
@frozen
public struct Vector<let Count: Int, Element: ~Copyable>: ~Copyable {
private var storage: Builtin.FixedArray<Count, Element>
public init(_ valueForIndex: (Int) -> Element) {
storage = Builtin.emplace { rawPointer in
let base = UnsafeMutablePointer<Element>(rawPointer)
for i in 0..<Count {
(base + i).initialize(to: valueForIndex(i))
}
}
}
public subscript(i: Int) -> Element {
_read {
assert(i >= 0 && i < Count)
let rawPointer = Builtin.addressOfBorrow(self)
let base = UnsafePointer<Element>(rawPointer)
yield ((base + i).pointee)
}
_modify {
assert(i >= 0 && i < Count)
let rawPointer = Builtin.addressof(&self)
let base = UnsafeMutablePointer<Element>(rawPointer)
yield (&(base + i).pointee)
}
}
}
extension Vector: Copyable where Element: Copyable {
public init(repeating value: Element) {
self.init { _ in value }
}
}
extension Vector: BitwiseCopyable where Element: BitwiseCopyable {}
func main() {
var v = Vector<4, String>(repeating: "x") // OK
//v[0] = 1
//v[1] = 2
//v[2] = 3
//v[3] = 4
print("break here")
}
main()