Skip to content
Closed
Show file tree
Hide file tree
Changes from 1 commit
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
Prev Previous commit
Next Next commit
* Used clippy --fix to fix issues in vm
* Imported Range in vm/src/anystr.rs
  • Loading branch information
terryluan12 committed Dec 29, 2025
commit 37a0fe091296fa7a9b2adcd09d71aeda15ce47f0
22 changes: 12 additions & 10 deletions crates/vm/src/anystr.rs
Original file line number Diff line number Diff line change
Expand Up @@ -6,6 +6,8 @@ use crate::{
};
use num_traits::{cast::ToPrimitive, sign::Signed};

use core::ops::Range;

#[derive(FromArgs)]
pub struct SplitArgs<T: TryFromObject> {
#[pyarg(any, default)]
Expand Down Expand Up @@ -43,7 +45,7 @@ pub struct StartsEndsWithArgs {
}

impl StartsEndsWithArgs {
pub fn get_value(self, len: usize) -> (PyObjectRef, Option<std::ops::Range<usize>>) {
pub fn get_value(self, len: usize) -> (PyObjectRef, Option<Range<usize>>) {
let range = if self.start.is_some() || self.end.is_some() {
Some(adjust_indices(self.start, self.end, len))
} else {
Expand All @@ -56,7 +58,7 @@ impl StartsEndsWithArgs {
pub fn prepare<S, F>(self, s: &S, len: usize, substr: F) -> Option<(PyObjectRef, &S)>
where
S: ?Sized + AnyStr,
F: Fn(&S, std::ops::Range<usize>) -> &S,
F: Fn(&S, Range<usize>) -> &S,
{
let (affix, range) = self.get_value(len);
let substr = if let Some(range) = range {
Expand Down Expand Up @@ -87,7 +89,7 @@ pub fn adjust_indices(
start: Option<PyIntRef>,
end: Option<PyIntRef>,
len: usize,
) -> std::ops::Range<usize> {
) -> Range<usize> {
let mut start = start.map_or(0, saturate_to_isize);
let mut end = end.map_or(len as isize, saturate_to_isize);
if end > len as isize {
Expand All @@ -111,7 +113,7 @@ pub trait StringRange {
fn is_normal(&self) -> bool;
}

impl StringRange for std::ops::Range<usize> {
impl StringRange for Range<usize> {
fn is_normal(&self) -> bool {
self.start <= self.end
}
Expand Down Expand Up @@ -144,9 +146,9 @@ pub trait AnyStr {
fn to_container(&self) -> Self::Container;
fn as_bytes(&self) -> &[u8];
fn elements(&self) -> impl Iterator<Item = Self::Char>;
fn get_bytes(&self, range: std::ops::Range<usize>) -> &Self;
fn get_bytes(&self, range: Range<usize>) -> &Self;
// FIXME: get_chars is expensive for str
fn get_chars(&self, range: std::ops::Range<usize>) -> &Self;
fn get_chars(&self, range: Range<usize>) -> &Self;
fn bytes_len(&self) -> usize;
// NOTE: str::chars().count() consumes the O(n) time. But pystr::char_len does cache.
// So using chars_len directly is too expensive and the below method shouldn't be implemented.
Expand Down Expand Up @@ -254,7 +256,7 @@ pub trait AnyStr {
}

#[inline]
fn py_find<F>(&self, needle: &Self, range: std::ops::Range<usize>, find: F) -> Option<usize>
fn py_find<F>(&self, needle: &Self, range: Range<usize>, find: F) -> Option<usize>
where
F: Fn(&Self, &Self) -> Option<usize>,
{
Expand All @@ -268,7 +270,7 @@ pub trait AnyStr {
}

#[inline]
fn py_count<F>(&self, needle: &Self, range: std::ops::Range<usize>, count: F) -> usize
fn py_count<F>(&self, needle: &Self, range: Range<usize>, count: F) -> usize
where
F: Fn(&Self, &Self) -> usize,
{
Expand All @@ -283,9 +285,9 @@ pub trait AnyStr {
let mut u = Self::Container::with_capacity(
(left + right) * fillchar.bytes_len() + self.bytes_len(),
);
u.extend(std::iter::repeat_n(fillchar, left));
u.extend(core::iter::repeat_n(fillchar, left));
u.push_str(self);
u.extend(std::iter::repeat_n(fillchar, right));
u.extend(core::iter::repeat_n(fillchar, right));
u
}

Expand Down
8 changes: 4 additions & 4 deletions crates/vm/src/buffer.rs
Original file line number Diff line number Diff line change
Expand Up @@ -545,7 +545,7 @@ macro_rules! make_pack_prim_int {
}
#[inline]
fn unpack_int<E: ByteOrder>(data: &[u8]) -> Self {
let mut x = [0; std::mem::size_of::<$T>()];
let mut x = [0; core::mem::size_of::<$T>()];
x.copy_from_slice(data);
E::convert(<$T>::from_ne_bytes(x))
}
Expand Down Expand Up @@ -681,15 +681,15 @@ fn pack_pascal(vm: &VirtualMachine, arg: PyObjectRef, buf: &mut [u8]) -> PyResul
}
let b = ArgBytesLike::try_from_object(vm, arg)?;
b.with_ref(|data| {
let string_length = std::cmp::min(std::cmp::min(data.len(), 255), buf.len() - 1);
let string_length = core::cmp::min(core::cmp::min(data.len(), 255), buf.len() - 1);
buf[0] = string_length as u8;
write_string(&mut buf[1..], data);
});
Ok(())
}

fn write_string(buf: &mut [u8], data: &[u8]) {
let len_from_data = std::cmp::min(data.len(), buf.len());
let len_from_data = core::cmp::min(data.len(), buf.len());
buf[..len_from_data].copy_from_slice(&data[..len_from_data]);
for byte in &mut buf[len_from_data..] {
*byte = 0
Expand All @@ -708,7 +708,7 @@ fn unpack_pascal(vm: &VirtualMachine, data: &[u8]) -> PyObjectRef {
return vm.ctx.new_bytes(vec![]).into();
}
};
let len = std::cmp::min(len as usize, data.len());
let len = core::cmp::min(len as usize, data.len());
vm.ctx.new_bytes(data[..len].to_vec()).into()
}

Expand Down
2 changes: 1 addition & 1 deletion crates/vm/src/builtins/bool.rs
Original file line number Diff line number Diff line change
Expand Up @@ -10,7 +10,7 @@ use crate::{
};
use malachite_bigint::Sign;
use num_traits::Zero;
use std::fmt::{Debug, Formatter};
use core::fmt::{Debug, Formatter};

impl ToPyObject for bool {
fn to_pyobject(self, vm: &VirtualMachine) -> PyObjectRef {
Expand Down
4 changes: 2 additions & 2 deletions crates/vm/src/builtins/builtin_func.rs
Original file line number Diff line number Diff line change
Expand Up @@ -7,7 +7,7 @@ use crate::{
function::{FuncArgs, PyComparisonValue, PyMethodDef, PyMethodFlags, PyNativeFn},
types::{Callable, Comparable, PyComparisonOp, Representable},
};
use std::fmt;
use alloc::fmt;

// PyCFunctionObject in CPython
#[pyclass(name = "builtin_function_or_method", module = false)]
Expand Down Expand Up @@ -212,7 +212,7 @@ impl Comparable for PyNativeMethod {
(None, None) => true,
_ => false,
};
let eq = eq && std::ptr::eq(zelf.func.value, other.func.value);
let eq = eq && core::ptr::eq(zelf.func.value, other.func.value);
Ok(eq.into())
} else {
Ok(PyComparisonValue::NotImplemented)
Expand Down
4 changes: 2 additions & 2 deletions crates/vm/src/builtins/bytearray.rs
Original file line number Diff line number Diff line change
Expand Up @@ -37,7 +37,7 @@ use crate::{
},
};
use bstr::ByteSlice;
use std::mem::size_of;
use core::mem::size_of;

#[pyclass(module = false, name = "bytearray", unhashable = true)]
#[derive(Debug, Default)]
Expand Down Expand Up @@ -687,7 +687,7 @@ impl Initializer for PyByteArray {
fn init(zelf: PyRef<Self>, options: Self::Args, vm: &VirtualMachine) -> PyResult<()> {
// First unpack bytearray and *then* get a lock to set it.
let mut inner = options.get_bytearray_inner(vm)?;
std::mem::swap(&mut *zelf.inner_mut(), &mut inner);
core::mem::swap(&mut *zelf.inner_mut(), &mut inner);
Ok(())
}
}
Expand Down
2 changes: 1 addition & 1 deletion crates/vm/src/builtins/bytes.rs
Original file line number Diff line number Diff line change
Expand Up @@ -30,7 +30,7 @@ use crate::{
};
use bstr::ByteSlice;
use std::sync::LazyLock;
use std::{mem::size_of, ops::Deref};
use core::{mem::size_of, ops::Deref};

#[pyclass(module = false, name = "bytes")]
#[derive(Clone, Debug)]
Expand Down
2 changes: 1 addition & 1 deletion crates/vm/src/builtins/code.rs
Original file line number Diff line number Diff line change
Expand Up @@ -601,7 +601,7 @@ impl PyCode {
pub fn co_code(&self, vm: &VirtualMachine) -> crate::builtins::PyBytesRef {
// SAFETY: CodeUnit is #[repr(C)] with size 2, so we can safely transmute to bytes
let bytes = unsafe {
std::slice::from_raw_parts(
core::slice::from_raw_parts(
self.code.instructions.as_ptr() as *const u8,
self.code.instructions.len() * 2,
)
Expand Down
2 changes: 1 addition & 1 deletion crates/vm/src/builtins/complex.rs
Original file line number Diff line number Diff line change
Expand Up @@ -13,7 +13,7 @@ use crate::{
use num_complex::Complex64;
use num_traits::Zero;
use rustpython_common::hash;
use std::num::Wrapping;
use core::num::Wrapping;

/// Create a complex number from a real part and an optional imaginary part.
///
Expand Down
4 changes: 2 additions & 2 deletions crates/vm/src/builtins/dict.rs
Original file line number Diff line number Diff line change
Expand Up @@ -24,7 +24,7 @@ use crate::{
vm::VirtualMachine,
};
use rustpython_common::lock::PyMutex;
use std::fmt;
use alloc::fmt;
use std::sync::LazyLock;

pub type DictContentType = dict_inner::Dict;
Expand Down Expand Up @@ -219,7 +219,7 @@ impl PyDict {

#[pymethod]
fn __sizeof__(&self) -> usize {
std::mem::size_of::<Self>() + self.entries.sizeof()
core::mem::size_of::<Self>() + self.entries.sizeof()
}

#[pymethod]
Expand Down
4 changes: 2 additions & 2 deletions crates/vm/src/builtins/function.rs
Original file line number Diff line number Diff line change
Expand Up @@ -149,7 +149,7 @@ impl PyFunction {
None
};

let arg_pos = |range: std::ops::Range<_>, name: &str| {
let arg_pos = |range: core::ops::Range<_>, name: &str| {
code.varnames
.iter()
.enumerate()
Expand Down Expand Up @@ -255,7 +255,7 @@ impl PyFunction {
}

if let Some(defaults) = defaults {
let n = std::cmp::min(nargs, n_expected_args);
let n = core::cmp::min(nargs, n_expected_args);
let i = n.saturating_sub(n_required);

// We have sufficient defaults, so iterate over the corresponding names and use
Expand Down
2 changes: 1 addition & 1 deletion crates/vm/src/builtins/genericalias.rs
Original file line number Diff line number Diff line change
Expand Up @@ -16,7 +16,7 @@ use crate::{
PyComparisonOp, Representable,
},
};
use std::fmt;
use alloc::fmt;

// attr_exceptions
static ATTR_EXCEPTIONS: [&str; 12] = [
Expand Down
2 changes: 1 addition & 1 deletion crates/vm/src/builtins/getset.rs
Original file line number Diff line number Diff line change
Expand Up @@ -158,7 +158,7 @@ impl Representable for PyGetSet {
fn repr_str(zelf: &Py<Self>, vm: &VirtualMachine) -> PyResult<String> {
let class = unsafe { zelf.class.borrow_static() };
// Special case for object type
if std::ptr::eq(class, vm.ctx.types.object_type) {
if core::ptr::eq(class, vm.ctx.types.object_type) {
Ok(format!("<attribute '{}'>", zelf.name))
} else {
Ok(format!(
Expand Down
8 changes: 4 additions & 4 deletions crates/vm/src/builtins/int.rs
Original file line number Diff line number Diff line change
Expand Up @@ -20,8 +20,8 @@ use crate::{
use malachite_bigint::{BigInt, Sign};
use num_integer::Integer;
use num_traits::{One, Pow, PrimInt, Signed, ToPrimitive, Zero};
use std::fmt;
use std::ops::{Neg, Not};
use alloc::fmt;
use core::ops::{Neg, Not};

#[pyclass(module = false, name = "int")]
#[derive(Debug)]
Expand Down Expand Up @@ -289,7 +289,7 @@ impl PyInt {
I::try_from(self.as_bigint()).map_err(|_| {
vm.new_overflow_error(format!(
"Python int too large to convert to Rust {}",
std::any::type_name::<I>()
core::any::type_name::<I>()
))
})
}
Expand Down Expand Up @@ -444,7 +444,7 @@ impl PyInt {

#[pymethod]
fn __sizeof__(&self) -> usize {
std::mem::size_of::<Self>() + (((self.value.bits() + 7) & !7) / 8) as usize
core::mem::size_of::<Self>() + (((self.value.bits() + 7) & !7) / 8) as usize
}

#[pymethod]
Expand Down
14 changes: 7 additions & 7 deletions crates/vm/src/builtins/list.rs
Original file line number Diff line number Diff line change
Expand Up @@ -172,7 +172,7 @@ impl PyList {

#[pymethod]
fn clear(&self) {
let _removed = std::mem::take(self.borrow_vec_mut().deref_mut());
let _removed = core::mem::take(self.borrow_vec_mut().deref_mut());
}

#[pymethod]
Expand All @@ -188,8 +188,8 @@ impl PyList {

#[pymethod]
fn __sizeof__(&self) -> usize {
std::mem::size_of::<Self>()
+ self.elements.read().capacity() * std::mem::size_of::<PyObjectRef>()
core::mem::size_of::<Self>()
+ self.elements.read().capacity() * core::mem::size_of::<PyObjectRef>()
}

#[pymethod]
Expand Down Expand Up @@ -324,9 +324,9 @@ impl PyList {
// replace list contents with [] for duration of sort.
// this prevents keyfunc from messing with the list and makes it easy to
// check if it tries to append elements to it.
let mut elements = std::mem::take(self.borrow_vec_mut().deref_mut());
let mut elements = core::mem::take(self.borrow_vec_mut().deref_mut());
let res = do_sort(vm, &mut elements, options.key, options.reverse);
std::mem::swap(self.borrow_vec_mut().deref_mut(), &mut elements);
core::mem::swap(self.borrow_vec_mut().deref_mut(), &mut elements);
res?;

if !elements.is_empty() {
Expand Down Expand Up @@ -375,7 +375,7 @@ impl MutObjectSequenceOp for PyList {
inner.get(index).map(|r| r.as_ref())
}

fn do_lock(&self) -> impl std::ops::Deref<Target = [PyObjectRef]> {
fn do_lock(&self) -> impl core::ops::Deref<Target = [PyObjectRef]> {
self.borrow_vec()
}
}
Expand All @@ -397,7 +397,7 @@ impl Initializer for PyList {
} else {
vec![]
};
std::mem::swap(zelf.borrow_vec_mut().deref_mut(), &mut elements);
core::mem::swap(zelf.borrow_vec_mut().deref_mut(), &mut elements);
Ok(())
}
}
Expand Down
2 changes: 1 addition & 1 deletion crates/vm/src/builtins/map.rs
Original file line number Diff line number Diff line change
Expand Up @@ -42,7 +42,7 @@ impl PyMap {
fn __length_hint__(&self, vm: &VirtualMachine) -> PyResult<usize> {
self.iterators.iter().try_fold(0, |prev, cur| {
let cur = cur.as_ref().to_owned().length_hint(0, vm)?;
let max = std::cmp::max(prev, cur);
let max = core::cmp::max(prev, cur);
Ok(max)
})
}
Expand Down
2 changes: 1 addition & 1 deletion crates/vm/src/builtins/memory.rs
Original file line number Diff line number Diff line change
Expand Up @@ -30,7 +30,7 @@ use crossbeam_utils::atomic::AtomicCell;
use itertools::Itertools;
use rustpython_common::lock::PyMutex;
use std::sync::LazyLock;
use std::{cmp::Ordering, fmt::Debug, mem::ManuallyDrop, ops::Range};
use core::{cmp::Ordering, fmt::Debug, mem::ManuallyDrop, ops::Range};

#[derive(FromArgs)]
pub struct PyMemoryViewNewArgs {
Expand Down
2 changes: 1 addition & 1 deletion crates/vm/src/builtins/object.rs
Original file line number Diff line number Diff line change
Expand Up @@ -218,7 +218,7 @@ fn object_getstate_default(obj: &PyObject, required: bool, vm: &VirtualMachine)
// basicsize += std::mem::size_of::<PyObjectRef>();
// }
if let Some(ref slot_names) = slot_names {
basicsize += std::mem::size_of::<PyObjectRef>() * slot_names.__len__();
basicsize += core::mem::size_of::<PyObjectRef>() * slot_names.__len__();
}
if obj.class().slots.basicsize > basicsize {
return Err(
Expand Down
4 changes: 2 additions & 2 deletions crates/vm/src/builtins/property.rs
Original file line number Diff line number Diff line change
Expand Up @@ -10,7 +10,7 @@ use crate::{
function::{FuncArgs, PySetterValue},
types::{Constructor, GetDescriptor, Initializer},
};
use std::sync::atomic::{AtomicBool, Ordering};
use core::sync::atomic::{AtomicBool, Ordering};

#[pyclass(module = false, name = "property", traverse)]
#[derive(Debug)]
Expand All @@ -21,7 +21,7 @@ pub struct PyProperty {
doc: PyRwLock<Option<PyObjectRef>>,
name: PyRwLock<Option<PyObjectRef>>,
#[pytraverse(skip)]
getter_doc: std::sync::atomic::AtomicBool,
getter_doc: core::sync::atomic::AtomicBool,
}

impl PyPayload for PyProperty {
Expand Down
2 changes: 1 addition & 1 deletion crates/vm/src/builtins/range.rs
Original file line number Diff line number Diff line change
Expand Up @@ -18,7 +18,7 @@ use crossbeam_utils::atomic::AtomicCell;
use malachite_bigint::{BigInt, Sign};
use num_integer::Integer;
use num_traits::{One, Signed, ToPrimitive, Zero};
use std::cmp::max;
use core::cmp::max;
use std::sync::LazyLock;

// Search flag passed to iter_search
Expand Down
Loading