Skip to content

Constify some methods and general code cleanups #5812

New issue

Have a question about this project? Sign up for a free GitHub account to open an issue and contact its maintainers and the community.

By clicking “Sign up for GitHub”, you agree to our terms of service and privacy statement. We’ll occasionally send you account related emails.

Already on GitHub? Sign in to your account

Merged
merged 4 commits into from
Jun 22, 2025
Merged
Show file tree
Hide file tree
Changes from all commits
Commits
File filter

Filter by extension

Filter by extension

Conversations
Failed to load comments.
Loading
Jump to
Jump to file
Failed to load files.
Loading
Diff view
Diff view
12 changes: 11 additions & 1 deletion vm/src/builtins/str.rs
Original file line number Diff line number Diff line change
Expand Up @@ -528,7 +528,7 @@
radd?.call((zelf,), vm)
} else {
Err(vm.new_type_error(format!(
"can only concatenate str (not \"{}\") to str",
r#"can only concatenate str (not "{}") to str"#,
other.class().name()
)))
}
Expand Down Expand Up @@ -570,11 +570,12 @@
hash => hash,
}
}

#[cold]
fn _compute_hash(&self, vm: &VirtualMachine) -> hash::PyHash {
let hash_val = vm.state.hash_secret.hash_bytes(self.as_bytes());
debug_assert_ne!(hash_val, hash::SENTINEL);
// like with char_len, we don't need a cmpxchg loop, since it'll always be the same value

Check warning on line 578 in vm/src/builtins/str.rs

View workflow job for this annotation

GitHub Actions / Check Rust code with rustfmt and clippy

Unknown word (cmpxchg)
self.hash.store(hash_val, atomic::Ordering::Relaxed);
hash_val
}
Expand All @@ -583,6 +584,7 @@
pub fn byte_len(&self) -> usize {
self.data.len()
}

#[inline]
pub fn is_empty(&self) -> bool {
self.data.is_empty()
Expand Down Expand Up @@ -1439,6 +1441,7 @@
struct CharLenStr<'a>(&'a str, usize);
impl std::ops::Deref for CharLenStr<'_> {
type Target = str;

fn deref(&self) -> &Self::Target {
self.0
}
Expand Down Expand Up @@ -1852,6 +1855,7 @@
fn as_ref(&self) -> Option<&Wtf8> {
Some(self.as_wtf8())
}

fn is_empty(&self) -> bool {
self.data.is_empty()
}
Expand All @@ -1861,6 +1865,7 @@
fn as_ref(&self) -> Option<&str> {
self.data.as_str()
}

fn is_empty(&self) -> bool {
self.data.is_empty()
}
Expand All @@ -1870,6 +1875,7 @@
fn as_ref(&self) -> Option<&AsciiStr> {
self.data.as_ascii()
}

fn is_empty(&self) -> bool {
self.data.is_empty()
}
Expand All @@ -1893,9 +1899,11 @@
fn is_lowercase(self) -> bool {
self.is_lowercase()
}

fn is_uppercase(self) -> bool {
self.is_uppercase()
}

fn bytes_len(self) -> usize {
self.len_utf8()
}
Expand Down Expand Up @@ -2122,9 +2130,11 @@
fn is_lowercase(self) -> bool {
self.is_lowercase()
}

fn is_uppercase(self) -> bool {
self.is_uppercase()
}

fn bytes_len(self) -> usize {
1
}
Expand Down Expand Up @@ -2250,8 +2260,8 @@
("Format This As Title String", "fOrMaT thIs aS titLe String"),
("Format,This-As*Title;String", "fOrMaT,thIs-aS*titLe;String"),
("Getint", "getInt"),
("Greek Ωppercases ...", "greek ωppercases ..."),

Check warning on line 2263 in vm/src/builtins/str.rs

View workflow job for this annotation

GitHub Actions / Check Rust code with rustfmt and clippy

