diff options
| author | Alex Crichton <alex@alexcrichton.com> | 2013-08-05 19:16:29 -0700 |
|---|---|---|
| committer | Corey Richardson <corey@octayn.net> | 2013-08-07 22:41:14 -0400 |
| commit | 1b103912eaacef82dfee940a3c3e8ecb693417bc (patch) | |
| tree | 45f7f9ae450ddfe9304e3101c823a5b8c68bff24 | |
| parent | 403c52d2ae1a59dea1a3b04ad794a66bd687d067 (diff) | |
| download | rust-1b103912eaacef82dfee940a3c3e8ecb693417bc.tar.gz rust-1b103912eaacef82dfee940a3c3e8ecb693417bc.zip | |
Add some documentation about globals in ffi docs
| -rw-r--r-- | doc/tutorial-ffi.md | 42 |
1 files changed, 42 insertions, 0 deletions
diff --git a/doc/tutorial-ffi.md b/doc/tutorial-ffi.md index 047b57e56a6..d1aa793e5fc 100644 --- a/doc/tutorial-ffi.md +++ b/doc/tutorial-ffi.md @@ -228,6 +228,48 @@ unsafe fn kaboom(ptr: *int) -> int { *ptr } This function can only be called from an `unsafe` block or another `unsafe` function. +# Accessing foreign globals + +Foreign APIs often export a global variable which could do something like track +global state. In order to access these variables, you declare them in `extern` +blocks with the `static` keyword: + +~~~{.xfail-test} +use std::libc; + +#[link_args = "-lreadline"] +extern { + static rl_readline_version: libc::c_int; +} + +fn main() { + println(fmt!("You have readline version %d installed.", + rl_readline_version as int)); +} +~~~ + +Alternatively, you may need to alter global state provided by a foreign +interface. To do this, statics can be declared with `mut` so rust can mutate +them. + +~~~{.xfail-test} +use std::libc; +use std::ptr; + +#[link_args = "-lreadline"] +extern { + static mut rl_prompt: *libc::c_char; +} + +fn main() { + do "[my-awesome-shell] $".as_c_str |buf| { + unsafe { rl_prompt = buf; } + // get a line, process it + unsafe { rl_prompt = ptr::null(); } + } +} +~~~ + # Foreign calling conventions Most foreign code exposes a C ABI, and Rust uses the platform's C calling convention by default when |
