forked from bonsai/harakit
getopt.rs(3): refactor to remove as much as possible from unsafe
This commit is contained in:
parent
0164b681c0
commit
88b0d55440
@ -31,31 +31,24 @@ pub enum OptError {
|
|||||||
|
|
||||||
// Function signature
|
// Function signature
|
||||||
pub trait GetOpt {
|
pub trait GetOpt {
|
||||||
fn getopt(
|
fn getopt(&self, optstring: &str) -> Option<Result<Opt, OptError>>;
|
||||||
argv: Vec<String>,
|
|
||||||
optstring: &str
|
|
||||||
) -> Option<Result<Opt, OptError>>;
|
|
||||||
}
|
}
|
||||||
|
|
||||||
impl GetOpt for Vec<String> {
|
impl GetOpt for Vec<String> {
|
||||||
fn getopt(
|
fn getopt(&self, optstring: &str) -> Option<Result<Opt, OptError>> {
|
||||||
argv: Vec<String>,
|
let argv = self
|
||||||
optstring: &str
|
.iter()
|
||||||
) -> Option<Result<Opt, OptError>> {
|
.cloned()
|
||||||
|
.map(CString::new)
|
||||||
|
.map(Result::unwrap)
|
||||||
|
.map(|x| x.as_ptr() as c_char)
|
||||||
|
.collect::<Vec<c_char>>()
|
||||||
|
.as_ptr() as *const *mut c_char;
|
||||||
|
let opts = CString::new(optstring).unwrap().into_raw();
|
||||||
|
let len = self.len() as c_int;
|
||||||
|
|
||||||
unsafe {
|
unsafe {
|
||||||
let args = argv
|
let a = match getopt(len, argv, opts) {
|
||||||
.iter()
|
|
||||||
.cloned()
|
|
||||||
.map(CString::new)
|
|
||||||
.map(Result::unwrap)
|
|
||||||
.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 a = match getopt(len, args, opts) {
|
|
||||||
/* From getopt(3p):
|
/* From getopt(3p):
|
||||||
*
|
*
|
||||||
* The getopt() function shall return the next option character
|
* The getopt() function shall return the next option character
|
||||||
|
Loading…
Reference in New Issue
Block a user