How do I jump between buffer types?

Starting with:

var dummy: XXX = //...

Need to pass it through a closure of the form:

(inout UnsafeMutableBufferPointer<YYY>) -> ZZZ

where I've ensured that XXX and YYY are layout compatible. Note that the buffer pointer is passed as inout, so I could have to copy back any changes to the buffer to the original dummy manually if necessary.

Have you tried this function? withUnsafeMutableBytes(of:_:) | Apple Developer Documentation

I believe this is the motivation for withMemoryRebound(to:capacity:_:). If you also need a pointer to XXX I would use it in combination with withUnsafeMutablepointer(to:_:):

var a: String = "a"
// Get an UnsafeMutablePointer<String>.
withUnsafeMutablePointer(to: &a) { strPtr in
  // Get that as a UnsafeMutablePointer<UInt8>.
  // This is where we need layout compatability.
  strPtr.withMemoryRebound(to: UInt8.self, capacity: 1) { uintPtr in
    // Modify it as UInt8.
    uintPtr.pointee += 1
  }
}
print(a) // prints "b"