There are two pitches that would address this:
The later also has an implementation that you could use with some tweaks: https://github.com/swiftlang/swift/pull/63511/files