Skip to content

Commit 2db9e9d

Browse files
authored
+ convenience APIs since most people will never use the underlying current value (#27)
1 parent 6c545ed commit 2db9e9d

File tree

1 file changed

+18
-0
lines changed

1 file changed

+18
-0
lines changed

Sources/InstrumentationBaggage/Baggage.swift

Lines changed: 18 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -236,5 +236,23 @@ extension Baggage {
236236
/// A `Baggage` automatically propagated through task-local storage. This API enables binding a top-level `Baggage` and passing it
237237
/// implicitly to any child tasks when using structured concurrency.
238238
@TaskLocal public static var current: Baggage?
239+
240+
/// Convenience API to bind the task-local ``Baggage/current`` to the passed `value`, and execute the passed `operation`.
241+
///
242+
/// To access the task-local value, use `Baggage.current`.
243+
///
244+
/// SeeAlso: [Swift Task Locals](https://developer.apple.com/documentation/swift/tasklocal)
245+
func withValue<T>(_ value: Baggage?, operation: () throws -> T) rethrows -> T {
246+
try Baggage.$current.withValue(value, operation: operation)
247+
}
248+
249+
/// Convenience API to bind the task-local ``Baggage/current`` to the passed `value`, and execute the passed `operation`.
250+
///
251+
/// To access the task-local value, use `Baggage.current`.
252+
///
253+
/// SeeAlso: [Swift Task Locals](https://developer.apple.com/documentation/swift/tasklocal)
254+
func withValue<T>(_ value: Baggage?, operation: () async throws -> T) async rethrows -> T {
255+
try await Baggage.$current.withValue(value, operation: operation)
256+
}
239257
}
240258
#endif

0 commit comments

Comments
 (0)