liveBook · Manning

Dynamic library

As explained in Refactoring to Rust

A dynamic module is a compiled extension loaded into a host program at runtime rather than statically embedded in it.

A dynamic library packages compiled functionality separately from the program that uses it. A dynamic module can be loaded into a host program at runtime, allowing functionality to be updated without rebuilding the entire host. section 3.2.2 section 4.2

For a C calculator to call a Rust function, Cargo must produce a C-compatible library rather than its default Rust-specific rlib. Set the crate type to cdylib, then run cargo build. section 3.2.2

[lib]
crate-type = ["cdylib"]

This Cargo.toml setting builds a C-compatible dynamic library. section 3.2.2

The C program declares solve and calls it with an input line and a pointer for the result. The Rust function must use C calling conventions and expose the expected symbol name. section 3.2.2

Listing 3.6 Rust solve function that can be exported as compatible with C
#[no_mangle]
pub extern "C" fn solve(
  line: *const c_char, solution: *mut c_int) -> c_int {
  0
}

extern "C" selects C calling conventions, while no_mangle preserves the solve symbol name. section 3.2.2

The stub returns success but does not write a result, so the calculator can print an uninitialized value. A later version checks for a null output pointer before writing through it; input conversion likewise checks for null and handles invalid UTF-8. The linker must also be able to find the library when compiling the C program. section 3.2.2

An NGINX dynamic module

An NGINX module can provide an HTTP handler that calls Rust while keeping NGINX-specific initialization in C. NGINX loads the compiled module separately, so updating the module need not require rebuilding the entire NGINX binary. section 4.2

load_module ../nginx-1.19.3/objs/ngx_http_calculator.so;  #1

worker_processes 1;
daemon off;
error_log /dev/stderr info;  #2

events {
  worker_connections  1024;
}

http {
  access_log /dev/stdout;  #3

  server {
    listen       8080;

    location /calculate {
      calculate on;  #4
    }
  }
}

The configuration loads the module’s .so file and enables its calculate directive for a location. section 4.2

A successful module build is not enough if a required symbol is missing. For example, NGINX reports an undefined symbol when it tries to load a module whose calculator handler has not been supplied. section 4.2

How Refactoring to Rust covers it

How deep it goes
Walks through library configuration, symbol exports, linking and module loading, while leaving NGINX initialization in C.
Examples
Uses a C calculator calling Rust and an NGINX calculator module configured as an HTTP handler.
What you can do after
Operate/configure a Cargo cdylib and an NGINX dynamic module, and recognize an undefined-symbol loading error.
How it connects
Connects dynamic libraries with foreign function interfaces, C calling conventions, symbol linking and pointer safety.

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.