Skip to content

Commit 8851a24

Browse files
committed
Simplify Representable impls
1 parent 23cfdff commit 8851a24

Some content is hidden

Large Commits have some content hidden by default. Use the searchbox below for content that may be hidden.

47 files changed

+289
-261
lines changed

vm/src/builtins/asyncgenerator.rs

Lines changed: 5 additions & 5 deletions
Original file line numberDiff line numberDiff line change
@@ -1,4 +1,4 @@
1-
use super::{PyCode, PyGenericAlias, PyStr, PyStrRef, PyType, PyTypeRef};
1+
use super::{PyCode, PyGenericAlias, PyStrRef, PyType, PyTypeRef};
22
use crate::{
33
builtins::PyBaseExceptionRef,
44
class::PyClassImpl,
@@ -127,8 +127,8 @@ impl PyAsyncGen {
127127

128128
impl Representable for PyAsyncGen {
129129
#[inline]
130-
fn repr(zelf: &crate::Py<Self>, vm: &VirtualMachine) -> PyResult<PyStrRef> {
131-
Ok(PyStr::from(zelf.inner.repr(zelf.as_object(), zelf.get_id(), vm)).into_ref(vm))
130+
fn repr_str(zelf: &Py<Self>, vm: &VirtualMachine) -> PyResult<String> {
131+
Ok(zelf.inner.repr(zelf.as_object(), zelf.get_id(), vm))
132132
}
133133
}
134134

@@ -267,7 +267,7 @@ impl PyAsyncGenASend {
267267

268268
impl IterNextIterable for PyAsyncGenASend {}
269269
impl IterNext for PyAsyncGenASend {
270-
fn next(zelf: &crate::Py<Self>, vm: &VirtualMachine) -> PyResult<PyIterReturn> {
270+
fn next(zelf: &Py<Self>, vm: &VirtualMachine) -> PyResult<PyIterReturn> {
271271
PyIterReturn::from_pyresult(zelf.send(vm.ctx.none(), vm), vm)
272272
}
273273
}
@@ -413,7 +413,7 @@ impl PyAsyncGenAThrow {
413413

414414
impl IterNextIterable for PyAsyncGenAThrow {}
415415
impl IterNext for PyAsyncGenAThrow {
416-
fn next(zelf: &crate::Py<Self>, vm: &VirtualMachine) -> PyResult<PyIterReturn> {
416+
fn next(zelf: &Py<Self>, vm: &VirtualMachine) -> PyResult<PyIterReturn> {
417417
PyIterReturn::from_pyresult(zelf.send(vm.ctx.none(), vm), vm)
418418
}
419419
}

vm/src/builtins/bool.rs

Lines changed: 8 additions & 16 deletions
Original file line numberDiff line numberDiff line change
@@ -182,25 +182,17 @@ impl AsNumber for PyBool {
182182
impl Representable for PyBool {
183183
#[inline]
184184
fn slot_repr(zelf: &PyObject, vm: &VirtualMachine) -> PyResult<PyStrRef> {
185-
if get_value(zelf) {
186-
Ok(vm.ctx.names.True.to_owned())
185+
let name = if get_value(zelf.as_object()) {
186+
vm.ctx.names.True
187187
} else {
188-
Ok(vm.ctx.names.False.to_owned())
189-
}
190-
}
191-
192-
#[inline]
193-
fn __repr__(zelf: PyObjectRef, vm: &VirtualMachine) -> PyResult<PyStrRef> {
194-
Self::slot_repr(&zelf, vm)
188+
vm.ctx.names.False
189+
};
190+
Ok(name.to_owned())
195191
}
196192

197-
#[inline]
198-
fn repr(zelf: &crate::Py<Self>, vm: &VirtualMachine) -> PyResult<PyStrRef> {
199-
if get_value(zelf.as_object()) {
200-
Ok(vm.ctx.names.True.to_owned())
201-
} else {
202-
Ok(vm.ctx.names.False.to_owned())
203-
}
193+
#[cold]
194+
fn repr_str(_zelf: &Py<Self>, _vm: &VirtualMachine) -> PyResult<String> {
195+
unreachable!("use slot_repr instead")
204196
}
205197
}
206198

vm/src/builtins/builtin_func.rs

Lines changed: 6 additions & 7 deletions
Original file line numberDiff line numberDiff line change
@@ -109,7 +109,7 @@ impl PyBuiltinFunction {
109109
impl Callable for PyBuiltinFunction {
110110
type Args = FuncArgs;
111111
#[inline]
112-
fn call(zelf: &crate::Py<Self>, args: FuncArgs, vm: &VirtualMachine) -> PyResult {
112+
fn call(zelf: &Py<Self>, args: FuncArgs, vm: &VirtualMachine) -> PyResult {
113113
(zelf.value.func)(vm, args)
114114
}
115115
}
@@ -156,8 +156,8 @@ impl PyBuiltinFunction {
156156

157157
impl Representable for PyBuiltinFunction {
158158
#[inline]
159-
fn repr(zelf: &crate::Py<Self>, vm: &VirtualMachine) -> PyResult<PyStrRef> {
160-
Ok(PyStr::from(format!("<built-in function {}>", zelf.value.name)).into_ref(vm))
159+
fn repr_str(zelf: &Py<Self>, _vm: &VirtualMachine) -> PyResult<String> {
160+
Ok(format!("<built-in function {}>", zelf.value.name))
161161
}
162162
}
163163

@@ -210,7 +210,7 @@ impl GetDescriptor for PyBuiltinMethod {
210210
impl Callable for PyBuiltinMethod {
211211
type Args = FuncArgs;
212212
#[inline]
213-
fn call(zelf: &crate::Py<Self>, args: FuncArgs, vm: &VirtualMachine) -> PyResult {
213+
fn call(zelf: &Py<Self>, args: FuncArgs, vm: &VirtualMachine) -> PyResult {
214214
(zelf.value.func)(vm, args)
215215
}
216216
}
@@ -266,13 +266,12 @@ impl PyBuiltinMethod {
266266

267267
impl Representable for PyBuiltinMethod {
268268
#[inline]
269-
fn repr(zelf: &crate::Py<Self>, vm: &VirtualMachine) -> PyResult<PyStrRef> {
270-
Ok(PyStr::from(format!(
269+
fn repr_str(zelf: &Py<Self>, _vm: &VirtualMachine) -> PyResult<String> {
270+
Ok(format!(
271271
"<method '{}' of '{}' objects>",
272272
&zelf.value.name,
273273
zelf.class.name()
274274
))
275-
.into_ref(vm))
276275
}
277276
}
278277

vm/src/builtins/bytearray.rs

Lines changed: 7 additions & 9 deletions
Original file line numberDiff line numberDiff line change
@@ -1,7 +1,7 @@
11
//! Implementation of the python bytearray object.
22
use super::{
3-
PositionIterInternal, PyBytes, PyBytesRef, PyDictRef, PyIntRef, PyStr, PyStrRef, PyTuple,
4-
PyTupleRef, PyType, PyTypeRef,
3+
PositionIterInternal, PyBytes, PyBytesRef, PyDictRef, PyIntRef, PyStrRef, PyTuple, PyTupleRef,
4+
PyType, PyTypeRef,
55
};
66
use crate::{
77
anystr::{self, AnyStr},
@@ -294,7 +294,7 @@ impl PyByteArray {
294294
}
295295
}
296296

297-
fn irepeat(zelf: &crate::Py<Self>, n: isize, vm: &VirtualMachine) -> PyResult<()> {
297+
fn irepeat(zelf: &Py<Self>, n: isize, vm: &VirtualMachine) -> PyResult<()> {
298298
if n == 1 {
299299
return Ok(());
300300
}
@@ -733,7 +733,7 @@ impl Initializer for PyByteArray {
733733

734734
impl Comparable for PyByteArray {
735735
fn cmp(
736-
zelf: &crate::Py<Self>,
736+
zelf: &Py<Self>,
737737
other: &PyObject,
738738
op: PyComparisonOp,
739739
vm: &VirtualMachine,
@@ -882,12 +882,10 @@ impl Iterable for PyByteArray {
882882

883883
impl Representable for PyByteArray {
884884
#[inline]
885-
fn repr(zelf: &crate::Py<Self>, vm: &VirtualMachine) -> PyResult<PyStrRef> {
885+
fn repr_str(zelf: &Py<Self>, vm: &VirtualMachine) -> PyResult<String> {
886886
let class = zelf.class();
887887
let class_name = class.name();
888-
zelf.inner()
889-
.repr(Some(&class_name), vm)
890-
.map(|s| PyStr::from(s).into_ref(vm))
888+
zelf.inner().repr(Some(&class_name), vm)
891889
}
892890
}
893891

@@ -932,7 +930,7 @@ impl Unconstructible for PyByteArrayIterator {}
932930

933931
impl IterNextIterable for PyByteArrayIterator {}
934932
impl IterNext for PyByteArrayIterator {
935-
fn next(zelf: &crate::Py<Self>, vm: &VirtualMachine) -> PyResult<PyIterReturn> {
933+
fn next(zelf: &Py<Self>, vm: &VirtualMachine) -> PyResult<PyIterReturn> {
936934
zelf.internal.lock().next(|bytearray, pos| {
937935
let buf = bytearray.borrow_buf();
938936
Ok(PyIterReturn::from_result(

vm/src/builtins/bytes.rs

Lines changed: 6 additions & 9 deletions
Original file line numberDiff line numberDiff line change
@@ -1,6 +1,5 @@
11
use super::{
2-
PositionIterInternal, PyDictRef, PyIntRef, PyStr, PyStrRef, PyTuple, PyTupleRef, PyType,
3-
PyTypeRef,
2+
PositionIterInternal, PyDictRef, PyIntRef, PyStrRef, PyTuple, PyTupleRef, PyType, PyTypeRef,
43
};
54
use crate::{
65
anystr::{self, AnyStr},
@@ -647,14 +646,14 @@ impl AsNumber for PyBytes {
647646

648647
impl Hashable for PyBytes {
649648
#[inline]
650-
fn hash(zelf: &crate::Py<Self>, vm: &VirtualMachine) -> PyResult<PyHash> {
649+
fn hash(zelf: &Py<Self>, vm: &VirtualMachine) -> PyResult<PyHash> {
651650
Ok(zelf.inner.hash(vm))
652651
}
653652
}
654653

655654
impl Comparable for PyBytes {
656655
fn cmp(
657-
zelf: &crate::Py<Self>,
656+
zelf: &Py<Self>,
658657
other: &PyObject,
659658
op: PyComparisonOp,
660659
vm: &VirtualMachine,
@@ -688,10 +687,8 @@ impl Iterable for PyBytes {
688687

689688
impl Representable for PyBytes {
690689
#[inline]
691-
fn repr(zelf: &crate::Py<Self>, vm: &VirtualMachine) -> PyResult<PyStrRef> {
692-
zelf.inner
693-
.repr(None, vm)
694-
.map(|s| PyStr::from(s).into_ref(vm))
690+
fn repr_str(zelf: &Py<Self>, vm: &VirtualMachine) -> PyResult<String> {
691+
zelf.inner.repr(None, vm)
695692
}
696693
}
697694

@@ -732,7 +729,7 @@ impl Unconstructible for PyBytesIterator {}
732729

733730
impl IterNextIterable for PyBytesIterator {}
734731
impl IterNext for PyBytesIterator {
735-
fn next(zelf: &crate::Py<Self>, vm: &VirtualMachine) -> PyResult<PyIterReturn> {
732+
fn next(zelf: &Py<Self>, vm: &VirtualMachine) -> PyResult<PyIterReturn> {
736733
zelf.internal.lock().next(|bytes, pos| {
737734
Ok(PyIterReturn::from_result(
738735
bytes

vm/src/builtins/classmethod.rs

Lines changed: 8 additions & 7 deletions
Original file line numberDiff line numberDiff line change
@@ -1,4 +1,4 @@
1-
use super::{PyBoundMethod, PyStr, PyStrRef, PyType, PyTypeRef};
1+
use super::{PyBoundMethod, PyStr, PyType, PyTypeRef};
22
use crate::{
33
class::PyClassImpl,
44
common::lock::PyMutex,
@@ -158,23 +158,24 @@ impl PyClassMethod {
158158

159159
impl Representable for PyClassMethod {
160160
#[inline]
161-
fn repr(zelf: &crate::Py<Self>, vm: &VirtualMachine) -> PyResult<PyStrRef> {
161+
fn repr_str(zelf: &Py<Self>, vm: &VirtualMachine) -> PyResult<String> {
162162
let callable = zelf.callable.lock().repr(vm).unwrap();
163163
let class = Self::class(vm);
164164

165-
match (
165+
let repr = match (
166166
class
167167
.qualname(vm)
168168
.downcast_ref::<PyStr>()
169169
.map(|n| n.as_str()),
170170
class.module(vm).downcast_ref::<PyStr>().map(|m| m.as_str()),
171171
) {
172-
(None, _) => Err(vm.new_type_error("Unknown qualified name".into())),
172+
(None, _) => return Err(vm.new_type_error("Unknown qualified name".into())),
173173
(Some(qualname), Some(module)) if module != "builtins" => {
174-
Ok(PyStr::from(format!("<{module}.{qualname}({callable})>")).into_ref(vm))
174+
format!("<{module}.{qualname}({callable})>")
175175
}
176-
_ => Ok(PyStr::from(format!("<{}({})>", class.slot_name(), callable)).into_ref(vm)),
177-
}
176+
_ => format!("<{}({})>", class.slot_name(), callable),
177+
};
178+
Ok(repr)
178179
}
179180
}
180181

vm/src/builtins/code.rs

Lines changed: 3 additions & 4 deletions
Original file line numberDiff line numberDiff line change
@@ -2,7 +2,7 @@
22
33
*/
44

5-
use super::{PyStr, PyStrRef, PyTupleRef, PyType, PyTypeRef};
5+
use super::{PyStrRef, PyTupleRef, PyType, PyTypeRef};
66
use crate::{
77
builtins::PyStrInterned,
88
bytecode::{self, AsBag, BorrowedConstant, CodeFlags, Constant, ConstantBag},
@@ -221,16 +221,15 @@ impl PyCode {}
221221

222222
impl Representable for PyCode {
223223
#[inline]
224-
fn repr(zelf: &crate::Py<Self>, vm: &VirtualMachine) -> PyResult<PyStrRef> {
224+
fn repr_str(zelf: &Py<Self>, _vm: &VirtualMachine) -> PyResult<String> {
225225
let code = &zelf.code;
226-
Ok(PyStr::from(format!(
226+
Ok(format!(
227227
"<code object {} at {:#x} file {:?}, line {}>",
228228
code.obj_name,
229229
zelf.get_id(),
230230
code.source_path.as_str(),
231231
code.first_line_number
232232
))
233-
.into_ref(vm))
234233
}
235234
}
236235

vm/src/builtins/complex.rs

Lines changed: 6 additions & 6 deletions
Original file line numberDiff line numberDiff line change
@@ -1,4 +1,4 @@
1-
use super::{float, PyStr, PyStrRef, PyType, PyTypeRef};
1+
use super::{float, PyStr, PyType, PyTypeRef};
22
use crate::{
33
class::PyClassImpl,
44
convert::{ToPyObject, ToPyResult},
@@ -371,7 +371,7 @@ impl PyComplex {
371371

372372
impl Comparable for PyComplex {
373373
fn cmp(
374-
zelf: &crate::Py<Self>,
374+
zelf: &Py<Self>,
375375
other: &PyObject,
376376
op: PyComparisonOp,
377377
vm: &VirtualMachine,
@@ -401,7 +401,7 @@ impl Comparable for PyComplex {
401401

402402
impl Hashable for PyComplex {
403403
#[inline]
404-
fn hash(zelf: &crate::Py<Self>, _vm: &VirtualMachine) -> PyResult<hash::PyHash> {
404+
fn hash(zelf: &Py<Self>, _vm: &VirtualMachine) -> PyResult<hash::PyHash> {
405405
let value = zelf.value;
406406

407407
let re_hash =
@@ -455,7 +455,7 @@ impl AsNumber for PyComplex {
455455

456456
impl Representable for PyComplex {
457457
#[inline]
458-
fn repr(zelf: &crate::Py<Self>, vm: &VirtualMachine) -> PyResult<PyStrRef> {
458+
fn repr_str(zelf: &Py<Self>, _vm: &VirtualMachine) -> PyResult<String> {
459459
// TODO: when you fix this, move it to rustpython_common::complex::repr and update
460460
// ast/src/unparse.rs + impl Display for Constant in ast/src/constant.rs
461461
let Complex64 { re, im } = zelf.value;
@@ -470,7 +470,7 @@ impl Representable for PyComplex {
470470
// positive empty => return im_part, integer => drop ., fractional => float_ops
471471
let re_part = if re == 0.0 {
472472
if re.is_sign_positive() {
473-
return Ok(PyStr::from(im_part).into_ref(vm));
473+
return Ok(im_part);
474474
} else {
475475
re.to_string()
476476
}
@@ -489,7 +489,7 @@ impl Representable for PyComplex {
489489
}
490490
result.push_str(&im_part);
491491
result.push(')');
492-
Ok(PyStr::from(result).into_ref(vm))
492+
Ok(result)
493493
}
494494
}
495495

vm/src/builtins/coroutine.rs

Lines changed: 5 additions & 5 deletions
Original file line numberDiff line numberDiff line change
@@ -1,4 +1,4 @@
1-
use super::{PyCode, PyStr, PyStrRef, PyType};
1+
use super::{PyCode, PyStrRef, PyType};
22
use crate::{
33
class::PyClassImpl,
44
coroutine::Coro,
@@ -103,14 +103,14 @@ impl Unconstructible for PyCoroutine {}
103103

104104
impl Representable for PyCoroutine {
105105
#[inline]
106-
fn repr(zelf: &crate::Py<Self>, vm: &VirtualMachine) -> PyResult<PyStrRef> {
107-
Ok(PyStr::from(zelf.inner.repr(zelf.as_object(), zelf.get_id(), vm)).into_ref(vm))
106+
fn repr_str(zelf: &Py<Self>, vm: &VirtualMachine) -> PyResult<String> {
107+
Ok(zelf.inner.repr(zelf.as_object(), zelf.get_id(), vm))
108108
}
109109
}
110110

111111
impl IterNextIterable for PyCoroutine {}
112112
impl IterNext for PyCoroutine {
113-
fn next(zelf: &crate::Py<Self>, vm: &VirtualMachine) -> PyResult<PyIterReturn> {
113+
fn next(zelf: &Py<Self>, vm: &VirtualMachine) -> PyResult<PyIterReturn> {
114114
Self::send(zelf.to_owned(), vm.ctx.none(), vm)
115115
}
116116
}
@@ -149,7 +149,7 @@ impl PyCoroutineWrapper {
149149

150150
impl IterNextIterable for PyCoroutineWrapper {}
151151
impl IterNext for PyCoroutineWrapper {
152-
fn next(zelf: &crate::Py<Self>, vm: &VirtualMachine) -> PyResult<PyIterReturn> {
152+
fn next(zelf: &Py<Self>, vm: &VirtualMachine) -> PyResult<PyIterReturn> {
153153
Self::send(zelf.to_owned(), vm.ctx.none(), vm)
154154
}
155155
}

vm/src/builtins/descriptor.rs

Lines changed: 3 additions & 4 deletions
Original file line numberDiff line numberDiff line change
@@ -1,4 +1,4 @@
1-
use super::{PyStr, PyStrRef, PyType, PyTypeRef};
1+
use super::{PyStr, PyType, PyTypeRef};
22
use crate::{
33
class::PyClassImpl,
44
function::PySetterValue,
@@ -190,13 +190,12 @@ impl Unconstructible for MemberDescrObject {}
190190

191191
impl Representable for MemberDescrObject {
192192
#[inline]
193-
fn repr(zelf: &crate::Py<Self>, vm: &VirtualMachine) -> PyResult<PyStrRef> {
194-
Ok(PyStr::from(format!(
193+
fn repr_str(zelf: &Py<Self>, _vm: &VirtualMachine) -> PyResult<String> {
194+
Ok(format!(
195195
"<member '{}' of '{}' objects>",
196196
zelf.common.name,
197197
zelf.common.typ.name(),
198198
))
199-
.into_ref(vm))
200199
}
201200
}
202201

0 commit comments

Comments
 (0)