Skip to content

Commit

Permalink
Add some docs to errno module
Browse files Browse the repository at this point in the history
  • Loading branch information
PolyMeilex committed Jan 7, 2024
1 parent 88bb1ba commit 47f3223
Showing 1 changed file with 35 additions and 0 deletions.
35 changes: 35 additions & 0 deletions src/errno.rs
Original file line number Diff line number Diff line change
@@ -1,3 +1,16 @@
//! Safe wrappers around errno functions
//!
//! # Example
//! ```
//! use nix::errno::{Errno, set_errno};
//!
//! set_errno(Errno::EIO);
//! assert_eq!(Errno::last(), Errno::EIO);
//!
//! Errno::clear();
//! assert_eq!(Errno::last(), Errno::from_i32(0));
//! ```

use crate::Result;
use cfg_if::cfg_if;
use libc::{c_int, c_void};
Expand Down Expand Up @@ -53,6 +66,15 @@ pub fn errno() -> i32 {
}

/// Sets the platform-specific value of errno.
///
/// # Example
/// ```
/// use nix::errno::{Errno, set_errno};
///
/// set_errno(Errno::EIO);
///
/// assert_eq!(Errno::last(), Errno::EIO);
/// ```
pub fn set_errno(errno: Errno) {
// Safe because errno is a thread-local variable
unsafe {
Expand All @@ -73,6 +95,19 @@ impl Errno {
from_i32(err)
}

/// Sets the platform-specific errno to no-error
///
/// ```
/// use nix::errno::{Errno, set_errno};
///
/// set_errno(Errno::EIO);
///
/// Errno::clear();
///
/// let err = Errno::last();
/// assert_ne!(err, Errno::EIO);
/// assert_eq!(err, Errno::from_i32(0));
/// ```
pub fn clear() {
clear()
}
Expand Down

0 comments on commit 47f3223

Please sign in to comment.