# Assigning UnsafeRawPointer to UnsafeMutablePointer\<UInt8\>?

**URL:** <https://forums.swift.org/t/assigning-unsaferawpointer-to-unsafemutablepointer-uint8/73848>\
**Category:** Using Swift\
**Created:** [August 9, 2024, 12:56am UTC](https://forums.swift.org/t/assigning-unsaferawpointer-to-unsafemutablepointer-uint8/73848 "2024-08-09T00:56:20Z")\
**Posts on this page:** 2\
**Page:** 1

<div class="post-metadata">

**Author:** ![JetForMe](https://sea2.discourse-cdn.com/swift/user_avatar/forums.swift.org/jetforme/32/482_2.png) [@JetForMe](https://forums.swift.org/u/JetForMe)\
**Post date:** [August 9, 2024, 12:56am UTC](https://forums.swift.org/t/assigning-unsaferawpointer-to-unsafemutablepointer-uint8/73848/1 "2024-08-09T00:56:20Z")

</div>

In trying to call `zlib` functions from Swift, I'm struggling to assign `UnsafeRawPointer` to `UnsafeMutablePointer<UInt8>`. `zlib` has a struct:

```swift
public struct z_stream_s {
    public init()
    public init(next_in: UnsafeMutablePointer<Bytef>!, avail_in: uInt, total_in: uLong, next_out: UnsafeMutablePointer<Bytef>!, avail_out: uInt, total_out: uLong, msg: UnsafeMutablePointer<CChar>!, state: OpaquePointer!, zalloc: alloc_func!, zfree: free_func!, opaque: voidpf!, data_type: Int32, adler: uLong, reserved: uLong)
    public var next_in: UnsafeMutablePointer<Bytef>!
    public var avail_in: uInt
    public var total_in: uLong
    public var next_out: UnsafeMutablePointer<Bytef>!
    public var avail_out: uInt
    public var total_out: uLong
    public var msg: UnsafeMutablePointer<CChar>!
    public var state: OpaquePointer!
    public var zalloc: alloc_func!
    public var zfree: free_func!
    public var opaque: voidpf!
    public var data_type: Int32
    public var adler: uLong
    public var reserved: uLong
}

```

`Bytef` is `UInt8`.

I have this code:

```swift
let samples = try [T](unsafeUninitializedCapacity: sampleCount)
	{ destBuffer, initializedCount in
		let sourceBuffer = UnsafeMutableRawBufferPointer.allocate(byteCount: Int(tileByteCount), alignment: MemoryLayout<UInt8>.alignment)
		let decompressedSize = try sourceBuffer.withUnsafeBytes
			{ (inCompressedBytes: UnsafeRawBufferPointer) -> Int in
				state.next_in = inCompressedBytes
			}
	}

```

But Swift doesn't like that. I've tried `inCompressedBytes.baseAddress!`, `inCompressedBytes.assumingMemoryBound(to: UInt8.self)`, and `inCompressedBytes.baseAddress!.assumingMemoryBound(to: UInt8.self)`

With other similar C decompression APIs, I haven't had this much trouble, in part because they declare the input buffer pointer to be const. But I also can't seem to assign the output buffer pointer:

```swift
state.next_out = destBuffer.baseAddress!

```

I get "Cannot assign value of type 'UnsafeMutablePointer' to type 'UnsafeMutablePointer' (aka 'UnsafeMutablePointer')".

When I pass that to `compression_decode_buffer()`, which also takes a `UnsafeMutablePointer<UInt8>`, everything is fine.

---

<div class="post-metadata">

**Author:** ![1-877-547-7272](https://avatars.discourse-cdn.com/v4/letter/1/278dde/32.png) [@1-877-547-7272](https://forums.swift.org/u/1-877-547-7272)\
**Post date:** [August 9, 2024, 8:55am UTC](https://forums.swift.org/t/assigning-unsaferawpointer-to-unsafemutablepointer-uint8/73848/2 "2024-08-09T08:55:07Z")

</div>

You can convert an `UnsafeRawPointer` to an `UnsafeMutablePointer<UInt8>` with

```swift
let converted = UnsafeMutablePointer(mutating: unsafeRawPointer.bindMemory(to: UInt8.self, capacity: count))

```

It should be noted that in some cases, mutating the value referenced by a pointer is undefined behavior, so converting immutable pointers to mutable pointers should be done with care.

> [@JetForMe](#):
>
> But Swift doesn't like that. I've tried `inCompressedBytes.baseAddress!`, `inCompressedBytes.assumingMemoryBound(to: UInt8.self)`, and `inCompressedBytes.baseAddress!.assumingMemoryBound(to: UInt8.self)`

`assumingMemoryBound` should only be used when you know that a raw pointer is bound to a certain type. Memory allocated by `UnsafeMutableRawPointerBuffer.allocate(byteCount:alignment:)` isn’t bound to any type, so you should use `bindMemory` instead. Or, just use `UnsafeMutablePointerBuffer<UInt8>.allocate(capacity:)`.

> [@JetForMe](#):
>
> I have this code:
> 
> ```swift
> let samples = try [T](unsafeUninitializedCapacity: sampleCount)
> { destBuffer, initializedCount in
> let sourceBuffer = UnsafeMutableRawBufferPointer.allocate(byteCount: Int(tileByteCount), alignment: MemoryLayout<UInt8>.alignment)
> let decompressedSize = try sourceBuffer.withUnsafeBytes
> { (inCompressedBytes: UnsafeRawBufferPointer) -> Int in
> state.next_in = inCompressedBytes
> }
> }
> 
> ```

Would this code work?

```swift
let samples = [T](unsafeUninitializedCapacity: sampleCount) {
    (destBuffer, initializedCount) in
    let sourceBuffer = UnsafeMutableBufferPointer<UInt8>.allocate(capacity: Int(tileByteCount))
    state.next_in = sourceBuffer.baseAddress!
    let decompressedSize = ...
    // deallocate sourceBuffer
}

```

> [@JetForMe](#):
>
> With other similar C decompression APIs, I haven't had this much trouble, in part because they declare the input buffer pointer to be const. But I also can't seem to assign the output buffer pointer:
> 
> ```swift
> state.next_out = destBuffer.baseAddress!
> 
> ```
> 
> I get "Cannot assign value of type 'UnsafeMutablePointer' to type 'UnsafeMutablePointer' (aka 'UnsafeMutablePointer')".

Sometimes, the diagnostics get the problem wrong. Usually, it means that you’ve made a mistake somewhere else in your code. Unfortunately, I can’t tell you where.

> [@JetForMe](#):
>
> When I pass that to `compression_decode_buffer()`, which also takes a `UnsafeMutablePointer<UInt8>`, everything is fine.

Since version 5.6, Swift has some implicit conversions for pointers in declarations from C. See [SE-0324](https://github.com/swiftlang/swift-evolution/blob/main/proposals/0324-c-lang-pointer-arg-conversion.md).

* * *

I hope this helps!
