Skip to content

Commit

Permalink
Add a private field to some large structs
Browse files Browse the repository at this point in the history
Adding a private field to these structs will prevent clients from
directly constructing them.  This will allow us to add new fields
to them in a semver compatible way.

This change itself is a semver incompatible change, though.
  • Loading branch information
eminence committed Oct 4, 2020
1 parent 800619b commit 3f5462e
Show file tree
Hide file tree
Showing 3 changed files with 16 additions and 0 deletions.
7 changes: 7 additions & 0 deletions src/meminfo.rs
Original file line number Diff line number Diff line change
Expand Up @@ -22,9 +22,15 @@ use super::{convert_to_kibibytes, FileWrapper, ProcResult};
/// This imprecision in /proc/meminfo is known,
/// but is not corrected due to legacy concerns -
/// programs rely on /proc/meminfo to specify size with the "kB" string.
///
/// New fields to this struct may be added at any time (even without a major or minor semver bump).
#[derive(Debug)]
#[allow(non_snake_case)]
pub struct Meminfo {
// this private field prevents clients from directly constructing this object.
// this allows us (procfs) to add fields in a semver compatible way
_private: (),

/// Total usable RAM (i.e., physical RAM minus a few reserved bits and the kernel binary code).
pub mem_total: u64,
/// The sum of [LowFree](#structfield.low_free) + [HighFree](#structfield.high_free).
Expand Down Expand Up @@ -302,6 +308,7 @@ impl Meminfo {
// if there's anything still left in the map at the end, that
// means we probably have a bug/typo, or are out-of-date
let meminfo = Meminfo {
_private: (),
mem_total: expect!(map.remove("MemTotal")),
mem_free: expect!(map.remove("MemFree")),
mem_available: map.remove("MemAvailable"),
Expand Down
5 changes: 5 additions & 0 deletions src/process/stat.rs
Original file line number Diff line number Diff line change
Expand Up @@ -23,8 +23,12 @@ macro_rules! since_kernel {
/// To construct one of these structures, you have to first create a [Process](crate::process::Process).
///
/// Not all fields are available in every kernel. These fields have `Option<T>` types.
///
/// New fields to this struct may be added at any time (even without a major or minor semver bump).
#[derive(Debug, Clone)]
pub struct Stat {
_private: (),

/// The process ID.
pub pid: i32,
/// The filename of the executable, in parentheses.
Expand Down Expand Up @@ -309,6 +313,7 @@ impl Stat {
let exit_code = since_kernel!(3, 5, 0, expect!(from_iter(&mut rest)));

Ok(Stat {
_private: (),
pid,
comm,
state,
Expand Down
4 changes: 4 additions & 0 deletions src/process/status.rs
Original file line number Diff line number Diff line change
Expand Up @@ -11,8 +11,11 @@ use std::io::{BufRead, BufReader, Read};
/// isn't totally reliable, since some kernels might backport certain fields, or fields might
/// only be present if certain kernel configuration options are enabled. Be prepared to
/// handle `None` values.
///
/// New fields to this struct may be added at any time (even without a major or minor semver bump).
#[derive(Debug, Clone)]
pub struct Status {
_private: (),
/// Command run by this process.
pub name: String,
/// Process umask, expressed in octal with a leading zero; see umask(2). (Since Linux 4.7.)
Expand Down Expand Up @@ -189,6 +192,7 @@ impl Status {
}

let status = Status {
_private: (),
name: expect!(map.remove("Name")),
umask: map
.remove("Umask")
Expand Down

0 comments on commit 3f5462e

Please sign in to comment.