Skip to content

Commit d2458fd

Browse files
committed
Use TryFrom for DefaultKeyring
1 parent 59eed51 commit d2458fd

File tree

2 files changed

+21
-13
lines changed

2 files changed

+21
-13
lines changed

keyutils-raw/src/types.rs

Lines changed: 19 additions & 12 deletions
Original file line numberDiff line numberDiff line change
@@ -24,8 +24,11 @@
2424
// (INCLUDING NEGLIGENCE OR OTHERWISE) ARISING IN ANY WAY OUT OF THE USE OF THIS
2525
// SOFTWARE, EVEN IF ADVISED OF THE POSSIBILITY OF SUCH DAMAGE.
2626

27+
use std::convert::TryFrom;
28+
use std::num::NonZeroI32;
29+
2730
/// Alias for the key_serial_t kernel type, representing a keyring (or key).
28-
pub type KeyringSerial = std::num::NonZeroI32;
31+
pub type KeyringSerial = NonZeroI32;
2932

3033
/// Alias for the key_perm_t kernel type, representing a keyring's (or key's)
3134
/// permission bits.
@@ -68,19 +71,23 @@ pub enum DefaultKeyring {
6871
DefaultKeyring = 0,
6972
}
7073

71-
impl From<libc::c_long> for DefaultKeyring {
72-
fn from(id: libc::c_long) -> DefaultKeyring {
74+
#[derive(Debug, PartialEq, Eq)]
75+
pub struct UnknownDefault(libc::c_long);
76+
77+
impl TryFrom<libc::c_long> for DefaultKeyring {
78+
type Error = UnknownDefault;
79+
fn try_from(id: libc::c_long) -> Result<DefaultKeyring, UnknownDefault> {
7380
use self::DefaultKeyring::*;
7481
match id {
75-
x if x == NoChange as libc::c_long => NoChange,
76-
x if x == ThreadKeyring as libc::c_long => ThreadKeyring,
77-
x if x == ProcessKeyring as libc::c_long => ProcessKeyring,
78-
x if x == SessionKeyring as libc::c_long => SessionKeyring,
79-
x if x == UserKeyring as libc::c_long => UserKeyring,
80-
x if x == UserSessionKeyring as libc::c_long => UserSessionKeyring,
81-
x if x == GroupKeyring as libc::c_long => GroupKeyring,
82-
x if x == DefaultKeyring as libc::c_long => DefaultKeyring,
83-
_ => panic!("Invalid value for a default keyring: {}", id),
82+
x if x == NoChange as libc::c_long => Ok(NoChange),
83+
x if x == ThreadKeyring as libc::c_long => Ok(ThreadKeyring),
84+
x if x == ProcessKeyring as libc::c_long => Ok(ProcessKeyring),
85+
x if x == SessionKeyring as libc::c_long => Ok(SessionKeyring),
86+
x if x == UserKeyring as libc::c_long => Ok(UserKeyring),
87+
x if x == UserSessionKeyring as libc::c_long => Ok(UserSessionKeyring),
88+
x if x == GroupKeyring as libc::c_long => Ok(GroupKeyring),
89+
x if x == DefaultKeyring as libc::c_long => Ok(DefaultKeyring),
90+
x => Err(UnknownDefault(x)),
8491
}
8592
}
8693
}

src/api.rs

Lines changed: 2 additions & 1 deletion
Original file line numberDiff line numberDiff line change
@@ -25,6 +25,7 @@
2525
// SOFTWARE, EVEN IF ADVISED OF THE POSSIBILITY OF SUCH DAMAGE.
2626

2727
use std::borrow::Borrow;
28+
use std::convert::TryInto;
2829
use std::ffi::CString;
2930
use std::mem;
3031
use std::ptr;
@@ -111,7 +112,7 @@ impl Keyring {
111112
pub fn set_default(keyring: DefaultKeyring) -> Result<DefaultKeyring> {
112113
let ret = unsafe { keyctl_set_reqkey_keyring(keyring as libc::c_int) };
113114
check_call(ret)?;
114-
Ok(ret.into())
115+
Ok(ret.try_into().unwrap())
115116
}
116117

117118
/// Requests a keyring with the given description by searching the thread, process, and session

0 commit comments

Comments
 (0)