From 2db9e9d8f852f7d000f5387e9273f5f582f8e828 Mon Sep 17 00:00:00 2001 From: Konrad `ktoso` Malawski Date: Sun, 6 Nov 2022 18:56:47 +0900 Subject: [PATCH] + convenience APIs since most people will never use the underlying `current` value (#27) --- Sources/InstrumentationBaggage/Baggage.swift | 18 ++++++++++++++++++ 1 file changed, 18 insertions(+) diff --git a/Sources/InstrumentationBaggage/Baggage.swift b/Sources/InstrumentationBaggage/Baggage.swift index de023dc..f3af187 100644 --- a/Sources/InstrumentationBaggage/Baggage.swift +++ b/Sources/InstrumentationBaggage/Baggage.swift @@ -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(_ 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(_ value: Baggage?, operation: () async throws -> T) async rethrows -> T { + try await Baggage.$current.withValue(value, operation: operation) + } } #endif