getopt.rs(3): safe api around optind reading and setting

This commit is contained in:
Emma Tebibyte 2024-06-05 11:54:55 -06:00
parent 05cde15105
commit e862b7fec6
Signed by untrusted user: emma
GPG Key ID: 06FA419A1698C270

View File

@ -34,10 +34,16 @@ extern "C" {
pub struct Opt {
pub arg: Option<String>,
pub ind: *mut i32,
ind: *mut i32,
pub opt: String,
}
impl Opt {
pub fn index(&self) -> usize { unsafe { *self.ind as usize } }
pub fn set_index(&self, ind: i32) { unsafe { *self.ind = ind; } }
}
pub enum OptError {
MissingArg(String),
UnknownOpt(String),
@ -121,9 +127,8 @@ impl GetOpt for Vec<String> {
* characters, it is unspecified how getopt()
* determines which options have already been processed.
*
* This API is unsafe and can be utilized by
* dereferencing the ind field before reading to and
* writing from the optind memory. */
* This API is can be utilized with the index() and
* set_index() methods implemented for Opt. */
ind: std::ptr::addr_of_mut!(optind),
opt: opt.to_string(), /* option itself */
}))