|
1 | 1 | use super::base::CDATA_BUFFER_METHODS; |
2 | 2 | use super::{PyCArray, PyCData, PyCSimple, PyCStructure, StgInfo, StgInfoFlags}; |
3 | | -use crate::protocol::{BufferDescriptor, PyBuffer, PyNumberMethods}; |
4 | | -use crate::types::{AsBuffer, AsNumber, Constructor, Initializer}; |
| 3 | +use crate::atomic_func; |
| 4 | +use crate::protocol::{BufferDescriptor, PyBuffer, PyMappingMethods, PyNumberMethods}; |
| 5 | +use crate::types::{AsBuffer, AsMapping, AsNumber, Constructor, Initializer}; |
5 | 6 | use crate::{ |
6 | 7 | AsObject, Py, PyObject, PyObjectRef, PyPayload, PyRef, PyResult, VirtualMachine, |
7 | 8 | builtins::{PyBytes, PyInt, PyList, PySlice, PyStr, PyType, PyTypeRef}, |
@@ -260,7 +261,7 @@ impl Initializer for PyCPointer { |
260 | 261 |
|
261 | 262 | #[pyclass( |
262 | 263 | flags(BASETYPE, IMMUTABLETYPE), |
263 | | - with(Constructor, Initializer, AsNumber, AsBuffer) |
| 264 | + with(Constructor, Initializer, AsNumber, AsBuffer, AsMapping) |
264 | 265 | )] |
265 | 266 | impl PyCPointer { |
266 | 267 | /// Get the pointer value stored in buffer as usize |
@@ -785,6 +786,27 @@ impl AsNumber for PyCPointer { |
785 | 786 | } |
786 | 787 | } |
787 | 788 |
|
| 789 | +impl AsMapping for PyCPointer { |
| 790 | + fn as_mapping() -> &'static PyMappingMethods { |
| 791 | + use std::sync::LazyLock; |
| 792 | + static AS_MAPPING: LazyLock<PyMappingMethods> = LazyLock::new(|| PyMappingMethods { |
| 793 | + subscript: atomic_func!(|mapping, needle, vm| { |
| 794 | + let zelf = PyCPointer::mapping_downcast(mapping); |
| 795 | + PyCPointer::__getitem__(zelf, needle.to_owned(), vm) |
| 796 | + }), |
| 797 | + ass_subscript: atomic_func!(|mapping, needle, value, vm| { |
| 798 | + let zelf = PyCPointer::mapping_downcast(mapping); |
| 799 | + match value { |
| 800 | + Some(value) => PyCPointer::__setitem__(zelf, needle.to_owned(), value, vm), |
| 801 | + None => Err(vm.new_type_error("Pointer does not support item deletion")), |
| 802 | + } |
| 803 | + }), |
| 804 | + ..PyMappingMethods::NOT_IMPLEMENTED |
| 805 | + }); |
| 806 | + &AS_MAPPING |
| 807 | + } |
| 808 | +} |
| 809 | + |
788 | 810 | impl AsBuffer for PyCPointer { |
789 | 811 | fn as_buffer(zelf: &Py<Self>, _vm: &VirtualMachine) -> PyResult<PyBuffer> { |
790 | 812 | let stg_info = zelf |
|
0 commit comments