Swift の Array やら ArraySlice やらポインタの変換まとめ

# Swift の以下の型の間で、直接相互に変換できるものを網羅してみる。

```
UnsafeRawPointer
UnsafeMutableRawPointer

UnsafePointer< Int >
UnsafeMutablePointer< Int >

UnsafeRawBufferPointer
UnsafeMutableRawBufferPointer

UnsafeBufferPointer< Int >
UnsafeMutableBufferPointer< Int >

Array< Int >
ArraySlice< Int >

OpaquePointer
```

|ポインタ名                         |変更|型|サイズ|
|---                              |---|---|---|
|UnsafeRawPointer                 |   |   |   |
|UnsafeMutableRawPointer          |○  |   |   |
|UnsafePointer< Int >             |   |○  |   |
|UnsafeMutablePointer< Int >      |○  |○  |   |
|UnsafeRawBufferPointer           |   |   |○  |
|UnsafeMutableRawBufferPointer    |○  |   |○  |
|UnsafeBufferPointer< Int >       |   |○  |○  |
|UnsafeMutableBufferPointer< Int >|○  |○  |○  |
|let Array< Int >                 |   |○  |○  |
|var Array< Int >                 |○  |○  |○  |
|let ArraySlice< Int >            |   |○  |○  |
|var ArraySlice< Int >            |○  |○  |○  |
|OpaquePointer                    |   |   |   |

* 変更できないものからできるものには変換するには、`mutating:`を使う。
* 型のないものからあるものには変換するには、`assumingMemoryBound( to: Int.self )`を使う。
* サイズのないものからあるものには変換できない。
* `withUnsafeRawBufferPointer` という名前のメソッドはない。`withUnsafeBytes`を使う。
* `withUnsafeMutableRawBufferPointer` という名前のメソッドはない。`withUnsafeMutableBytes`を使う。




## var Array< Int > から

```swift

var	w	: Array< Int >						= [ 1, 2, 3 ]

let	_	: UnsafeRawPointer					= UnsafeRawPointer( w )
let	_	: UnsafeMutableRawPointer			= UnsafeMutableRawPointer( mutating: w )

let	_	: UnsafePointer< Int >				= UnsafePointer( w )
let	_	: UnsafeMutablePointer< Int >		= UnsafeMutablePointer( mutating: w )

let	_	: UnsafeRawBufferPointer			= w.withUnsafeBytes { $0 }
let	_	: UnsafeMutableRawBufferPointer		= w.withUnsafeMutableBytes { $0 }

let	_	: UnsafeBufferPointer< Int >		= w.withUnsafeBufferPointer { $0 }
let	_	: UnsafeMutableBufferPointer< Int >	= w.withUnsafeMutableBufferPointer { $0 }

let	_	: ArraySlice< Int >					= ArraySlice( w )
	
let	_	: OpaquePointer						= OpaquePointer( w )
```

## let Array< Int > から

```swift

let	w	: Array< Int >						= [ 1, 2, 3 ]

let	_	: UnsafeRawPointer					= UnsafeRawPointer( w )
let	_	: UnsafeMutableRawPointer			= UnsafeMutableRawPointer( mutating: w )

let	_	: UnsafePointer< Int >				= UnsafePointer( w )
let	_	: UnsafeMutablePointer< Int >		= UnsafeMutablePointer( mutating: w )

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	_	: ArraySlice< Int >					= ArraySlice( w )

let	_	: OpaquePointer						= OpaquePointer( w )
```

## var ArraySlice< Int > から

```swift

var	w	: ArraySlice< Int >					= ArraySlice( [ 1, 2, 3 ] )

let	_	: UnsafeRawPointer					= w.withUnsafeBytes { $0.baseAddress! }
let	_	: UnsafeMutableRawPointer			= w.withUnsafeMutableBytes { $0.baseAddress! }

let	_	: UnsafePointer< Int >				= w.withUnsafeBufferPointer { $0.baseAddress! }
let	_	: UnsafeMutablePointer< Int >		= w.withUnsafeMutableBufferPointer { $0.baseAddress! }

let	_	: UnsafeRawBufferPointer			= w.withUnsafeBytes { $0 }
let	_	: UnsafeMutableRawBufferPointer		= w.withUnsafeMutableBytes { $0 }

let	_	: UnsafeBufferPointer< Int >		= w.withUnsafeBufferPointer { $0 }
let	_	: UnsafeMutableBufferPointer< Int >	= w.withUnsafeMutableBufferPointer { $0 }

let	_	: Array< Int >						= Array( w )
	
let	_	: OpaquePointer						= OpaquePointer( w.withUnsafeBufferPointer { $0.baseAddress! } )
```

## let ArraySlice< Int > から

```swift

let	w	: ArraySlice< Int >					= ArraySlice( [ 1, 2, 3 ] )

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! } )
```

## UnsafeRawPointer から

