Skip to content

Commit

Permalink
move obj downcast to AsMapping
Browse files Browse the repository at this point in the history
  • Loading branch information
qingshi163 committed Dec 1, 2021
1 parent 06fbba6 commit 6a996b0
Show file tree
Hide file tree
Showing 13 changed files with 40 additions and 35 deletions.
6 changes: 3 additions & 3 deletions stdlib/src/array.rs
Original file line number Diff line number Diff line change
Expand Up @@ -1241,12 +1241,12 @@ mod array {

impl PyArray {
const MAPPING_METHODS: PyMappingMethods = PyMappingMethods {
length: Some(|mapping, _vm| Ok(mapping.obj_as::<Self>().len())),
length: Some(|mapping, _vm| Ok(Self::mapping_downcast(mapping).len())),
subscript: Some(|mapping, needle, vm| {
mapping.obj_as::<Self>().getitem(needle.to_owned(), vm)
Self::mapping_downcast(mapping).getitem(needle.to_owned(), vm)
}),
ass_subscript: Some(|mapping, needle, value, vm| {
let zelf = mapping.obj_as::<Self>();
let zelf = Self::mapping_downcast(mapping);
if let Some(value) = value {
Self::setitem(zelf.to_owned(), needle.to_owned(), value, vm)
} else {
Expand Down
6 changes: 3 additions & 3 deletions vm/src/builtins/bytearray.rs
Original file line number Diff line number Diff line change
Expand Up @@ -679,12 +679,12 @@ impl PyByteArray {

impl PyByteArray {
const MAPPING_METHODS: PyMappingMethods = PyMappingMethods {
length: Some(|mapping, _vm| Ok(mapping.obj_as::<Self>().len())),
length: Some(|mapping, _vm| Ok(Self::mapping_downcast(mapping).len())),
subscript: Some(|mapping, needle, vm| {
mapping.obj_as::<Self>().getitem(needle.to_owned(), vm)
Self::mapping_downcast(mapping).getitem(needle.to_owned(), vm)
}),
ass_subscript: Some(|mapping, needle, value, vm| {
let zelf = mapping.obj_as::<Self>();
let zelf = Self::mapping_downcast(mapping);
if let Some(value) = value {
Self::setitem(zelf.to_owned(), needle.to_owned(), value, vm)
} else {
Expand Down
4 changes: 2 additions & 2 deletions vm/src/builtins/bytes.rs
Original file line number Diff line number Diff line change
Expand Up @@ -525,9 +525,9 @@ impl PyBytes {

impl PyBytes {
const MAPPING_METHODS: PyMappingMethods = PyMappingMethods {
length: Some(|mapping, _vm| Ok(mapping.obj_as::<Self>().len())),
length: Some(|mapping, _vm| Ok(Self::mapping_downcast(mapping).len())),
subscript: Some(|mapping, needle, vm| {
mapping.obj_as::<Self>().getitem(needle.to_owned(), vm)
Self::mapping_downcast(mapping).getitem(needle.to_owned(), vm)
}),
ass_subscript: None,
};
Expand Down
8 changes: 5 additions & 3 deletions vm/src/builtins/dict.rs
Original file line number Diff line number Diff line change
Expand Up @@ -426,10 +426,12 @@ impl PyDict {

impl PyDict {
pub(crate) const MAPPING_METHODS: PyMappingMethods = PyMappingMethods {
length: Some(|mapping, _vm| Ok(mapping.obj_as::<Self>().len())),
subscript: Some(|mapping, needle, vm| mapping.obj_as::<Self>().inner_getitem(needle, vm)),
length: Some(|mapping, _vm| Ok(Self::mapping_downcast(mapping).len())),
subscript: Some(|mapping, needle, vm| {
Self::mapping_downcast(mapping).inner_getitem(needle, vm)
}),
ass_subscript: Some(|mapping, needle, value, vm| {
let zelf = mapping.obj_as::<Self>();
let zelf = Self::mapping_downcast(mapping);
if let Some(value) = value {
zelf.inner_setitem(needle, value, vm)
} else {
Expand Down
2 changes: 1 addition & 1 deletion vm/src/builtins/genericalias.rs
Original file line number Diff line number Diff line change
Expand Up @@ -292,7 +292,7 @@ impl PyGenericAlias {
const MAPPING_METHODS: PyMappingMethods = PyMappingMethods {
length: None,
subscript: Some(|mapping, needle, vm| {
mapping.obj_as::<Self>().getitem(needle.to_owned(), vm)
Self::mapping_downcast(mapping).getitem(needle.to_owned(), vm)
}),
ass_subscript: None,
};
Expand Down
6 changes: 3 additions & 3 deletions vm/src/builtins/list.rs
Original file line number Diff line number Diff line change
Expand Up @@ -391,13 +391,13 @@ impl<'a> MutObjectSequenceOp<'a> for PyList {

impl PyList {
const MAPPING_METHODS: PyMappingMethods = PyMappingMethods {
length: Some(|mapping, _vm| Ok(mapping.obj_as::<Self>().len())),
length: Some(|mapping, _vm| Ok(Self::mapping_downcast(mapping).len())),
subscript: Some(|mapping, needle, vm| {
let zelf = mapping.obj_as::<Self>();
let zelf = Self::mapping_downcast(mapping);
Self::getitem(zelf.to_owned(), needle.to_owned(), vm)
}),
ass_subscript: Some(|mapping, needle, value, vm| {
let zelf = mapping.obj_as::<Self>();
let zelf = Self::mapping_downcast(mapping);
if let Some(value) = value {
zelf.setitem(needle.to_owned(), value, vm)
} else {
Expand Down
2 changes: 1 addition & 1 deletion vm/src/builtins/mappingproxy.rs
Original file line number Diff line number Diff line change
Expand Up @@ -159,7 +159,7 @@ impl PyMappingProxy {
const MAPPING_METHODS: PyMappingMethods = PyMappingMethods {
length: None,
subscript: Some(|mapping, needle, vm| {
mapping.obj_as::<Self>().getitem(needle.to_owned(), vm)
Self::mapping_downcast(mapping).getitem(needle.to_owned(), vm)
}),
ass_subscript: None,
};
Expand Down
6 changes: 3 additions & 3 deletions vm/src/builtins/memory.rs
Original file line number Diff line number Diff line change
Expand Up @@ -940,13 +940,13 @@ impl Drop for PyMemoryView {

impl PyMemoryView {
const MAPPING_METHODS: PyMappingMethods = PyMappingMethods {
length: Some(|mapping, vm| mapping.obj_as::<Self>().len(vm)),
length: Some(|mapping, vm| Self::mapping_downcast(mapping).len(vm)),
subscript: Some(|mapping, needle, vm| {
let zelf = mapping.obj_as::<Self>();
let zelf = Self::mapping_downcast(mapping);
Self::getitem(zelf.to_owned(), needle.to_owned(), vm)
}),
ass_subscript: Some(|mapping, needle, value, vm| {
let zelf = mapping.obj_as::<Self>();
let zelf = Self::mapping_downcast(mapping);
if let Some(value) = value {
Self::setitem(zelf.to_owned(), needle.to_owned(), value, vm)
} else {
Expand Down
5 changes: 2 additions & 3 deletions vm/src/builtins/pystr.rs
Original file line number Diff line number Diff line change
Expand Up @@ -1266,10 +1266,9 @@ impl AsMapping for PyStr {

impl PyStr {
const MAPPING_METHODS: PyMappingMethods = PyMappingMethods {
length: Some(|mapping, _vm| Ok(mapping.obj_as::<Self>().len())),
length: Some(|mapping, _vm| Ok(Self::mapping_downcast(mapping).len())),
subscript: Some(|mapping, needle, vm| {
mapping
.obj_as::<Self>()
Self::mapping_downcast(mapping)
.getitem(needle.to_owned(), vm)
.map(|x| x.into_ref(vm).into())
}),
Expand Down
13 changes: 9 additions & 4 deletions vm/src/builtins/range.rs
Original file line number Diff line number Diff line change
Expand Up @@ -380,12 +380,17 @@ impl PyRange {
impl PyRange {
const MAPPING_METHODS: PyMappingMethods = PyMappingMethods {
length: Some(|mapping, vm| {
mapping.obj_as::<Self>().len().to_usize().ok_or_else(|| {
vm.new_overflow_error("RustPython int too large to convert to C ssize_t".to_owned())
})
Self::mapping_downcast(mapping)
.len()
.to_usize()
.ok_or_else(|| {
vm.new_overflow_error(
"RustPython int too large to convert to C ssize_t".to_owned(),
)
})
}),
subscript: Some(|mapping, needle, vm| {
mapping.obj_as::<Self>().getitem(needle.to_owned(), vm)
Self::mapping_downcast(mapping).getitem(needle.to_owned(), vm)
}),
ass_subscript: None,
};
Expand Down
4 changes: 2 additions & 2 deletions vm/src/builtins/tuple.rs
Original file line number Diff line number Diff line change
Expand Up @@ -310,9 +310,9 @@ impl PyTuple {

impl PyTuple {
const MAPPING_METHODS: PyMappingMethods = PyMappingMethods {
length: Some(|mapping, _vm| Ok(mapping.obj_as::<Self>().len())),
length: Some(|mapping, _vm| Ok(Self::mapping_downcast(mapping).len())),
subscript: Some(|mapping, needle, vm| {
let zelf = mapping.obj_as::<Self>();
let zelf = Self::mapping_downcast(mapping);
Self::getitem(zelf.to_owned(), needle.to_owned(), vm)
}),
ass_subscript: None,
Expand Down
7 changes: 1 addition & 6 deletions vm/src/protocol/mapping.rs
Original file line number Diff line number Diff line change
Expand Up @@ -5,8 +5,7 @@ use crate::{
},
common::lock::OnceCell,
function::IntoPyResult,
IdProtocol, PyObject, PyObjectPayload, PyObjectRef, PyObjectView, PyResult, TypeProtocol,
VirtualMachine,
IdProtocol, PyObject, PyObjectRef, PyResult, TypeProtocol, VirtualMachine,
};

// Mapping protocol
Expand Down Expand Up @@ -65,10 +64,6 @@ impl PyMapping<'_> {
self.methods(vm).subscript.is_some()
}

pub fn obj_as<T: PyObjectPayload>(&self) -> &PyObjectView<T> {
unsafe { self.obj.downcast_unchecked_ref::<T>() }
}

pub fn methods(&self, vm: &VirtualMachine) -> &PyMappingMethods {
self.methods.get_or_init(|| {
if let Some(f) = self
Expand Down
6 changes: 5 additions & 1 deletion vm/src/types/slot.rs
Original file line number Diff line number Diff line change
Expand Up @@ -2,7 +2,7 @@ use crate::common::{hash::PyHash, lock::PyRwLock};
use crate::{
builtins::{PyInt, PyStrRef, PyType, PyTypeRef},
function::{FromArgs, FuncArgs, IntoPyResult, OptionalArg},
protocol::{PyBuffer, PyIterReturn, PyMappingMethods},
protocol::{PyBuffer, PyIterReturn, PyMapping, PyMappingMethods},
utils::Either,
IdProtocol, PyComparisonValue, PyObject, PyObjectRef, PyObjectView, PyRef, PyResult, PyValue,
TypeProtocol, VirtualMachine,
Expand Down Expand Up @@ -783,6 +783,10 @@ pub trait AsMapping: PyValue {
}

fn as_mapping(zelf: &PyObjectView<Self>, vm: &VirtualMachine) -> PyMappingMethods;

fn mapping_downcast<'a>(mapping: &'a PyMapping) -> &'a PyObjectView<Self> {
unsafe { mapping.obj.downcast_unchecked_ref() }
}
}

#[pyimpl]
Expand Down

0 comments on commit 6a996b0

Please sign in to comment.