Skip to content

Commit 2418655

Browse files
committed
Deref for ArgIntoComplex
1 parent 84e2b52 commit 2418655

File tree

2 files changed

+30
-30
lines changed

2 files changed

+30
-30
lines changed

stdlib/src/cmath.rs

Lines changed: 24 additions & 24 deletions
Original file line numberDiff line numberDiff line change
@@ -24,15 +24,15 @@ 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.
@@ -44,53 +44,53 @@ mod cmath {
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,8 +194,8 @@ 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();
197+
let a = *args.a;
198+
let b = *args.b;
199199
let rel_tol = args.rel_tol.map_or(1e-09, Into::into);
200200
let abs_tol = args.abs_tol.map_or(0.0, Into::into);
201201

vm/src/function/number.rs

Lines changed: 6 additions & 6 deletions
Original file line numberDiff line numberDiff line change
@@ -17,6 +17,12 @@ pub struct ArgIntoComplex {
1717
value: Complex64,
1818
}
1919

20+
impl From<ArgIntoComplex> for Complex64 {
21+
fn from(arg: ArgIntoComplex) -> Self {
22+
arg.value
23+
}
24+
}
25+
2026
impl Deref for ArgIntoComplex {
2127
type Target = Complex64;
2228

@@ -25,12 +31,6 @@ impl Deref for ArgIntoComplex {
2531
}
2632
}
2733

28-
impl ArgIntoComplex {
29-
pub fn to_complex(self) -> Complex64 {
30-
self.value
31-
}
32-
}
33-
3434
impl TryFromObject for ArgIntoComplex {
3535
// Equivalent to PyComplex_AsCComplex
3636
fn try_from_object(vm: &VirtualMachine, obj: PyObjectRef) -> PyResult<Self> {

0 commit comments

Comments
 (0)