constant sequence methods

This commit is contained in:
Kangzhi Shi
2021-11-13 21:24:02 +02:00
committed by Jeong YunWon
parent 937375ad5b
commit 410ca13743
4 changed files with 91 additions and 85 deletions

View File

@@ -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<Self>,
_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::<Self>().len())),
concat: Some(|seq, other, vm| {
seq.obj_as::<Self>()
.inner()
.concat(other, vm)
.map(|x| PyByteArray::from(x).into_object(vm))
}),
repeat: Some(|seq, n, vm| {
seq.obj_as::<Self>()
.mul(n as isize, vm)
.map(|x| x.into_object(vm))
}),
item: Some(|seq, i, vm| seq.obj_as::<Self>().inner().item(i, vm)),
ass_item: Some(|seq, i, value, vm| {
let zelf = seq.obj_as::<Self>();
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 =
<Either<PyBytesInner, PyIntRef>>::try_from_object(vm, other.to_owned())?;
seq.obj_as::<Self>().contains(other, vm)
}),
inplace_concat: Some(|seq, other, vm| {
let other = ArgBytesLike::try_from_object(vm, other.to_owned())?;
let zelf = seq.obj_as::<Self>().to_owned();
Self::iadd(zelf, other, vm).map(|x| x.into())
}),
inplace_repeat: Some(|seq, n, vm| {
let zelf = seq.obj_as::<Self>().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::<Self>().len())),
concat: Some(|seq, other, vm| {
seq.obj_as::<Self>()
.inner()
.concat(other, vm)
.map(|x| PyByteArray::from(x).into_object(vm))
}),
repeat: Some(|seq, n, vm| {
seq.obj_as::<Self>()
.mul(n as isize, vm)
.map(|x| x.into_object(vm))
}),
item: Some(|seq, i, vm| seq.obj_as::<Self>().inner().item(i, vm)),
ass_item: Some(|seq, i, value, vm| {
let zelf = seq.obj_as::<Self>();
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 = <Either<PyBytesInner, PyIntRef>>::try_from_object(vm, other.to_owned())?;
seq.obj_as::<Self>().contains(other, vm)
}),
inplace_concat: Some(|seq, other, vm| {
let other = ArgBytesLike::try_from_object(vm, other.to_owned())?;
let zelf = seq.obj_as::<Self>().to_owned();
Self::iadd(zelf, other, vm).map(|x| x.into())
}),
inplace_repeat: Some(|seq, n, vm| {
let zelf = seq.obj_as::<Self>().to_owned();
Self::imul(zelf, n as isize, vm).map(|x| x.into())
}),
};
}
impl Unhashable for PyByteArray {}
impl Iterable for PyByteArray {

View File

@@ -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<Self>,
_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::<Self>().len())),
concat: Some(|seq, other, vm| {
seq.obj_as::<Self>()
.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::<Self>().repeat(n)).into())),
item: Some(|seq, i, vm| seq.obj_as::<Self>().inner.item(i, vm)),
contains: Some(|seq, other, vm| {
let other =
<Either<PyBytesInner, PyIntRef>>::try_from_object(vm, other.to_owned())?;
seq.obj_as::<Self>().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::<Self>().len())),
concat: Some(|seq, other, vm| {
seq.obj_as::<Self>()
.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::<Self>().repeat(n)).into())),
item: Some(|seq, i, vm| seq.obj_as::<Self>().inner.item(i, vm)),
contains: Some(|seq, other, vm| {
let other = <Either<PyBytesInner, PyIntRef>>::try_from_object(vm, other.to_owned())?;
seq.obj_as::<Self>().contains(other, vm)
}),
..*PySequenceMethods::not_implemented()
};
}
impl Hashable for PyBytes {
#[inline]
fn hash(zelf: &crate::PyObjectView<Self>, vm: &VirtualMachine) -> PyResult<PyHash> {

View File

@@ -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<Self>,
_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::<Self>();
zelf.try_not_released(vm)?;
zelf.len(vm)
}),
item: Some(|seq, i, vm| {
let zelf = seq.obj_as::<Self>();
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::<Self>();
zelf.try_not_released(vm)?;
zelf.len(vm)
}),
item: Some(|seq, i, vm| {
let zelf = seq.obj_as::<Self>();
zelf.try_not_released(vm)?;
zelf.getitem_by_idx(i, vm)
}),
..*PySequenceMethods::not_implemented()
};
}
impl Comparable for PyMemoryView {
fn cmp(
zelf: &crate::PyObjectView<Self>,

View File

@@ -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,
};