Skip to content

[stdlib] Default initializers for Span-family types #82866

New issue

Have a question about this project? Sign up for a free GitHub account to open an issue and contact its maintainers and the community.

By clicking “Sign up for GitHub”, you agree to our terms of service and privacy statement. We’ll occasionally send you account related emails.

Already on GitHub? Sign in to your account

Merged
Merged
Show file tree
Hide file tree
Changes from all commits
Commits
File filter

Filter by extension

Filter by extension

Conversations
Failed to load comments.
Loading
Jump to
Jump to file
Failed to load files.
Loading
Diff view
Diff view
8 changes: 8 additions & 0 deletions stdlib/public/core/Span/MutableRawSpan.swift
Original file line number Diff line number Diff line change
Expand Up @@ -32,6 +32,14 @@ public struct MutableRawSpan: ~Copyable & ~Escapable {
unsafe _pointer._unsafelyUnwrappedUnchecked
}

@_alwaysEmitIntoClient
@inline(__always)
@lifetime(immortal)
public init() {
unsafe _pointer = nil
_count = 0
}

@unsafe
@_unsafeNonescapableResult
@_alwaysEmitIntoClient
Expand Down
8 changes: 8 additions & 0 deletions stdlib/public/core/Span/MutableSpan.swift
Original file line number Diff line number Diff line change
Expand Up @@ -33,6 +33,14 @@ public struct MutableSpan<Element: ~Copyable>
unsafe _pointer._unsafelyUnwrappedUnchecked
}

@_alwaysEmitIntoClient
@inline(__always)
@lifetime(immortal)
public init() {
unsafe _pointer = nil
_count = 0
}

@unsafe
@_unsafeNonescapableResult
@_alwaysEmitIntoClient
Expand Down
2 changes: 1 addition & 1 deletion stdlib/public/core/Span/RawSpan.swift
Original file line number Diff line number Diff line change
Expand Up @@ -54,7 +54,7 @@ public struct RawSpan: ~Escapable, Copyable, BitwiseCopyable {
@_alwaysEmitIntoClient
@inline(__always)
@lifetime(immortal)
internal init() {
public init() {
unsafe _pointer = nil
_count = 0
}
Expand Down
2 changes: 1 addition & 1 deletion stdlib/public/core/Span/Span.swift
Original file line number Diff line number Diff line change
Expand Up @@ -55,7 +55,7 @@ public struct Span<Element: ~Copyable>: ~Escapable, Copyable, BitwiseCopyable {
@_alwaysEmitIntoClient
@inline(__always)
@lifetime(immortal)
internal init() {
public init() {
unsafe _pointer = nil
_count = 0
}
Expand Down
5 changes: 4 additions & 1 deletion test/stdlib/Span/MutableRawSpanTests.swift
Original file line number Diff line number Diff line change
Expand Up @@ -29,10 +29,13 @@ suite.test("Basic Initializer")

var s = Array("\(#file)+\(#function)--\(Int.random(in: 1000...9999))".utf8)
s.withUnsafeMutableBytes {
let b = MutableRawSpan(_unsafeBytes: $0)
var b = MutableRawSpan(_unsafeBytes: $0)
expectEqual(b.byteCount, $0.count)
expectFalse(b.isEmpty)
expectEqual(b.byteOffsets, 0..<$0.count)

b = MutableRawSpan()
expectEqual(b.byteCount, 0)
}
}

Expand Down
8 changes: 5 additions & 3 deletions test/stdlib/Span/MutableSpanTests.swift
Original file line number Diff line number Diff line change
Expand Up @@ -30,9 +30,11 @@ suite.test("Initialize with ordinary element")
let capacity = 4
var s = (0..<capacity).map({ "\(#file)+\(#function)--\($0)" })
s.withUnsafeMutableBufferPointer {
let b = MutableSpan(_unsafeElements: $0)
let c = b.count
expectEqual(c, $0.count)
var b = MutableSpan(_unsafeElements: $0)
expectEqual(b.count, $0.count)

b = MutableSpan()
expectEqual(b.count, 0)
}
}

Expand Down
5 changes: 4 additions & 1 deletion test/stdlib/Span/RawSpanTests.swift
Original file line number Diff line number Diff line change
Expand Up @@ -30,9 +30,12 @@ suite.test("Initialize with Span<Int>")
let capacity = 4
Array(0..<capacity).withUnsafeBufferPointer {
let intSpan = Span(_unsafeElements: $0)
let span = RawSpan(_elements: intSpan)
var span = RawSpan(_elements: intSpan)
expectEqual(span.byteCount, capacity*MemoryLayout<Int>.stride)
expectFalse(span.isEmpty)

span = RawSpan()
expectTrue(span.isEmpty)
}

let a: [Int] = []
Expand Down
3 changes: 3 additions & 0 deletions test/stdlib/Span/SpanTests.swift
Original file line number Diff line number Diff line change
Expand Up @@ -120,6 +120,9 @@ suite.test("Initialize with ordinary element")
let pointer: UnsafeMutablePointer = $0.baseAddress!
span = Span(_unsafeStart: pointer, count: $0.count)
expectEqual(span.count, capacity)

span = Span()
expectEqual(span.count, 0)
}
}

Expand Down