Skip to content

Update for SE-0107: Migrate Void->Raw. #472

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 4 commits into from
Jul 26, 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
44 changes: 24 additions & 20 deletions Foundation/Data.swift
Original file line number Diff line number Diff line change
Expand Up @@ -16,11 +16,11 @@ import Darwin
import Glibc
#endif

internal func __NSDataInvokeDeallocatorUnmap(_ mem: UnsafeMutablePointer<Void>, _ length: Int) -> Void {
internal func __NSDataInvokeDeallocatorUnmap(_ mem: UnsafeMutableRawPointer, _ length: Int) -> Void {
munmap(mem, length)
}

internal func __NSDataInvokeDeallocatorFree(_ mem: UnsafeMutablePointer<Void>, _ length: Int) -> Void {
internal func __NSDataInvokeDeallocatorFree(_ mem: UnsafeMutableRawPointer, _ length: Int) -> Void {
free(mem)
}

Expand All @@ -34,30 +34,30 @@ internal final class _SwiftNSData : NSData, _SwiftNativeFoundationType {
// Take ownership.
__wrapped = .Immutable(Unmanaged.passRetained(_unsafeReferenceCast(immutableObject, to: ImmutableType.self)))

let dummyPointer = unsafeBitCast(NSData.self, to: UnsafeMutablePointer<Void>.self)
let dummyPointer = unsafeBitCast(NSData.self, to: UnsafeMutableRawPointer.self)
super.init(bytes: dummyPointer, length: 0, copy: false, deallocator: nil)
}

init(mutableObject: AnyObject) {
// Take ownership.
__wrapped = .Mutable(Unmanaged.passRetained(_unsafeReferenceCast(mutableObject, to: MutableType.self)))
let dummyPointer = unsafeBitCast(NSData.self, to: UnsafeMutablePointer<Void>.self)
let dummyPointer = unsafeBitCast(NSData.self, to: UnsafeMutableRawPointer.self)
super.init(bytes: dummyPointer, length: 0, copy: false, deallocator: nil)
}

internal required init(unmanagedImmutableObject: Unmanaged<ImmutableType>) {
// Take ownership.
__wrapped = .Immutable(unmanagedImmutableObject)

let dummyPointer = unsafeBitCast(NSData.self, to: UnsafeMutablePointer<Void>.self)
let dummyPointer = unsafeBitCast(NSData.self, to: UnsafeMutableRawPointer.self)
super.init(bytes: dummyPointer, length: 0, copy: false, deallocator: nil)
}

internal required init(unmanagedMutableObject: Unmanaged<MutableType>) {
// Take ownership.
__wrapped = .Mutable(unmanagedMutableObject)

let dummyPointer = unsafeBitCast(NSData.self, to: UnsafeMutablePointer<Void>.self)
let dummyPointer = unsafeBitCast(NSData.self, to: UnsafeMutableRawPointer.self)
super.init(bytes: dummyPointer, length: 0, copy: false, deallocator: nil)
}

Expand All @@ -82,19 +82,19 @@ internal final class _SwiftNSData : NSData, _SwiftNativeFoundationType {
}
}

override var bytes : UnsafePointer<Void> {
override var bytes : UnsafeRawPointer {
return _mapUnmanaged { $0.bytes }
}

// override func subdata(with range: NSRange) -> Data {
// return _mapUnmanaged { $0.subdata(with: range) }
// }
//
// override func getBytes(_ buffer: UnsafeMutablePointer<Void>, length: Int) {
// override func getBytes(_ buffer: UnsafeMutableRawPointer, length: Int) {
// return _mapUnmanaged { $0.getBytes(buffer, length: length) }
// }
//
// override func getBytes(_ buffer: UnsafeMutablePointer<Void>, range: NSRange) {
// override func getBytes(_ buffer: UnsafeMutableRawPointer, range: NSRange) {
// return _mapUnmanaged { $0.getBytes(buffer, range: range) }
// }
//
Expand All @@ -112,7 +112,7 @@ internal final class _SwiftNSData : NSData, _SwiftNativeFoundationType {
// }
// }
//
// override func enumerateByteRanges(using block: @noescape (UnsafePointer<Void>, NSRange, UnsafeMutablePointer<ObjCBool>) -> Void) {
// override func enumerateByteRanges(using block: @noescape (UnsafeRawPointer, NSRange, UnsafeMutablePointer<ObjCBool>) -> Void) {
// return _mapUnmanaged { $0.enumerateBytes(block) }
// }
//
Expand Down Expand Up @@ -163,7 +163,7 @@ public struct Data : ReferenceConvertible, CustomStringConvertible, Equatable, H
/// A custom deallocator.
case custom((UnsafeMutablePointer<UInt8>, Int) -> Void)

fileprivate var _deallocator : ((UnsafeMutablePointer<Void>, Int) -> Void)? {
fileprivate var _deallocator : ((UnsafeMutableRawPointer, Int) -> Void)? {
switch self {
case .unmap:
return { __NSDataInvokeDeallocatorUnmap($0, $1) }
Expand All @@ -173,7 +173,8 @@ public struct Data : ReferenceConvertible, CustomStringConvertible, Equatable, H
return nil
case .custom(let b):
return { (ptr, len) in
b(UnsafeMutablePointer<UInt8>(ptr), len)
let bytePtr = ptr.bindMemory(to: UInt8.self, capacity: len)
b(bytePtr, len)
}
}
}
Expand All @@ -186,7 +187,7 @@ public struct Data : ReferenceConvertible, CustomStringConvertible, Equatable, H
///
/// - parameter bytes: A pointer to the memory. It will be copied.
/// - parameter count: The number of bytes to copy.
public init(bytes: UnsafePointer<Void>, count: Int) {
public init(bytes: UnsafeRawPointer, count: Int) {
_wrapped = _SwiftNSData(immutableObject: NSData(bytes: bytes, length: count))
}

Expand Down Expand Up @@ -279,8 +280,8 @@ public struct Data : ReferenceConvertible, CustomStringConvertible, Equatable, H
}

public init?(count: Int) {
if let memory = malloc(count) {
self.init(bytesNoCopy: UnsafeMutablePointer<UInt8>(memory), count: count, deallocator: .free)
if let memory = malloc(count)?.bindMemory(to: UInt8.self, capacity: count) {
self.init(bytesNoCopy: memory, count: count, deallocator: .free)
} else {
return nil
}
Expand All @@ -304,7 +305,7 @@ public struct Data : ReferenceConvertible, CustomStringConvertible, Equatable, H
}
}

private func _getUnsafeBytesPointer() -> UnsafePointer<Void> {
private func _getUnsafeBytesPointer() -> UnsafeRawPointer {
return _mapUnmanaged { return $0.bytes }
}

Expand All @@ -314,10 +315,11 @@ public struct Data : ReferenceConvertible, CustomStringConvertible, Equatable, H
public func withUnsafeBytes<ResultType, ContentType>(_ body: @noescape (UnsafePointer<ContentType>) throws -> ResultType) rethrows -> ResultType {
let bytes = _getUnsafeBytesPointer()
defer { _fixLifetime(self)}
return try body(UnsafePointer(bytes))
let contentPtr = bytes.bindMemory(to: ContentType.self, capacity: count / strideof(ContentType.self))
return try body(contentPtr)
}

private mutating func _getUnsafeMutableBytesPointer() -> UnsafeMutablePointer<Void> {
private mutating func _getUnsafeMutableBytesPointer() -> UnsafeMutableRawPointer {
return _applyUnmanagedMutation {
return $0.mutableBytes
}
Expand All @@ -330,7 +332,8 @@ public struct Data : ReferenceConvertible, CustomStringConvertible, Equatable, H
public mutating func withUnsafeMutableBytes<ResultType, ContentType>(_ body: @noescape (UnsafeMutablePointer<ContentType>) throws -> ResultType) rethrows -> ResultType {
let mutableBytes = _getUnsafeMutableBytesPointer()
defer { _fixLifetime(self)}
return try body(UnsafeMutablePointer(mutableBytes))
let contentPtr = mutableBytes.bindMemory(to: ContentType.self, capacity: count / strideof(ContentType.self))
return try body(contentPtr)
}

// MARK: -
Expand Down Expand Up @@ -437,7 +440,8 @@ public struct Data : ReferenceConvertible, CustomStringConvertible, Equatable, H
_mapUnmanaged {
$0.enumerateBytes { (ptr, range, stop) in
var stopv = false
block(buffer: UnsafeBufferPointer(start: UnsafePointer<UInt8>(ptr), count: range.length), byteIndex: range.length, stop: &stopv)
let bytePtr = ptr.bindMemory(to: UInt8.self, capacity: range.length)
block(buffer: UnsafeBufferPointer(start: bytePtr, count: range.length), byteIndex: range.length, stop: &stopv)
if stopv {
stop.pointee = true
}
Expand Down
2 changes: 1 addition & 1 deletion Foundation/IndexPath.swift
Original file line number Diff line number Diff line change
Expand Up @@ -158,7 +158,7 @@ public struct IndexPath : ReferenceConvertible, Equatable, Hashable, MutableColl
if count == 0 {
_indexes = []
} else {
var ptr = UnsafeMutablePointer<Element>(malloc(count * sizeof(Element.self)))
var ptr = malloc(count * sizeof(Element.self))?.bindMemory(to: Element.self, capacity: count * sizeof(Element.self))
defer { free(ptr) }

nsIndexPath.getIndexes(ptr!, range: NSMakeRange(0, count))
Expand Down
8 changes: 4 additions & 4 deletions Foundation/NSArray.swift
Original file line number Diff line number Diff line change
Expand Up @@ -70,7 +70,7 @@ public class NSArray : NSObject, NSCopying, NSMutableCopying, NSSecureCoding, NS
// 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
aDecoder.decodeValue(ofObjCType: "i", at: UnsafeMutablePointer<Void>(ptr))
aDecoder.decodeValue(ofObjCType: "i", at: UnsafeMutableRawPointer(ptr))
}
let objects = UnsafeMutablePointer<AnyObject?>.allocate(capacity: Int(cnt))
for idx in 0..<cnt {
Expand Down Expand Up @@ -388,13 +388,13 @@ public class NSArray : NSObject, NSCopying, NSMutableCopying, NSSecureCoding, NS
}))
}

public func sortedArray(_ comparator: @noescape @convention(c) (AnyObject, AnyObject, UnsafeMutablePointer<Swift.Void>?) -> Int, context: UnsafeMutablePointer<Swift.Void>?) -> [AnyObject] {
public func sortedArray(_ comparator: @noescape @convention(c) (AnyObject, AnyObject, UnsafeMutableRawPointer?) -> Int, context: UnsafeMutableRawPointer?) -> [AnyObject] {
return sortedArray([]) { lhs, rhs in
return ComparisonResult(rawValue: comparator(lhs, rhs, context))!
}
}

public func sortedArray(_ comparator: @noescape @convention(c) (AnyObject, AnyObject, UnsafeMutablePointer<Swift.Void>?) -> Int, context: UnsafeMutablePointer<Swift.Void>?, hint: Data?) -> [AnyObject] {
public func sortedArray(_ comparator: @noescape @convention(c) (AnyObject, AnyObject, UnsafeMutableRawPointer?) -> Int, context: UnsafeMutableRawPointer?, hint: Data?) -> [AnyObject] {
return sortedArray([]) { lhs, rhs in
return ComparisonResult(rawValue: comparator(lhs, rhs, context))!
}
Expand Down Expand Up @@ -834,7 +834,7 @@ public class NSMutableArray : NSArray {
}
}

public func sortUsingFunction(_ compare: @convention(c) (AnyObject, AnyObject, UnsafeMutablePointer<Void>?) -> Int, context: UnsafeMutablePointer<Void>?) {
public func sortUsingFunction(_ compare: @convention(c) (AnyObject, AnyObject, UnsafeMutableRawPointer?) -> Int, context: UnsafeMutableRawPointer?) {
self.setArray(self.sortedArray(compare, context: context))
}

Expand Down
2 changes: 1 addition & 1 deletion Foundation/NSCFArray.swift
Original file line number Diff line number Diff line change
Expand Up @@ -34,7 +34,7 @@ internal final class _NSCFArray : NSMutableArray {
}

override func insert(_ anObject: AnyObject, at index: Int) {
CFArrayInsertValueAtIndex(_cfMutableObject, index, unsafeBitCast(anObject, to: UnsafePointer<Void>.self))
CFArrayInsertValueAtIndex(_cfMutableObject, index, unsafeBitCast(anObject, to: UnsafeRawPointer.self))
}

override func removeObject(at index: Int) {
Expand Down
10 changes: 5 additions & 5 deletions Foundation/NSCFDictionary.swift
Original file line number Diff line number Diff line change
Expand Up @@ -37,7 +37,7 @@ internal final class _NSCFDictionary : NSMutableDictionary {
}

override func objectForKey(_ aKey: AnyObject) -> AnyObject? {
let value = CFDictionaryGetValue(_cfObject, unsafeBitCast(aKey, to: UnsafePointer<Void>.self))
let value = CFDictionaryGetValue(_cfObject, unsafeBitCast(aKey, to: UnsafeRawPointer.self))
if value != nil {
return unsafeBitCast(value, to: AnyObject.self)
} else {
Expand All @@ -64,7 +64,7 @@ internal final class _NSCFDictionary : NSMutableDictionary {
let cf = dict._cfObject
count = CFDictionaryGetCount(cf)

let keys = UnsafeMutablePointer<UnsafePointer<Void>?>.allocate(capacity: count)
let keys = UnsafeMutablePointer<UnsafeRawPointer?>.allocate(capacity: count)
CFDictionaryGetKeysAndValues(cf, keys, nil)

for idx in 0..<count {
Expand All @@ -81,11 +81,11 @@ internal final class _NSCFDictionary : NSMutableDictionary {
}

override func removeObject(forKey aKey: AnyObject) {
CFDictionaryRemoveValue(_cfMutableObject, unsafeBitCast(aKey, to: UnsafePointer<Void>.self))
CFDictionaryRemoveValue(_cfMutableObject, unsafeBitCast(aKey, to: UnsafeRawPointer.self))
}

override func setObject(_ anObject: AnyObject, forKey aKey: NSObject) {
CFDictionarySetValue(_cfMutableObject, unsafeBitCast(aKey, to: UnsafePointer<Void>.self), unsafeBitCast(anObject, to: UnsafePointer<Void>.self))
CFDictionarySetValue(_cfMutableObject, unsafeBitCast(aKey, to: UnsafeRawPointer.self), unsafeBitCast(anObject, to: UnsafeRawPointer.self))
}

override var classForCoder: AnyClass {
Expand Down Expand Up @@ -152,7 +152,7 @@ internal func _CFSwiftDictionaryGetValuesAndKeys(_ dictionary: AnyObject, valueb
}
}

internal func _CFSwiftDictionaryApplyFunction(_ dictionary: AnyObject, applier: @convention(c) (AnyObject, AnyObject, UnsafeMutablePointer<Void>) -> Void, context: UnsafeMutablePointer<Void>) {
internal func _CFSwiftDictionaryApplyFunction(_ dictionary: AnyObject, applier: @convention(c) (AnyObject, AnyObject, UnsafeMutableRawPointer) -> Void, context: UnsafeMutableRawPointer) {
(dictionary as! NSDictionary).enumerateKeysAndObjects([]) { key, value, _ in
applier(key, value, context)
}
Expand Down
9 changes: 5 additions & 4 deletions Foundation/NSCFString.swift
Original file line number Diff line number Diff line change
Expand Up @@ -59,13 +59,14 @@ internal class _NSCFString : NSMutableString {

internal final class _NSCFConstantString : _NSCFString {
internal var _ptr : UnsafePointer<UInt8> {
let ptr = unsafeAddress(of: self) + sizeof(OpaquePointer.self) + sizeof(Int32.self) + sizeof(Int32.self) + sizeof(_CFInfo.self)
return UnsafePointer<UnsafePointer<UInt8>>(ptr).pointee
let offset = sizeof(OpaquePointer.self) + sizeof(Int32.self) + sizeof(Int32.self) + sizeof(_CFInfo.self)
let ptr = Unmanaged.passUnretained(self).toOpaque()
return ptr.load(fromByteOffset: offset, as: UnsafePointer<UInt8>.self)
}
internal var _length : UInt32 {
let offset = sizeof(OpaquePointer.self) + sizeof(Int32.self) + sizeof(Int32.self) + sizeof(_CFInfo.self) + sizeof(UnsafePointer<UInt8>.self)
let ptr = unsafeAddress(of: self) + offset
return UnsafePointer<UInt32>(ptr).pointee
let ptr = Unmanaged.passUnretained(self).toOpaque()
return ptr.load(fromByteOffset: offset, as: UInt32.self)
}

required init(characters: UnsafePointer<unichar>, length: Int) {
Expand Down
10 changes: 5 additions & 5 deletions Foundation/NSCache.swift
Original file line number Diff line number Diff line change
Expand Up @@ -22,7 +22,7 @@ public class Cache: NSObject {
}
}

private var _entries = Dictionary<UnsafePointer<Void>, NSCacheEntry>()
private var _entries = Dictionary<UnsafeRawPointer, NSCacheEntry>()
private let _lock = Lock()
private var _totalCost = 0
private var _byCost: NSCacheEntry?
Expand All @@ -41,7 +41,7 @@ public class Cache: NSObject {
public func object(forKey key: AnyObject) -> AnyObject? {
var object: AnyObject?

let keyRef = unsafeBitCast(key, to: UnsafePointer<Void>.self)
let keyRef = unsafeBitCast(key, to: UnsafeRawPointer.self)

_lock.lock()
if let entry = _entries[keyRef] {
Expand Down Expand Up @@ -84,7 +84,7 @@ public class Cache: NSObject {
}

public func setObject(_ obj: AnyObject, forKey key: AnyObject, cost g: Int) {
let keyRef = unsafeBitCast(key, to: UnsafePointer<Void>.self)
let keyRef = unsafeBitCast(key, to: UnsafeRawPointer.self)

_lock.lock()
_totalCost += g
Expand Down Expand Up @@ -152,13 +152,13 @@ public class Cache: NSObject {

_lock.lock()
for entry in toRemove {
_entries.removeValue(forKey: unsafeBitCast(entry.key, to: UnsafePointer<Void>.self)) // the cost list is already fixed up in the purge routines
_entries.removeValue(forKey: unsafeBitCast(entry.key, to: UnsafeRawPointer.self)) // the cost list is already fixed up in the purge routines
}
_lock.unlock()
}

public func removeObject(forKey key: AnyObject) {
let keyRef = unsafeBitCast(key, to: UnsafePointer<Void>.self)
let keyRef = unsafeBitCast(key, to: UnsafeRawPointer.self)

_lock.lock()
if let entry = _entries.removeValue(forKey: keyRef) {
Expand Down
10 changes: 5 additions & 5 deletions Foundation/NSCalendar.swift
Original file line number Diff line number Diff line change
Expand Up @@ -100,11 +100,11 @@ extension Calendar {
public class Calendar: NSObject, NSCopying, NSSecureCoding {
typealias CFType = CFCalendar
private var _base = _CFInfo(typeID: CFCalendarGetTypeID())
private var _identifier: UnsafeMutablePointer<Void>? = nil
private var _locale: UnsafeMutablePointer<Void>? = nil
private var _localeID: UnsafeMutablePointer<Void>? = nil
private var _tz: UnsafeMutablePointer<Void>? = nil
private var _cal: UnsafeMutablePointer<Void>? = nil
private var _identifier: UnsafeMutableRawPointer? = nil
private var _locale: UnsafeMutableRawPointer? = nil
private var _localeID: UnsafeMutableRawPointer? = nil
private var _tz: UnsafeMutableRawPointer? = nil
private var _cal: UnsafeMutableRawPointer? = nil

internal var _cfObject: CFType {
return unsafeBitCast(self, to: CFCalendar.self)
Expand Down
4 changes: 2 additions & 2 deletions Foundation/NSCharacterSet.swift
Original file line number Diff line number Diff line change
Expand Up @@ -33,9 +33,9 @@ public class NSCharacterSet : NSObject, NSCopying, NSMutableCopying, NSCoding {
typealias CFType = CFCharacterSet
private var _base = _CFInfo(typeID: CFCharacterSetGetTypeID())
private var _hashValue = CFHashCode(0)
private var _buffer: UnsafeMutablePointer<Void>? = nil
private var _buffer: UnsafeMutableRawPointer? = nil
private var _length = CFIndex(0)
private var _annex: UnsafeMutablePointer<Void>? = nil
private var _annex: UnsafeMutableRawPointer? = nil

internal var _cfObject: CFType {
return unsafeBitCast(self, to: CFType.self)
Expand Down
Loading