File: lifetime_underscored_dependence.swift

package info (click to toggle)
swiftlang 6.2.3-1
  • links: PTS, VCS
  • area: main
  • in suites: forky, sid
  • size: 2,856,264 kB
  • sloc: cpp: 9,995,718; ansic: 2,234,019; asm: 1,092,167; python: 313,940; objc: 82,726; f90: 80,126; lisp: 38,373; pascal: 25,580; sh: 20,378; ml: 5,058; perl: 4,751; makefile: 4,725; awk: 3,535; javascript: 3,018; xml: 918; fortran: 664; cs: 573; ruby: 396
file content (79 lines) | stat: -rw-r--r-- 2,273 bytes parent folder | download
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
60
61
62
63
64
65
66
67
68
69
70
71
72
73
74
75
76
77
78
79
public struct AnotherView : ~Escapable {
  @usableFromInline let _ptr: UnsafeRawBufferPointer
  @usableFromInline let _count: Int
  @_lifetime(borrow ptr)
  internal init(_ ptr: UnsafeRawBufferPointer, _ count: Int) {
    self._ptr = ptr
    self._count = count
  }
}

public struct BufferView : ~Escapable {
  @usableFromInline let _ptr: UnsafeRawBufferPointer
  @usableFromInline let _count: Int
  @usableFromInline 
  @_lifetime(borrow ptr)
  internal init(_ ptr: UnsafeRawBufferPointer, _ count: Int) {
    self._ptr = ptr
    self._count = count
  }

  @inlinable
  @_lifetime(borrow a)
  internal init(_ ptr: UnsafeRawBufferPointer, _ a: borrowing Array<Int>) {
    let bv = BufferView(ptr, a.count)
    self = _overrideLifetime(bv, borrowing: a)
  }
  @inlinable
  @_lifetime(copy a)
  internal init(_ ptr: UnsafeRawBufferPointer, _ a: consuming AnotherView) {
    let bv = BufferView(ptr, a._count)
    self = _overrideLifetime(bv, copying: a)
  }
}

@inlinable
@_lifetime(copy x)
public func derive(_ x: consuming BufferView) -> BufferView {
  let pointer = x._ptr
  let bv = BufferView(pointer, x._count)
  return _overrideLifetime(bv, copying: x)
}

@inlinable
public func use(_ x: consuming BufferView) {}

@inlinable
@_lifetime(copy view)
public func consumeAndCreate(_ view: consuming BufferView) -> BufferView {
  let pointer = view._ptr
  let bv = BufferView(pointer, view._count)
  return _overrideLifetime(bv, copying: view)
}

// FIXME: Filed rdar://150398673 ([nonescapable] allocbox-to-stack fails causing lifetime diagnostics to fail)
// Remove _overrideLifetime when this is fixed.
@inlinable
@_lifetime(copy this, copy that)
public func deriveThisOrThat(_ this: consuming BufferView, _ that: consuming BufferView) -> BufferView {
  if (Int.random(in: 1..<100) == 0) {
    let thisView = BufferView(this._ptr, this._count)
    return _overrideLifetime(thisView, copying: this)
  }
  let thatView = BufferView(that._ptr, that._count)
  return _overrideLifetime(thatView, copying: that)
}

public struct Container {
  var buffer: UnsafeRawBufferPointer
  var object: AnyObject
}

extension Container {
  public var storage: BufferView {
    get {
      let view = BufferView(buffer, 1)
      return _overrideLifetime(view, borrowing: self)
    }
  }
}