From 88b0d5544001d326496b335bf626562c68481c1c Mon Sep 17 00:00:00 2001 From: emma Date: Thu, 11 Apr 2024 20:21:01 -0600 Subject: [PATCH] getopt.rs(3): refactor to remove as much as possible from unsafe --- src/getopt.rs | 35 ++++++++++++++--------------------- 1 file changed, 14 insertions(+), 21 deletions(-) diff --git a/src/getopt.rs b/src/getopt.rs index f7668c6..b95108c 100644 --- a/src/getopt.rs +++ b/src/getopt.rs @@ -31,31 +31,24 @@ pub enum OptError { // Function signature pub trait GetOpt { - fn getopt( - argv: Vec, - optstring: &str - ) -> Option>; + fn getopt(&self, optstring: &str) -> Option>; } impl GetOpt for Vec { - fn getopt( - argv: Vec, - optstring: &str - ) -> Option> { + fn getopt(&self, optstring: &str) -> Option> { + let argv = self + .iter() + .cloned() + .map(CString::new) + .map(Result::unwrap) + .map(|x| x.as_ptr() as c_char) + .collect::>() + .as_ptr() as *const *mut c_char; + let opts = CString::new(optstring).unwrap().into_raw(); + let len = self.len() as c_int; + unsafe { - let args = argv - .iter() - .cloned() - .map(CString::new) - .map(Result::unwrap) - .map(|x| x.as_ptr() as c_char) - .collect::>() - .as_ptr() as *const *mut c_char; - - let len = argv.len() as c_int; - let opts = CString::new(optstring).unwrap().into_raw(); - - let a = match getopt(len, args, opts) { + let a = match getopt(len, argv, opts) { /* From getopt(3p): * * The getopt() function shall return the next option character