Skip to content

Commit ba19f78

Browse files
committed
more code nits
1 parent f4734c3 commit ba19f78

4 files changed

Lines changed: 47 additions & 32 deletions

File tree

‎crates/vm/src/builtins/function.rs‎

Lines changed: 10 additions & 3 deletions
Original file line numberDiff line numberDiff line change
@@ -33,23 +33,26 @@ fn format_missing_args(
3333
missing: &mut Vec<impl core::fmt::Display>,
3434
) -> String {
3535
let count = missing.len();
36+
3637
let last = if missing.len() > 1 {
3738
missing.pop()
3839
} else {
3940
None
4041
};
42+
4143
let (and, right): (&str, String) = if let Some(last) = last {
4244
(
4345
if missing.len() == 1 {
4446
"' and '"
4547
} else {
4648
"', and '"
4749
},
48-
format!("{last}"),
50+
last.to_string(),
4951
)
5052
} else {
5153
("", String::new())
5254
};
55+
5356
format!(
5457
"{qualname}() missing {count} required {kind} argument{}: '{}{}{right}'",
5558
if count == 1 { "" } else { "s" },
@@ -1268,12 +1271,12 @@ impl PyBoundMethod {
12681271
}
12691272

12701273
#[inline]
1271-
pub(crate) fn function_obj(&self) -> &PyObjectRef {
1274+
pub(crate) const fn function_obj(&self) -> &PyObjectRef {
12721275
&self.function
12731276
}
12741277

12751278
#[inline]
1276-
pub(crate) fn self_obj(&self) -> &PyObjectRef {
1279+
pub(crate) const fn self_obj(&self) -> &PyObjectRef {
12771280
&self.object
12781281
}
12791282

@@ -1398,6 +1401,7 @@ impl Representable for PyBoundMethod {
13981401
pub(crate) struct PyCell {
13991402
contents: PyMutex<Option<PyObjectRef>>,
14001403
}
1404+
14011405
pub(crate) type PyCellRef = PyRef<PyCell>;
14021406

14031407
impl PyPayload for PyCell {
@@ -1426,6 +1430,7 @@ impl PyCell {
14261430
pub(crate) fn get(&self) -> Option<PyObjectRef> {
14271431
self.contents.lock().clone()
14281432
}
1433+
14291434
pub(crate) fn set(&self, x: Option<PyObjectRef>) {
14301435
*self.contents.lock() = x;
14311436
}
@@ -1435,6 +1440,7 @@ impl PyCell {
14351440
self.get()
14361441
.ok_or_else(|| vm.new_value_error("Cell is empty"))
14371442
}
1443+
14381444
#[pygetset(setter)]
14391445
fn set_cell_contents(&self, x: PySetterValue) {
14401446
match x {
@@ -1488,6 +1494,7 @@ pub(crate) fn vectorcall_function(
14881494
args.truncate(nargs);
14891495
FuncArgs::from(args)
14901496
};
1497+
14911498
zelf.invoke(func_args, vm)
14921499
}
14931500

‎crates/vm/src/builtins/tuple.rs‎

Lines changed: 29 additions & 21 deletions
Original file line numberDiff line numberDiff line change
@@ -296,13 +296,13 @@ impl<R> PyTuple<R> {
296296

297297
#[inline]
298298
#[must_use]
299-
pub fn len(&self) -> usize {
299+
pub const fn len(&self) -> usize {
300300
self.elements.len()
301301
}
302302

303303
#[inline]
304304
#[must_use]
305-
pub fn is_empty(&self) -> bool {
305+
pub const fn is_empty(&self) -> bool {
306306
self.elements.is_empty()
307307
}
308308

@@ -725,23 +725,29 @@ pub(crate) fn init(context: &'static Context) {
725725
}
726726

727727
pub(super) fn tuple_hash(elements: &[PyObjectRef], vm: &VirtualMachine) -> PyResult<PyHash> {
728-
#[cfg(target_pointer_width = "64")]
729-
const PRIME1: PyUHash = 11400714785074694791;
730-
#[cfg(target_pointer_width = "64")]
731-
const PRIME2: PyUHash = 14029467366897019727;
732-
#[cfg(target_pointer_width = "64")]
733-
const PRIME5: PyUHash = 2870177450012600261;
734-
#[cfg(target_pointer_width = "64")]
735-
const ROTATE: u32 = 31;
736-
737-
#[cfg(target_pointer_width = "32")]
738-
const PRIME1: PyUHash = 2654435761;
739-
#[cfg(target_pointer_width = "32")]
740-
const PRIME2: PyUHash = 2246822519;
741-
#[cfg(target_pointer_width = "32")]
742-
const PRIME5: PyUHash = 374761393;
743-
#[cfg(target_pointer_width = "32")]
744-
const ROTATE: u32 = 13;
728+
const PRIME1: PyUHash = cfg_select! {
729+
target_pointer_width = "64" => 11400714785074694791,
730+
target_pointer_width = "32" => 2654435761,
731+
_ => unreachable!(),
732+
};
733+
734+
const PRIME2: PyUHash = cfg_select! {
735+
target_pointer_width = "64" => 14029467366897019727,
736+
target_pointer_width = "32" => 2246822519,
737+
_ => unreachable!(),
738+
};
739+
740+
const PRIME5: PyUHash = cfg_select! {
741+
target_pointer_width = "64" => 2870177450012600261,
742+
target_pointer_width = "32" => 374761393,
743+
_ => unreachable!(),
744+
};
745+
746+
const ROTATE: u32 = cfg_select! {
747+
target_pointer_width = "64" => 31,
748+
target_pointer_width = "32" => 13,
749+
_ => unreachable!(),
750+
};
745751

746752
let mut acc = PRIME5;
747753
let len = elements.len() as PyUHash;
@@ -755,8 +761,10 @@ pub(super) fn tuple_hash(elements: &[PyObjectRef], vm: &VirtualMachine) -> PyRes
755761

756762
acc = acc.wrapping_add(len ^ (PRIME5 ^ 3527539));
757763

758-
if acc as PyHash == -1 {
764+
let acc_pyhash = acc as PyHash;
765+
if acc_pyhash == -1 {
759766
return Ok(1546275796);
760767
}
761-
Ok(acc as PyHash)
768+
769+
Ok(acc_pyhash)
762770
}

‎crates/vm/src/builtins/weakproxy.rs‎

Lines changed: 1 addition & 1 deletion
Original file line numberDiff line numberDiff line change
@@ -136,7 +136,7 @@ impl IterNext for PyWeakProxy {
136136
fn next(zelf: &Py<Self>, vm: &VirtualMachine) -> PyResult<PyIterReturn> {
137137
let obj = zelf.try_upgrade(vm)?;
138138
if obj.class().slots.iternext.load().is_none() {
139-
return Err(vm.new_type_error("Weakref proxy referenced a non-iterator".to_owned()));
139+
return Err(vm.new_type_error("Weakref proxy referenced a non-iterator"));
140140
}
141141
PyIter::new(obj).next(vm)
142142
}

‎crates/vm/src/builtins/weakref.rs‎

Lines changed: 7 additions & 7 deletions
Original file line numberDiff line numberDiff line change
@@ -32,6 +32,7 @@ impl PyPayload for PyWeak {
3232

3333
impl Callable for PyWeak {
3434
type Args = ();
35+
3536
#[inline]
3637
fn call(zelf: &Py<Self>, _: Self::Args, vm: &VirtualMachine) -> PyResult {
3738
Ok(vm.unwrap_or_none(zelf.upgrade()))
@@ -50,10 +51,10 @@ impl Constructor for PyWeak {
5051
.ok_or_else(|| vm.new_type_error("__new__ expected at least 1 argument, got 0"))?;
5152
let callback = positional.next();
5253
if let Some(_extra) = positional.next() {
53-
return Err(vm.new_type_error(format!(
54-
"__new__ expected at most 2 arguments, got {}",
55-
3 + positional.count()
56-
)));
54+
let got = positional.count() + 3;
55+
return Err(
56+
vm.new_type_error(format!("__new__ expected at most 2 arguments, got {got}"))
57+
);
5758
}
5859
let weak = referent.downgrade_with_typ(callback, cls, vm)?;
5960
Ok(weak.into())
@@ -151,7 +152,7 @@ impl Representable for PyWeak {
151152
#[inline]
152153
fn repr_str(zelf: &Py<Self>, _vm: &VirtualMachine) -> PyResult<String> {
153154
let id = zelf.get_id();
154-
let repr = if let Some(o) = zelf.upgrade() {
155+
Ok(if let Some(o) = zelf.upgrade() {
155156
format!(
156157
"<weakref at {:#x}; to '{}' at {:#x}>",
157158
id,
@@ -160,8 +161,7 @@ impl Representable for PyWeak {
160161
)
161162
} else {
162163
format!("<weakref at {id:#x}; dead>")
163-
};
164-
Ok(repr)
164+
})
165165
}
166166
}
167167

0 commit comments

Comments
 (0)