Unsafe in Rust
Use case with sysinfo
by Guillaume Gomez
Who am I?
Rust language reviewer and contributor.
Member of:
- rustdoc team (team leader)
- docs.rs team
- tools team
I am a Huawei engineer.
### What is "FFI"?
"FFI" stands for "Foreign Function Interface".
In short: declaring items from a C library in your Rust code to use them.
### NonNull doesn't solve all issues!
* You can still have concurrent access to the data pointed by the pointer.
* You still have to free the memory yourself.
* You still need to initialize the memory you allocated to the pointer.
* You can still have dangling pointers.
### Wrapping pointers is the key
### Unifying different low-level APIs
Traits are the key!
### Example with system CPU usage
1. Get diff time
2. Get maximum CPU time
2. Get CPU time
### On Linux
Read and parse content from `/proc/stat`...
That's it!
### On macOS
1. Use __sysctl__ to get number of processors
2. Get the processors' info with __host_processor_info__
### On Windows
Very "opaque".
1. Open connection to "query engine"
2. Create a query (looks like "% Processor time")
3. Translate the query
4. Run the query
### And it was just for one information
This is why it's very important to handle it with traits to ensure each platform provides the same information the same way.
### Limitations
Unfortunately, not all information can be retrieved on all systems.
Components are a good example.
### Going around some limitations
Study case: getting a process environment on mac.
Thank you for listening!
More advanced explanations on
< blog.guillaume-gomez.fr >
< guillaume1.gomez@gmail.com >
@GuillaumeGomez
@imperioworld_