From 276608233e74e1eb478f2af654095bdbd57cd3a6 Mon Sep 17 00:00:00 2001 From: Moreal Date: Sat, 6 Nov 2021 15:46:41 +0900 Subject: [PATCH] Implement `AsMapping` for `PyStr` --- vm/src/builtins/pystr.rs | 45 +++++++++++++++++++++++++++++++++++----- 1 file changed, 40 insertions(+), 5 deletions(-) diff --git a/vm/src/builtins/pystr.rs b/vm/src/builtins/pystr.rs index 6327d0bdd..9f0d1d436 100644 --- a/vm/src/builtins/pystr.rs +++ b/vm/src/builtins/pystr.rs @@ -7,15 +7,15 @@ use crate::{ anystr::{self, adjust_indices, AnyStr, AnyStrContainer, AnyStrWrapper}, format::{FormatSpec, FormatString, FromTemplate}, function::{ArgIterable, FuncArgs, IntoPyException, IntoPyObject, OptionalArg, OptionalOption}, - protocol::PyIterReturn, + protocol::{PyIterReturn, PyMappingMethods}, sliceable::PySliceableSequence, types::{ - Comparable, Constructor, Hashable, IterNext, IterNextIterable, Iterable, PyComparisonOp, - Unconstructible, + AsMapping, Comparable, Constructor, Hashable, IterNext, IterNextIterable, Iterable, + PyComparisonOp, Unconstructible, }, utils::Either, IdProtocol, ItemProtocol, PyClassDef, PyClassImpl, PyComparisonValue, PyContext, PyObject, - PyObjectRef, PyRef, PyResult, PyValue, TypeProtocol, VirtualMachine, + PyObjectRef, PyObjectView, PyRef, PyResult, PyValue, TypeProtocol, VirtualMachine, }; use ascii::{AsciiStr, AsciiString}; use bstr::ByteSlice; @@ -363,7 +363,10 @@ impl PyStr { } } -#[pyimpl(flags(BASETYPE), with(Hashable, Comparable, Iterable, Constructor))] +#[pyimpl( + flags(BASETYPE), + with(AsMapping, Hashable, Comparable, Iterable, Constructor) +)] impl PyStr { #[pymethod(magic)] fn add(zelf: PyRef, other: PyObjectRef, vm: &VirtualMachine) -> PyResult { @@ -1245,6 +1248,38 @@ impl Iterable for PyStr { } } +impl AsMapping for PyStr { + fn as_mapping(_zelf: &PyObjectView, _vm: &VirtualMachine) -> PyMappingMethods { + PyMappingMethods { + length: Some(Self::length), + subscript: Some(Self::subscript), + ass_subscript: None, + } + } + + #[inline] + fn length(zelf: PyObjectRef, vm: &VirtualMachine) -> PyResult { + Self::downcast_ref(&zelf, vm).map(|zelf| Ok(zelf.len()))? + } + + #[inline] + fn subscript(zelf: PyObjectRef, needle: PyObjectRef, vm: &VirtualMachine) -> PyResult { + Self::downcast_ref(&zelf, vm) + .map(|zelf| zelf.getitem(needle, vm))? + .map(|item| item.into_object(vm)) + } + + #[cold] + fn ass_subscript( + zelf: PyObjectRef, + _needle: PyObjectRef, + _value: Option, + _vm: &VirtualMachine, + ) -> PyResult<()> { + unreachable!("ass_subscript not implemented for {}", zelf.class()) + } +} + #[derive(FromArgs)] struct EncodeArgs { #[pyarg(any, default)]