pwd

Struct Passwd

Source
pub struct Passwd {
    pub name: String,
    pub passwd: Option<String>,
    pub uid: u32,
    pub gid: u32,
    pub gecos: Option<String>,
    pub dir: String,
    pub shell: String,
}
Expand description

The main struct for the library, a safe version of the POSIX struct passwd

There are 2 ways to construct a Passwd instance (other than assigning fields by hand). You can look up a user account by username with Passwd::from_name(String), or by uid with Passwd::from_uid(u32).

There is a shortcut function, Passwd::current_user(), which is just short for Passwd::from_uid(unsafe { libc::getuid() } as u32).

Fields§

§name: String§passwd: Option<String>§uid: u32§gid: u32§gecos: Option<String>§dir: String§shell: String

Implementations§

Source§

impl Passwd

Source

pub fn from_name(name: &str) -> Result<Option<Passwd>>

Looks up the username and returns a Passwd with the user’s values, if the user is found

This is Result<Option<>> because the operation to convert a rust String to a cstring could fail

§Example
use pwd::Passwd;

let pwd = Passwd::from_name("bob")?;

if let Some(passwd) = pwd {
    println!("uid is {}", passwd.uid);
}
Source

pub fn from_uid(uid: u32) -> Option<Passwd>

Looks up the uid and returns a Passwd with the user’s values, if the user is found

§Example
use libc::getuid;
use pwd::Passwd;

let uid = unsafe { getuid() };
let pwd = Passwd::from_uid(uid as u32);

if let Some(passwd) = pwd {
    println!("username is {}", passwd.name);
}
Source

pub fn current_user() -> Option<Passwd>

Shortcut for Passwd::from_uid(libc::getuid() as u32), so see the docs for that constructor

§Example
use pwd::Passwd;

let pwd = Passwd::current_user();

if let Some(passwd) = pwd {
    println!("username is {}", passwd.name);
}
Source

pub fn iter() -> PasswdIter

Returns an iterator over all entries in the /etc/passwd file

§Example
use pwd::Passwd;

let passwds = Passwd::iter();

for passwd in passwds {
    println!("username is {}", passwd.name);
}

Trait Implementations§

Source§

impl Clone for Passwd

Source§

fn clone(&self) -> Passwd

Returns a copy of the value. Read more
1.0.0 · Source§

fn clone_from(&mut self, source: &Self)

Performs copy-assignment from source. Read more
Source§

impl Debug for Passwd

Source§

fn fmt(&self, f: &mut Formatter<'_>) -> Result

Formats the value using the given formatter. Read more
Source§

impl PartialEq for Passwd

Source§

fn eq(&self, other: &Passwd) -> bool

Tests for self and other values to be equal, and is used by ==.
1.0.0 · Source§

fn ne(&self, other: &Rhs) -> bool

Tests for !=. The default implementation is almost always sufficient, and should not be overridden without very good reason.
Source§

impl StructuralPartialEq for Passwd

Auto Trait Implementations§

§

impl Freeze for Passwd

§

impl RefUnwindSafe for Passwd

§

impl Send for Passwd

§

impl Sync for Passwd

§

impl Unpin for Passwd

§

impl UnwindSafe for Passwd

Blanket Implementations§

Source§

impl<T> Any for T
where T: 'static + ?Sized,

Source§

fn type_id(&self) -> TypeId

Gets the TypeId of self. Read more
Source§

impl<T> Borrow<T> for T
where T: ?Sized,

Source§

fn borrow(&self) -> &T

Immutably borrows from an owned value. Read more
Source§

impl<T> BorrowMut<T> for T
where T: ?Sized,

Source§

fn borrow_mut(&mut self) -> &mut T

Mutably borrows from an owned value. Read more
Source§

impl<T> CloneToUninit for T
where T: Clone,

Source§

unsafe fn clone_to_uninit(&self, dst: *mut u8)

🔬This is a nightly-only experimental API. (clone_to_uninit)
Performs copy-assignment from self to dst. Read more
Source§

impl<T> From<T> for T

Source§

fn from(t: T) -> T

Returns the argument unchanged.

Source§

impl<T, U> Into<U> for T
where U: From<T>,

Source§

fn into(self) -> U

Calls U::from(self).

That is, this conversion is whatever the implementation of From<T> for U chooses to do.

Source§

impl<T> ToOwned for T
where T: Clone,

Source§

type Owned = T

The resulting type after obtaining ownership.
Source§

fn to_owned(&self) -> T

Creates owned data from borrowed data, usually by cloning. Read more
Source§

fn clone_into(&self, target: &mut T)

Uses borrowed data to replace owned data, usually by cloning. Read more
Source§

impl<T, U> TryFrom<U> for T
where U: Into<T>,

Source§

type Error = Infallible

The type returned in the event of a conversion error.
Source§

fn try_from(value: U) -> Result<T, <T as TryFrom<U>>::Error>

Performs the conversion.
Source§

impl<T, U> TryInto<U> for T
where U: TryFrom<T>,

Source§

type Error = <U as TryFrom<T>>::Error

The type returned in the event of a conversion error.
Source§

fn try_into(self) -> Result<U, <U as TryFrom<T>>::Error>

Performs the conversion.