Numberick icon indicating copy to clipboard operation
Numberick copied to clipboard

Swift 5.9 parameter ownership modifiers

Open oscbyspro opened this issue 2 years ago • 1 comments

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 { ... }

oscbyspro avatar Aug 11 '23 11:08 oscbyspro

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.

oscbyspro avatar Oct 25 '23 17:10 oscbyspro