From 14ce6aa49a96ad3b4076452bebe7c43409963e55 Mon Sep 17 00:00:00 2001 From: snowapril Date: Tue, 21 Sep 2021 00:10:24 +0900 Subject: [PATCH] add slot_as_mapping slots with atomic cell --- vm/src/slots.rs | 18 ++++++++++++++++-- 1 file changed, 16 insertions(+), 2 deletions(-) diff --git a/vm/src/slots.rs b/vm/src/slots.rs index 37382e6ec..f2b668043 100644 --- a/vm/src/slots.rs +++ b/vm/src/slots.rs @@ -1,4 +1,4 @@ -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, &VirtualMachine) -> PyResult<()>; pub(crate) type BufferFunc = fn(&PyObjectRef, &VirtualMachine) -> PyResult; +pub(crate) type MappingFunc = fn(&PyObjectRef, &VirtualMachine) -> PyResult; pub(crate) type IterFunc = fn(PyObjectRef, &VirtualMachine) -> PyResult; pub(crate) type IterNextFunc = fn(&PyObjectRef, &VirtualMachine) -> PyResult; @@ -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>, // More standard operations (here for binary compatibility) pub hash: AtomicCell>, @@ -532,6 +533,19 @@ pub trait AsBuffer: PyValue { fn as_buffer(zelf: &PyRef, vm: &VirtualMachine) -> PyResult; } +#[pyimpl] +pub trait AsMapping: PyValue { + #[pyslot] + fn slot_as_mapping(zelf: &PyObjectRef, vm: &VirtualMachine) -> PyResult { + 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, vm: &VirtualMachine) -> PyResult; +} + #[pyimpl] pub trait Iterable: PyValue { #[pyslot]