Logo Questions Linux Laravel Mysql Ubuntu Git Menu
 

Cast a Swift struct to UnsafeMutablePointer<Void>

Is there a way to cast a Swift struct's address to a void UnsafeMutablePointer?
I tried this without success:

struct TheStruct {
    var a:Int = 0
}

var myStruct = TheStruct()
var address = UnsafeMutablePointer<Void>(&myStruct)

Thanks!

EDIT: the context
I am actually trying to port to Swift the first example in Learning CoreAudio.
This is what I have done until now:

func myAQInputCallback(inUserData:UnsafeMutablePointer<Void>,
    inQueue:AudioQueueRef,
    inBuffer:AudioQueueBufferRef,
    inStartTime:UnsafePointer<AudioTimeStamp>,
    inNumPackets:UInt32,
    inPacketDesc:UnsafePointer<AudioStreamPacketDescription>)
 { }

struct MyRecorder {
    var recordFile:     AudioFileID = AudioFileID()
    var recordPacket:   Int64       = 0
    var running:        Boolean     = 0
}

var queue:AudioQueueRef = AudioQueueRef()
AudioQueueNewInput(&asbd,
    myAQInputCallback,
    &recorder,  // <- this is where I *think* a void pointer is demanded
    nil,
    nil,
    UInt32(0),
    &queue)

I am doing efforts to stay in Swift, but if this turns out to be more a problem than an advantage, I will end up linking to a C function.

EDIT: bottome line
If you came to this question because you are trying to use a CoreAudio's AudioQueue in Swift... don't. (read the comments for details)

like image 819
popisar Avatar asked Apr 10 '15 08:04

popisar


People also ask

How are unsafe pointers exposed in Swift?

In Swift, unsafe pointers are exposed via nine types: There is also AutoreleasingUnsafeMutablePointer<T> that is used only for Objective-C interoperability. It corresponds to an Objective-C pointer T __autoreleasing *, where T is an Objective-C pointer type [1].

What is unsafemutablepointer in C++?

A pointer for accessing and manipulating data of a specific type. You use instances of the UnsafeMutablePointer type to access data of a specific type in memory. The type of data that a pointer can access is the pointer’s Pointee type. UnsafeMutablePointer provides no automated memory management or alignment guarantees.

What is an unsafe type in Swift?

An unsafe type does not have some of Swift’s safety features, such as bounds check, automatic memory management. It is possible to violate memory, access unallocated memory, or interpret memory as a wrong type by means of unsafe pointers.

What is an unmanaged pointer in Swift?

OpaquePointer – a pointer for which we do not know the type of data it points to. It can be that the type of pointee is determined by the value of some other variable or cannot be represented in Swift [4]. Unmanaged – a manual-memory-managed pointer. ManagedBufferPointer – a pointer to ManagedBuffer.


2 Answers

As far as I know, the shortest way is:

var myStruct = TheStruct()
var address = withUnsafeMutablePointer(&myStruct) {UnsafeMutablePointer<Void>($0)}

But, why you need this? If you want pass it as a parameter, you can (and should):

func foo(arg:UnsafeMutablePointer<Void>) {
    //...
}

var myStruct = TheStruct()
foo(&myStruct)
like image 118
rintaro Avatar answered Nov 16 '22 04:11

rintaro


Most of the method prototypes have been changed as Swift evolved over the years. Here is the syntax for Swift 5:

var struct = TheStruct()

var unsafeMutablePtrToStruct = withUnsafeMutablePointer(to: &struct) {
    $0.withMemoryRebound(to: TheStruct.self, capacity: 1) {
        (unsafePointer: UnsafeMutablePointer<TheStruct>) in

        unsafePointer
    }
}
like image 22
Aamir Avatar answered Nov 16 '22 04:11

Aamir