Merge pull request #3040 from DimitrisJim/refactor_optionals

Clean up more instances of matching on Optional.
This commit is contained in:
Jeong YunWon
2021-09-12 06:23:04 +09:00
committed by GitHub
7 changed files with 16 additions and 37 deletions

View File

@@ -355,10 +355,7 @@ impl PyDict {
) -> PyResult {
match self.entries.pop(vm, &key)? {
Some(value) => Ok(value),
None => match default {
OptionalArg::Present(default) => Ok(default),
OptionalArg::Missing => Err(vm.new_key_error(key)),
},
None => default.ok_or_else(|| vm.new_key_error(key)),
}
}

View File

@@ -41,11 +41,9 @@ impl SlotConstructor for PyEnumerate {
type Args = EnumerateArgs;
fn py_new(cls: PyTypeRef, args: Self::Args, vm: &VirtualMachine) -> PyResult {
let counter = match args.start {
OptionalArg::Present(start) => start.as_bigint().clone(),
OptionalArg::Missing => BigInt::zero(),
};
let counter = args
.start
.map_or_else(BigInt::zero, |start| start.as_bigint().clone());
let iterator = iterator::get_iter(vm, args.iterable)?;
PyEnumerate {
counter: PyRwLock::new(counter),

View File

@@ -519,10 +519,7 @@ mod decl {
) -> PyResult {
iterator::call_next(vm, &iterator).or_else(|err| {
if err.isinstance(&vm.ctx.exceptions.stop_iteration) {
match default_value {
OptionalArg::Missing => Err(err),
OptionalArg::Present(value) => Ok(value),
}
default_value.ok_or(err)
} else {
Err(err)
}

View File

@@ -105,10 +105,7 @@ impl SlotConstructor for PyBool {
actual_type
)));
}
let val = match x {
OptionalArg::Present(val) => boolval(vm, val)?,
OptionalArg::Missing => false,
};
let val = x.map_or(Ok(false), |val| boolval(vm, val))?;
Ok(vm.ctx.new_bool(val))
}
}

View File

@@ -909,14 +909,13 @@ impl PyStr {
pad: fn(&str, usize, char, usize) -> String,
vm: &VirtualMachine,
) -> PyResult<String> {
let fillchar = match fillchar {
OptionalArg::Present(ref s) => s.value.chars().exactly_one().map_err(|_| {
let fillchar = fillchar.map_or(Ok(' '), |ref s| {
s.value.chars().exactly_one().map_err(|_| {
vm.new_type_error(
"The fill character must be exactly one character long".to_owned(),
)
}),
OptionalArg::Missing => Ok(' '),
}?;
})
})?;
Ok(if self.len() as isize >= width {
String::from(self.as_str())
} else {

View File

@@ -69,15 +69,8 @@ struct IsCloseArgs {
fn math_isclose(args: IsCloseArgs, vm: &VirtualMachine) -> PyResult<bool> {
let a = args.a.to_f64();
let b = args.b.to_f64();
let rel_tol = match args.rel_tol {
OptionalArg::Missing => 1e-09,
OptionalArg::Present(ref value) => value.to_f64(),
};
let abs_tol = match args.abs_tol {
OptionalArg::Missing => 0.0,
OptionalArg::Present(ref value) => value.to_f64(),
};
let rel_tol = args.rel_tol.map_or(1e-09, |value| value.to_f64());
let abs_tol = args.abs_tol.map_or(0.0, |value| value.to_f64());
if rel_tol < 0.0 || abs_tol < 0.0 {
return Err(vm.new_value_error("tolerances must be non-negative".to_owned()));

View File

@@ -654,12 +654,10 @@ mod _sre {
group: OptionalArg<PyObjectRef>,
vm: &VirtualMachine,
) -> PyResult<(isize, isize)> {
let index = match group {
OptionalArg::Present(group) => self
.get_index(group, vm)
.ok_or_else(|| vm.new_index_error("no such group".to_owned()))?,
OptionalArg::Missing => 0,
};
let index = group.map_or(Ok(0), |group| {
self.get_index(group, vm)
.ok_or_else(|| vm.new_index_error("no such group".to_owned()))
})?;
Ok(self.regs[index])
}