Unknown word (ωppercases)

Check warning on line 2263 in vm/src/builtins/str.rs

View workflow job for this annotation

GitHub Actions / Check Rust code with rustfmt and clippy

Unknown word (Ωppercases)
("Greek ῼitlecases ...", "greek ῳitlecases ..."),

Check warning on line 2264 in vm/src/builtins/str.rs

View workflow job for this annotation

GitHub Actions / Check Rust code with rustfmt and clippy

Unknown word (ῳitlecases)

Check warning on line 2264 in vm/src/builtins/str.rs

View workflow job for this annotation

GitHub Actions / Check Rust code with rustfmt and clippy

Unknown word (ῼitlecases)
];
for (title, input) in tests {
assert_eq!(PyStr::from(input).title().as_str(), Ok(title));
Expand All @@ -2265,8 +2275,8 @@
"A Titlecased Line",
"A\nTitlecased Line",
"A Titlecased, Line",
"Greek Ωppercases ...",

Check warning on line 2278 in vm/src/builtins/str.rs

View workflow job for this annotation

GitHub Actions / Check Rust code with rustfmt and clippy

Unknown word (Ωppercases)
"Greek ῼitlecases ...",

Check warning on line 2279 in vm/src/builtins/str.rs

View workflow job for this annotation

GitHub Actions / Check Rust code with rustfmt and clippy

Unknown word (ῼitlecases)
];

