From 8f7eb1679fa988ad97b0e6dbe0ef5d1edef18351 Mon Sep 17 00:00:00 2001 From: Robin Krahl Date: Mon, 28 May 2018 20:32:10 +0000 Subject: Restructure trait implementations for wrappers For DeviceWrapper, the traits GenerateOtp and Device are now directly implemented instead of complicated template implementations. For User and Admin, the wrapper implementations are replaced with an implementation of the Deref trait for easier access. --- src/device.rs | 64 ++++++++++++++++++++++++-------------------------------- src/tests/pro.rs | 5 +++-- 2 files changed, 30 insertions(+), 39 deletions(-) diff --git a/src/device.rs b/src/device.rs index 25e4596..8f7dc00 100644 --- a/src/device.rs +++ b/src/device.rs @@ -2,6 +2,7 @@ use config::{Config, RawConfig}; use libc; use nitrokey_sys; use std::ffi::CString; +use std::ops::Deref; use std::os::raw::c_int; use misc::Authenticate; use otp::{ConfigureOtp, GenerateOtp, OtpMode, OtpSlotData, RawOtpSlotData}; @@ -403,43 +404,34 @@ fn connect_model(model: Model) -> bool { unsafe { nitrokey_sys::NK_login_enum(model) == 1 } } -impl> GenerateOtp for T { +impl DeviceWrapper { + fn device(&self) -> &Device { + match *self { + DeviceWrapper::Storage(_) => panic!("..."), + DeviceWrapper::Pro(ref pro) => pro, + } + } +} + +impl GenerateOtp for DeviceWrapper { fn get_hotp_slot_name(&self, slot: u8) -> Result { - self.as_ref().get_hotp_slot_name(slot) + self.device().get_hotp_slot_name(slot) } fn get_totp_slot_name(&self, slot: u8) -> Result { - self.as_ref().get_totp_slot_name(slot) + self.device().get_totp_slot_name(slot) } fn get_hotp_code(&self, slot: u8) -> Result { - self.as_ref().get_hotp_code(slot) + self.device().get_hotp_code(slot) } fn get_totp_code(&self, slot: u8) -> Result { - self.as_ref().get_totp_code(slot) + self.device().get_totp_code(slot) } } -impl + GenerateOtp> Device for T {} - -impl AsRef for DeviceWrapper { - fn as_ref(&self) -> &(GenerateOtp + 'static) { - match *self { - DeviceWrapper::Storage(_) => panic!("..."), - DeviceWrapper::Pro(ref pro) => pro, - } - } -} - -impl AsRef for DeviceWrapper { - fn as_ref(&self) -> &(Device + 'static) { - match *self { - DeviceWrapper::Storage(_) => panic!("..."), - DeviceWrapper::Pro(ref pro) => pro, - } - } -} +impl Device for DeviceWrapper {} impl Authenticate for DeviceWrapper { fn authenticate_user(self, password: &str) -> Result, (Self, CommandError)> { @@ -534,12 +526,6 @@ impl Device for Pro {} impl GenerateOtp for Pro {} -impl AsRef for User { - fn as_ref(&self) -> &(Device + 'static) { - &self.device - } -} - impl User { /// Forgets the user authentication and returns an unauthenticated device. This method /// consumes the authenticated device. It does not perform any actual commands on the @@ -549,6 +535,14 @@ impl User { } } +impl Deref for User { + type Target = T; + + fn deref(&self) -> &Self::Target { + &self.device + } +} + impl GenerateOtp for User { fn get_hotp_code(&self, slot: u8) -> Result { unsafe { @@ -580,14 +574,10 @@ impl AuthenticatedDevice for User { } } -impl AsRef for Admin { - fn as_ref(&self) -> &(GenerateOtp + 'static) { - &self.device - } -} +impl Deref for Admin { + type Target = T; -impl AsRef for Admin { - fn as_ref(&self) -> &(Device + 'static) { + fn deref(&self) -> &Self::Target { &self.device } } diff --git a/src/tests/pro.rs b/src/tests/pro.rs index e52c287..5415ff9 100644 --- a/src/tests/pro.rs +++ b/src/tests/pro.rs @@ -1,4 +1,5 @@ use std::ffi::CStr; +use std::ops::Deref; use {Admin, Authenticate, CommandError, CommandStatus, Config, ConfigureOtp, Device, GenerateOtp, OtpMode, OtpSlotData, Pro}; @@ -101,7 +102,7 @@ fn hotp() { assert_eq!(CommandStatus::Success, admin.write_config(config)); configure_hotp(&admin); - check_hotp_codes(&admin); + check_hotp_codes(admin.deref()); configure_hotp(&admin); check_hotp_codes(&admin.device()); @@ -203,7 +204,7 @@ fn totp() { assert_eq!(CommandStatus::Success, admin.write_config(config)); configure_totp(&admin); - check_totp_codes(&admin); + check_totp_codes(admin.deref()); configure_totp(&admin); check_totp_codes(&admin.device()); -- cgit v1.2.1