From e6f5b6ad1a52d06be4e32a5495a0177497f81868 Mon Sep 17 00:00:00 2001 From: ShaharNaveh <50263213+ShaharNaveh@users.noreply.github.com> Date: Sat, 21 Jun 2025 13:08:22 +0300 Subject: [PATCH] constify a bunch of methods --- vm/src/function/argument.rs | 8 ++++++-- vm/src/function/buffer.rs | 8 ++++---- vm/src/object/core.rs | 1 + vm/src/object/ext.rs | 4 ++++ vm/src/object/traverse.rs | 1 + vm/src/protocol/number.rs | 4 ++-- vm/src/types/slot.rs | 2 +- 7 files changed, 19 insertions(+), 9 deletions(-) diff --git a/vm/src/function/argument.rs b/vm/src/function/argument.rs index 5033ee762..cf9d035bb 100644 --- a/vm/src/function/argument.rs +++ b/vm/src/function/argument.rs @@ -302,12 +302,14 @@ pub trait FromArgOptional { type Inner: TryFromObject; fn from_inner(x: Self::Inner) -> Self; } + impl FromArgOptional for OptionalArg { type Inner = T; fn from_inner(x: T) -> Self { Self::Present(x) } } + impl FromArgOptional for T { type Inner = Self; fn from_inner(x: Self) -> Self { @@ -342,7 +344,7 @@ where } impl KwArgs { - pub fn new(map: IndexMap) -> Self { + pub const fn new(map: IndexMap) -> Self { KwArgs(map) } @@ -354,11 +356,13 @@ impl KwArgs { self.0.is_empty() } } + impl FromIterator<(String, T)> for KwArgs { fn from_iter>(iter: I) -> Self { KwArgs(iter.into_iter().collect()) } } + impl Default for KwArgs { fn default() -> Self { KwArgs(IndexMap::new()) @@ -408,7 +412,7 @@ where } impl PosArgs { - pub fn new(args: Vec) -> Self { + pub const fn new(args: Vec) -> Self { Self(args) } diff --git a/vm/src/function/buffer.rs b/vm/src/function/buffer.rs index 40a0e04d7..b8ef77192 100644 --- a/vm/src/function/buffer.rs +++ b/vm/src/function/buffer.rs @@ -51,11 +51,11 @@ impl ArgBytesLike { f(&self.borrow_buf()) } - pub fn len(&self) -> usize { + pub const fn len(&self) -> usize { self.0.desc.len } - pub fn is_empty(&self) -> bool { + pub const fn is_empty(&self) -> bool { self.len() == 0 } @@ -103,11 +103,11 @@ impl ArgMemoryBuffer { f(&mut self.borrow_buf_mut()) } - pub fn len(&self) -> usize { + pub const fn len(&self) -> usize { self.0.desc.len } - pub fn is_empty(&self) -> bool { + pub const fn is_empty(&self) -> bool { self.len() == 0 } } diff --git a/vm/src/object/core.rs b/vm/src/object/core.rs index 8edcb4dfd..cc314cce6 100644 --- a/vm/src/object/core.rs +++ b/vm/src/object/core.rs @@ -491,6 +491,7 @@ pub struct PyObject(PyInner); impl Deref for PyObjectRef { type Target = PyObject; + #[inline(always)] fn deref(&self) -> &PyObject { unsafe { self.ptr.as_ref() } diff --git a/vm/src/object/ext.rs b/vm/src/object/ext.rs index b2bc6eec4..41443aa56 100644 --- a/vm/src/object/ext.rs +++ b/vm/src/object/ext.rs @@ -47,6 +47,7 @@ where fmt::Display::fmt(&**self, f) } } + impl fmt::Display for Py where T: PyObjectPayload + fmt::Display, @@ -72,6 +73,7 @@ impl PyExact { impl Deref for PyExact { type Target = Py; + #[inline(always)] fn deref(&self) -> &Py { &self.inner @@ -108,6 +110,7 @@ impl AsRef> for PyExact { impl std::borrow::ToOwned for PyExact { type Owned = PyRefExact; + fn to_owned(&self) -> Self::Owned { let owned = self.inner.to_owned(); unsafe { PyRefExact::new_unchecked(owned) } @@ -181,6 +184,7 @@ impl TryFromObject for PyRefExact { impl Deref for PyRefExact { type Target = PyExact; + #[inline(always)] fn deref(&self) -> &PyExact { unsafe { PyExact::ref_unchecked(self.inner.deref()) } diff --git a/vm/src/object/traverse.rs b/vm/src/object/traverse.rs index 46e5daff0..3e2efefdf 100644 --- a/vm/src/object/traverse.rs +++ b/vm/src/object/traverse.rs @@ -158,6 +158,7 @@ unsafe impl Traverse for (A,) { self.0.traverse(tracer_fn); } } + trace_tuple!((A, 0), (B, 1)); trace_tuple!((A, 0), (B, 1), (C, 2)); trace_tuple!((A, 0), (B, 1), (C, 2), (D, 3)); diff --git a/vm/src/protocol/number.rs b/vm/src/protocol/number.rs index 94c4f3979..0a05cbfb8 100644 --- a/vm/src/protocol/number.rs +++ b/vm/src/protocol/number.rs @@ -18,7 +18,7 @@ pub type PyNumberTernaryFunc = fn(&PyObject, &PyObject, &PyObject, &VirtualMachi impl PyObject { #[inline] - pub fn to_number(&self) -> PyNumber<'_> { + pub const fn to_number(&self) -> PyNumber<'_> { PyNumber(self) } @@ -440,7 +440,7 @@ impl Deref for PyNumber<'_> { } impl<'a> PyNumber<'a> { - pub(crate) fn obj(self) -> &'a PyObject { + pub(crate) const fn obj(self) -> &'a PyObject { self.0 } diff --git a/vm/src/types/slot.rs b/vm/src/types/slot.rs index e2121973e..7c92dbd92 100644 --- a/vm/src/types/slot.rs +++ b/vm/src/types/slot.rs @@ -1116,7 +1116,7 @@ impl PyComparisonOp { } } - pub fn operator_token(self) -> &'static str { + pub const fn operator_token(self) -> &'static str { match self { Self::Lt => "<", Self::Le => "<=",