From d66616a4fa71609231688119c40a3a0ec39a17ab Mon Sep 17 00:00:00 2001 From: Robin Krahl Date: Thu, 7 Jun 2018 03:21:57 +0200 Subject: Implement easier CString creation The new get_cstring method in util returns a Result, so mast callers can just use the ? operator to unwrap the result instead of cumbersome unwrapping code. --- src/util.rs | 6 +++++- 1 file changed, 5 insertions(+), 1 deletion(-) (limited to 'src/util.rs') diff --git a/src/util.rs b/src/util.rs index 1608952..75de799 100644 --- a/src/util.rs +++ b/src/util.rs @@ -2,7 +2,7 @@ use libc::{c_void, free}; use nitrokey_sys; use rand::{OsRng, Rng}; use std; -use std::ffi::CStr; +use std::ffi::{CStr, CString}; use std::os::raw::{c_char, c_int}; /// Error types returned by Nitrokey device or by the library. @@ -110,6 +110,10 @@ pub fn generate_password(length: usize) -> std::io::Result> { return Ok(data); } +pub fn get_cstring>>(s: T) -> Result { + CString::new(s).or(Err(CommandError::InvalidString)) +} + impl From for CommandError { fn from(value: c_int) -> Self { match value { -- cgit v1.2.1