Skip to content

Commit 7e1209b

Browse files
brsonclaude
andcommitted
Add libc crate documentation
Co-Authored-By: Claude Opus 4.6 <noreply@anthropic.com>
1 parent 9e5f6f1 commit 7e1209b

4 files changed

Lines changed: 92 additions & 3 deletions

File tree

crates/rustmax-doctest/src/generate.rs

Lines changed: 1 addition & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -200,6 +200,7 @@ fn rewrite_imports(code: &str) -> String {
200200
"unicode_segmentation", "num_bigint", "semver", "mime", "crossbeam",
201201
"blake3", "sha2", "powerletters", "flate2", "env_logger",
202202
"derive_more", "num_enum", "rand", "bytes", "proptest", "socket2",
203+
"libc",
203204
];
204205

205206
let mut result = code.to_string();
Lines changed: 60 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -0,0 +1,60 @@
1+
Raw FFI bindings to platform-specific system libraries.
2+
3+
- Crate [`::libc`].
4+
- [docs.rs](https://docs.rs/libc)
5+
- [crates.io](https://crates.io/crates/libc)
6+
- [GitHub](https://github.com/rust-lang/libc)
7+
8+
---
9+
10+
`libc` provides raw bindings to the C standard library
11+
and platform-specific system APIs.
12+
It is the foundational crate for most Rust FFI that
13+
needs to interoperate with system and native libraries.
14+
15+
The crate exports types, constants, and function signatures
16+
matching the platform's C headers.
17+
On Linux this includes POSIX APIs and Linux-specific extensions;
18+
on macOS, the Darwin system APIs; on Windows, a small subset of CRT functions.
19+
20+
Common uses include:
21+
- Calling POSIX functions like [`open`], [`read`], [`write`], [`mmap`]
22+
- Accessing system constants like [`EINVAL`], [`O_RDONLY`], [`SIGTERM`]
23+
- Defining types for FFI function signatures ([`c_int`], [`c_char`], [`size_t`])
24+
- Low-level memory operations like [`malloc`] and [`free`]
25+
26+
Note that basic C type aliases like `c_int` and `c_char`
27+
are available directly in [`std::ffi`].
28+
The [`nix`](https://docs.rs/nix)
29+
and [`rustix`](https://docs.rs/rustix)
30+
crates provide additional high-level safe Unix API bindings.
31+
For Windows, the [`windows`](https://docs.rs/windows)
32+
and [`windows-sys`](https://docs.rs/windows-sys) crates
33+
provide bindings to the Win32 API.
34+
35+
## Examples
36+
37+
Querying system configuration not exposed by `std`:
38+
39+
```rust
40+
let page_size = unsafe { libc::sysconf(libc::_SC_PAGESIZE) };
41+
assert!(page_size > 0);
42+
43+
let uid = unsafe { libc::getuid() };
44+
let pid = unsafe { libc::getpid() };
45+
assert!(pid > 0);
46+
```
47+
48+
[`open`]: crate::libc::open
49+
[`read`]: crate::libc::read
50+
[`write`]: crate::libc::write
51+
[`mmap`]: crate::libc::mmap
52+
[`EINVAL`]: crate::libc::EINVAL
53+
[`O_RDONLY`]: crate::libc::O_RDONLY
54+
[`SIGTERM`]: crate::libc::SIGTERM
55+
[`c_int`]: crate::libc::c_int
56+
[`c_char`]: crate::libc::c_char
57+
[`size_t`]: crate::libc::size_t
58+
[`malloc`]: crate::libc::malloc
59+
[`free`]: crate::libc::free
60+
[`std`]: crate::std

crates/rustmax/src/lib.rs

Lines changed: 1 addition & 3 deletions
Original file line numberDiff line numberDiff line change
@@ -493,9 +493,7 @@ pub mod json5 {
493493

494494
#[cfg(feature = "libc")]
495495
pub mod libc {
496-
//! Bindings to the C standard library.
497-
//!
498-
//! See crate [`::libc`].
496+
#![doc = include_str!("../doc-src/crate-libc.md")]
499497

500498
pub use ::libc::*;
501499
}

src/linksubs.json5

Lines changed: 30 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -609,6 +609,36 @@
609609
"crate::termcolor::Buffer":
610610
"api/termcolor/struct.Buffer.html",
611611

612+
// libc
613+
"crate::libc::open":
614+
"api/libc/fn.open.html",
615+
"crate::libc::read":
616+
"api/libc/fn.read.html",
617+
"crate::libc::write":
618+
"api/libc/fn.write.html",
619+
"crate::libc::mmap":
620+
"api/libc/fn.mmap.html",
621+
"crate::libc::EINVAL":
622+
"api/libc/constant.EINVAL.html",
623+
"crate::libc::O_RDONLY":
624+
"api/libc/constant.O_RDONLY.html",
625+
"crate::libc::SIGTERM":
626+
"api/libc/constant.SIGTERM.html",
627+
"crate::libc::c_int":
628+
"api/libc/type.c_int.html",
629+
"crate::libc::c_char":
630+
"api/libc/type.c_char.html",
631+
"crate::libc::size_t":
632+
"api/libc/type.size_t.html",
633+
"crate::libc::malloc":
634+
"api/libc/fn.malloc.html",
635+
"crate::libc::free":
636+
"api/libc/fn.free.html",
637+
"crate::std":
638+
"api/std/index.html",
639+
"crate::std::ffi":
640+
"api/std/ffi/index.html",
641+
612642
// zip
613643
"crate::zip::ZipArchive":
614644
"api/zip/read/struct.ZipArchive.html",

0 commit comments

Comments
 (0)