@@ -2344,6 +2344,12 @@ mod setres {
2344
2344
2345
2345
2346
2346
#[ 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.
2347
2353
pub struct User {
2348
2354
/// username
2349
2355
pub name : String ,
@@ -2394,13 +2400,14 @@ impl From<libc::passwd> for User {
2394
2400
}
2395
2401
}
2396
2402
2403
+ /// Representation of a group, based on `libc::group`
2397
2404
#[ derive( Debug , Clone , PartialEq ) ]
2398
2405
pub struct Group {
2399
2406
/// group name
2400
2407
pub name : String ,
2401
2408
/// group ID
2402
2409
pub gid : Gid ,
2403
- /// list of members
2410
+ /// list of group members
2404
2411
pub mem : Vec < String >
2405
2412
}
2406
2413
@@ -2435,29 +2442,62 @@ impl Group {
2435
2442
}
2436
2443
2437
2444
#[ 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
+ /// ```
2441
2459
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
2443
2463
/// [getpwuid_r(3)](http://pubs.opengroup.org/onlinepubs/9699919799/functions/getpwuid_r.html)
2444
2464
Uid ( Uid ) ,
2445
- /// Get a user by name. Internally, this function calls
2465
+ /// Get a user by name.
2466
+ ///
2467
+ /// Internally, this function calls
2446
2468
/// [getpwnam_r(3)](http://pubs.opengroup.org/onlinepubs/9699919799/functions/getpwuid_r.html)
2447
2469
Name ( String ) ,
2448
2470
UidWithBufsize ( Uid , usize ) ,
2449
2471
NameWithBufsize ( String , usize )
2450
2472
}
2451
2473
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
+ /// ```
2452
2491
#[ 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.
2456
2492
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
2458
2496
/// [getgrgid_r(3)](http://pubs.opengroup.org/onlinepubs/9699919799/functions/getpwuid_r.html)
2459
2497
Gid ( Gid ) ,
2460
- /// Get a group by name. Internally, this function calls
2498
+ /// Get a group by name.
2499
+ ///
2500
+ /// Internally, this function calls
2461
2501
/// [getgrnam_r(3)](http://pubs.opengroup.org/onlinepubs/9699919799/functions/getpwuid_r.html)
2462
2502
Name ( String ) ,
2463
2503
GidWithBufsize ( Gid , usize ) ,
@@ -2582,7 +2622,9 @@ mod usergroupiter {
2582
2622
use super :: { Error , User , Group , PWGRP_BUFSIZE } ;
2583
2623
use std:: { mem, ptr} ;
2584
2624
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
2586
2628
///
2587
2629
/// ```
2588
2630
/// use nix::unistd::Users;
@@ -2596,6 +2638,8 @@ mod usergroupiter {
2596
2638
///
2597
2639
/// ```
2598
2640
///
2641
+ /// # Safety
2642
+ ///
2599
2643
/// This iterator should not be used in different threads without synchronization; while doing so
2600
2644
/// will not cause undefined behavior, because modern systems lack re-entrant versions of
2601
2645
/// `setpwent` and `endpwent`, it is very likely that iterators running in different threads will
@@ -2644,7 +2688,9 @@ mod usergroupiter {
2644
2688
}
2645
2689
}
2646
2690
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
2648
2694
///
2649
2695
/// ```
2650
2696
/// use nix::unistd::Groups;
@@ -2655,9 +2701,10 @@ mod usergroupiter {
2655
2701
/// gr.name,
2656
2702
/// gr.gid)))
2657
2703
/// .collect::<Vec<_>>();
2658
- ///
2659
2704
/// ```
2660
2705
///
2706
+ /// # Safety
2707
+ ///
2661
2708
/// This iterator should not be used in different threads without synchronization; while doing so
2662
2709
/// will not cause undefined behavior, because modern systems lack re-entrant versions of
2663
2710
/// `setgrent` and `endgrent`, it is very likely that iterators running in different threads will
0 commit comments