Skip to content

Commit

Permalink
+ convenience APIs since most people will never use the underlying `c…
Browse files Browse the repository at this point in the history
…urrent` value (#27)
  • Loading branch information
ktoso authored Nov 6, 2022
1 parent 6c545ed commit 2db9e9d
Showing 1 changed file with 18 additions and 0 deletions.
18 changes: 18 additions & 0 deletions Sources/InstrumentationBaggage/Baggage.swift
Original file line number Diff line number Diff line change
Expand Up @@ -236,5 +236,23 @@ extension Baggage {
/// A `Baggage` automatically propagated through task-local storage. This API enables binding a top-level `Baggage` and passing it
/// implicitly to any child tasks when using structured concurrency.
@TaskLocal public static var current: Baggage?

/// Convenience API to bind the task-local ``Baggage/current`` to the passed `value`, and execute the passed `operation`.
///
/// To access the task-local value, use `Baggage.current`.
///
/// SeeAlso: [Swift Task Locals](https://developer.apple.com/documentation/swift/tasklocal)
func withValue<T>(_ value: Baggage?, operation: () throws -> T) rethrows -> T {
try Baggage.$current.withValue(value, operation: operation)
}

/// Convenience API to bind the task-local ``Baggage/current`` to the passed `value`, and execute the passed `operation`.
///
/// To access the task-local value, use `Baggage.current`.
///
/// SeeAlso: [Swift Task Locals](https://developer.apple.com/documentation/swift/tasklocal)
func withValue<T>(_ value: Baggage?, operation: () async throws -> T) async rethrows -> T {
try await Baggage.$current.withValue(value, operation: operation)
}
}
#endif

0 comments on commit 2db9e9d

Please sign in to comment.