Skip to content

Commit b4b18c9

Browse files
committed
Simplify AsSequence
1 parent 04dd8d3 commit b4b18c9

File tree

12 files changed

+38
-149
lines changed

12 files changed

+38
-149
lines changed

vm/src/builtins/bytearray.rs

+2-8
Original file line numberDiff line numberDiff line change
@@ -36,7 +36,7 @@ use crate::{
3636
TryFromBorrowedObject, TryFromObject, VirtualMachine,
3737
};
3838
use bstr::ByteSlice;
39-
use std::{borrow::Cow, mem::size_of};
39+
use std::mem::size_of;
4040

4141
#[pyclass(module = false, name = "bytearray")]
4242
#[derive(Debug, Default)]
@@ -784,13 +784,7 @@ impl AsMapping for PyByteArray {
784784
}
785785

786786
impl AsSequence for PyByteArray {
787-
fn as_sequence(_zelf: &Py<Self>, _vm: &VirtualMachine) -> Cow<'static, PySequenceMethods> {
788-
Cow::Borrowed(&Self::SEQUENCE_METHODS)
789-
}
790-
}
791-
792-
impl PyByteArray {
793-
const SEQUENCE_METHODS: PySequenceMethods = PySequenceMethods {
787+
const AS_SEQUENCE: PySequenceMethods = PySequenceMethods {
794788
length: Some(|seq, _vm| Ok(Self::sequence_downcast(seq).len())),
795789
concat: Some(|seq, other, vm| {
796790
Self::sequence_downcast(seq)

vm/src/builtins/bytes.rs

+2-8
Original file line numberDiff line numberDiff line change
@@ -25,7 +25,7 @@ use crate::{
2525
TryFromBorrowedObject, TryFromObject, VirtualMachine,
2626
};
2727
use bstr::ByteSlice;
28-
use std::{borrow::Cow, mem::size_of, ops::Deref};
28+
use std::{mem::size_of, ops::Deref};
2929

3030
#[pyclass(module = false, name = "bytes")]
3131
#[derive(Clone, Debug)]
@@ -577,13 +577,7 @@ impl AsMapping for PyBytes {
577577
}
578578

579579
impl AsSequence for PyBytes {
580-
fn as_sequence(_zelf: &Py<Self>, _vm: &VirtualMachine) -> Cow<'static, PySequenceMethods> {
581-
Cow::Borrowed(&Self::SEQUENCE_METHODS)
582-
}
583-
}
584-
585-
impl PyBytes {
586-
const SEQUENCE_METHODS: PySequenceMethods = PySequenceMethods {
580+
const AS_SEQUENCE: PySequenceMethods = PySequenceMethods {
587581
length: Some(|seq, _vm| Ok(Self::sequence_downcast(seq).len())),
588582
concat: Some(|seq, other, vm| {
589583
Self::sequence_downcast(seq)

vm/src/builtins/dict.rs

+5-26
Original file line numberDiff line numberDiff line change
@@ -25,7 +25,7 @@ use crate::{
2525
AsObject, Context, Py, PyObject, PyObjectRef, PyPayload, PyRef, PyResult, TryFromObject,
2626
};
2727
use rustpython_common::lock::PyMutex;
28-
use std::{borrow::Cow, fmt};
28+
use std::fmt;
2929

3030
pub type DictContentType = dictdatatype::Dict;
3131

@@ -478,13 +478,7 @@ impl AsMapping for PyDict {
478478
}
479479

480480
impl AsSequence for PyDict {
481-
fn as_sequence(_zelf: &Py<Self>, _vm: &VirtualMachine) -> Cow<'static, PySequenceMethods> {
482-
Cow::Borrowed(&Self::SEQUENCE_METHODS)
483-
}
484-
}
485-
486-
impl PyDict {
487-
const SEQUENCE_METHODS: PySequenceMethods = PySequenceMethods {
481+
const AS_SEQUENCE: PySequenceMethods = PySequenceMethods {
488482
contains: Some(|seq, target, vm| Self::sequence_downcast(seq).entries.contains(vm, target)),
489483
..*PySequenceMethods::not_implemented()
490484
};
@@ -1054,12 +1048,7 @@ impl Comparable for PyDictKeys {
10541048
}
10551049

10561050
impl AsSequence for PyDictKeys {
1057-
fn as_sequence(_zelf: &Py<Self>, _vm: &VirtualMachine) -> Cow<'static, PySequenceMethods> {
1058-
Cow::Borrowed(&Self::SEQUENCE_METHODS)
1059-
}
1060-
}
1061-
impl PyDictKeys {
1062-
const SEQUENCE_METHODS: PySequenceMethods = PySequenceMethods {
1051+
const AS_SEQUENCE: PySequenceMethods = PySequenceMethods {
10631052
length: Some(|seq, _vm| Ok(Self::sequence_downcast(seq).len())),
10641053
contains: Some(|seq, target, vm| {
10651054
Self::sequence_downcast(seq)
@@ -1108,12 +1097,7 @@ impl Comparable for PyDictItems {
11081097
}
11091098

11101099
impl AsSequence for PyDictItems {
1111-
fn as_sequence(_zelf: &Py<Self>, _vm: &VirtualMachine) -> Cow<'static, PySequenceMethods> {
1112-
Cow::Borrowed(&Self::SEQUENCE_METHODS)
1113-
}
1114-
}
1115-
impl PyDictItems {
1116-
const SEQUENCE_METHODS: PySequenceMethods = PySequenceMethods {
1100+
const AS_SEQUENCE: PySequenceMethods = PySequenceMethods {
11171101
length: Some(|seq, _vm| Ok(Self::sequence_downcast(seq).len())),
11181102
contains: Some(|seq, target, vm| {
11191103
Self::sequence_downcast(seq)
@@ -1130,12 +1114,7 @@ impl PyDictValues {}
11301114
impl Unconstructible for PyDictValues {}
11311115

11321116
impl AsSequence for PyDictValues {
1133-
fn as_sequence(_zelf: &Py<Self>, _vm: &VirtualMachine) -> Cow<'static, PySequenceMethods> {
1134-
Cow::Borrowed(&Self::SEQUENCE_METHODS)
1135-
}
1136-
}
1137-
impl PyDictValues {
1138-
const SEQUENCE_METHODS: PySequenceMethods = PySequenceMethods {
1117+
const AS_SEQUENCE: PySequenceMethods = PySequenceMethods {
11391118
length: Some(|seq, _vm| Ok(Self::sequence_downcast(seq).len())),
11401119
..*PySequenceMethods::not_implemented()
11411120
};

vm/src/builtins/list.rs

+2-10
Original file line numberDiff line numberDiff line change
@@ -19,7 +19,7 @@ use crate::{
1919
vm::VirtualMachine,
2020
AsObject, Context, Py, PyObject, PyObjectRef, PyPayload, PyRef, PyResult,
2121
};
22-
use std::{borrow::Cow, fmt, ops::DerefMut};
22+
use std::{fmt, ops::DerefMut};
2323

2424
/// Built-in mutable sequence.
2525
///
@@ -404,15 +404,7 @@ impl AsMapping for PyList {
404404
}
405405

406406
impl AsSequence for PyList {
407-
fn as_sequence(
408-
_zelf: &crate::Py<Self>,
409-
_vm: &VirtualMachine,
410-
) -> Cow<'static, PySequenceMethods> {
411-
Cow::Borrowed(&Self::SEQUENCE_METHDOS)
412-
}
413-
}
414-
impl PyList {
415-
const SEQUENCE_METHDOS: PySequenceMethods = PySequenceMethods {
407+
const AS_SEQUENCE: PySequenceMethods = PySequenceMethods {
416408
length: Some(|seq, _vm| Ok(Self::sequence_downcast(seq).len())),
417409
concat: Some(|seq, other, vm| {
418410
Self::sequence_downcast(seq)

vm/src/builtins/mappingproxy.rs

+1-11
Original file line numberDiff line numberDiff line change
@@ -7,7 +7,6 @@ use crate::{
77
types::{AsMapping, AsSequence, Constructor, Iterable},
88
AsObject, Context, Py, PyObject, PyObjectRef, PyPayload, PyRef, PyResult, VirtualMachine,
99
};
10-
use std::borrow::Cow;
1110

1211
#[pyclass(module = false, name = "mappingproxy")]
1312
#[derive(Debug)]
@@ -168,16 +167,7 @@ impl AsMapping for PyMappingProxy {
168167
}
169168

170169
impl AsSequence for PyMappingProxy {
171-
fn as_sequence(
172-
_zelf: &crate::Py<Self>,
173-
_vm: &VirtualMachine,
174-
) -> Cow<'static, PySequenceMethods> {
175-
Cow::Borrowed(&Self::SEQUENCE_METHODS)
176-
}
177-
}
178-
179-
impl PyMappingProxy {
180-
const SEQUENCE_METHODS: PySequenceMethods = PySequenceMethods {
170+
const AS_SEQUENCE: PySequenceMethods = PySequenceMethods {
181171
contains: Some(|seq, target, vm| Self::sequence_downcast(seq)._contains(target, vm)),
182172
..*PySequenceMethods::not_implemented()
183173
};

vm/src/builtins/memory.rs

+2-8
Original file line numberDiff line numberDiff line change
@@ -24,7 +24,7 @@ use crate::{
2424
};
2525
use crossbeam_utils::atomic::AtomicCell;
2626
use itertools::Itertools;
27-
use std::{borrow::Cow, cmp::Ordering, fmt::Debug, mem::ManuallyDrop, ops::Range};
27+
use std::{cmp::Ordering, fmt::Debug, mem::ManuallyDrop, ops::Range};
2828

2929
#[derive(FromArgs)]
3030
pub struct PyMemoryViewNewArgs {
@@ -976,13 +976,7 @@ impl AsMapping for PyMemoryView {
976976
}
977977

978978
impl AsSequence for PyMemoryView {
979-
fn as_sequence(_zelf: &Py<Self>, _vm: &VirtualMachine) -> Cow<'static, PySequenceMethods> {
980-
Cow::Borrowed(&Self::SEQUENCE_METHODS)
981-
}
982-
}
983-
984-
impl PyMemoryView {
985-
const SEQUENCE_METHODS: PySequenceMethods = PySequenceMethods {
979+
const AS_SEQUENCE: PySequenceMethods = PySequenceMethods {
986980
length: Some(|seq, vm| {
987981
let zelf = Self::sequence_downcast(seq);
988982
zelf.try_not_released(vm)?;

vm/src/builtins/range.rs

+14-21
Original file line numberDiff line numberDiff line change
@@ -16,7 +16,7 @@ use crossbeam_utils::atomic::AtomicCell;
1616
use num_bigint::{BigInt, Sign};
1717
use num_integer::Integer;
1818
use num_traits::{One, Signed, ToPrimitive, Zero};
19-
use std::{borrow::Cow, cmp::max};
19+
use std::cmp::max;
2020

2121
// Search flag passed to iter_search
2222
enum SearchType {
@@ -389,20 +389,6 @@ impl PyRange {
389389
.try_to_primitive::<isize>(vm)
390390
.map(|x| x as usize)
391391
}
392-
393-
const SEQUENCE_METHDOS: PySequenceMethods = PySequenceMethods {
394-
length: Some(|seq, vm| Self::sequence_downcast(seq).protocol_length(vm)),
395-
item: Some(|seq, i, vm| {
396-
Self::sequence_downcast(seq)
397-
.get(&i.into())
398-
.map(|x| PyInt::from(x).into_ref(vm).into())
399-
.ok_or_else(|| vm.new_index_error("index out of range".to_owned()))
400-
}),
401-
contains: Some(|seq, needle, vm| {
402-
Ok(Self::sequence_downcast(seq).contains(needle.to_owned(), vm))
403-
}),
404-
..*PySequenceMethods::not_implemented()
405-
};
406392
}
407393

408394
impl AsMapping for PyRange {
@@ -416,12 +402,19 @@ impl AsMapping for PyRange {
416402
}
417403

418404
impl AsSequence for PyRange {
419-
fn as_sequence(
420-
_zelf: &crate::Py<Self>,
421-
_vm: &VirtualMachine,
422-
) -> Cow<'static, PySequenceMethods> {
423-
Cow::Borrowed(&Self::SEQUENCE_METHDOS)
424-
}
405+
const AS_SEQUENCE: PySequenceMethods = PySequenceMethods {
406+
length: Some(|seq, vm| Self::sequence_downcast(seq).protocol_length(vm)),
407+
item: Some(|seq, i, vm| {
408+
Self::sequence_downcast(seq)
409+
.get(&i.into())
410+
.map(|x| PyInt::from(x).into_ref(vm).into())
411+
.ok_or_else(|| vm.new_index_error("index out of range".to_owned()))
412+
}),
413+
contains: Some(|seq, needle, vm| {
414+
Ok(Self::sequence_downcast(seq).contains(needle.to_owned(), vm))
415+
}),
416+
..*PySequenceMethods::not_implemented()
417+
};
425418
}
426419

427420
impl Hashable for PyRange {

vm/src/builtins/set.rs

+2-21
Original file line numberDiff line numberDiff line change
@@ -20,7 +20,6 @@ use crate::{
2020
vm::VirtualMachine,
2121
AsObject, Context, Py, PyObject, PyObjectRef, PyPayload, PyRef, PyResult, TryFromObject,
2222
};
23-
use std::borrow::Cow;
2423
use std::{fmt, ops::Deref};
2524

2625
pub type SetContentType = dictdatatype::Dict<()>;
@@ -663,16 +662,7 @@ impl Initializer for PySet {
663662
}
664663

665664
impl AsSequence for PySet {
666-
fn as_sequence(
667-
_zelf: &crate::Py<Self>,
668-
_vm: &VirtualMachine,
669-
) -> Cow<'static, PySequenceMethods> {
670-
Cow::Borrowed(&Self::SEQUENCE_METHODS)
671-
}
672-
}
673-
674-
impl PySet {
675-
const SEQUENCE_METHODS: PySequenceMethods = PySequenceMethods {
665+
const AS_SEQUENCE: PySequenceMethods = PySequenceMethods {
676666
length: Some(|seq, _vm| Ok(Self::sequence_downcast(seq).len())),
677667
contains: Some(|seq, needle, vm| Self::sequence_downcast(seq).inner.contains(needle, vm)),
678668
..*PySequenceMethods::not_implemented()
@@ -904,16 +894,7 @@ impl PyFrozenSet {
904894
}
905895

906896
impl AsSequence for PyFrozenSet {
907-
fn as_sequence(
908-
_zelf: &crate::Py<Self>,
909-
_vm: &VirtualMachine,
910-
) -> Cow<'static, PySequenceMethods> {
911-
Cow::Borrowed(&Self::SEQUENCE_METHODS)
912-
}
913-
}
914-
915-
impl PyFrozenSet {
916-
const SEQUENCE_METHODS: PySequenceMethods = PySequenceMethods {
897+
const AS_SEQUENCE: PySequenceMethods = PySequenceMethods {
917898
length: Some(|seq, _vm| Ok(Self::sequence_downcast(seq).len())),
918899
contains: Some(|seq, needle, vm| Self::sequence_downcast(seq).inner.contains(needle, vm)),
919900
..*PySequenceMethods::not_implemented()

vm/src/builtins/str.rs

+1-10
Original file line numberDiff line numberDiff line change
@@ -1309,16 +1309,7 @@ impl AsMapping for PyStr {
13091309
}
13101310

13111311
impl AsSequence for PyStr {
1312-
fn as_sequence(
1313-
_zelf: &Py<Self>,
1314-
_vm: &VirtualMachine,
1315-
) -> std::borrow::Cow<'static, PySequenceMethods> {
1316-
std::borrow::Cow::Borrowed(&Self::SEQUENCE_METHDOS)
1317-
}
1318-
}
1319-
1320-
impl PyStr {
1321-
const SEQUENCE_METHDOS: PySequenceMethods = PySequenceMethods {
1312+
const AS_SEQUENCE: PySequenceMethods = PySequenceMethods {
13221313
length: Some(|seq, _vm| Ok(Self::sequence_downcast(seq).len())),
13231314
concat: Some(|seq, other, vm| {
13241315
let zelf = Self::sequence_downcast(seq);

vm/src/builtins/tuple.rs

+2-11
Original file line numberDiff line numberDiff line change
@@ -17,7 +17,7 @@ use crate::{
1717
vm::VirtualMachine,
1818
AsObject, Context, Py, PyObject, PyObjectRef, PyPayload, PyRef, PyResult, TryFromObject,
1919
};
20-
use std::{borrow::Cow, fmt, marker::PhantomData};
20+
use std::{fmt, marker::PhantomData};
2121

2222
/// tuple() -> empty tuple
2323
/// tuple(iterable) -> tuple initialized from iterable's items
@@ -354,16 +354,7 @@ impl AsMapping for PyTuple {
354354
}
355355

356356
impl AsSequence for PyTuple {
357-
fn as_sequence(
358-
_zelf: &crate::Py<Self>,
359-
_vm: &VirtualMachine,
360-
) -> Cow<'static, PySequenceMethods> {
361-
Cow::Borrowed(&Self::SEQUENCE_METHDOS)
362-
}
363-
}
364-
365-
impl PyTuple {
366-
const SEQUENCE_METHDOS: PySequenceMethods = PySequenceMethods {
357+
const AS_SEQUENCE: PySequenceMethods = PySequenceMethods {
367358
length: Some(|seq, _vm| Ok(Self::sequence_downcast(seq).len())),
368359
concat: Some(|seq, other, vm| {
369360
let zelf = Self::sequence_downcast(seq);

vm/src/stdlib/collections.rs

+1-10
Original file line numberDiff line numberDiff line change
@@ -518,16 +518,7 @@ mod _collections {
518518
}
519519

520520
impl AsSequence for PyDeque {
521-
fn as_sequence(
522-
_zelf: &crate::Py<Self>,
523-
_vm: &VirtualMachine,
524-
) -> std::borrow::Cow<'static, PySequenceMethods> {
525-
std::borrow::Cow::Borrowed(&Self::SEQUENCE_METHDOS)
526-
}
527-
}
528-
529-
impl PyDeque {
530-
const SEQUENCE_METHDOS: PySequenceMethods = PySequenceMethods {
521+
const AS_SEQUENCE: PySequenceMethods = PySequenceMethods {
531522
length: Some(|seq, _vm| Ok(Self::sequence_downcast(seq).len())),
532523
concat: Some(|seq, other, vm| {
533524
Self::sequence_downcast(seq)

vm/src/types/slot.rs

+4-5
Original file line numberDiff line numberDiff line change
@@ -958,15 +958,14 @@ pub trait AsMapping: PyPayload {
958958

959959
#[pyimpl]
960960
pub trait AsSequence: PyPayload {
961+
const AS_SEQUENCE: PySequenceMethods;
962+
961963
#[inline]
962964
#[pyslot]
963-
fn slot_as_sequence(zelf: &PyObject, vm: &VirtualMachine) -> Cow<'static, PySequenceMethods> {
964-
let zelf = unsafe { zelf.downcast_unchecked_ref::<Self>() };
965-
Self::as_sequence(zelf, vm)
965+
fn as_sequence(_zelf: &PyObject, _vm: &VirtualMachine) -> Cow<'static, PySequenceMethods> {
966+
Cow::Borrowed(&Self::AS_SEQUENCE)
966967
}
967968

968-
fn as_sequence(zelf: &Py<Self>, vm: &VirtualMachine) -> Cow<'static, PySequenceMethods>;
969-
970969
fn sequence_downcast<'a>(seq: &'a PySequence) -> &'a Py<Self> {
971970
unsafe { seq.obj.downcast_unchecked_ref() }
972971
}

0 commit comments

Comments
 (0)