liveBook · Manning

Function pointer type

As explained in Refactoring to Rust

A function pointer is a value that refers to a callable function and can be passed as an argument or stored for later invocation.

A function pointer type describes a callable function passed by its memory location instead of by name. In Rust FFI, an NGINX body-reading API accepts a callback that runs after the request body loads. section 4.4

Passing an NGINX callback

Rust declares the C-compatible callback type as `Option<unsafe extern "C" fn(r: *mut ngx_http_request_t)>`. The handler passes `Some(read_body_handler)` to `ngx_http_read_client_request_body`; the callback does not need to be exported by name because C receives its location. The equivalent C declaration is `typedef void (*ngx_http_client_body_handler_pt)(ngx_http_request_t *r);`.

How Refactoring to Rust covers it

How deep it goes
Defines the Rust and C callback signatures and demonstrates invocation, but defers general function-pointer behavior beyond this FFI case.
Examples
Demonstrates `read_body_handler` passed as `Some(...)` to NGINX request-body parsing.
What it assumes
Assumes familiarity with Rust function signatures, C declarations, raw pointers, and unsafe operations.
What you can do after
Apply it via Rust FFI by declaring a C-compatible callback type and passing a matching handler to an API.
How it connects
Connects function pointers with callbacks, foreign-function interfaces, C calling conventions, unsafe functions, and NGINX request handling.

Also explained in

Compare how 4 books explain function pointer type →

Related concepts

This explainer was generated by Manning's knowledge graph from the text of Refactoring to Rust. The section links point to the parts of the book it draws on.