# Data.withUnsafeBytes and type annotations

**URL:** <https://forums.swift.org/t/data-withunsafebytes-and-type-annotations/89585>\
**Category:** Using Swift\
**Tags:** c-interop, pointers\
**Created:** [September 15, 2026, 10:13pm UTC](https://forums.swift.org/t/data-withunsafebytes-and-type-annotations/89585 "2026-09-15T22:13:44Z")\
**Posts on this page:** 5\
**Page:** 1

<div class="post-metadata">

**Author:** ![jdf-id-au](https://sea2.discourse-cdn.com/swift/user_avatar/forums.swift.org/jdf-id-au/32/13573_2.png) [@jdf-id-au](https://forums.swift.org/u/jdf-id-au)\
**Post date:** [September 15, 2026, 10:13pm UTC](https://forums.swift.org/t/data-withunsafebytes-and-type-annotations/89585/1 "2026-09-15T22:13:44Z")

</div>

Hi everyone,

I'm searching through multi-gigabyte files for newlines, starting with

```swift
let raw = try Data(contentsOf: url, options: .alwaysMapped)

```

The following implementation compiles and works, but seems slow.

```swift
raw.withUnsafeBytes { (buf: UnsafeRawBufferPointer) in
  if let ba: UnsafeRawPointer = buf.baseAddress {
    let ptr = ba.bindMemory(to: UInt8.self, capacity: raw.count)
    for i in 0..<raw.count {
      if ptr[i] == 0x0a {newlines.append(i)}
    }
  }
}

```

I want to try `memchr` but the following doesn't compile, with _Type of expression is ambiguous without a type annotation_ on the `.withUnsafeBytes`:

```swift
raw.withUnsafeBytes { (buf: UnsafeRawBufferPointer) in
  if let ba: UnsafeRawPointer = buf.baseAddress {
    let ptr = ba.bindMemory(to: UInt8.self, capacity: raw.count)
    var offset: Int = 0
    while true {
      guard offset < raw.count else { return }
      let start = ptr + offset
      let found = memchr(start, 0x0a, raw.count - offset)
      guard let found = found else { return }
      guard Int(bitPattern: found) > 0 else { return }
      offset += found - start
      newlines.append(offset)
      offset += 1
    }
  }
}

```

I tried several variations on a theme of type annotation but couldn't satisfy the compiler.

What am I doing wrong?

---

<div class="post-metadata">

**Author:** ![glessard](https://sea2.discourse-cdn.com/swift/user_avatar/forums.swift.org/glessard/32/409_2.png) [@glessard](https://forums.swift.org/u/glessard)\
**Post date:** [September 15, 2026, 10:58pm UTC](https://forums.swift.org/t/data-withunsafebytes-and-type-annotations/89585/2 "2026-09-15T22:58:53Z")

</div>

This is a frequent issue with type checking errors in long closures, and the solution is usually to break them up.

In this case, the problem seems to be that `memchr` returns a _mutable_ pointer. In the following I moved the distance calculation to a helper function that takes care of type-matching between `found` and `start`.

```swift
import Glibc

let raw: [UInt8] = []

let locations: [Int] = raw.withUnsafeBytes { b -> [Int] in
  b.withMemoryRebound(to: UInt8.self) { bytes -> [Int] in
    var newlines = [Int]()
    var offset = 0
    while offset < bytes.count {
      let start = bytes.baseAddress! + offset
      let found = memchr(start, 0xa, bytes.count - offset)
      guard let found, Int(bitPattern: found) > 0 else { break }
      offset += distance(from: start, to: found)
      newlines.append(offset)
      offset += 1
    }
    return newlines
  }
}

func distance(
    from start: UnsafeRawPointer, to end: UnsafeRawPointer
) -> Int {
    start.distance(to: end)
}

```

---

<div class="post-metadata">

**Author:** ![itaiferber](https://sea2.discourse-cdn.com/swift/user_avatar/forums.swift.org/itaiferber/32/33840_2.png) [@itaiferber](https://forums.swift.org/u/itaiferber)\
**Post date:** [September 15, 2026, 10:59pm UTC](https://forums.swift.org/t/data-withunsafebytes-and-type-annotations/89585/3 "2026-09-15T22:59:53Z")

</div>

@glessard covered handling `memchr` directly but an alternative: since `Data` is already a `Collection` of `UInt8`, you should be able to use [`Collection.indices(of:)`](https://developer.apple.com/documentation/swift/collection/indices(of:)) to get effectively this exact result:

```swift
let raw = try Data(contentsOf: url, options: .alwaysMapped)
let newlines = raw.indices(of: 0x0A)

```

May be worth comparing the performance.

---

<div class="post-metadata">

**Author:** ![glessard](https://sea2.discourse-cdn.com/swift/user_avatar/forums.swift.org/glessard/32/409_2.png) [@glessard](https://forums.swift.org/u/glessard)\
**Post date:** [September 16, 2026, 12:27am UTC](https://forums.swift.org/t/data-withunsafebytes-and-type-annotations/89585/4 "2026-09-16T00:27:17Z")

</div>

I reduced the problem and filed an issue: [Bad error message after type-checking error · Issue #92306 · swiftlang/swift · GitHub](https://github.com/swiftlang/swift/issues/92306)

---

<div class="post-metadata">

**Author:** ![jdf-id-au](https://sea2.discourse-cdn.com/swift/user_avatar/forums.swift.org/jdf-id-au/32/13573_2.png) [@jdf-id-au](https://forums.swift.org/u/jdf-id-au)\
**Post date:** [September 16, 2026, 4:41am UTC](https://forums.swift.org/t/data-withunsafebytes-and-type-annotations/89585/5 "2026-09-16T04:41:33Z")

</div>

You folks are amazing thank you. (I was about to throw a tantrum and just do it in C, but this kept me here!)

@itaiferber `.indices` intriguingly returns a RangeSet (presumably conveying strides etc); I used `.ranges.flatMap { Array ($0) }` to bring it back to something compatible with my smooth brain 🙂