for s in pos {
Expand Down
8 changes: 6 additions & 2 deletions vm/src/function/argument.rs
Original file line number Diff line number Diff line change
Expand Up @@ -118,8 +118,8 @@
{
// last `kwarg_names.len()` elements of args in order of appearance in the call signature
let total_argc = args.len();
let kwargc = kwarg_names.len();

Check warning on line 121 in vm/src/function/argument.rs

View workflow job for this annotation

GitHub Actions / Check Rust code with rustfmt and clippy

Unknown word (kwargc)
let posargc = total_argc - kwargc;

Check warning on line 122 in vm/src/function/argument.rs

View workflow job for this annotation

GitHub Actions / Check Rust code with rustfmt and clippy

Unknown word (kwargc)

let posargs = args.by_ref().take(posargc).collect();

Expand Down Expand Up @@ -302,12 +302,14 @@
type Inner: TryFromObject;
fn from_inner(x: Self::Inner) -> Self;
}

impl<T: TryFromObject> FromArgOptional for OptionalArg<T> {
type Inner = T;
fn from_inner(x: T) -> Self {
Self::Present(x)
}
}

impl<T: TryFromObject> FromArgOptional for T {
type Inner = Self;
fn from_inner(x: Self) -> Self {
Expand Down Expand Up @@ -342,7 +344,7 @@
}

impl<T> KwArgs<T> {
pub fn new(map: IndexMap<String, T>) -> Self {
pub const fn new(map: IndexMap<String, T>) -> Self {
KwArgs(map)
}

Expand All @@ -354,11 +356,13 @@
self.0.is_empty()
}
}

impl<T> FromIterator<(String, T)> for KwArgs<T> {
fn from_iter<I: IntoIterator<Item = (String, T)>>(iter: I) -> Self {
KwArgs(iter.into_iter().collect())
}
}

impl<T> Default for KwArgs<T> {
fn default() -> Self {
KwArgs(IndexMap::new())
Expand Down Expand Up @@ -408,7 +412,7 @@
}

impl<T> PosArgs<T> {
pub fn new(args: Vec<T>) -> Self {
pub const fn new(args: Vec<T>) -> Self {
Self(args)
}

Expand Down
8 changes: 4 additions & 4 deletions vm/src/function/buffer.rs
Original file line number Diff line number Diff line change
Expand Up @@ -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
}

Expand Down Expand Up @@ -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
}
}
Expand Down
1 change: 1 addition & 0 deletions vm/src/object/core.rs
Original file line number Diff line number Diff line change
Expand Up @@ -491,6 +491,7 @@ pub struct PyObject(PyInner<Erased>);

impl Deref for PyObjectRef {
type Target = PyObject;

#[inline(always)]
fn deref(&self) -> &PyObject {
unsafe { self.ptr.as_ref() }
Expand Down
4 changes: 4 additions & 0 deletions vm/src/object/ext.rs
Original file line number Diff line number Diff line change
Expand Up @@ -47,6 +47,7 @@ where
fmt::Display::fmt(&**self, f)
}
}

impl<T: fmt::Display> fmt::Display for Py<T>
where
T: PyObjectPayload + fmt::Display,
Expand All @@ -72,6 +73,7 @@ impl<T: PyPayload> PyExact<T> {

impl<T: PyPayload> Deref for PyExact<T> {
type Target = Py<T>;

#[inline(always)]
fn deref(&self) -> &Py<T> {
&self.inner
Expand Down Expand Up @@ -108,6 +110,7 @@ impl<T: PyObjectPayload> AsRef<Py<T>> for PyExact<T> {

impl<T: PyPayload> std::borrow::ToOwned for PyExact<T> {
type Owned = PyRefExact<T>;

fn to_owned(&self) -> Self::Owned {
let owned = self.inner.to_owned();
unsafe { PyRefExact::new_unchecked(owned) }
Expand Down Expand Up @@ -181,6 +184,7 @@ impl<T: PyPayload> TryFromObject for PyRefExact<T> {

impl<T: PyPayload> Deref for PyRefExact<T> {
type Target = PyExact<T>;

#[inline(always)]
fn deref(&self) -> &PyExact<T> {
unsafe { PyExact::ref_unchecked(self.inner.deref()) }
Expand Down
1 change: 1 addition & 0 deletions vm/src/object/traverse.rs
Original file line number Diff line number Diff line change
Expand Up @@ -158,6 +158,7 @@ unsafe impl<A: Traverse> 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));
Expand Down
7 changes: 1 addition & 6 deletions vm/src/protocol/buffer.rs
Original file line number Diff line number Diff line change
Expand Up @@ -378,15 +378,10 @@
}

pub fn is_zero_in_shape(&self) -> bool {
for (shape, _, _) in self.dim_desc.iter().cloned() {
if shape == 0 {
return true;
}
}
false
self.dim_desc.iter().any(|(shape, _, _)| *shape == 0)
}

// TODO: support fortain order

Check warning on line 384 in vm/src/protocol/buffer.rs

View workflow job for this annotation

GitHub Actions / Check Rust code with rustfmt and clippy

Unknown word (fortain)
}

pub trait BufferResizeGuard {
Expand Down
3 changes: 2 additions & 1 deletion vm/src/protocol/iter.rs
Original file line number Diff line number Diff line change
Expand Up @@ -107,6 +107,7 @@ where
O: Borrow<PyObject>,
{
type Target = PyObject;

#[inline(always)]
fn deref(&self) -> &Self::Target {
self.0.borrow()
Expand Down Expand Up @@ -242,7 +243,7 @@ impl<'a, T, O> PyIterIter<'a, T, O>
where
O: Borrow<PyObject>,
{
pub fn new(vm: &'a VirtualMachine, obj: O, length_hint: Option<usize>) -> Self {
pub const fn new(vm: &'a VirtualMachine, obj: O, length_hint: Option<usize>) -> Self {
Self {
vm,
obj,
Expand Down
4 changes: 2 additions & 2 deletions vm/src/protocol/number.rs
Original file line number Diff line number Diff line change
Expand Up @@ -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)
}

Expand Down Expand Up @@ -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
}

Expand Down
2 changes: 1 addition & 1 deletion vm/src/types/slot.rs
Original file line number Diff line number Diff line change
Expand Up @@ -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 => "<=",
Expand Down
Loading