If my understanding is correct a function pointer is in some ways similar to a raw data pointer. Whereas a data pointer usually tells you what type of data it is pointing to, a function pointer tells you how to call the code at the location it points to. Unlike a data pointer, a function pointer cannot be dereferenced as it does not point to any meaningful data. The ‘data’ (actually code) a function pointer points to can only be accessed through calling the function pointer. Accessing data pointed to by a raw data pointer (
*mut T) is considered unsafe as the lifetime of the data pointed to is unknown, the pointer might be dangling or otherwise invalid. However calling a function pointer is not unsafe, even though function pointers (obtained through FFI) can be(come) dangling or invalid as well. For example:
- Load a dynamic library
- Obtain a function pointer to a function from this library
- Unload the library
- Call the function pointer
- Segmentation fault/access violation
For some reason function pointers are always assumed to point to valid static memory. Even if you were to design the dynamic library loading code in such a way that it would only return a reference to a function pointer whose lifetime is restricted by the lifetime of library, it does not matter. Function pointers, just like raw data pointers and most primitive types implement
Copy. The only workaround I see is to wrap function pointers in closures since closures are not
Copy. However, closures are not function pointers. They cannot be used in place of function pointers, they do not have the notion of unsafety (
unsafe keyword) and no external linkage, among other differences.
TL;DR: A raw pointer is to a reference as a function pointer is to …? A closure? But not really.