Skip to content

Instantly share code, notes, and snippets.

View tayloraswift's full-sized avatar

taylorswift tayloraswift

View GitHub Profile
@tayloraswift
tayloraswift / swiftpointers3.md
Last active August 13, 2017 00:11
Even better Swift pointers

Improved pointers

Introduction

Swift’s pointer types are an important interface for low-level memory manipulation, but the current API design is not very consistent, complete, or convenient. Many memory methods demand a capacity: or count: argument, forcing the user to manually track the size of the memory block, even though most of the time this is either unnecessary, or redundant as buffer pointers track this information natively. In some places, poor naming choices and overengineered function signatures compromise memory safety by leading users to believe that they have allocated or freed memory when in fact, they have not.

enum Math<F> where F:FloatingPoint
{
typealias V2 = (x:F, y:F)
typealias V3 = (x:F, y:F, z:F)
@inline(__always)
static
func add(_ v1:V3, _ v2:V3) -> V3
{
return (v1.x + v2.x, v1.y + v2.y, v1.z + v2.z)
// whether or not the string conversions live in the main definition depends on whether
// or not the strings are used for debugging/UI purposes, or have an actual internal role
// in the program
enum CasualAnswer
{
case noWay,
yeahSure
// Supported conversions from: Bool
init(_ value:Bool)

More improved Swift pointers

Introduction

Swift’s pointer types are an important interface for low-level memory manipulation, but the current API design is not very consistent, or convenient. Many memory methods demand a capacity: or count: argument, forcing the user to manually track the size of the memory block, even though most of the time this is either unnecessary, or redundant as buffer pointers track this information natively. In some places, poor naming choices and overengineered function signatures compromise memory safety by leading users to believe that they have allocated or freed memory when in fact, they have not.

Improved Swift pointers

  • Proposal: SE-1989
  • Authors: Kelvin Ma
  • Review Manager: TBD
  • Status: Awaiting review

Introduction

Swift currently offers two sets of pointer types — singular pointers such as UnsafeMutablePointer, and vector (buffer) pointers such as UnsafeMutableBufferPointer. This implies a natural separation of tasks the two kinds of pointers are meant to do. For example, buffer pointers implement Collection conformance, while singular pointers do not.