Skip to content

Commit d4d5a00

Browse files
ctrlcctrlvdario23
authored andcommitted
Better documentation to conform to standards
1 parent 86ee6fa commit d4d5a00

File tree

1 file changed

+61
-14
lines changed

1 file changed

+61
-14
lines changed

src/unistd.rs

Lines changed: 61 additions & 14 deletions
Original file line numberDiff line numberDiff line change
@@ -2344,6 +2344,12 @@ mod setres {
23442344

23452345

23462346
#[derive(Debug, Clone, PartialEq, Default)]
2347+
/// Representation of a user, based on `libc::passwd`
2348+
///
2349+
/// The reason some fields in this struct are `String` and others are `CString` is because some
2350+
/// fields are based on the user's locale, which could be non-UTF8, while other fields are
2351+
/// guaranteed to conform to [NAME_REGEX](https://serverfault.com/a/73101/407341), which only
2352+
/// contains ASCII.
23472353
pub struct User {
23482354
/// username
23492355
pub name: String,
@@ -2394,13 +2400,14 @@ impl From<libc::passwd> for User {
23942400
}
23952401
}
23962402

2403+
/// Representation of a group, based on `libc::group`
23972404
#[derive(Debug, Clone, PartialEq)]
23982405
pub struct Group {
23992406
/// group name
24002407
pub name: String,
24012408
/// group ID
24022409
pub gid: Gid,
2403-
/// list of members
2410+
/// list of group members
24042411
pub mem: Vec<String>
24052412
}
24062413

@@ -2435,29 +2442,62 @@ impl Group {
24352442
}
24362443

24372444
#[derive(Debug)]
2438-
/// Query a group. The buffer size variants should not be needed 99% of the time; the default
2439-
/// buffer size of 1024 should be more than adequate. Only use the buffer size variants if you
2440-
/// receive an ERANGE error without them, or you know that you will be likely to.
2445+
/// Query a user.
2446+
///
2447+
/// The buffer size variants should not be needed 99% of the time; the default buffer size of 1024
2448+
/// should be more than adequate. Only use the buffer size variants if you receive an ERANGE error
2449+
/// without them, or you know that you will be likely to.
2450+
///
2451+
/// # Example
2452+
///
2453+
/// ```
2454+
/// let find = String::from("root");
2455+
/// // Returns an Option<Result<User>>, thus the double unwrap.
2456+
/// let res = User::query( UserQuery::Name(find) ).unwrap().unwrap();
2457+
/// assert!(res.name == find);
2458+
/// ```
24412459
pub enum UserQuery {
2442-
/// Get a user by UID. Internally, this function calls
2460+
/// Get a user by UID.
2461+
///
2462+
/// Internally, this function calls
24432463
/// [getpwuid_r(3)](http://pubs.opengroup.org/onlinepubs/9699919799/functions/getpwuid_r.html)
24442464
Uid(Uid),
2445-
/// Get a user by name. Internally, this function calls
2465+
/// Get a user by name.
2466+
///
2467+
/// Internally, this function calls
24462468
/// [getpwnam_r(3)](http://pubs.opengroup.org/onlinepubs/9699919799/functions/getpwuid_r.html)
24472469
Name(String),
24482470
UidWithBufsize(Uid, usize),
24492471
NameWithBufsize(String, usize)
24502472
}
24512473

2474+
/// Query a group.
2475+
///
2476+
/// The buffer size variants should not be needed 99% of the time; the default buffer size of 1024
2477+
/// should be more than adequate. Only use the buffer size variants if you receive an ERANGE error
2478+
/// without them, or you know that you will be likely to.
2479+
///
2480+
/// # Example
2481+
///
2482+
/// ```ignore
2483+
/// use nix::unistd::{Group, GroupQuery};
2484+
/// // On many systems there's no `root` group; on FreeBSD for example it's known as `wheel`
2485+
/// let find = String::from("root");
2486+
/// // Returns an Option<Result<Group>>, thus the double unwrap. Will panic if there's no `root`
2487+
/// // group.
2488+
/// let res = Group::query( GroupQuery::Name(find) ).unwrap().unwrap();
2489+
/// assert!(res.name == find);
2490+
/// ```
24522491
#[derive(Debug)]
2453-
/// Query a user. The buffer size variants should not be needed 99% of the time; the default buffer
2454-
/// size of 1024 should be more than adequate. Only use the buffer size variants if you receive an
2455-
/// ERANGE error without them, or you know that you will be likely to.
24562492
pub enum GroupQuery {
2457-
/// Get a group by GID. Internally, this function calls
2493+
/// Get a group by GID.
2494+
///
2495+
/// Internally, this function calls
24582496
/// [getgrgid_r(3)](http://pubs.opengroup.org/onlinepubs/9699919799/functions/getpwuid_r.html)
24592497
Gid(Gid),
2460-
/// Get a group by name. Internally, this function calls
2498+
/// Get a group by name.
2499+
///
2500+
/// Internally, this function calls
24612501
/// [getgrnam_r(3)](http://pubs.opengroup.org/onlinepubs/9699919799/functions/getpwuid_r.html)
24622502
Name(String),
24632503
GidWithBufsize(Gid, usize),
@@ -2582,7 +2622,9 @@ mod usergroupiter {
25822622
use super::{Error, User, Group, PWGRP_BUFSIZE};
25832623
use std::{mem, ptr};
25842624

2585-
/// This iterator can be used to get all of the users on the system. For example:
2625+
/// Used to get all of the users on the system.
2626+
///
2627+
/// # Example
25862628
///
25872629
/// ```
25882630
/// use nix::unistd::Users;
@@ -2596,6 +2638,8 @@ mod usergroupiter {
25962638
///
25972639
/// ```
25982640
///
2641+
/// # Safety
2642+
///
25992643
/// This iterator should not be used in different threads without synchronization; while doing so
26002644
/// will not cause undefined behavior, because modern systems lack re-entrant versions of
26012645
/// `setpwent` and `endpwent`, it is very likely that iterators running in different threads will
@@ -2644,7 +2688,9 @@ mod usergroupiter {
26442688
}
26452689
}
26462690

2647-
/// This iterator can be used to get all of the groups on the system. For example:
2691+
/// Used to get all of the groups on the system.
2692+
///
2693+
/// # Example
26482694
///
26492695
/// ```
26502696
/// use nix::unistd::Groups;
@@ -2655,9 +2701,10 @@ mod usergroupiter {
26552701
/// gr.name,
26562702
/// gr.gid)))
26572703
/// .collect::<Vec<_>>();
2658-
///
26592704
/// ```
26602705
///
2706+
/// # Safety
2707+
///
26612708
/// This iterator should not be used in different threads without synchronization; while doing so
26622709
/// will not cause undefined behavior, because modern systems lack re-entrant versions of
26632710
/// `setgrent` and `endgrent`, it is very likely that iterators running in different threads will

0 commit comments

Comments
 (0)