Created
September 13, 2020 14:57
-
-
Save codelynx/83c7bc12e1f8b3e1e9757640a3ba29e8 to your computer and use it in GitHub Desktop.
[swift] Unsafe Pointer conversion
This file contains hidden or bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
| // | |
| // Swift UnsafePointer converter | |
| // Kaz Yoshikawa | |
| // | |
| // These code demonstrates how to convert swift pointers to the other forms of pointers. | |
| // | |
| // Source: | |
| // Swift の Array やら ArraySlice やらポインタの変換まとめ | |
| // https://qiita.com/Satachito/items/4c39c9b06304e4d86660 | |
| // | |
| // | |
| import Foundation | |
| // [Template] | |
| // let _: UnsafeRawPointer | |
| // let _: UnsafeMutableRawPointer | |
| // let _: UnsafePointer<Int> | |
| // let _: UnsafeMutablePointer<Int> | |
| // let _: UnsafeRawBufferPointer | |
| // let _: UnsafeMutableRawBufferPointer | |
| // let _: UnsafeBufferPointer<Int> | |
| // let _: UnsafeMutableBufferPointer<Int> | |
| // let _: Array<Int> | |
| // let _: ArraySlice<Int> | |
| // let _: OpaquePointer | |
| func convert(a: [Int]) { | |
| let _: UnsafeRawPointer = UnsafeRawPointer(a) | |
| let _: UnsafeMutableRawPointer = UnsafeMutableRawPointer(mutating: a) | |
| let _: UnsafePointer<Int> = UnsafePointer(a) | |
| let _: UnsafeMutablePointer<Int> = UnsafeMutablePointer(mutating: a) | |
| let _: UnsafeRawBufferPointer = a.withUnsafeBytes { $0 } | |
| // let _: UnsafeMutableRawBufferPointer = a.withUnsafeMutableBytes { $0 } | |
| let _: UnsafeBufferPointer<Int> = a.withUnsafeBufferPointer { $0 } | |
| // let _: UnsafeMutableBufferPointer< Int > = a.withUnsafeMutableBufferPointer { $0 } | |
| let _: ArraySlice<Int> = ArraySlice(a) | |
| let _: OpaquePointer = OpaquePointer(a) | |
| } | |
| func convert(w: ArraySlice<Int>) { | |
| let _: UnsafeRawPointer = w.withUnsafeBytes { $0.baseAddress! } | |
| let _: UnsafeMutableRawPointer = UnsafeMutableRawPointer(mutating: w.withUnsafeBytes { $0.baseAddress! }) | |
| let _: UnsafePointer<Int> = w.withUnsafeBufferPointer { $0.baseAddress! } | |
| let _: UnsafeMutablePointer<Int> = UnsafeMutablePointer(mutating: w.withUnsafeBufferPointer { $0.baseAddress! }) | |
| let _: UnsafeRawBufferPointer = w.withUnsafeBytes { $0 } | |
| let _: UnsafeMutableRawBufferPointer = UnsafeMutableRawBufferPointer(mutating: w.withUnsafeBytes { $0 }) | |
| let _: UnsafeBufferPointer<Int> = w.withUnsafeBufferPointer { $0 } | |
| let _: UnsafeMutableBufferPointer<Int> = UnsafeMutableBufferPointer(mutating: w.withUnsafeBufferPointer { $0 }) | |
| let _: Array<Int> = Array(w) | |
| let _: OpaquePointer = OpaquePointer(w.withUnsafeBufferPointer { $0.baseAddress! }) | |
| } | |
| func convert(w: UnsafeRawPointer) { | |
| // let _: UnsafeRawPointer | |
| let _: UnsafeMutableRawPointer = UnsafeMutableRawPointer(mutating: w) | |
| let _: UnsafePointer<Int> = w.assumingMemoryBound(to: Int.self) | |
| let _: UnsafeMutablePointer<Int> = UnsafeMutablePointer(mutating: w.assumingMemoryBound(to: Int.self)) | |
| // let _: UnsafeRawBufferPointer | |
| // let _: UnsafeMutableRawBufferPointer | |
| // let _: UnsafeBufferPointer<Int> | |
| // let _: UnsafeMutableBufferPointer<Int> | |
| // let _: Array<Int> | |
| // let _: ArraySlice<Int> | |
| let _: OpaquePointer = OpaquePointer(w) | |
| } | |
| func convert(w: UnsafeMutableRawPointer) { | |
| let _: UnsafeRawPointer = UnsafeRawPointer(w) | |
| // let _: UnsafeMutableRawPointer | |
| let _: UnsafePointer<Int> = UnsafePointer(w.assumingMemoryBound(to: Int.self)) | |
| let _: UnsafeMutablePointer<Int> = w.assumingMemoryBound(to: Int.self) | |
| // let _: UnsafeRawBufferPointer | |
| // let _: UnsafeMutableBufferPointer | |
| // let _: UnsafeBufferPointer<Int> | |
| // let _: UnsafeMutableBufferPointer | |
| // let _: Array<Int> | |
| let _: OpaquePointer = OpaquePointer(w) | |
| } | |
| func convert(w: UnsafePointer<Int>) { | |
| let _: UnsafeRawPointer = UnsafeRawPointer(w) | |
| let _: UnsafeMutableRawPointer = UnsafeMutableRawPointer(mutating: w) | |
| // let _: UnsafePointer<Int> | |
| let _: UnsafeMutablePointer<Int> = UnsafeMutablePointer(mutating: w) | |
| // let _: UnsafeRawBufferPointer | |
| // let _: nsafeMutableRawBufferPointer | |
| // let _: UnsafeBufferPointer<Int> | |
| // let _: UnsafeMutableBufferPointer<Int> | |
| // let _: Array<Int> | |
| // let _: ArraySlice<Int> | |
| let _: OpaquePointer = OpaquePointer(w) | |
| } | |
| func convert(w: UnsafeMutablePointer<Int>) { | |
| let _: UnsafeRawPointer = UnsafeRawPointer(w) | |
| let _: UnsafeMutableRawPointer = UnsafeMutableRawPointer(w) | |
| let _: UnsafePointer<Int> = UnsafePointer(w) | |
| // let _: UnsafeMutablePointer<Int> | |
| // let _: UnsafeRawBufferPointer | |
| // let _: UnsafeMutableRawBufferPointer | |
| // let _: UnsafeBufferPointer<Int> | |
| // let _: UnsafeMutableBufferPointer<Int> | |
| // let _: Array<Int> | |
| // let _: ArraySlice<Int> | |
| let _: OpaquePointer = OpaquePointer(w) | |
| } | |
| func convert(w: UnsafeRawBufferPointer) { | |
| let _: UnsafeRawPointer = w.baseAddress! | |
| let _: UnsafeMutableRawPointer = UnsafeMutableRawPointer(mutating: w.baseAddress!) | |
| let _: UnsafePointer<Int> = w.baseAddress!.assumingMemoryBound(to: Int.self) | |
| let _: UnsafeMutablePointer<Int> = UnsafeMutablePointer(mutating: w.baseAddress!.assumingMemoryBound(to: Int.self)) | |
| // let _: UnsafeRawBufferPointer | |
| // let _: UnsafeMutableRawBufferPointer | |
| // let _: UnsafeBufferPointer<Int> | |
| let _: UnsafeMutableRawBufferPointer = UnsafeMutableRawBufferPointer(mutating: w) | |
| // let _: Array<Int> | |
| // let _: ArraySlice<Int> | |
| let _: OpaquePointer = OpaquePointer(w.baseAddress!) | |
| } | |
| func convert(w: UnsafeMutableRawBufferPointer) { | |
| let _: UnsafeRawPointer = UnsafeRawPointer(w.baseAddress!) | |
| let _: UnsafeMutableRawPointer = w.baseAddress! | |
| let _: UnsafePointer<Int> = UnsafePointer(w.baseAddress!.assumingMemoryBound(to: Int.self)) | |
| let _: UnsafeMutablePointer<Int> = w.baseAddress!.assumingMemoryBound(to: Int.self) | |
| let _: UnsafeRawBufferPointer = UnsafeRawBufferPointer(w) | |
| // let _: UnsafeMutableRawBufferPointer | |
| // let _: UnsafeBufferPointer<Int> | |
| // let _: UnsafeMutableBufferPointer<Int> | |
| // let _: Array<Int> | |
| // let _: ArraySlice<Int> | |
| let _: OpaquePointer = OpaquePointer(w.baseAddress!) | |
| } | |
| func convert(w: UnsafeBufferPointer<Int>) { | |
| let _: UnsafeRawPointer = UnsafeRawPointer(w.baseAddress!) | |
| let _: UnsafeMutableRawPointer = UnsafeMutableRawPointer(mutating: w.baseAddress!) | |
| let _: UnsafePointer<Int> = UnsafePointer(w.baseAddress!) | |
| let _: UnsafeMutablePointer<Int> = UnsafeMutablePointer(mutating: w.baseAddress!) | |
| let _: UnsafeRawBufferPointer = UnsafeRawBufferPointer(w) | |
| let _: UnsafeMutableRawBufferPointer = UnsafeMutableRawBufferPointer(mutating: UnsafeRawBufferPointer(w)) | |
| // let _: UnsafeBufferPointer<Int> | |
| let _: UnsafeMutableBufferPointer<Int> = UnsafeMutableBufferPointer(mutating: w) | |
| let _: Array<Int> = Array(w) | |
| let _: ArraySlice<Int> = ArraySlice(w) | |
| let _: OpaquePointer = OpaquePointer(w.baseAddress!) | |
| } | |
| func convert(w: OpaquePointer) { | |
| let _: UnsafeRawPointer = UnsafeRawPointer(w) | |
| let _: UnsafeMutableRawPointer = UnsafeMutableRawPointer(w) | |
| let _: UnsafePointer<Int> = UnsafePointer(w) | |
| let _: UnsafeMutablePointer<Int> = UnsafeMutablePointer(w) | |
| // let _: UnsafeRawBufferPointer | |
| // let _: UnsafeMutableRawBufferPointer | |
| // let _: UnsafeBufferPointer<Int> | |
| // let _: UnsafeMutableBufferPointer<Int> | |
| // let _: Array<Int> | |
| // let _: ArraySlice<Int> | |
| // let _: OpaquePointer | |
| } | |
Sign up for free
to join this conversation on GitHub.
Already have an account?
Sign in to comment