```swift

let	w	: UnsafeRawPointer					= UnsafeRawPointer( [ 1, 2, 3 ] )

let	_	: UnsafeMutableRawPointer			= UnsafeMutableRawPointer( mutating: w )

let	_	: UnsafePointer< Int >				= w.assumingMemoryBound( to: Int.self )
Int.self )
let	_	: UnsafeMutablePointer< Int >		= UnsafeMutablePointer( mutating: w.assumingMemoryBound( to: Int.self ) )
let	_	: UnsafeMutablePointer< Int >		= UnsafeMutableRawPointer( mutating: w ).assumingMemoryBound( to:  Int.self ) )   // こんなのも可能

let	_	: OpaquePointer						= OpaquePointer( w )
```

## UnsafeMutableRawPointer から

```swift

let	w	: UnsafeMutableRawPointer			= UnsafeMutableRawPointer( mutating: [ 1, 2, 3 ] )

let	_	: UnsafeRawPointer					= UnsafeRawPointer( w )

let	_	: UnsafePointer< Int >				= UnsafePointer( w.assumingMemoryBound( to: Int.self ) )
let	_	: UnsafeMutablePointer< Int >		= w.assumingMemoryBound( to: Int.self )
	
let	_	: OpaquePointer						= OpaquePointer( w )
```

## UnsafePointer< Int > から

```swift

let	w	: UnsafePointer< Int >				= UnsafePointer( [ 1, 2, 3 ] )

let	_	: UnsafeRawPointer					= UnsafeRawPointer( w )
let	_	: UnsafeMutableRawPointer			= UnsafeMutableRawPointer( mutating: w )

let	_	: UnsafeMutablePointer< Int >		= UnsafeMutablePointer( mutating: w )
	
let	_	: OpaquePointer						= OpaquePointer( w )
```

## UnsafeMutablePointer< Int > から

```swift

let	w	: UnsafeMutablePointer< Int >		= UnsafeMutablePointer( mutating: [ 1, 2, 3 ] )

let	_	: UnsafeRawPointer					= UnsafeRawPointer( w )
let	_	: UnsafeMutableRawPointer			= UnsafeMutableRawPointer( w )

let	_	: UnsafePointer< Int >				= UnsafePointer( w )
	
let	_	: OpaquePointer						= OpaquePointer( w )
```

## UnsafeRawBufferPointer から

```swift

let	w	: UnsafeRawBufferPointer			= [ 1, 2, 3 ].withUnsafeBytes { $0 }

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	_	: UnsafeMutableRawBufferPointer		= UnsafeMutableRawBufferPointer( mutating: w )

let	_	: OpaquePointer						= OpaquePointer( w.baseAddress! )
```

## UnsafeMutableRawBufferPointer から

```swift

var	wArray = [ 1, 2, 3 ]
let	w	: UnsafeMutableRawBufferPointer		= wArray.withUnsafeMutableBytes { $0 }

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	_	: OpaquePointer						= OpaquePointer( w.baseAddress! )
```

## UnsafeBufferPointer< Int > から

```swift

let	w	: UnsafeBufferPointer				= [ 1, 2, 3 ].withUnsafeBufferPointer { $0 }

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	_	: UnsafeMutableBufferPointer< Int >	= UnsafeMutableBufferPointer( mutating: w )

let	_	: Array< Int >						= Array( w )
let	_	: ArraySlice< Int >					= ArraySlice( w )

let	_	: OpaquePointer						= OpaquePointer( w.baseAddress! )
```

## UnsafeMutableBufferPointer< Int > から

```swift

var	wArray = [ 1, 2, 3 ]
let	w	: UnsafeMutableBufferPointer 		= wArray.withUnsafeMutableBufferPointer { $0 }

let	_	: UnsafeRawPointer					= UnsafeRawPointer( w.baseAddress! )
let	_	: UnsafeMutableRawPointer			= UnsafeMutableRawPointer( w.baseAddress! )

let	_	: UnsafePointer< Int >				= UnsafePointer( w.baseAddress! )
let	_	: UnsafeMutablePointer< Int >		= UnsafeMutablePointer( w.baseAddress! )

let	_	: UnsafeRawBufferPointer			= UnsafeRawBufferPointer( w )
let	_	: UnsafeMutableRawBufferPointer		= UnsafeMutableRawBufferPointer( w )

let	_	: UnsafeBufferPointer< Int >		= UnsafeBufferPointer( w )

let	_	: Array< Int >						= Array( w )
let	_	: ArraySlice< Int >					= ArraySlice( w )

let	_	: OpaquePointer						= OpaquePointer( w.baseAddress! )
```

## OpaquePointer から

```swift

let	w	: OpaquePointer						= OpaquePointer( [ 1, 2, 3 ] )

let	_	: UnsafeRawPointer					= UnsafeRawPointer( w )
let	_	: UnsafeMutableRawPointer			= UnsafeMutableRawPointer( w )

let	_	: UnsafePointer< Int >				= UnsafePointer( w )
let	_	: UnsafeMutablePointer< Int >		= UnsafeMutablePointer( w )
```