Skip to content

Commit 37daf18

Browse files
committed
Use option-like operator for OptionalArg
1 parent 1aa9dd7 commit 37daf18

File tree

7 files changed

+11
-37
lines changed

7 files changed

+11
-37
lines changed

stdlib/src/bisect.rs

Lines changed: 3 additions & 4 deletions
Original file line numberDiff line numberDiff line change
@@ -22,10 +22,9 @@ mod _bisect {
2222
arg: OptionalArg<PyObjectRef>,
2323
vm: &VirtualMachine,
2424
) -> PyResult<Option<isize>> {
25-
Ok(match arg {
26-
OptionalArg::Present(v) => Some(vm.to_index(&v)?.try_to_primitive(vm)?),
27-
OptionalArg::Missing => None,
28-
})
25+
arg.into_option()
26+
.map(|v| vm.to_index(&v)?.try_to_primitive(vm))
27+
.transpose()
2928
}
3029

3130
// Handles defaults for lo, hi.

stdlib/src/re.rs

Lines changed: 1 addition & 4 deletions
Original file line numberDiff line numberDiff line change
@@ -296,10 +296,7 @@ mod re {
296296
}
297297

298298
fn extract_flags(flags: OptionalArg<usize>) -> PyRegexFlags {
299-
match flags {
300-
OptionalArg::Present(flags) => PyRegexFlags::from_int(flags),
301-
OptionalArg::Missing => Default::default(),
302-
}
299+
flags.map_or_else(Default::default, PyRegexFlags::from_int)
303300
}
304301

305302
#[pyfunction]

stdlib/src/unicodedata.rs

Lines changed: 1 addition & 6 deletions
Original file line numberDiff line numberDiff line change
@@ -101,12 +101,7 @@ mod unicodedata {
101101
}
102102
}
103103
}
104-
match default {
105-
OptionalArg::Present(obj) => Ok(obj),
106-
OptionalArg::Missing => {
107-
Err(vm.new_value_error("character name not found!".to_owned()))
108-
}
109-
}
104+
default.ok_or_else(|| vm.new_value_error("character name not found!".to_owned()))
110105
}
111106

112107
#[pymethod]

stdlib/src/zlib.rs

Lines changed: 3 additions & 8 deletions
Original file line numberDiff line numberDiff line change
@@ -366,15 +366,10 @@ mod zlib {
366366
#[pymethod]
367367
fn flush(&self, length: OptionalArg<isize>, vm: &VirtualMachine) -> PyResult<Vec<u8>> {
368368
let length = match length {
369-
OptionalArg::Present(l) => {
370-
if l <= 0 {
371-
return Err(
372-
vm.new_value_error("length must be greater than zero".to_owned())
373-
);
374-
} else {
375-
l as usize
376-
}
369+
OptionalArg::Present(l) if l <= 0 => {
370+
return Err(vm.new_value_error("length must be greater than zero".to_owned()));
377371
}
372+
OptionalArg::Present(l) => l as usize,
378373
OptionalArg::Missing => DEF_BUF_SIZE,
379374
};
380375

vm/src/builtins/int.rs

Lines changed: 1 addition & 6 deletions
Original file line numberDiff line numberDiff line change
@@ -607,12 +607,7 @@ impl PyInt {
607607
args: IntFromByteArgs,
608608
vm: &VirtualMachine,
609609
) -> PyResult<PyRef<Self>> {
610-
let signed = if let OptionalArg::Present(signed) = args.signed {
611-
signed.to_bool()
612-
} else {
613-
false
614-
};
615-
610+
let signed = args.signed.map_or(false, |s| s.to_bool());
616611
let value = match (args.byteorder.as_str(), signed) {
617612
("big", true) => BigInt::from_signed_bytes_be(&args.bytes.elements),
618613
("big", false) => BigInt::from_bytes_be(Sign::Plus, &args.bytes.elements),

vm/src/function/argument.rs

Lines changed: 1 addition & 4 deletions
Original file line numberDiff line numberDiff line change
@@ -469,10 +469,7 @@ pub type OptionalOption<T = PyObjectRef> = OptionalArg<Option<T>>;
469469
impl<T> OptionalOption<T> {
470470
#[inline]
471471
pub fn flatten(self) -> Option<T> {
472-
match self {
473-
OptionalArg::Present(Some(value)) => Some(value),
474-
_ => None,
475-
}
472+
self.into_option().flatten()
476473
}
477474
}
478475

vm/src/stdlib/os.rs

Lines changed: 1 addition & 5 deletions
Original file line numberDiff line numberDiff line change
@@ -933,11 +933,7 @@ pub(super) mod _os {
933933

934934
#[pyfunction]
935935
fn scandir(path: OptionalArg<PyPathLike>, vm: &VirtualMachine) -> PyResult {
936-
let path = match path {
937-
OptionalArg::Present(path) => path,
938-
OptionalArg::Missing => PyPathLike::new_str("."),
939-
};
940-
936+
let path = path.unwrap_or_else(|| PyPathLike::new_str("."));
941937
let entries = fs::read_dir(path.path).map_err(|err| err.to_pyexception(vm))?;
942938
Ok(ScandirIterator {
943939
entries: PyRwLock::new(entries),

0 commit comments

Comments
 (0)