diff --git a/vm/src/builtins/bytearray.rs b/vm/src/builtins/bytearray.rs index abea39ba6..ca8a116d5 100644 --- a/vm/src/builtins/bytearray.rs +++ b/vm/src/builtins/bytearray.rs @@ -17,7 +17,6 @@ use crate::{ PyMappedRwLockReadGuard, PyMappedRwLockWriteGuard, PyMutex, PyRwLock, PyRwLockReadGuard, PyRwLockWriteGuard, }, - static_cell, }, function::{ArgBytesLike, ArgIterable, FuncArgs, IntoPyObject, OptionalArg, OptionalOption}, protocol::{ @@ -771,49 +770,49 @@ impl AsSequence for PyByteArray { _zelf: &PyObjectView, _vm: &VirtualMachine, ) -> Cow<'static, PySequenceMethods> { - static_cell! { - static METHODS: PySequenceMethods; - } - Cow::Borrowed(METHODS.get_or_init(|| PySequenceMethods { - length: Some(|seq, _vm| Ok(seq.obj_as::().len())), - concat: Some(|seq, other, vm| { - seq.obj_as::() - .inner() - .concat(other, vm) - .map(|x| PyByteArray::from(x).into_object(vm)) - }), - repeat: Some(|seq, n, vm| { - seq.obj_as::() - .mul(n as isize, vm) - .map(|x| x.into_object(vm)) - }), - item: Some(|seq, i, vm| seq.obj_as::().inner().item(i, vm)), - ass_item: Some(|seq, i, value, vm| { - let zelf = seq.obj_as::(); - if let Some(value) = value { - zelf.setitem_by_idx(i, value, vm) - } else { - zelf.delitem_by_idx(i, vm) - } - }), - contains: Some(|seq, other, vm| { - let other = - >::try_from_object(vm, other.to_owned())?; - seq.obj_as::().contains(other, vm) - }), - inplace_concat: Some(|seq, other, vm| { - let other = ArgBytesLike::try_from_object(vm, other.to_owned())?; - let zelf = seq.obj_as::().to_owned(); - Self::iadd(zelf, other, vm).map(|x| x.into()) - }), - inplace_repeat: Some(|seq, n, vm| { - let zelf = seq.obj_as::().to_owned(); - Self::imul(zelf, n as isize, vm).map(|x| x.into()) - }), - })) + Cow::Borrowed(&Self::SEQUENCE_METHODS) } } +impl PyByteArray { + const SEQUENCE_METHODS: PySequenceMethods = PySequenceMethods { + length: Some(|seq, _vm| Ok(seq.obj_as::().len())), + concat: Some(|seq, other, vm| { + seq.obj_as::() + .inner() + .concat(other, vm) + .map(|x| PyByteArray::from(x).into_object(vm)) + }), + repeat: Some(|seq, n, vm| { + seq.obj_as::() + .mul(n as isize, vm) + .map(|x| x.into_object(vm)) + }), + item: Some(|seq, i, vm| seq.obj_as::().inner().item(i, vm)), + ass_item: Some(|seq, i, value, vm| { + let zelf = seq.obj_as::(); + if let Some(value) = value { + zelf.setitem_by_idx(i, value, vm) + } else { + zelf.delitem_by_idx(i, vm) + } + }), + contains: Some(|seq, other, vm| { + let other = >::try_from_object(vm, other.to_owned())?; + seq.obj_as::().contains(other, vm) + }), + inplace_concat: Some(|seq, other, vm| { + let other = ArgBytesLike::try_from_object(vm, other.to_owned())?; + let zelf = seq.obj_as::().to_owned(); + Self::iadd(zelf, other, vm).map(|x| x.into()) + }), + inplace_repeat: Some(|seq, n, vm| { + let zelf = seq.obj_as::().to_owned(); + Self::imul(zelf, n as isize, vm).map(|x| x.into()) + }), + }; +} + impl Unhashable for PyByteArray {} impl Iterable for PyByteArray { diff --git a/vm/src/builtins/bytes.rs b/vm/src/builtins/bytes.rs index 0f01716f7..5f73ee3fa 100644 --- a/vm/src/builtins/bytes.rs +++ b/vm/src/builtins/bytes.rs @@ -6,7 +6,7 @@ use crate::{ bytes_decode, ByteInnerFindOptions, ByteInnerNewOptions, ByteInnerPaddingOptions, ByteInnerSplitOptions, ByteInnerTranslateOptions, DecodeArgs, PyBytesInner, }, - common::{hash::PyHash, lock::PyMutex, static_cell}, + common::{hash::PyHash, lock::PyMutex}, function::{ ArgBytesLike, ArgIterable, IntoPyObject, IntoPyResult, OptionalArg, OptionalOption, }, @@ -574,29 +574,29 @@ impl AsSequence for PyBytes { _zelf: &PyObjectView, _vm: &VirtualMachine, ) -> Cow<'static, PySequenceMethods> { - static_cell! { - static METHODS: PySequenceMethods; - } - Cow::Borrowed(METHODS.get_or_init(|| PySequenceMethods { - length: Some(|seq, _vm| Ok(seq.obj_as::().len())), - concat: Some(|seq, other, vm| { - seq.obj_as::() - .inner - .concat(other, vm) - .map(|x| vm.ctx.new_bytes(x).into()) - }), - repeat: Some(|seq, n, vm| Ok(vm.ctx.new_bytes(seq.obj_as::().repeat(n)).into())), - item: Some(|seq, i, vm| seq.obj_as::().inner.item(i, vm)), - contains: Some(|seq, other, vm| { - let other = - >::try_from_object(vm, other.to_owned())?; - seq.obj_as::().contains(other, vm) - }), - ..Default::default() - })) + Cow::Borrowed(&Self::SEQUENCE_METHODS) } } +impl PyBytes { + const SEQUENCE_METHODS: PySequenceMethods = PySequenceMethods { + length: Some(|seq, _vm| Ok(seq.obj_as::().len())), + concat: Some(|seq, other, vm| { + seq.obj_as::() + .inner + .concat(other, vm) + .map(|x| vm.ctx.new_bytes(x).into()) + }), + repeat: Some(|seq, n, vm| Ok(vm.ctx.new_bytes(seq.obj_as::().repeat(n)).into())), + item: Some(|seq, i, vm| seq.obj_as::().inner.item(i, vm)), + contains: Some(|seq, other, vm| { + let other = >::try_from_object(vm, other.to_owned())?; + seq.obj_as::().contains(other, vm) + }), + ..*PySequenceMethods::not_implemented() + }; +} + impl Hashable for PyBytes { #[inline] fn hash(zelf: &crate::PyObjectView, vm: &VirtualMachine) -> PyResult { diff --git a/vm/src/builtins/memory.rs b/vm/src/builtins/memory.rs index b0983b129..a97a8d3af 100644 --- a/vm/src/builtins/memory.rs +++ b/vm/src/builtins/memory.rs @@ -7,7 +7,6 @@ use crate::{ borrow::{BorrowedValue, BorrowedValueMut}, hash::PyHash, lock::OnceCell, - static_cell, }, function::{FuncArgs, IntoPyObject, OptionalArg}, protocol::{BufferDescriptor, BufferMethods, PyBuffer, PyMappingMethods, VecBuffer}, @@ -983,25 +982,26 @@ impl AsSequence for PyMemoryView { _zelf: &PyObjectView, _vm: &VirtualMachine, ) -> Cow<'static, PySequenceMethods> { - static_cell! { - static METHODS: PySequenceMethods; - } - Cow::Borrowed(METHODS.get_or_init(|| PySequenceMethods { - length: Some(|seq, vm| { - let zelf = seq.obj_as::(); - zelf.try_not_released(vm)?; - zelf.len(vm) - }), - item: Some(|seq, i, vm| { - let zelf = seq.obj_as::(); - zelf.try_not_released(vm)?; - zelf.getitem_by_idx(i, vm) - }), - ..Default::default() - })) + Cow::Borrowed(&Self::SEQUENCE_METHODS) } } +impl PyMemoryView { + const SEQUENCE_METHODS: PySequenceMethods = PySequenceMethods { + length: Some(|seq, vm| { + let zelf = seq.obj_as::(); + zelf.try_not_released(vm)?; + zelf.len(vm) + }), + item: Some(|seq, i, vm| { + let zelf = seq.obj_as::(); + zelf.try_not_released(vm)?; + zelf.getitem_by_idx(i, vm) + }), + ..*PySequenceMethods::not_implemented() + }; +} + impl Comparable for PyMemoryView { fn cmp( zelf: &crate::PyObjectView, diff --git a/vm/src/protocol/sequence.rs b/vm/src/protocol/sequence.rs index 347a09ec5..be375b514 100644 --- a/vm/src/protocol/sequence.rs +++ b/vm/src/protocol/sequence.rs @@ -5,7 +5,6 @@ use itertools::Itertools; use crate::{ builtins::{PyList, PySlice}, - common::static_cell, function::IntoPyObject, IdProtocol, PyArithmeticValue, PyObject, PyObjectPayload, PyObjectRef, PyObjectView, PyResult, PyValue, TryFromObject, TypeProtocol, VirtualMachine, @@ -29,11 +28,8 @@ pub struct PySequenceMethods { } impl PySequenceMethods { - pub fn not_implemented() -> &'static Self { - static_cell! { - static NOT_IMPLEMENTED: PySequenceMethods; - } - NOT_IMPLEMENTED.get_or_init(Self::default) + pub const fn not_implemented() -> &'static Self { + &NOT_IMPLEMENTED } } @@ -361,3 +357,14 @@ pub fn try_imul_for_inplace_repeat(a: &PyObject, n: usize, vm: &VirtualMachine) } Err(vm.new_type_error(format!("'{}' object can't be repeated", a.class().name()))) } + +const NOT_IMPLEMENTED: PySequenceMethods = PySequenceMethods { + length: None, + concat: None, + repeat: None, + item: None, + ass_item: None, + contains: None, + inplace_concat: None, + inplace_repeat: None, +};