One final observation:
By using the inout & marker it is taking your compression_stream and turning it into a UnsafeMutablePointer<compression_stream> automatically. (You could have also just passed streamPointer and not needed this special conversion.)
Yes, but people should realize this line copies the stream object (which seems like extra work):
var stream = streamPointer.pointee
Readers can also easily make the mistake of thinking that stream
and streamPointer.pointee
are the same object. When in fact, the memory pointed to by streamPointer
is never even initialized in your example.
Unfortunately, you do need to dynamically allocate compression_stream
since youβre not initializing it in Swift code. But to avoid deferencing streamPointer everywhere, you can do something like this:
func initStream(stream: inout compression_stream) {...}
func destroyStream(stream: inout compression_stream) {...}
// 2: initialize the stream
initStream(stream: &streamPointer.pointee)
...
destroyStream(stream: &streamPointer.pointee)