Skip to content

Commit 84e2b52

Browse files
committed
Deref for ArgIntoFloat
1 parent 9f17a50 commit 84e2b52

File tree

6 files changed

+70
-65
lines changed

6 files changed

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

Lines changed: 3 additions & 3 deletions
Original file line numberDiff line numberDiff line change
@@ -38,7 +38,7 @@ mod cmath {
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.
@@ -196,8 +196,8 @@ mod cmath {
196196
fn isclose(args: IsCloseArgs, vm: &VirtualMachine) -> PyResult<bool> {
197197
let a = args.a.to_complex();
198198
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());
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()));

stdlib/src/math.rs

Lines changed: 39 additions & 41 deletions
Original file line numberDiff line numberDiff line change
@@ -21,7 +21,7 @@ mod math {
2121
// Helper macro:
2222
macro_rules! call_math_func {
2323
( $fun:ident, $name:ident, $vm:ident ) => {{
24-
let value = $name.to_f64();
24+
let value = *$name;
2525
let result = value.$fun();
2626
result_or_overflow(value, result, $vm)
2727
}};
@@ -46,17 +46,17 @@ mod math {
4646

4747
#[pyfunction]
4848
fn isfinite(x: ArgIntoFloat) -> bool {
49-
x.to_f64().is_finite()
49+
x.is_finite()
5050
}
5151

5252
#[pyfunction]
5353
fn isinf(x: ArgIntoFloat) -> bool {
54-
x.to_f64().is_infinite()
54+
x.is_infinite()
5555
}
5656

5757
#[pyfunction]
5858
fn isnan(x: ArgIntoFloat) -> bool {
59-
x.to_f64().is_nan()
59+
x.is_nan()
6060
}
6161

6262
#[derive(FromArgs)]
@@ -73,10 +73,10 @@ mod math {
7373

7474
#[pyfunction]
7575
fn isclose(args: IsCloseArgs, vm: &VirtualMachine) -> PyResult<bool> {
76-
let a = args.a.to_f64();
77-
let b = args.b.to_f64();
78-
let rel_tol = args.rel_tol.map_or(1e-09, |value| value.to_f64());
79-
let abs_tol = args.abs_tol.map_or(0.0, |value| value.to_f64());
76+
let a = *args.a;
77+
let b = *args.b;
78+
let rel_tol = args.rel_tol.map_or(1e-09, |value| value.into());
79+
let abs_tol = args.abs_tol.map_or(0.0, |value| value.into());
8080

8181
if rel_tol < 0.0 || abs_tol < 0.0 {
8282
return Err(vm.new_value_error("tolerances must be non-negative".to_owned()));
@@ -107,12 +107,10 @@ mod math {
107107

108108
#[pyfunction]
109109
fn copysign(x: ArgIntoFloat, y: ArgIntoFloat) -> f64 {
110-
let a = x.to_f64();
111-
let b = y.to_f64();
112-
if a.is_nan() || b.is_nan() {
113-
a
110+
if x.is_nan() || y.is_nan() {
111+
x.into()
114112
} else {
115-
a.copysign(b)
113+
x.copysign(*y)
116114
}
117115
}
118116

@@ -129,7 +127,7 @@ mod math {
129127

130128
#[pyfunction]
131129
fn log(x: ArgIntoFloat, base: OptionalArg<ArgIntoFloat>, vm: &VirtualMachine) -> PyResult<f64> {
132-
let x = x.to_f64();
130+
let x = *x;
133131
base.map_or_else(
134132
|| {
135133
if x.is_nan() || x > 0.0_f64 {
@@ -138,13 +136,13 @@ mod math {
138136
Err(vm.new_value_error("math domain error".to_owned()))
139137
}
140138
},
141-
|base| Ok(x.log(base.to_f64())),
139+
|base| Ok(x.log(*base)),
142140
)
143141
}
144142

145143
#[pyfunction]
146144
fn log1p(x: ArgIntoFloat, vm: &VirtualMachine) -> PyResult<f64> {
147-
let x = x.to_f64();
145+
let x = *x;
148146
if x.is_nan() || x > -1.0_f64 {
149147
Ok((x + 1.0_f64).ln())
150148
} else {
@@ -154,7 +152,7 @@ mod math {
154152

155153
#[pyfunction]
156154
fn log2(x: ArgIntoFloat, vm: &VirtualMachine) -> PyResult<f64> {
157-
let x = x.to_f64();
155+
let x = *x;
158156
if x.is_nan() || x > 0.0_f64 {
159157
Ok(x.log2())
160158
} else {
@@ -164,7 +162,7 @@ mod math {
164162

165163
#[pyfunction]
166164
fn log10(x: ArgIntoFloat, vm: &VirtualMachine) -> PyResult<f64> {
167-
let x = x.to_f64();
165+
let x = *x;
168166
if x.is_nan() || x > 0.0_f64 {
169167
Ok(x.log10())
170168
} else {
@@ -174,8 +172,8 @@ mod math {
174172

175173
#[pyfunction]
176174
fn pow(x: ArgIntoFloat, y: ArgIntoFloat, vm: &VirtualMachine) -> PyResult<f64> {
177-
let x = x.to_f64();
178-
let y = y.to_f64();
175+
let x = *x;
176+
let y = *y;
179177

180178
if x < 0.0 && x.is_finite() && y.fract() != 0.0 && y.is_finite() {
181179
return Err(vm.new_value_error("math domain error".to_owned()));
@@ -192,7 +190,7 @@ mod math {
192190

193191
#[pyfunction]
194192
fn sqrt(value: ArgIntoFloat, vm: &VirtualMachine) -> PyResult<f64> {
195-
let value = value.to_f64();
193+
let value = *value;
196194
if value.is_sign_negative() {
197195
return Err(vm.new_value_error("math domain error".to_owned()));
198196
}
@@ -213,7 +211,7 @@ mod math {
213211
// Trigonometric functions:
214212
#[pyfunction]
215213
fn acos(x: ArgIntoFloat, vm: &VirtualMachine) -> PyResult<f64> {
216-
let x = x.to_f64();
214+
let x = *x;
217215
if x.is_nan() || (-1.0_f64..=1.0_f64).contains(&x) {
218216
Ok(x.acos())
219217
} else {
@@ -223,7 +221,7 @@ mod math {
223221

224222
#[pyfunction]
225223
fn asin(x: ArgIntoFloat, vm: &VirtualMachine) -> PyResult<f64> {
226-
let x = x.to_f64();
224+
let x = *x;
227225
if x.is_nan() || (-1.0_f64..=1.0_f64).contains(&x) {
228226
Ok(x.asin())
229227
} else {
@@ -238,7 +236,7 @@ mod math {
238236

239237
#[pyfunction]
240238
fn atan2(y: ArgIntoFloat, x: ArgIntoFloat) -> f64 {
241-
y.to_f64().atan2(x.to_f64())
239+
y.atan2(*x)
242240
}
243241

244242
#[pyfunction]
@@ -337,19 +335,19 @@ mod math {
337335

338336
#[pyfunction]
339337
fn degrees(x: ArgIntoFloat) -> f64 {
340-
x.to_f64() * (180.0 / std::f64::consts::PI)
338+
*x * (180.0 / std::f64::consts::PI)
341339
}
342340

343341
#[pyfunction]
344342
fn radians(x: ArgIntoFloat) -> f64 {
345-
x.to_f64() * (std::f64::consts::PI / 180.0)
343+
*x * (std::f64::consts::PI / 180.0)
346344
}
347345

348346
// Hyperbolic functions:
349347

350348
#[pyfunction]
351349
fn acosh(x: ArgIntoFloat, vm: &VirtualMachine) -> PyResult<f64> {
352-
let x = x.to_f64();
350+
let x = *x;
353351
if x.is_sign_negative() || x.is_zero() {
354352
Err(vm.new_value_error("math domain error".to_owned()))
355353
} else {
@@ -364,7 +362,7 @@ mod math {
364362

365363
#[pyfunction]
366364
fn atanh(x: ArgIntoFloat, vm: &VirtualMachine) -> PyResult<f64> {
367-
let x = x.to_f64();
365+
let x = *x;
368366
if x >= 1.0_f64 || x <= -1.0_f64 {
369367
Err(vm.new_value_error("math domain error".to_owned()))
370368
} else {
@@ -390,7 +388,7 @@ mod math {
390388
// Special functions:
391389
#[pyfunction]
392390
fn erf(x: ArgIntoFloat) -> f64 {
393-
let x = x.to_f64();
391+
let x = *x;
394392
if x.is_nan() {
395393
x
396394
} else {
@@ -400,7 +398,7 @@ mod math {
400398

401399
#[pyfunction]
402400
fn erfc(x: ArgIntoFloat) -> f64 {
403-
let x = x.to_f64();
401+
let x = *x;
404402
if x.is_nan() {
405403
x
406404
} else {
@@ -410,7 +408,7 @@ mod math {
410408

411409
#[pyfunction]
412410
fn gamma(x: ArgIntoFloat) -> f64 {
413-
let x = x.to_f64();
411+
let x = *x;
414412
if x.is_finite() {
415413
puruspe::gamma(x)
416414
} else if x.is_nan() || x.is_sign_positive() {
@@ -422,7 +420,7 @@ mod math {
422420

423421
#[pyfunction]
424422
fn lgamma(x: ArgIntoFloat) -> f64 {
425-
let x = x.to_f64();
423+
let x = *x;
426424
if x.is_finite() {
427425
puruspe::ln_gamma(x)
428426
} else if x.is_nan() {
@@ -474,7 +472,7 @@ mod math {
474472

475473
#[pyfunction]
476474
fn frexp(x: ArgIntoFloat) -> (f64, i32) {
477-
let value = x.to_f64();
475+
let value = *x;
478476
if value.is_finite() {
479477
let (m, exp) = float_ops::ufrexp(value);
480478
(m * value.signum(), exp)
@@ -541,7 +539,7 @@ mod math {
541539
let mut inf_sum = 0.0;
542540

543541
for obj in seq.iter(vm)? {
544-
let mut x = obj?.to_f64();
542+
let mut x = *obj?;
545543

546544
let xsave = x;
547545
let mut j = 0;
@@ -726,7 +724,7 @@ mod math {
726724

727725
#[pyfunction]
728726
fn modf(x: ArgIntoFloat) -> (f64, f64) {
729-
let x = x.to_f64();
727+
let x = *x;
730728
if !x.is_finite() {
731729
if x.is_infinite() {
732730
return (0.0_f64.copysign(x), x);
@@ -740,12 +738,12 @@ mod math {
740738

741739
#[pyfunction]
742740
fn nextafter(x: ArgIntoFloat, y: ArgIntoFloat) -> f64 {
743-
float_ops::nextafter(x.to_f64(), y.to_f64())
741+
float_ops::nextafter(*x, *y)
744742
}
745743

746744
#[pyfunction]
747745
fn ulp(x: ArgIntoFloat) -> f64 {
748-
float_ops::ulp(x.to_f64())
746+
float_ops::ulp(*x)
749747
}
750748

751749
fn fmod(x: f64, y: f64) -> f64 {
@@ -758,8 +756,8 @@ mod math {
758756

759757
#[pyfunction(name = "fmod")]
760758
fn py_fmod(x: ArgIntoFloat, y: ArgIntoFloat, vm: &VirtualMachine) -> PyResult<f64> {
761-
let x = x.to_f64();
762-
let y = y.to_f64();
759+
let x = *x;
760+
let y = *y;
763761

764762
let r = fmod(x, y);
765763

@@ -772,8 +770,8 @@ mod math {
772770

773771
#[pyfunction]
774772
fn remainder(x: ArgIntoFloat, y: ArgIntoFloat, vm: &VirtualMachine) -> PyResult<f64> {
775-
let x = x.to_f64();
776-
let y = y.to_f64();
773+
let x = *x;
774+
let y = *y;
777775

778776
if x.is_finite() && y.is_finite() {
779777
if y == 0.0 {

stdlib/src/statistics.rs

Lines changed: 1 addition & 1 deletion
Original file line numberDiff line numberDiff line change
@@ -126,7 +126,7 @@ mod _statistics {
126126
sigma: ArgIntoFloat,
127127
vm: &VirtualMachine,
128128
) -> PyResult<f64> {
129-
normal_dist_inv_cdf(p.to_f64(), mu.to_f64(), sigma.to_f64())
129+
normal_dist_inv_cdf(*p, *mu, *sigma)
130130
.ok_or_else(|| vm.new_value_error("inv_cdf undefined for these parameters".to_owned()))
131131
}
132132
}

vm/src/cformat.rs

Lines changed: 12 additions & 14 deletions
Original file line numberDiff line numberDiff line change
@@ -447,18 +447,16 @@ impl CFormatSpec {
447447
},
448448
CFormatType::Float(_) => {
449449
let type_name = obj.class().name().to_string();
450-
let value = ArgIntoFloat::try_from_object(vm, obj)
451-
.map_err(|e| {
452-
if e.fast_isinstance(&vm.ctx.exceptions.type_error) {
453-
// formatfloat in bytesobject.c generates its own specific exception
454-
// text in this case, mirror it here.
455-
vm.new_type_error(format!("float argument required, not {}", type_name))
456-
} else {
457-
e
458-
}
459-
})?
460-
.to_f64();
461-
Ok(self.format_float(value).into_bytes())
450+
let value = ArgIntoFloat::try_from_object(vm, obj).map_err(|e| {
451+
if e.fast_isinstance(&vm.ctx.exceptions.type_error) {
452+
// formatfloat in bytesobject.c generates its own specific exception
453+
// text in this case, mirror it here.
454+
vm.new_type_error(format!("float argument required, not {}", type_name))
455+
} else {
456+
e
457+
}
458+
})?;
459+
Ok(self.format_float(value.into()).into_bytes())
462460
}
463461
CFormatType::Character => {
464462
if let Some(i) = obj.payload::<PyInt>() {
@@ -542,8 +540,8 @@ impl CFormatSpec {
542540
}
543541
},
544542
CFormatType::Float(_) => {
545-
let value = ArgIntoFloat::try_from_object(vm, obj)?.to_f64();
546-
Ok(self.format_float(value))
543+
let value = ArgIntoFloat::try_from_object(vm, obj)?;
544+
Ok(self.format_float(value.into()))
547545
}
548546
CFormatType::Character => {
549547
if let Some(i) = obj.payload::<PyInt>() {

vm/src/function/number.rs

Lines changed: 13 additions & 4 deletions
Original file line numberDiff line numberDiff line change
@@ -57,10 +57,6 @@ pub struct ArgIntoFloat {
5757
}
5858

5959
impl ArgIntoFloat {
60-
pub fn to_f64(self) -> f64 {
61-
self.value
62-
}
63-
6460
pub fn vec_into_f64(v: Vec<Self>) -> Vec<f64> {
6561
// TODO: Vec::into_raw_parts once stabilized
6662
let mut v = std::mem::ManuallyDrop::new(v);
@@ -70,6 +66,19 @@ impl ArgIntoFloat {
7066
}
7167
}
7268

69+
impl From<ArgIntoFloat> for f64 {
70+
fn from(arg: ArgIntoFloat) -> Self {
71+
arg.value
72+
}
73+
}
74+
75+
impl Deref for ArgIntoFloat {
76+
type Target = f64;
77+
fn deref(&self) -> &Self::Target {
78+
&self.value
79+
}
80+
}
81+
7382
impl TryFromObject for ArgIntoFloat {
7483
// Equivalent to PyFloat_AsDouble.
7584
fn try_from_object(vm: &VirtualMachine, obj: PyObjectRef) -> PyResult<Self> {

0 commit comments

Comments
 (0)