Skip to content

Commit b050741

Browse files
committed
move mapping protocol to rustpython_vm::protocol
Signed-off-by: snowapril <sinjihng@gmail.com>
1 parent ee04326 commit b050741

File tree

14 files changed

+54
-51
lines changed

14 files changed

+54
-51
lines changed

vm/src/builtins/bytearray.rs

+1-2
Original file line numberDiff line numberDiff line change
@@ -9,15 +9,14 @@ use crate::common::{
99
};
1010
use crate::{
1111
anystr::{self, AnyStr},
12-
builtins::dict::PyMapping,
1312
bytesinner::{
1413
bytes_decode, bytes_from_object, value_from_object, ByteInnerFindOptions,
1514
ByteInnerNewOptions, ByteInnerPaddingOptions, ByteInnerSplitOptions,
1615
ByteInnerTranslateOptions, DecodeArgs, PyBytesInner,
1716
},
1817
byteslike::ArgBytesLike,
1918
function::{ArgIterable, FuncArgs, OptionalArg, OptionalOption},
20-
protocol::{BufferInternal, BufferOptions, PyBuffer, ResizeGuard},
19+
protocol::{BufferInternal, BufferOptions, PyBuffer, PyMapping, ResizeGuard},
2120
sliceable::{PySliceableSequence, PySliceableSequenceMut, SequenceIndex},
2221
slots::{
2322
AsBuffer, AsMapping, Callable, Comparable, Hashable, Iterable, IteratorIterable,

vm/src/builtins/bytes.rs

+1-2
Original file line numberDiff line numberDiff line change
@@ -1,15 +1,14 @@
11
use super::{PyDictRef, PyIntRef, PyStrRef, PyTupleRef, PyTypeRef};
22
use crate::{
33
anystr::{self, AnyStr},
4-
builtins::dict::PyMapping,
54
bytesinner::{
65
bytes_decode, ByteInnerFindOptions, ByteInnerNewOptions, ByteInnerPaddingOptions,
76
ByteInnerSplitOptions, ByteInnerTranslateOptions, DecodeArgs, PyBytesInner,
87
},
98
byteslike::ArgBytesLike,
109
common::hash::PyHash,
1110
function::{ArgIterable, OptionalArg, OptionalOption},
12-
protocol::{BufferInternal, BufferOptions, PyBuffer},
11+
protocol::{BufferInternal, BufferOptions, PyBuffer, PyMapping},
1312
slots::{
1413
AsBuffer, AsMapping, Callable, Comparable, Hashable, Iterable, IteratorIterable,
1514
PyComparisonOp, PyIter, SlotConstructor,

vm/src/builtins/dict.rs

+2-34
Original file line numberDiff line numberDiff line change
@@ -5,6 +5,7 @@ use crate::{
55
exceptions::PyBaseExceptionRef,
66
function::{ArgIterable, FuncArgs, KwArgs, OptionalArg},
77
iterator,
8+
protocol::PyMapping,
89
slots::{
910
AsMapping, Comparable, Hashable, Iterable, IteratorIterable, PyComparisonOp, PyIter,
1011
Unhashable,
@@ -13,7 +14,7 @@ use crate::{
1314
IdProtocol, IntoPyObject, ItemProtocol,
1415
PyArithmaticValue::*,
1516
PyAttributes, PyClassDef, PyClassImpl, PyComparisonValue, PyContext, PyObjectRef, PyRef,
16-
PyResult, PyValue, TryFromBorrowedObject, TypeProtocol,
17+
PyResult, PyValue, TypeProtocol,
1718
};
1819
use crossbeam_utils::atomic::AtomicCell;
1920
use std::fmt;
@@ -917,36 +918,3 @@ pub(crate) fn init(context: &PyContext) {
917918
PyDictItemIterator::extend_class(context, &context.types.dict_itemiterator_type);
918919
PyDictReverseItemIterator::extend_class(context, &context.types.dict_reverseitemiterator_type);
919920
}
920-
921-
#[allow(clippy::type_complexity)]
922-
pub struct PyMapping {
923-
pub length: Option<fn(PyObjectRef, &VirtualMachine) -> PyResult<usize>>,
924-
pub subscript: Option<fn(PyObjectRef, PyObjectRef, &VirtualMachine) -> PyResult>,
925-
pub ass_subscript:
926-
Option<fn(PyObjectRef, PyObjectRef, Option<PyObjectRef>, &VirtualMachine) -> PyResult<()>>,
927-
}
928-
929-
impl PyMapping {
930-
pub fn check(cls: &PyObjectRef, vm: &VirtualMachine) -> bool {
931-
if let Ok(mapping) = PyMapping::try_from_borrowed_object(vm, cls) {
932-
mapping.subscript.is_some()
933-
} else {
934-
false
935-
}
936-
}
937-
}
938-
939-
impl TryFromBorrowedObject for PyMapping {
940-
fn try_from_borrowed_object(vm: &VirtualMachine, obj: &PyObjectRef) -> PyResult<Self> {
941-
let obj_cls = obj.class();
942-
for cls in obj_cls.iter_mro() {
943-
if let Some(f) = cls.slots.as_mapping.load() {
944-
return f(obj, vm);
945-
}
946-
}
947-
Err(vm.new_type_error(format!(
948-
"a dict-like object is required, not '{}'",
949-
obj_cls.name()
950-
)))
951-
}
952-
}

vm/src/builtins/list.rs

+1-1
Original file line numberDiff line numberDiff line change
@@ -7,8 +7,8 @@ use crate::common::lock::{
77
PyMappedRwLockReadGuard, PyRwLock, PyRwLockReadGuard, PyRwLockWriteGuard,
88
};
99
use crate::{
10-
builtins::dict::PyMapping,
1110
function::{ArgIterable, FuncArgs, OptionalArg},
11+
protocol::PyMapping,
1212
sequence::{self, SimpleSeq},
1313
sliceable::{PySliceableSequence, PySliceableSequenceMut, SequenceIndex},
1414
slots::{

vm/src/builtins/mappingproxy.rs

+1-1
Original file line numberDiff line numberDiff line change
@@ -1,8 +1,8 @@
11
use super::{PyDict, PyList, PyStrRef, PyTuple, PyTypeRef};
22
use crate::{
3-
builtins::dict::PyMapping,
43
function::OptionalArg,
54
iterator,
5+
protocol::PyMapping,
66
slots::{AsMapping, Iterable, SlotConstructor},
77
IntoPyObject, ItemProtocol, PyClassImpl, PyContext, PyObjectRef, PyRef, PyResult, PyValue,
88
TryFromObject, TypeProtocol, VirtualMachine,

vm/src/builtins/memory.rs

+1-2
Original file line numberDiff line numberDiff line change
@@ -6,10 +6,9 @@ use crate::common::{
66
rc::PyRc,
77
};
88
use crate::{
9-
builtins::dict::PyMapping,
109
bytesinner::bytes_to_hex,
1110
function::{FuncArgs, OptionalArg},
12-
protocol::{BufferInternal, BufferOptions, PyBuffer},
11+
protocol::{BufferInternal, BufferOptions, PyBuffer, PyMapping},
1312
sliceable::{convert_slice, wrap_index, SequenceIndex},
1413
slots::{AsBuffer, AsMapping, Comparable, Hashable, PyComparisonOp, SlotConstructor},
1514
stdlib::pystruct::_struct::FormatSpec,

vm/src/builtins/pytype.rs

+1-1
Original file line numberDiff line numberDiff line change
@@ -4,8 +4,8 @@ use super::{
44
};
55
use crate::common::{ascii, lock::PyRwLock};
66
use crate::{
7-
builtins::dict::PyMapping,
87
function::{FuncArgs, KwArgs, OptionalArg},
8+
protocol::PyMapping,
99
slots::{self, Callable, PyTpFlags, PyTypeSlots, SlotGetattro, SlotSetattro},
1010
utils::Either,
1111
IdProtocol, PyAttributes, PyClassImpl, PyContext, PyLease, PyObjectRef, PyRef, PyResult,

vm/src/builtins/range.rs

+1-1
Original file line numberDiff line numberDiff line change
@@ -1,9 +1,9 @@
11
use super::{PyInt, PyIntRef, PySlice, PySliceRef, PyTypeRef};
22
use crate::common::hash::PyHash;
33
use crate::{
4-
builtins::dict::PyMapping,
54
function::{FuncArgs, OptionalArg},
65
iterator,
6+
protocol::PyMapping,
77
slots::{AsMapping, Comparable, Hashable, Iterable, IteratorIterable, PyComparisonOp, PyIter},
88
IdProtocol, IntoPyRef, PyClassImpl, PyContext, PyObjectRef, PyRef, PyResult, PyValue,
99
TryFromObject, TypeProtocol, VirtualMachine,

vm/src/builtins/tuple.rs

+1-1
Original file line numberDiff line numberDiff line change
@@ -3,10 +3,10 @@ use super::{
33
iter::IterStatus::{self, Active, Exhausted},
44
PyInt, PyTypeRef,
55
};
6-
use crate::builtins::dict::PyMapping;
76
use crate::common::hash::PyHash;
87
use crate::{
98
function::OptionalArg,
9+
protocol::PyMapping,
1010
sequence::{self, SimpleSeq},
1111
sliceable::PySliceableSequence,
1212
slots::{

vm/src/protocol/mapping.rs

+36
Original file line numberDiff line numberDiff line change
@@ -0,0 +1,36 @@
1+
//! Mapping protocol
2+
3+
use crate::{vm::VirtualMachine, PyObjectRef, PyResult, TryFromBorrowedObject, TypeProtocol};
4+
5+
#[allow(clippy::type_complexity)]
6+
pub struct PyMapping {
7+
pub length: Option<fn(PyObjectRef, &VirtualMachine) -> PyResult<usize>>,
8+
pub subscript: Option<fn(PyObjectRef, PyObjectRef, &VirtualMachine) -> PyResult>,
9+
pub ass_subscript:
10+
Option<fn(PyObjectRef, PyObjectRef, Option<PyObjectRef>, &VirtualMachine) -> PyResult<()>>,
11+
}
12+
13+
impl PyMapping {
14+
pub fn check(cls: &PyObjectRef, vm: &VirtualMachine) -> bool {
15+
if let Ok(mapping) = PyMapping::try_from_borrowed_object(vm, cls) {
16+
mapping.subscript.is_some()
17+
} else {
18+
false
19+
}
20+
}
21+
}
22+
23+
impl TryFromBorrowedObject for PyMapping {
24+
fn try_from_borrowed_object(vm: &VirtualMachine, obj: &PyObjectRef) -> PyResult<Self> {
25+
let obj_cls = obj.class();
26+
for cls in obj_cls.iter_mro() {
27+
if let Some(f) = cls.slots.as_mapping.load() {
28+
return f(obj, vm);
29+
}
30+
}
31+
Err(vm.new_type_error(format!(
32+
"a dict-like object is required, not '{}'",
33+
obj_cls.name()
34+
)))
35+
}
36+
}

vm/src/protocol/mod.rs

+2
Original file line numberDiff line numberDiff line change
@@ -1,3 +1,5 @@
11
mod buffer;
2+
mod mapping;
23

34
pub(crate) use buffer::{BufferInternal, BufferOptions, PyBuffer, ResizeGuard};
5+
pub(crate) use mapping::PyMapping;

vm/src/pyobject.rs

+1-1
Original file line numberDiff line numberDiff line change
@@ -9,7 +9,6 @@ use crate::{
99
builtinfunc::PyNativeFuncDef,
1010
bytearray, bytes,
1111
code::{self, PyCode},
12-
dict::PyMapping,
1312
getset::{IntoPyGetterFunc, IntoPySetterFunc, PyGetSet},
1413
namespace::PyNamespace,
1514
object, pystr,
@@ -20,6 +19,7 @@ use crate::{
2019
dictdatatype::Dict,
2120
exceptions::{self, PyBaseExceptionRef},
2221
function::{IntoFuncArgs, IntoPyNativeFunc},
22+
protocol::PyMapping,
2323
slots::{PyTpFlags, PyTypeSlots},
2424
types::{create_type_with_slots, TypeZoo},
2525
VirtualMachine,

vm/src/slots.rs

+2-2
Original file line numberDiff line numberDiff line change
@@ -1,8 +1,8 @@
1-
use crate::builtins::{dict::PyMapping, PyStrRef, PyTypeRef};
1+
use crate::builtins::{PyStrRef, PyTypeRef};
22
use crate::common::hash::PyHash;
33
use crate::common::lock::PyRwLock;
44
use crate::function::{FromArgs, FuncArgs, OptionalArg};
5-
use crate::protocol::PyBuffer;
5+
use crate::protocol::{PyBuffer, PyMapping};
66
use crate::utils::Either;
77
use crate::VirtualMachine;
88
use crate::{

vm/src/stdlib/array.rs

+3-3
Original file line numberDiff line numberDiff line change
@@ -12,12 +12,12 @@ mod array {
1212
};
1313
use crate::{
1414
builtins::{
15-
dict::PyMapping, IntoPyFloat, PyByteArray, PyBytes, PyBytesRef, PyIntRef, PyList,
16-
PyListRef, PySliceRef, PyStr, PyStrRef, PyTypeRef,
15+
IntoPyFloat, PyByteArray, PyBytes, PyBytesRef, PyIntRef, PyList, PyListRef, PySliceRef,
16+
PyStr, PyStrRef, PyTypeRef,
1717
},
1818
byteslike::ArgBytesLike,
1919
function::{ArgIterable, OptionalArg},
20-
protocol::{BufferInternal, BufferOptions, PyBuffer, ResizeGuard},
20+
protocol::{BufferInternal, BufferOptions, PyBuffer, PyMapping, ResizeGuard},
2121
sliceable::{saturate_index, PySliceableSequence, PySliceableSequenceMut, SequenceIndex},
2222
slots::{
2323
AsBuffer, AsMapping, Comparable, Iterable, IteratorIterable, PyComparisonOp, PyIter,

0 commit comments

Comments
 (0)