Skip to content
Closed
Show file tree
Hide file tree
Changes from 3 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
40 changes: 35 additions & 5 deletions Sources/W3CTraceContext/SpanID.swift
Original file line number Diff line number Diff line change
Expand Up @@ -16,11 +16,15 @@
///
/// [W3C TraceContext: parent-id](https://www.w3.org/TR/trace-context-1/#parent-id)
public struct SpanID: Sendable {
private let _bytes: Bytes
@usableFromInline
let _bytes: Bytes

/// An 8-byte array representation of the span ID.
public var bytes: [UInt8] {
withUnsafeBytes(of: _bytes, Array.init)
/// Calls the given closure with a pointer to the trace ID's underlying bytes.
///
/// - Parameter body: A closure receiving an `UnsafeRawBufferPointer` to the trace ID's underlying bytes.
@inlinable
public func withUnsafeBytes<T>(_ body: (UnsafeRawBufferPointer) throws -> T) rethrows -> T {
try Swift.withUnsafeBytes(of: _bytes, body)
}

/// Create a span ID from 8 bytes.
Expand Down Expand Up @@ -54,6 +58,32 @@ public struct SpanID: Sendable {
public typealias Bytes = (UInt8, UInt8, UInt8, UInt8, UInt8, UInt8, UInt8, UInt8)
}

extension SpanID: Collection {
public typealias Element = UInt8

public subscript(position: Int) -> UInt8 {
switch position {
case 0: _bytes.0
case 1: _bytes.1
case 2: _bytes.2
case 3: _bytes.3
case 4: _bytes.4
case 5: _bytes.5
case 6: _bytes.6
case 7: _bytes.7
default: fatalError("Index out of bounds")
}
}

public var startIndex: Int { 0 }
public var endIndex: Int { 8 }

public func index(after i: Int) -> Int {
precondition(i < endIndex, "Can't advance beyond endIndex")
return i + 1
}
}

extension SpanID: Equatable {
public static func == (lhs: Self, rhs: Self) -> Bool {
lhs._bytes.0 == rhs._bytes.0
Expand Down Expand Up @@ -81,7 +111,7 @@ extension SpanID: Hashable {
}

extension SpanID: Identifiable {
public var id: [UInt8] { bytes }
public var id: Self { self }
}

extension SpanID: CustomStringConvertible {
Expand Down
48 changes: 43 additions & 5 deletions Sources/W3CTraceContext/TraceID.swift
Original file line number Diff line number Diff line change
Expand Up @@ -16,11 +16,15 @@
///
/// [W3C TraceContext: trace-id](https://www.w3.org/TR/trace-context-1/#trace-id)
public struct TraceID: Sendable {
private let _bytes: Bytes
@usableFromInline
let _bytes: Bytes

/// A 16-byte array representation of the span ID.
public var bytes: [UInt8] {
withUnsafeBytes(of: _bytes, Array.init)
/// Calls the given closure with a pointer to the trace ID's underlying bytes.
///
/// - Parameter body: A closure receiving an `UnsafeRawBufferPointer` to the trace ID's underlying bytes.
@inlinable
public func withUnsafeBytes<T>(_ body: (UnsafeRawBufferPointer) throws -> T) rethrows -> T {
try Swift.withUnsafeBytes(of: _bytes, body)
}

/// Create a trace ID from 16 bytes.
Expand Down Expand Up @@ -58,6 +62,40 @@ public struct TraceID: Sendable {
)
}

extension TraceID: Collection {
public typealias Element = UInt8

public subscript(position: Int) -> UInt8 {
switch position {
case 0: _bytes.0
case 1: _bytes.1
case 2: _bytes.2
case 3: _bytes.3
case 4: _bytes.4
case 5: _bytes.5
case 6: _bytes.6
case 7: _bytes.7
case 8: _bytes.8
case 9: _bytes.9
case 10: _bytes.10
case 11: _bytes.11
case 12: _bytes.12
case 13: _bytes.13
case 14: _bytes.14
case 15: _bytes.15
default: fatalError("Index out of bounds")
}
}

public var startIndex: Int { 0 }
public var endIndex: Int { 16 }

public func index(after i: Int) -> Int {
precondition(i < endIndex, "Can't advance beyond endIndex")
return i + 1
}
}

extension TraceID: Equatable {
public static func == (lhs: Self, rhs: Self) -> Bool {
lhs._bytes.0 == rhs._bytes.0
Expand Down Expand Up @@ -101,7 +139,7 @@ extension TraceID: Hashable {
}

extension TraceID: Identifiable {
public var id: [UInt8] { bytes }
public var id: Self { self }
}

extension TraceID: CustomStringConvertible {
Expand Down
3 changes: 2 additions & 1 deletion Tests/W3CTraceContextTests/SpanIDTests.swift
Original file line number Diff line number Diff line change
Expand Up @@ -19,7 +19,8 @@ final class SpanIDTests: XCTestCase {
func test_bytes_returnsEightByteArrayRepresentation() {
let spanID = SpanID.oneToEight

XCTAssertEqual(spanID.bytes, [1, 2, 3, 4, 5, 6, 7, 8])
XCTAssertEqual(Array(spanID), [1, 2, 3, 4, 5, 6, 7, 8])
XCTAssertEqual(spanID.withUnsafeBytes(Array.init), [1, 2, 3, 4, 5, 6, 7, 8])
}

func test_equatableConformance() {
Expand Down
3 changes: 2 additions & 1 deletion Tests/W3CTraceContextTests/TraceIDTests.swift
Original file line number Diff line number Diff line change
Expand Up @@ -19,7 +19,8 @@ final class TraceIDTests: XCTestCase {
func test_bytes_returnsSixteenByteArrayRepresentation() {
let traceID = TraceID.oneToSixteen

XCTAssertEqual(traceID.bytes, [1, 2, 3, 4, 5, 6, 7, 8, 9, 10, 11, 12, 13, 14, 15, 16])
XCTAssertEqual(Array(traceID), [1, 2, 3, 4, 5, 6, 7, 8, 9, 10, 11, 12, 13, 14, 15, 16])
XCTAssertEqual(traceID.withUnsafeBytes(Array.init), [1, 2, 3, 4, 5, 6, 7, 8, 9, 10, 11, 12, 13, 14, 15, 16])
}

func test_equatableConformance() {
Expand Down