|
24 | 24 | // (INCLUDING NEGLIGENCE OR OTHERWISE) ARISING IN ANY WAY OUT OF THE USE OF THIS |
25 | 25 | // SOFTWARE, EVEN IF ADVISED OF THE POSSIBILITY OF SUCH DAMAGE. |
26 | 26 |
|
| 27 | +use std::convert::TryFrom; |
| 28 | +use std::num::NonZeroI32; |
| 29 | + |
27 | 30 | /// 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; |
29 | 32 |
|
30 | 33 | /// Alias for the key_perm_t kernel type, representing a keyring's (or key's) |
31 | 34 | /// permission bits. |
@@ -68,19 +71,23 @@ pub enum DefaultKeyring { |
68 | 71 | DefaultKeyring = 0, |
69 | 72 | } |
70 | 73 |
|
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> { |
73 | 80 | use self::DefaultKeyring::*; |
74 | 81 | 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)), |
84 | 91 | } |
85 | 92 | } |
86 | 93 | } |
0 commit comments