diff --git a/src/lib.rs b/src/lib.rs index 2d958f6..eb275e8 100644 --- a/src/lib.rs +++ b/src/lib.rs @@ -2,7 +2,7 @@ #![crate_type = "rlib"] #![crate_type = "dylib"] #![allow(unused_features)] -#![feature(core, libc, std_misc)] +#![feature(collections, core, libc, std_misc)] //! This is a library for getting information on Unix users and groups. //! @@ -92,7 +92,7 @@ //! ```rust //! use users::{Users, OSUsers}; //! let mut cache = OSUsers::empty_cache(); -//! match cache.get_group_by_name("admin".to_string()) { +//! match cache.get_group_by_name("admin") { //! None => {}, //! Some(group) => { //! println!("The '{}' group has the ID {}", group.name, group.gid); @@ -116,6 +116,9 @@ extern crate libc; use libc::{c_char, c_int, uid_t, gid_t, time_t}; +extern crate collections; +use collections::borrow::ToOwned; + use std::ffi::CStr; use std::ptr::read; use std::str::from_utf8_unchecked; @@ -131,13 +134,13 @@ pub trait Users { fn get_user_by_uid(&mut self, uid: i32) -> Option; /// Return a User object if one exists for the given username; otherwise, return None. - fn get_user_by_name(&mut self, username: String) -> Option; + fn get_user_by_name(&mut self, username: &str) -> Option; /// Return a Group object if one exists for the given group ID; otherwise, return None. fn get_group_by_gid(&mut self, gid: u32) -> Option; /// Return a Group object if one exists for the given groupname; otherwise, return None. - fn get_group_by_name(&mut self, group_name: String) -> Option; + fn get_group_by_name(&mut self, group_name: &str) -> Option; /// Return the user ID for the user running the process. fn get_current_uid(&mut self) -> i32; @@ -295,8 +298,10 @@ impl Users for OSUsers { } } - fn get_user_by_name(&mut self, username: String) -> Option { - match self.users_back.entry(username.clone()) { + fn get_user_by_name(&mut self, username: &str) -> Option { + // to_owned() could change here: + // https://github.com/rust-lang/rfcs/blob/master/text/0509-collections-reform-part-2.md#alternatives-to-toowned-on-entries + match self.users_back.entry(username.to_owned()) { Vacant(entry) => { let user = unsafe { passwd_to_user(getpwnam(username.as_ptr() as *const i8)) }; match user { @@ -338,8 +343,10 @@ impl Users for OSUsers { } } - fn get_group_by_name(&mut self, group_name: String) -> Option { - match self.groups_back.clone().entry(group_name.clone()) { + fn get_group_by_name(&mut self, group_name: &str) -> Option { + // to_owned() could change here: + // https://github.com/rust-lang/rfcs/blob/master/text/0509-collections-reform-part-2.md#alternatives-to-toowned-on-entries + match self.groups_back.clone().entry(group_name.to_owned()) { Vacant(entry) => { let user = unsafe { struct_to_group(getgrnam(group_name.as_ptr() as *const i8)) }; match user { @@ -398,7 +405,7 @@ pub fn get_user_by_uid(uid: i32) -> Option { } /// Return a User object if one exists for the given username; otherwise, return None. -pub fn get_user_by_name(username: String) -> Option { +pub fn get_user_by_name(username: &str) -> Option { OSUsers::empty_cache().get_user_by_name(username) } @@ -408,7 +415,7 @@ pub fn get_group_by_gid(gid: u32) -> Option { } /// Return a Group object if one exists for the given groupname; otherwise, return None. -pub fn get_group_by_name(group_name: String) -> Option { +pub fn get_group_by_name(group_name: &str) -> Option { OSUsers::empty_cache().get_group_by_name(group_name) } diff --git a/src/mock.rs b/src/mock.rs index 1f63110..a97f907 100644 --- a/src/mock.rs +++ b/src/mock.rs @@ -86,7 +86,7 @@ impl Users for MockUsers { self.users.get(&uid).cloned() } - fn get_user_by_name(&mut self, username: String) -> Option { + fn get_user_by_name(&mut self, username: &str) -> Option { self.users.values().find(|u| u.name == username).cloned() } @@ -94,7 +94,7 @@ impl Users for MockUsers { self.groups.get(&gid).cloned() } - fn get_group_by_name(&mut self, group_name: String) -> Option { + fn get_group_by_name(&mut self, group_name: &str) -> Option { self.groups.values().find(|g| g.name == group_name).cloned() } @@ -135,14 +135,14 @@ mod test { fn username() { let mut users = MockUsers::with_current_uid(1337); users.add_user(User { uid: 1440, name: "fred".to_string(), primary_group: 101 }); - assert_eq!(Some(1440), users.get_user_by_name("fred".to_string()).map(|u| u.uid)) + assert_eq!(Some(1440), users.get_user_by_name("fred").map(|u| u.uid)) } #[test] fn no_username() { let mut users = MockUsers::with_current_uid(1337); users.add_user(User { uid: 1440, name: "fred".to_string(), primary_group: 101 }); - assert_eq!(None, users.get_user_by_name("criminy".to_string()).map(|u| u.uid)) + assert_eq!(None, users.get_user_by_name("criminy").map(|u| u.uid)) } #[test] @@ -162,14 +162,14 @@ mod test { fn group_name() { let mut users = MockUsers::with_current_uid(0); users.add_group(Group { gid: 1337, name: "fred".to_string(), members: vec![], }); - assert_eq!(Some(1337), users.get_group_by_name("fred".to_string()).map(|g| g.gid)) + assert_eq!(Some(1337), users.get_group_by_name("fred").map(|g| g.gid)) } #[test] fn no_group_name() { let mut users = MockUsers::with_current_uid(0); users.add_group(Group { gid: 1337, name: "fred".to_string(), members: vec![], }); - assert_eq!(None, users.get_group_by_name("santa".to_string()).map(|g| g.gid)) + assert_eq!(None, users.get_group_by_name("santa").map(|g| g.gid)) } #[test]