Skip to content

Add UnsafeMutableRawPointer.init(mutating:) #3722

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 1 commit into from
Jul 24, 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
27 changes: 21 additions & 6 deletions stdlib/public/core/UnsafeRawPointer.swift.gyb
Original file line number Diff line number Diff line change
Expand Up @@ -68,7 +68,7 @@ public struct Unsafe${Mutable}RawPointer : Strideable, Hashable, _Pointer {
_rawValue = other._rawValue
}

/// Convert an Unsafe${Mutable}Pointer ${a_Self}.
/// Convert an Unsafe${Mutable}Pointer to ${a_Self}.
///
/// Returns nil if `other` is nil.
@_transparent
Expand All @@ -77,14 +77,29 @@ public struct Unsafe${Mutable}RawPointer : Strideable, Hashable, _Pointer {
_rawValue = unwrapped._rawValue
}

% if not mutable:
/// Convert an UnsafeMutableRawPointer ${a_Self}.
% if mutable:
/// Convert an UnsafeRawPointer to ${a_Self}.
@_transparent
public init(mutating other: UnsafeRawPointer) {
_rawValue = other._rawValue
}

/// Convert an UnsafeRawPointer to ${a_Self}.
///
/// Returns nil if `other` is nil.
@_transparent
public init?(mutating other: UnsafeRawPointer?) {
guard let unwrapped = other else { return nil }
_rawValue = unwrapped._rawValue
}
% else: # !mutable
/// Convert an UnsafeMutableRawPointer to ${a_Self}.
@_transparent
public init(_ other: UnsafeMutableRawPointer) {
_rawValue = other._rawValue
}

/// Convert an UnsafeMutableRawPointer ${a_Self}.
/// Convert an UnsafeMutableRawPointer to ${a_Self}.
///
/// Returns nil if `other` is nil.
@_transparent
Expand All @@ -93,13 +108,13 @@ public struct Unsafe${Mutable}RawPointer : Strideable, Hashable, _Pointer {
_rawValue = unwrapped._rawValue
}

/// Convert an UnsafeMutablePointer ${a_Self}.
/// Convert an UnsafeMutablePointer to ${a_Self}.
@_transparent
public init<T>(_ other: UnsafeMutablePointer<T>) {
_rawValue = other._rawValue
}

/// Convert an UnsafeMutablePointer ${a_Self}.
/// Convert an UnsafeMutablePointer to ${a_Self}.
///
/// Returns nil if `other` is nil.
@_transparent
Expand Down
19 changes: 19 additions & 0 deletions test/1_stdlib/UnsafePointer.swift.gyb
Original file line number Diff line number Diff line change
Expand Up @@ -75,6 +75,25 @@ ${SelfName}TestSuite.test("initFromOpaquePointer") {

% end

% for (SelfName, SelfType) in [
% ('UnsafeRawPointer', 'UnsafeRawPointer'),
% ('UnsafeMutableRawPointer', 'UnsafeMutableRawPointer')]:
UnsafeMutableRawPointerTestSuite.test("initFromMutating") {
let other = UnsafeRawPointer(bitPattern: 0x12345678)!
let ptr = UnsafeMutableRawPointer(mutating: other)
expectEqual(0x12345678, unsafeBitCast(ptr, to: Int.self))

let optionalOther: Optional = other
let optionalPointer = UnsafeMutableRawPointer(mutating: optionalOther)
expectNotEmpty(optionalPointer)
expectEqual(0x12345678, unsafeBitCast(optionalPointer, to: Int.self))

let nilOther: Optional<UnsafeRawPointer> = nil
let nilPointer = UnsafeMutableRawPointer(mutating: nilOther)
expectEmpty(nilPointer)
}
% end

% for (SelfName, SelfType) in [
% ('UnsafePointer', 'UnsafePointer<Float>'),
% ('UnsafeMutablePointer', 'UnsafeMutablePointer<Float>'),
Expand Down