add slot_as_mapping slots with atomic cell
-use crate::builtins::{PyStrRef, PyTypeRef};
+use crate::builtins::{dict::PyMapping, PyStrRef, PyTypeRef};
use crate::common::hash::PyHash;
use crate::common::lock::PyRwLock;
use crate::function::{FromArgs, FuncArgs, OptionalArg};
@@ -70,6 +70,7 @@ pub(crate) type GetattroFunc = fn(PyObjectRef, PyStrRef, &VirtualMachine) -> PyR
pub(crate) type SetattroFunc =
fn(&PyObjectRef, PyStrRef, Option<PyObjectRef>, &VirtualMachine) -> PyResult<()>;
pub(crate) type BufferFunc = fn(&PyObjectRef, &VirtualMachine) -> PyResult<PyBuffer>;
+pub(crate) type MappingFunc = fn(&PyObjectRef, &VirtualMachine) -> PyResult<PyMapping>;
pub(crate) type IterFunc = fn(PyObjectRef, &VirtualMachine) -> PyResult;
pub(crate) type IterNextFunc = fn(&PyObjectRef, &VirtualMachine) -> PyResult<PyIterReturn>;
@@ -85,7 +86,7 @@ pub struct PyTypeSlots {
// Method suites for standard classes
// tp_as_number
// tp_as_sequence
- // tp_as_mapping
+ pub as_mapping: AtomicCell<Option<MappingFunc>>,
// More standard operations (here for binary compatibility)
pub hash: AtomicCell<Option<HashFunc>>,
@@ -532,6 +533,19 @@ pub trait AsBuffer: PyValue {
fn as_buffer(zelf: &PyRef<Self>, vm: &VirtualMachine) -> PyResult<PyBuffer>;
}
+#[pyimpl]
+pub trait AsMapping: PyValue {
+ #[pyslot]
+ fn slot_as_mapping(zelf: &PyObjectRef, vm: &VirtualMachine) -> PyResult<PyMapping> {
+ let zelf = zelf
+ .downcast_ref()
+ .ok_or_else(|| vm.new_type_error("unexpected payload for as_mapping".to_owned()))?;
+ Self::as_mapping(zelf, vm)
+ }
+
+ fn as_mapping(zelf: &PyRef<Self>, vm: &VirtualMachine) -> PyResult<PyMapping>;
+}
+
#[pyimpl]
pub trait Iterable: PyValue {
#[pyslot]
