diff --git a/derive/src/pystructseq.rs b/derive/src/pystructseq.rs index 357c9af36..f86ffc30b 100644 --- a/derive/src/pystructseq.rs +++ b/derive/src/pystructseq.rs @@ -36,7 +36,7 @@ pub(crate) fn impl_pystruct_sequence( self.#field_names, vm, )),*]; - items.into_boxed_slice().into() + ::rustpython_vm::obj::objtuple::PyTuple::_new(items.into_boxed_slice()) } } }; diff --git a/vm/src/obj/objtuple.rs b/vm/src/obj/objtuple.rs index 7a114d646..c6b5abc7e 100644 --- a/vm/src/obj/objtuple.rs +++ b/vm/src/obj/objtuple.rs @@ -30,12 +30,6 @@ impl fmt::Debug for PyTuple { } } -impl From> for PyTuple { - fn from(elements: Box<[PyObjectRef]>) -> Self { - PyTuple { elements } - } -} - impl<'a> BorrowValue<'a> for PyTuple { type Borrowed = &'a [PyObjectRef]; @@ -93,6 +87,13 @@ pub(crate) fn get_value(obj: &PyObjectRef) -> &[PyObjectRef] { #[pyimpl(flags(BASETYPE), with(Hashable, Comparable))] impl PyTuple { + /// Creating a new tuple with given boxed slice. + /// NOTE: for usual case, you probably want to use PyTupleRef::with_elements. + /// Calling this function implies trying micro optimization for non-zero-sized tuple. + pub(crate) fn _new(elements: Box<[PyObjectRef]>) -> Self { + Self { elements } + } + #[pymethod(name = "__add__")] fn add( zelf: PyRef, diff --git a/vm/src/obj/objtype.rs b/vm/src/obj/objtype.rs index 8d1601807..3b1c7410d 100644 --- a/vm/src/obj/objtype.rs +++ b/vm/src/obj/objtype.rs @@ -184,7 +184,7 @@ impl PyClass { #[pyproperty(name = "__mro__")] fn get_mro(zelf: PyRef) -> PyTuple { let elements: Vec = zelf.iter_mro().map(|x| x.as_object().clone()).collect(); - elements.into_boxed_slice().into() + PyTuple::_new(elements.into_boxed_slice()) } #[pyproperty(magic)] diff --git a/vm/src/pyobject.rs b/vm/src/pyobject.rs index 16826cddd..c9e1832d4 100644 --- a/vm/src/pyobject.rs +++ b/vm/src/pyobject.rs @@ -140,7 +140,7 @@ impl PyContext { let false_value = create_object(PyInt::from(0), &types.bool_type); let empty_tuple = create_object( - PyTuple::from(Vec::new().into_boxed_slice()), + PyTuple::_new(Vec::new().into_boxed_slice()), &types.tuple_type, );