Skip to content

[SE-0127] changes to withUnsafe[Mutable]Pointer #481

New issue

Have a question about this project? Sign up for a free GitHub account to open an issue and contact its maintainers and the community.

By clicking “Sign up for GitHub”, you agree to our terms of service and privacy statement. We’ll occasionally send you account related emails.

Already on GitHub? Sign in to your account

Merged
merged 2 commits into from
Jul 27, 2016
Merged
Show file tree
Hide file tree
Changes from all commits
Commits
File filter

Filter by extension

Filter by extension

Conversations
Failed to load comments.
Loading
Jump to
Jump to file
Failed to load files.
Loading
Diff view
Diff view
2 changes: 1 addition & 1 deletion Foundation/DateInterval.swift
Original file line number Diff line number Diff line change
Expand Up @@ -155,7 +155,7 @@ public struct DateInterval : ReferenceConvertible, Comparable, Hashable {

public var hashValue: Int {
var buf: (UInt, UInt) = (UInt(start.timeIntervalSinceReferenceDate), UInt(end.timeIntervalSinceReferenceDate))
return withUnsafeMutablePointer(&buf) {
return withUnsafeMutablePointer(to: &buf) {
return Int(bitPattern: CFHashBytes(unsafeBitCast($0, to: UnsafeMutablePointer<UInt8>.self), CFIndex(sizeof(UInt.self) * 2)))
}
}
Expand Down
2 changes: 1 addition & 1 deletion Foundation/NSArray.swift
Original file line number Diff line number Diff line change
Expand Up @@ -69,7 +69,7 @@ public class NSArray : NSObject, NSCopying, NSMutableCopying, NSSecureCoding, NS
// We're stuck with (int) here (rather than unsigned int)
// because that's the way the code was originally written, unless
// we go to a new version of the class, which has its own problems.
withUnsafeMutablePointer(&cnt) { (ptr: UnsafeMutablePointer<UInt32>) -> Void in
withUnsafeMutablePointer(to: &cnt) { (ptr: UnsafeMutablePointer<UInt32>) -> Void in
aDecoder.decodeValue(ofObjCType: "i", at: UnsafeMutableRawPointer(ptr))
}
let objects = UnsafeMutablePointer<AnyObject?>.allocate(capacity: Int(cnt))
Expand Down
4 changes: 2 additions & 2 deletions Foundation/NSAttributedString.swift
Original file line number Diff line number Diff line change
Expand Up @@ -119,7 +119,7 @@ private extension AttributedString {

func _attributesAtIndex(_ location: Int, rangeInfo: RangeInfo) -> [String : AnyObject] {
var cfRange = CFRange()
return withUnsafeMutablePointer(&cfRange) { (cfRangePointer: UnsafeMutablePointer<CFRange>) -> [String : AnyObject] in
return withUnsafeMutablePointer(to: &cfRange) { (cfRangePointer: UnsafeMutablePointer<CFRange>) -> [String : AnyObject] in
// Get attributes value using CoreFoundation function
let value: CFDictionary
if rangeInfo.shouldFetchLongestEffectiveRange, let searchRange = rangeInfo.longestEffectiveRangeSearchRange {
Expand Down Expand Up @@ -149,7 +149,7 @@ private extension AttributedString {

func _attribute(_ attrName: String, atIndex location: Int, rangeInfo: RangeInfo) -> AnyObject? {
var cfRange = CFRange()
return withUnsafeMutablePointer(&cfRange) { (cfRangePointer: UnsafeMutablePointer<CFRange>) -> AnyObject? in
return withUnsafeMutablePointer(to: &cfRange) { (cfRangePointer: UnsafeMutablePointer<CFRange>) -> AnyObject? in
// Get attribute value using CoreFoundation function
let attribute: AnyObject?
if rangeInfo.shouldFetchLongestEffectiveRange, let searchRange = rangeInfo.longestEffectiveRangeSearchRange {
Expand Down
4 changes: 2 additions & 2 deletions Foundation/NSBundle.swift
Original file line number Diff line number Diff line change
Expand Up @@ -78,7 +78,7 @@ public class Bundle: NSObject {

public func preflight() throws {
var unmanagedError:Unmanaged<CFError>? = nil
try withUnsafeMutablePointer(&unmanagedError) { (unmanagedCFError: UnsafeMutablePointer<Unmanaged<CFError>?>) in
try withUnsafeMutablePointer(to: &unmanagedError) { (unmanagedCFError: UnsafeMutablePointer<Unmanaged<CFError>?>) in
CFBundlePreflightExecutable(_bundle, unmanagedCFError)
if let error = unmanagedCFError.pointee {
throw error.takeRetainedValue()._nsObject
Expand All @@ -88,7 +88,7 @@ public class Bundle: NSObject {

public func loadAndReturnError() throws {
var unmanagedError:Unmanaged<CFError>? = nil
try withUnsafeMutablePointer(&unmanagedError) { (unmanagedCFError: UnsafeMutablePointer<Unmanaged<CFError>?>) in
try withUnsafeMutablePointer(to: &unmanagedError) { (unmanagedCFError: UnsafeMutablePointer<Unmanaged<CFError>?>) in
CFBundleLoadExecutableAndReturnError(_bundle, unmanagedCFError)
if let error = unmanagedCFError.pointee {
let retainedValue = error.takeRetainedValue()
Expand Down
10 changes: 5 additions & 5 deletions Foundation/NSCalendar.swift
Original file line number Diff line number Diff line change
Expand Up @@ -394,8 +394,8 @@ public class Calendar: NSObject, NSCopying, NSSecureCoding {
public func range(of unit: Unit, forDate date: Date) -> DateInterval? {
var start: CFAbsoluteTime = 0.0
var ti: CFTimeInterval = 0.0
let res: Bool = withUnsafeMutablePointer(&start) { startp in
withUnsafeMutablePointer(&ti) { tip in
let res: Bool = withUnsafeMutablePointer(to: &start) { startp in
withUnsafeMutablePointer(to: &ti) { tip in
return CFCalendarGetTimeRangeOfUnit(_cfObject, unit._cfValue, date.timeIntervalSinceReferenceDate, startp, tip)
}
}
Expand Down Expand Up @@ -452,7 +452,7 @@ public class Calendar: NSObject, NSCopying, NSSecureCoding {
self.timeZone = comps.timeZone ?? timeZone

var at: CFAbsoluteTime = 0.0
let res: Bool = withUnsafeMutablePointer(&at) { t in
let res: Bool = withUnsafeMutablePointer(to: &at) { t in
return vector.withUnsafeMutableBufferPointer { (vectorBuffer: inout UnsafeMutableBufferPointer<Int32>) in
return _CFCalendarComposeAbsoluteTimeV(_cfObject, t, compDesc, vectorBuffer.baseAddress!, Int32(vectorBuffer.count))
}
Expand Down Expand Up @@ -556,7 +556,7 @@ public class Calendar: NSObject, NSCopying, NSSecureCoding {
var (vector, compDesc) = _convert(comps)
var at: CFAbsoluteTime = 0.0

let res: Bool = withUnsafeMutablePointer(&at) { t in
let res: Bool = withUnsafeMutablePointer(to: &at) { t in
return vector.withUnsafeMutableBufferPointer { (vectorBuffer: inout UnsafeMutableBufferPointer<Int32>) in
return _CFCalendarAddComponentsV(_cfObject, t, CFOptionFlags(opts.rawValue), compDesc, vectorBuffer.baseAddress!, Int32(vector.count))
}
Expand Down Expand Up @@ -942,7 +942,7 @@ public class Calendar: NSObject, NSCopying, NSSecureCoding {
/// - Note: Since this API is under consideration it may be either removed or revised in the near future
public func nextWeekendAfter(_ date: Date, options: Options) -> DateInterval? {
var range = _CFCalendarWeekendRange()
let res = withUnsafeMutablePointer(&range) { rangep in
let res = withUnsafeMutablePointer(to: &range) { rangep in
return _CFCalendarGetNextWeekend(_cfObject, rangep)
}
if res {
Expand Down
10 changes: 5 additions & 5 deletions Foundation/NSCoder.swift
Original file line number Diff line number Diff line change
Expand Up @@ -99,7 +99,7 @@ public class NSCoder : NSObject {

public func encode(_ object: AnyObject?) {
var object = object
withUnsafePointer(&object) { (ptr: UnsafePointer<AnyObject?>) -> Void in
withUnsafePointer(to: &object) { (ptr: UnsafePointer<AnyObject?>) -> Void in
encodeValue(ofObjCType: "@", at: unsafeBitCast(ptr, to: UnsafeRawPointer.self))
}
}
Expand All @@ -126,11 +126,11 @@ public class NSCoder : NSObject {

public func encodeBytes(_ byteaddr: UnsafeRawPointer?, length: Int) {
var newLength = UInt32(length)
withUnsafePointer(&newLength) { (ptr: UnsafePointer<UInt32>) -> Void in
withUnsafePointer(to: &newLength) { (ptr: UnsafePointer<UInt32>) -> Void in
encodeValue(ofObjCType: "I", at: ptr)
}
var empty: [Int8] = []
withUnsafePointer(&empty) {
withUnsafePointer(to: &empty) {
encodeArray(ofObjCType: "c", count: length, at: byteaddr ?? UnsafeRawPointer($0))
}
}
Expand All @@ -141,7 +141,7 @@ public class NSCoder : NSObject {
}

var obj: AnyObject? = nil
withUnsafeMutablePointer(&obj) { (ptr: UnsafeMutablePointer<AnyObject?>) -> Void in
withUnsafeMutablePointer(to: &obj) { (ptr: UnsafeMutablePointer<AnyObject?>) -> Void in
decodeValue(ofObjCType: "@", at: unsafeBitCast(ptr, to: UnsafeMutableRawPointer.self))
}
return obj
Expand All @@ -155,7 +155,7 @@ public class NSCoder : NSObject {
// TODO: This is disabled, as functions which return unsafe interior pointers are inherently unsafe when we have no autorelease pool.
public func decodeBytes(withReturnedLength lengthp: UnsafeMutablePointer<Int>) -> UnsafeMutableRawPointer? {
var length: UInt32 = 0
withUnsafeMutablePointer(&length) { (ptr: UnsafeMutablePointer<UInt32>) -> Void in
withUnsafeMutablePointer(to: &length) { (ptr: UnsafeMutablePointer<UInt32>) -> Void in
decodeValue(ofObjCType: "I", at: unsafeBitCast(ptr, to: UnsafeMutableRawPointer.self))
}
// we cannot autorelease here so instead the pending buffers will manage the lifespan of the returned data... this is wasteful but good enough...
Expand Down
4 changes: 2 additions & 2 deletions Foundation/NSData.swift
Original file line number Diff line number Diff line change
Expand Up @@ -270,7 +270,7 @@ extension NSData {
}

var info = stat()
let ret = withUnsafeMutablePointer(&info) { infoPointer -> Bool in
let ret = withUnsafeMutablePointer(to: &info) { infoPointer -> Bool in
if fstat(fd, infoPointer) < 0 {
return false
}
Expand Down Expand Up @@ -574,7 +574,7 @@ extension NSData {

public func enumerateBytes(_ block: @noescape (UnsafeRawPointer, NSRange, UnsafeMutablePointer<Bool>) -> Void) {
var stop = false
withUnsafeMutablePointer(&stop) { stopPointer in
withUnsafeMutablePointer(to: &stop) { stopPointer in
block(bytes, NSMakeRange(0, length), stopPointer)
}
}
Expand Down
4 changes: 2 additions & 2 deletions Foundation/NSDate.swift
Original file line number Diff line number Diff line change
Expand Up @@ -53,7 +53,7 @@ public class NSDate : NSObject, NSCopying, NSSecureCoding, NSCoding {

public convenience override init() {
var tv = timeval()
let _ = withUnsafeMutablePointer(&tv) { t in
let _ = withUnsafeMutablePointer(to: &tv) { t in
gettimeofday(t, nil)
}
var timestamp = TimeInterval(tv.tv_sec) - NSTimeIntervalSince1970
Expand All @@ -71,7 +71,7 @@ public class NSDate : NSObject, NSCopying, NSSecureCoding, NSCoding {
self.init(timeIntervalSinceReferenceDate: ti)
} else {
var ti: TimeInterval = 0.0
withUnsafeMutablePointer(&ti) { (ptr: UnsafeMutablePointer<Double>) -> Void in
withUnsafeMutablePointer(to: &ti) { (ptr: UnsafeMutablePointer<Double>) -> Void in
aDecoder.decodeValue(ofObjCType: "d", at: UnsafeMutableRawPointer(ptr))
}
self.init(timeIntervalSinceReferenceDate: ti)
Expand Down
2 changes: 1 addition & 1 deletion Foundation/NSDateFormatter.swift
Original file line number Diff line number Diff line change
Expand Up @@ -56,7 +56,7 @@ public class DateFormatter : Formatter {

public func date(from string: String) -> Date? {
var range = CFRange(location: 0, length: string.length)
let date = withUnsafeMutablePointer(&range) { (rangep: UnsafeMutablePointer<CFRange>) -> Date? in
let date = withUnsafeMutablePointer(to: &range) { (rangep: UnsafeMutablePointer<CFRange>) -> Date? in
guard let res = CFDateFormatterCreateDateFromString(kCFAllocatorSystemDefault, _cfObject, string._cfObject, rangep) else {
return nil
}
Expand Down
4 changes: 2 additions & 2 deletions Foundation/NSDictionary.swift
Original file line number Diff line number Diff line change
Expand Up @@ -125,7 +125,7 @@ public class NSDictionary : NSObject, NSCopying, NSMutableCopying, NSSecureCodin
// We're stuck with (int) here (rather than unsigned int)
// because that's the way the code was originally written, unless
// we go to a new version of the class, which has its own problems.
withUnsafeMutablePointer(&cnt) { (ptr: UnsafeMutablePointer<UInt32>) -> Void in
withUnsafeMutablePointer(to: &cnt) { (ptr: UnsafeMutablePointer<UInt32>) -> Void in
aDecoder.decodeValue(ofObjCType: "i", at: UnsafeMutableRawPointer(ptr))
}
let keys = UnsafeMutablePointer<NSObject>.allocate(capacity: Int(cnt))
Expand Down Expand Up @@ -486,7 +486,7 @@ public class NSDictionary : NSObject, NSCopying, NSMutableCopying, NSSecureCodin
getObjects(&objects, andKeys: &keys, count: count)
var stop = ObjCBool(false)
for idx in 0..<count {
withUnsafeMutablePointer(&stop, { stop in
withUnsafeMutablePointer(to: &stop, { stop in
block(keys[idx] as! NSObject, objects[idx], stop)
})

Expand Down
6 changes: 3 additions & 3 deletions Foundation/NSFileManager.swift
Original file line number Diff line number Diff line change
Expand Up @@ -252,7 +252,7 @@ public class FileManager: NSObject {
}

while let entry = readdir(dir!) {
if let entryName = withUnsafePointer(&entry.pointee.d_name, { (ptr) -> String? in
if let entryName = withUnsafePointer(to: &entry.pointee.d_name, { (ptr) -> String? in
return String(cString: UnsafePointer<Int8>(ptr))
}) {
// TODO: `entryName` should be limited in length to `entry.memory.d_namlen`.
Expand Down Expand Up @@ -294,15 +294,15 @@ public class FileManager: NSObject {
var entry = readdir(dir!)

while entry != nil {
if let entryName = withUnsafePointer(&entry!.pointee.d_name, { (ptr) -> String? in
if let entryName = withUnsafePointer(to: &entry!.pointee.d_name, { (ptr) -> String? in
let int8Ptr = unsafeBitCast(ptr, to: UnsafePointer<Int8>.self)
return String(cString: int8Ptr)
}) {
// TODO: `entryName` should be limited in length to `entry.memory.d_namlen`.
if entryName != "." && entryName != ".." {
contents.append(entryName)

if let entryType = withUnsafePointer(&entry!.pointee.d_type, { (ptr) -> Int32? in
if let entryType = withUnsafePointer(to: &entry!.pointee.d_type, { (ptr) -> Int32? in
let int32Ptr = unsafeBitCast(ptr, to: UnsafePointer<UInt8>.self)
return Int32(int32Ptr.pointee)
}) {
Expand Down
4 changes: 2 additions & 2 deletions Foundation/NSKeyedCoderOldStyleArray.swift
Original file line number Diff line number Diff line change
Expand Up @@ -68,7 +68,7 @@ internal final class _NSKeyedCoderOldStyleArray : NSObject, NSCopying, NSSecureC
for idx in 0..<self._count {
var type = Int8(self._type)

withUnsafePointer(&type) { typep in
withUnsafePointer(to: &type) { typep in
let addr = self._addr.advanced(by: idx * self._size)
aDecoder.decodeValue(ofObjCType: typep, at: addr)
}
Expand All @@ -83,7 +83,7 @@ internal final class _NSKeyedCoderOldStyleArray : NSObject, NSCopying, NSSecureC
for idx in 0..<self._count {
var type = Int8(self._type)

withUnsafePointer(&type) { typep in
withUnsafePointer(to: &type) { typep in
aCoder.encodeValue(ofObjCType: typep, at: self._addr + (idx * self._size))
}
}
Expand Down
6 changes: 3 additions & 3 deletions Foundation/NSLock.swift
Original file line number Diff line number Diff line change
Expand Up @@ -141,7 +141,7 @@ public class RecursiveLock: NSObject, Locking {
public override init() {
super.init()
var attrib = pthread_mutexattr_t()
withUnsafeMutablePointer(&attrib) { attrs in
withUnsafeMutablePointer(to: &attrib) { attrs in
pthread_mutexattr_settype(attrs, Int32(PTHREAD_MUTEX_RECURSIVE))
pthread_mutex_init(mutex, attrs)
}
Expand Down Expand Up @@ -208,12 +208,12 @@ public class Condition: NSObject, Locking {
ts.tv_sec = Int(floor(ti))
ts.tv_nsec = Int((ti - Double(ts.tv_sec)) * 1000000000.0)
var tv = timeval()
withUnsafeMutablePointer(&tv) { t in
withUnsafeMutablePointer(to: &tv) { t in
gettimeofday(t, nil)
ts.tv_sec += t.pointee.tv_sec
ts.tv_nsec += Int((t.pointee.tv_usec * 1000000) / 1000000000)
}
let retVal: Int32 = withUnsafePointer(&ts) { t in
let retVal: Int32 = withUnsafePointer(to: &ts) { t in
return pthread_cond_timedwait(cond, mutex, t)
}

Expand Down
26 changes: 13 additions & 13 deletions Foundation/NSNumber.swift
Original file line number Diff line number Diff line change
Expand Up @@ -272,7 +272,7 @@ public class NSNumber : NSValue {
public required convenience init?(coder aDecoder: NSCoder) {
if !aDecoder.allowsKeyedCoding {
var objCType: UnsafeMutablePointer<Int8>? = nil
withUnsafeMutablePointer(&objCType, { (ptr: UnsafeMutablePointer<UnsafeMutablePointer<Int8>?>) -> Void in
withUnsafeMutablePointer(to: &objCType, { (ptr: UnsafeMutablePointer<UnsafeMutablePointer<Int8>?>) -> Void in
aDecoder.decodeValue(ofObjCType: String(_NSSimpleObjCType.CharPtr), at: UnsafeMutableRawPointer(ptr))
})
if objCType == nil {
Expand Down Expand Up @@ -310,79 +310,79 @@ public class NSNumber : NSValue {

public var int8Value: Int8 {
var val: Int8 = 0
withUnsafeMutablePointer(&val) { (value: UnsafeMutablePointer<Int8>) -> Void in
withUnsafeMutablePointer(to: &val) { (value: UnsafeMutablePointer<Int8>) -> Void in
CFNumberGetValue(_cfObject, kCFNumberCharType, value)
}
return val
}

public var uint8Value: UInt8 {
var val: UInt8 = 0
withUnsafeMutablePointer(&val) { (value: UnsafeMutablePointer<UInt8>) -> Void in
withUnsafeMutablePointer(to: &val) { (value: UnsafeMutablePointer<UInt8>) -> Void in
CFNumberGetValue(_cfObject, kCFNumberCharType, value)
}
return val
}

public var int16Value: Int16 {
var val: Int16 = 0
withUnsafeMutablePointer(&val) { (value: UnsafeMutablePointer<Int16>) -> Void in
withUnsafeMutablePointer(to: &val) { (value: UnsafeMutablePointer<Int16>) -> Void in
CFNumberGetValue(_cfObject, kCFNumberShortType, value)
}
return val
}

public var uint16Value: UInt16 {
var val: UInt16 = 0
withUnsafeMutablePointer(&val) { (value: UnsafeMutablePointer<UInt16>) -> Void in
withUnsafeMutablePointer(to: &val) { (value: UnsafeMutablePointer<UInt16>) -> Void in
CFNumberGetValue(_cfObject, kCFNumberShortType, value)
}
return val
}

public var int32Value: Int32 {
var val: Int32 = 0
withUnsafeMutablePointer(&val) { (value: UnsafeMutablePointer<Int32>) -> Void in
withUnsafeMutablePointer(to: &val) { (value: UnsafeMutablePointer<Int32>) -> Void in
CFNumberGetValue(_cfObject, kCFNumberIntType, value)
}
return val
}

public var uint32Value: UInt32 {
var val: UInt32 = 0
withUnsafeMutablePointer(&val) { (value: UnsafeMutablePointer<UInt32>) -> Void in
withUnsafeMutablePointer(to: &val) { (value: UnsafeMutablePointer<UInt32>) -> Void in
CFNumberGetValue(_cfObject, kCFNumberIntType, value)
}
return val
}

public var int64Value: Int64 {
var val: Int64 = 0
withUnsafeMutablePointer(&val) { (value: UnsafeMutablePointer<Int64>) -> Void in
withUnsafeMutablePointer(to: &val) { (value: UnsafeMutablePointer<Int64>) -> Void in
CFNumberGetValue(_cfObject, kCFNumberLongLongType, value)
}
return val
}

public var uint64Value: UInt64 {
var val: UInt64 = 0
withUnsafeMutablePointer(&val) { (value: UnsafeMutablePointer<UInt64>) -> Void in
withUnsafeMutablePointer(to: &val) { (value: UnsafeMutablePointer<UInt64>) -> Void in
CFNumberGetValue(_cfObject, kCFNumberLongLongType, value)
}
return val
}

public var floatValue: Float {
var val: Float = 0
withUnsafeMutablePointer(&val) { (value: UnsafeMutablePointer<Float>) -> Void in
withUnsafeMutablePointer(to: &val) { (value: UnsafeMutablePointer<Float>) -> Void in
CFNumberGetValue(_cfObject, kCFNumberFloatType, value)
}
return val
}

public var doubleValue: Double {
var val: Double = 0
withUnsafeMutablePointer(&val) { (value: UnsafeMutablePointer<Double>) -> Void in
withUnsafeMutablePointer(to: &val) { (value: UnsafeMutablePointer<Double>) -> Void in
CFNumberGetValue(_cfObject, kCFNumberDoubleType, value)
}
return val
Expand All @@ -394,15 +394,15 @@ public class NSNumber : NSValue {

public var intValue: Int {
var val: Int = 0
withUnsafeMutablePointer(&val) { (value: UnsafeMutablePointer<Int>) -> Void in
withUnsafeMutablePointer(to: &val) { (value: UnsafeMutablePointer<Int>) -> Void in
CFNumberGetValue(_cfObject, kCFNumberLongType, value)
}
return val
}

public var uintValue: UInt {
var val: UInt = 0
withUnsafeMutablePointer(&val) { (value: UnsafeMutablePointer<UInt>) -> Void in
withUnsafeMutablePointer(to: &val) { (value: UnsafeMutablePointer<UInt>) -> Void in
CFNumberGetValue(_cfObject, kCFNumberLongType, value)
}
return val
Expand Down
Loading