Skip to content
Merged
Changes from all commits
Commits
File filter

Filter by extension

Filter by extension

Conversations
Failed to load comments.
Loading
Jump to
Jump to file
Failed to load files.
Loading
Diff view
Diff view
11 changes: 9 additions & 2 deletions src/pid.rs
Original file line number Diff line number Diff line change
Expand Up @@ -27,17 +27,21 @@ impl Pid {

/// Converts a `RawPid` into a `Pid`.
///
/// Returns `Some` for positive `RawPid`s. Otherwise, returns `None`.
/// Returns `Some` for positive values, and `None` for zero values.
///
/// This is safe because a `Pid` is a number without any guarantees for the
/// kernel. Non-child `Pid`s are always racy for any syscalls, but can only
/// cause logic errors. If you want race-free access to or control of
/// non-child processes, please consider other mechanisms like [pidfd] on
/// Linux.
///
/// Passing a negative number doesn't invoke undefined behavior, but it
/// may cause unexpected behavior.
///
/// [pidfd]: https://man7.org/linux/man-pages/man2/pidfd_open.2.html
#[inline]
pub const fn from_raw(raw: RawPid) -> Option<Self> {
debug_assert!(raw > 0);
match NonZeroI32::new(raw) {
Some(non_zero) => Some(Self(non_zero)),
None => None,
Expand All @@ -46,9 +50,12 @@ impl Pid {

/// Converts a known positive `RawPid` into a `Pid`.
///
/// Passing a negative number doesn't invoke undefined behavior, but it
/// may cause unexpected behavior.
///
/// # Safety
///
/// The caller must guarantee `raw` is positive.
/// The caller must guarantee `raw` is non-zero.
#[inline]
pub const unsafe fn from_raw_unchecked(raw: RawPid) -> Self {
debug_assert!(raw > 0);
Expand Down
Loading