getopt.rs(3): refactor to remove as much as possible from unsafe

This commit is contained in:
Emma Tebibyte 2024-04-11 20:21:01 -06:00
parent 0164b681c0
commit 88b0d55440
Signed by untrusted user: emma
GPG Key ID: 06FA419A1698C270

View File

@ -31,19 +31,12 @@ pub enum OptError {
// Function signature
pub trait GetOpt {
fn getopt(
argv: Vec<String>,
optstring: &str
) -> Option<Result<Opt, OptError>>;
fn getopt(&self, optstring: &str) -> Option<Result<Opt, OptError>>;
}
impl GetOpt for Vec<String> {
fn getopt(
argv: Vec<String>,
optstring: &str
) -> Option<Result<Opt, OptError>> {
unsafe {
let args = argv
fn getopt(&self, optstring: &str) -> Option<Result<Opt, OptError>> {
let argv = self
.iter()
.cloned()
.map(CString::new)
@ -51,11 +44,11 @@ impl GetOpt for Vec<String> {
.map(|x| x.as_ptr() as c_char)
.collect::<Vec<c_char>>()
.as_ptr() as *const *mut c_char;
let len = argv.len() as c_int;
let opts = CString::new(optstring).unwrap().into_raw();
let len = self.len() as c_int;
let a = match getopt(len, args, opts) {
unsafe {
let a = match getopt(len, argv, opts) {
/* From getopt(3p):
*
* The getopt() function shall return the next option character