Hi, I am trying to add some profiling to a small rust application for multithreading perfomance testing.
getrusage from posix seems to be a perfect match for my use case.
I found an unsafe wrapper of getrusage from the libc wrapper library but with no much documentation. also the fact that it is qualified as unsafe does not really make me want to use it.
Everything in libc is marked as unsafe on principle, because it comes from C, and Rust doesn't control what C does. It doesn't mean it's bad, it means you need to read manual of the C function and ensure that you don't misuse it.
Rust's libc doesn't do anything itself. It only exposes functions that already exist in C exactly as they are. So read manual for the getrusage for C, and everything will apply to Rust.
If you end up wrapping getrusage, we would love to add it to nix. We could even walk you through it's addition in a PR if you're interested in starting there. Do note, however, that docs for these functions are primarily in the man pages and higher-level wrapers usually defer to those docs primarily.
This defines a struct on stack. It's automatically freed when goes out of scope.
I've used mem::zeroed() here because Rust requires variables to be initialized with something (it's equivalent of memset with 0). Because getrusage is supposed to overwrite the entire struct, mem::uninitialized() would have worked as well (it pretends to initialize, but doesn't :)).
Default is roughly similar, but it only works on structs that explicitly implement it and use struct-specific defaults.