Skip to content

Commit a3fab06

Browse files
authored
Merge pull request RustPython#3666 from youknowone/deref-arginto
Use option-like operator for OptionArg & Deref for ArgInto*
2 parents 1aa9dd7 + 2418655 commit a3fab06

File tree

16 files changed

+140
-149
lines changed

16 files changed

+140
-149
lines changed

stdlib/src/array.rs

Lines changed: 2 additions & 2 deletions
Original file line numberDiff line numberDiff line change
@@ -512,11 +512,11 @@ mod array {
512512
}
513513

514514
fn f32_try_into_from_object(vm: &VirtualMachine, obj: PyObjectRef) -> PyResult<f32> {
515-
ArgIntoFloat::try_from_object(vm, obj).map(|x| x.to_f64() as f32)
515+
ArgIntoFloat::try_from_object(vm, obj).map(|x| *x as f32)
516516
}
517517

518518
fn f64_try_into_from_object(vm: &VirtualMachine, obj: PyObjectRef) -> PyResult<f64> {
519-
ArgIntoFloat::try_from_object(vm, obj).map(|x| x.to_f64())
519+
ArgIntoFloat::try_from_object(vm, obj).map(Into::into)
520520
}
521521

522522
fn pyfloat_from_f32(value: f32) -> PyFloat {

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/cmath.rs

Lines changed: 27 additions & 27 deletions
Original file line numberDiff line numberDiff line change
@@ -24,73 +24,73 @@ mod cmath {
2424
/// Return argument, also known as the phase angle, of a complex.
2525
#[pyfunction]
2626
fn phase(z: ArgIntoComplex) -> f64 {
27-
z.to_complex().arg()
27+
z.arg()
2828
}
2929

3030
/// Convert a complex from rectangular coordinates to polar coordinates.
3131
///
3232
/// r is the distance from 0 and phi the phase angle.
3333
#[pyfunction]
3434
fn polar(x: ArgIntoComplex) -> (f64, f64) {
35-
x.to_complex().to_polar()
35+
x.to_polar()
3636
}
3737

3838
/// Convert from polar coordinates to rectangular coordinates.
3939
#[pyfunction]
4040
fn rect(r: ArgIntoFloat, phi: ArgIntoFloat) -> Complex64 {
41-
Complex64::from_polar(r.to_f64(), phi.to_f64())
41+
Complex64::from_polar(*r, *phi)
4242
}
4343

4444
/// Checks if the real or imaginary part of z is infinite.
4545
#[pyfunction]
4646
fn isinf(z: ArgIntoComplex) -> bool {
47-
let Complex64 { re, im } = z.to_complex();
47+
let Complex64 { re, im } = *z;
4848
re.is_infinite() || im.is_infinite()
4949
}
5050

5151
/// Return True if both the real and imaginary parts of z are finite, else False.
5252
#[pyfunction]
5353
fn isfinite(z: ArgIntoComplex) -> bool {
54-
z.to_complex().is_finite()
54+
z.is_finite()
5555
}
5656

5757
/// Checks if the real or imaginary part of z not a number (NaN)..
5858
#[pyfunction]
5959
fn isnan(z: ArgIntoComplex) -> bool {
60-
z.to_complex().is_nan()
60+
z.is_nan()
6161
}
6262

6363
/// Return the exponential value e**z.
6464
#[pyfunction]
6565
fn exp(z: ArgIntoComplex, vm: &VirtualMachine) -> PyResult<Complex64> {
66-
let z = z.to_complex();
66+
let z = *z;
6767
result_or_overflow(z, z.exp(), vm)
6868
}
6969
/// Return the square root of z.
7070
#[pyfunction]
7171
fn sqrt(z: ArgIntoComplex) -> Complex64 {
72-
z.to_complex().sqrt()
72+
z.sqrt()
7373
}
7474
/// Return the sine of z
7575
#[pyfunction]
7676
fn sin(z: ArgIntoComplex) -> Complex64 {
77-
z.to_complex().sin()
77+
z.sin()
7878
}
7979

8080
#[pyfunction]
8181
fn asin(z: ArgIntoComplex) -> Complex64 {
82-
z.to_complex().asin()
82+
z.asin()
8383
}
8484

8585
/// Return the cosine of z
8686
#[pyfunction]
8787
fn cos(z: ArgIntoComplex) -> Complex64 {
88-
z.to_complex().cos()
88+
z.cos()
8989
}
9090

9191
#[pyfunction]
9292
fn acos(z: ArgIntoComplex) -> Complex64 {
93-
z.to_complex().acos()
93+
z.acos()
9494
}
9595

9696
/// log(z[, base]) -> the logarithm of z to the given base.
@@ -103,65 +103,65 @@ mod cmath {
103103
// which returns NaN when base is negative.
104104
// log10(z) / log10(base) yields correct results but division
105105
// doesn't handle pos/neg zero nicely. (i.e log(1, 0.5))
106-
z.to_complex().log(
106+
z.log(
107107
base.into_option()
108-
.map(|base| base.to_complex().re)
108+
.map(|base| base.re)
109109
.unwrap_or(std::f64::consts::E),
110110
)
111111
}
112112

113113
/// Return the base-10 logarithm of z.
114114
#[pyfunction]
115115
fn log10(z: ArgIntoComplex) -> Complex64 {
116-
z.to_complex().log(10.0)
116+
z.log(10.0)
117117
}
118118

119119
/// Return the inverse hyperbolic cosine of z.
120120
#[pyfunction]
121121
fn acosh(z: ArgIntoComplex) -> Complex64 {
122-
z.to_complex().acosh()
122+
z.acosh()
123123
}
124124

125125
/// Return the inverse tangent of z.
126126
#[pyfunction]
127127
fn atan(z: ArgIntoComplex) -> Complex64 {
128-
z.to_complex().atan()
128+
z.atan()
129129
}
130130

131131
/// Return the inverse hyperbolic tangent of z.
132132
#[pyfunction]
133133
fn atanh(z: ArgIntoComplex) -> Complex64 {
134-
z.to_complex().atanh()
134+
z.atanh()
135135
}
136136

137137
/// Return the tangent of z.
138138
#[pyfunction]
139139
fn tan(z: ArgIntoComplex) -> Complex64 {
140-
z.to_complex().tan()
140+
z.tan()
141141
}
142142

143143
/// Return the hyperbolic tangent of z.
144144
#[pyfunction]
145145
fn tanh(z: ArgIntoComplex) -> Complex64 {
146-
z.to_complex().tanh()
146+
z.tanh()
147147
}
148148

149149
/// Return the hyperbolic sin of z.
150150
#[pyfunction]
151151
fn sinh(z: ArgIntoComplex) -> Complex64 {
152-
z.to_complex().sinh()
152+
z.sinh()
153153
}
154154

155155
/// Return the hyperbolic cosine of z.
156156
#[pyfunction]
157157
fn cosh(z: ArgIntoComplex) -> Complex64 {
158-
z.to_complex().cosh()
158+
z.cosh()
159159
}
160160

161161
/// Return the inverse hyperbolic sine of z.
162162
#[pyfunction]
163163
fn asinh(z: ArgIntoComplex) -> Complex64 {
164-
z.to_complex().asinh()
164+
z.asinh()
165165
}
166166

167167
#[derive(FromArgs)]
@@ -194,10 +194,10 @@ mod cmath {
194194
/// not close to anything, even itself. inf and -inf are only close to themselves.
195195
#[pyfunction]
196196
fn isclose(args: IsCloseArgs, vm: &VirtualMachine) -> PyResult<bool> {
197-
let a = args.a.to_complex();
198-
let b = args.b.to_complex();
199-
let rel_tol = args.rel_tol.map_or(1e-09, |value| value.to_f64());
200-
let abs_tol = args.abs_tol.map_or(0.0, |value| value.to_f64());
197+
let a = *args.a;
198+
let b = *args.b;
199+
let rel_tol = args.rel_tol.map_or(1e-09, Into::into);
200+
let abs_tol = args.abs_tol.map_or(0.0, Into::into);
201201

202202
if rel_tol < 0.0 || abs_tol < 0.0 {
203203
return Err(vm.new_value_error("tolerances must be non-negative".to_owned()));

0 commit comments

Comments
 (0)