Numberick
Numberick copied to clipboard
Swift 5.9 parameter ownership modifiers
Introduction
Swift 5.9 (SE-0377) introduces borrowing and consuming parameter ownership modifiers.
An immediate use case
I'm working on some utility methods with the following inout convention:
@inlinable public static func incrementSufficientUnsignedInteger<T>(
_ pointee: inout T, by digit: T.Element, plus bit: inout Bool, at index: inout T.Index)
where T: MutableCollection, T.Element: NBKFixedWidthInteger & NBKUnsignedInteger { ... }
These are used instead of the much more ergonomic return convention:
@inlinable public static func incrementSufficientUnsignedInteger<T>(
_ pointee: inout T, by digit: T.Element, plus bit: Bool, at index: T.Index) -> (index: T.Index, overflow: Bool)
where T: MutableCollection, T.Element: NBKFixedWidthInteger & NBKUnsignedInteger { ... }
I've tried every reasonable combination of attributes, but can't make it perform as well as the inout version. I assume this is related to parameter ownership, and that I want to consume the arguments. I'm not sure this is the solution, but I hope so:
@inlinable public static func incrementSufficientUnsignedInteger<T>(
_ pointee: inout T, by digit: consuming T.Element, plus bit: consuming Bool, at index: consuming T.Index) -> (index: T.Index, overflow: Bool)
where T: MutableCollection, T.Element: NBKFixedWidthInteger & NBKUnsignedInteger { ... }
Hm. I'm not convinced it's a solution to the problem mentioned, but there are still other uses for it. More so with UIntXL (#33), where it is important to avoid unnecessary copy-on-writes.