From d602a26f831e4c4326b1575659a434b51ae6bd3f Mon Sep 17 00:00:00 2001 From: ShaharNaveh <50263213+ShaharNaveh@users.noreply.github.com> Date: Tue, 29 Jul 2025 02:23:15 +0200 Subject: [PATCH 1/4] Apply some clipy lints --- common/src/boxvec.rs | 2 +- common/src/format.rs | 4 +- compiler/codegen/src/compile.rs | 14 +-- compiler/codegen/src/unparse.rs | 2 +- compiler/core/src/bytecode.rs | 66 ++++++++-- compiler/core/src/frozen.rs | 2 + compiler/core/src/marshal.rs | 52 +++++++- compiler/literal/src/escape.rs | 34 +++--- compiler/literal/src/float.rs | 2 +- examples/call_between_rust_and_python.rs | 2 +- src/settings.rs | 4 +- src/shell/helper.rs | 2 +- stdlib/src/unicodedata.rs | 4 +- vm/src/builtins/code.rs | 28 ++--- vm/src/builtins/dict.rs | 2 +- vm/src/builtins/genericalias.rs | 8 +- vm/src/builtins/namespace.rs | 2 +- vm/src/builtins/slice.rs | 2 +- vm/src/builtins/str.rs | 2 +- vm/src/builtins/super.rs | 2 +- vm/src/builtins/traceback.rs | 4 +- vm/src/builtins/tuple.rs | 12 +- vm/src/frame.rs | 6 +- vm/src/stdlib/ast/python.rs | 2 +- vm/src/stdlib/io.rs | 2 +- vm/src/stdlib/itertools.rs | 2 +- vm/src/stdlib/posix.rs | 16 +-- vm/src/stdlib/symtable.rs | 2 +- vm/src/stdlib/thread.rs | 2 +- vm/src/stdlib/typing.rs | 2 +- vm/sre_engine/src/engine.rs | 18 +-- vm/sre_engine/src/string.rs | 8 +- wtf8/src/core_str_count.rs | 4 +- wtf8/src/lib.rs | 147 ++++++++++++----------- 34 files changed, 281 insertions(+), 182 deletions(-) diff --git a/common/src/boxvec.rs b/common/src/boxvec.rs index 4f3928e56b..8687ba7f7f 100644 --- a/common/src/boxvec.rs +++ b/common/src/boxvec.rs @@ -165,7 +165,7 @@ impl BoxVec { if index >= self.len() { None } else { - self.drain(index..index + 1).next() + self.drain(index..=index).next() } } diff --git a/common/src/format.rs b/common/src/format.rs index 9b2a37d450..120eefcbc3 100644 --- a/common/src/format.rs +++ b/common/src/format.rs @@ -389,7 +389,7 @@ impl FormatSpec { fn insert_separator(mut magnitude_str: String, inter: i32, sep: char, sep_cnt: i32) -> String { let magnitude_len = magnitude_str.len() as i32; - for i in 1..sep_cnt + 1 { + for i in 1..=sep_cnt { magnitude_str.insert((magnitude_len - inter * i) as usize, sep); } magnitude_str @@ -754,7 +754,7 @@ impl FormatSpec { let inter = self.get_separator_interval().try_into().unwrap(); let len = magnitude_str.len() as i32; let separated_magnitude = - FormatSpec::add_magnitude_separators_for_char(magnitude_str, inter, sep, len); + Self::add_magnitude_separators_for_char(magnitude_str, inter, sep, len); Ok(separated_magnitude) } None => Ok(magnitude_str), diff --git a/compiler/codegen/src/compile.rs b/compiler/codegen/src/compile.rs index 7a85c1272b..bd66c0c3d6 100644 --- a/compiler/codegen/src/compile.rs +++ b/compiler/codegen/src/compile.rs @@ -360,7 +360,7 @@ impl Compiler { fblock: Vec::with_capacity(MAXBLOCKS), symbol_table_index: 0, // Module is always the first symbol table }; - Compiler { + Self { code_stack: vec![module_code], symbol_table_stack: Vec::new(), source_file, @@ -380,7 +380,7 @@ impl Compiler { /// Check if the slice is a two-element slice (no step) // = is_two_element_slice - fn is_two_element_slice(slice: &Expr) -> bool { + const fn is_two_element_slice(slice: &Expr) -> bool { matches!(slice, Expr::Slice(s) if s.step.is_none()) } @@ -882,7 +882,7 @@ impl Compiler { self._name_inner(name, |i| &mut i.metadata.names) } fn varname(&mut self, name: &str) -> CompileResult { - if Compiler::is_forbidden_arg_name(name) { + if Self::is_forbidden_arg_name(name) { return Err(self.error(CodegenErrorType::SyntaxError(format!( "cannot assign to {name}", )))); @@ -3435,7 +3435,7 @@ impl Compiler { // Create a new tuple of attribute names. let mut attr_names = vec![]; - for name in kwd_attrs.iter() { + for name in &kwd_attrs { // Py_NewRef(name) is emulated by cloning the name into a PyObject. attr_names.push(ConstantData::Str { value: name.as_str().to_string().into(), @@ -3667,7 +3667,7 @@ impl Compiler { pc.stores.insert(insert_pos + j, elem); } // Also perform the same rotation on the evaluation stack. - for _ in 0..(i_stores + 1) { + for _ in 0..=i_stores { self.pattern_helper_rotate(i_control + 1)?; } } @@ -4547,7 +4547,7 @@ impl Compiler { }) => { let prev_ctx = self.ctx; let name = "".to_owned(); - let default_params = Default::default(); + let default_params = Parameters::default(); let params = parameters.as_deref().unwrap_or(&default_params); // Prepare defaults before entering function @@ -4577,7 +4577,7 @@ impl Compiler { let have_kwdefaults = !kw_with_defaults.is_empty(); if have_kwdefaults { let default_kw_count = kw_with_defaults.len(); - for (arg, default) in kw_with_defaults.iter() { + for (arg, default) in &kw_with_defaults { self.emit_load_const(ConstantData::Str { value: arg.name.as_str().into(), }); diff --git a/compiler/codegen/src/unparse.rs b/compiler/codegen/src/unparse.rs index afabb15b21..c2bca02adb 100644 --- a/compiler/codegen/src/unparse.rs +++ b/compiler/codegen/src/unparse.rs @@ -464,7 +464,7 @@ impl<'a, 'b, 'c> Unparser<'a, 'b, 'c> { if let Some(vararg) = &args.vararg { self.unparse_arg(vararg)?; } - for kwarg in args.kwonlyargs.iter() { + for kwarg in &args.kwonlyargs { self.p_delim(&mut first, ", ")?; self.unparse_function_arg(kwarg)?; } diff --git a/compiler/core/src/bytecode.rs b/compiler/core/src/bytecode.rs index d22ccd697f..8e8104234f 100644 --- a/compiler/core/src/bytecode.rs +++ b/compiler/core/src/bytecode.rs @@ -42,8 +42,10 @@ pub trait Constant: Sized { impl Constant for ConstantData { type Name = String; + fn borrow_constant(&self) -> BorrowedConstant<'_, Self> { use BorrowedConstant::*; + match self { Self::Integer { value } => Integer { value }, Self::Float { value } => Float { value: *value }, @@ -62,21 +64,28 @@ impl Constant for ConstantData { /// A Constant Bag pub trait ConstantBag: Sized + Copy { type Constant: Constant; + fn make_constant(&self, constant: BorrowedConstant<'_, C>) -> Self::Constant; + fn make_int(&self, value: BigInt) -> Self::Constant; + fn make_tuple(&self, elements: impl Iterator) -> Self::Constant; + fn make_code(&self, code: CodeObject) -> Self::Constant; + fn make_name(&self, name: &str) -> ::Name; } pub trait AsBag { type Bag: ConstantBag; + #[allow(clippy::wrong_self_convention)] fn as_bag(self) -> Self::Bag; } impl AsBag for Bag { type Bag = Self; + fn as_bag(self) -> Self { self } @@ -87,22 +96,27 @@ pub struct BasicBag; impl ConstantBag for BasicBag { type Constant = ConstantData; + fn make_constant(&self, constant: BorrowedConstant<'_, C>) -> Self::Constant { constant.to_owned() } + fn make_int(&self, value: BigInt) -> Self::Constant { ConstantData::Integer { value } } + fn make_tuple(&self, elements: impl Iterator) -> Self::Constant { ConstantData::Tuple { elements: elements.collect(), } } + fn make_code(&self, code: CodeObject) -> Self::Constant { ConstantData::Code { code: Box::new(code), } } + fn make_name(&self, name: &str) -> ::Name { name.to_owned() } @@ -163,11 +177,13 @@ impl CodeFlags { #[derive(Copy, Clone, PartialEq, Eq)] #[repr(transparent)] pub struct OpArgByte(pub u8); + impl OpArgByte { pub const fn null() -> Self { Self(0) } } + impl fmt::Debug for OpArgByte { fn fmt(&self, f: &mut fmt::Formatter<'_>) -> fmt::Result { self.0.fmt(f) @@ -178,6 +194,7 @@ impl fmt::Debug for OpArgByte { #[derive(Copy, Clone, Debug)] #[repr(transparent)] pub struct OpArg(pub u32); + impl OpArg { pub const fn null() -> Self { Self(0) @@ -185,7 +202,7 @@ impl OpArg { /// Returns how many CodeUnits a instruction with this op_arg will be encoded as #[inline] - pub fn instr_size(self) -> usize { + pub const fn instr_size(self) -> usize { (self.0 > 0xff) as usize + (self.0 > 0xff_ff) as usize + (self.0 > 0xff_ff_ff) as usize + 1 } @@ -219,19 +236,22 @@ impl OpArgState { } (ins.op, arg) } + #[inline(always)] pub fn extend(&mut self, arg: OpArgByte) -> OpArg { self.state = (self.state << 8) | u32::from(arg.0); OpArg(self.state) } + #[inline(always)] - pub fn reset(&mut self) { + pub const fn reset(&mut self) { self.state = 0 } } pub trait OpArgType: Copy { fn from_op_arg(x: u32) -> Option; + fn to_op_arg(self) -> u32; } @@ -240,6 +260,7 @@ impl OpArgType for u32 { fn from_op_arg(x: u32) -> Option { Some(x) } + #[inline(always)] fn to_op_arg(self) -> u32 { self @@ -251,6 +272,7 @@ impl OpArgType for bool { fn from_op_arg(x: u32) -> Option { Some(x != 0) } + #[inline(always)] fn to_op_arg(self) -> u32 { self as u32 @@ -263,6 +285,7 @@ macro_rules! op_arg_enum_impl { fn to_op_arg(self) -> u32 { self as u32 } + fn from_op_arg(x: u32) -> Option { Some(match u8::try_from(x).ok()? { $($value => Self::$var,)* @@ -291,13 +314,15 @@ pub struct Arg(PhantomData); impl Arg { #[inline] - pub fn marker() -> Self { + pub const fn marker() -> Self { Self(PhantomData) } + #[inline] pub fn new(arg: T) -> (Self, OpArg) { (Self(PhantomData), OpArg(arg.to_op_arg())) } + #[inline] pub fn new_single(arg: T) -> (Self, OpArgByte) where @@ -305,17 +330,20 @@ impl Arg { { (Self(PhantomData), OpArgByte(arg.into())) } + #[inline(always)] pub fn get(self, arg: OpArg) -> T { self.try_get(arg).unwrap() } + #[inline(always)] pub fn try_get(self, arg: OpArg) -> Option { T::from_op_arg(arg.0) } - #[inline(always)] + /// # Safety /// T::from_op_arg(self) must succeed + #[inline(always)] pub unsafe fn get_unchecked(self, arg: OpArg) -> T { // SAFETY: requirements forwarded from caller unsafe { T::from_op_arg(arg.0).unwrap_unchecked() } @@ -327,6 +355,7 @@ impl PartialEq for Arg { true } } + impl Eq for Arg {} impl fmt::Debug for Arg { @@ -346,6 +375,7 @@ impl OpArgType for Label { fn from_op_arg(x: u32) -> Option { Some(Self(x)) } + #[inline(always)] fn to_op_arg(self) -> u32 { self.0 @@ -369,6 +399,7 @@ impl OpArgType for ConversionFlag { _ => None, } } + #[inline] fn to_op_arg(self) -> u32 { self as i8 as u8 as u32 @@ -681,8 +712,10 @@ pub enum Instruction { ExtendedArg, // If you add a new instruction here, be sure to keep LAST_INSTRUCTION updated } + // This must be kept up to date to avoid marshaling errors const LAST_INSTRUCTION: Instruction = Instruction::ExtendedArg; + const _: () = assert!(mem::size_of::() == 1); impl From for u8 { @@ -716,7 +749,7 @@ pub struct CodeUnit { const _: () = assert!(mem::size_of::() == 2); impl CodeUnit { - pub fn new(op: Instruction, arg: OpArgByte) -> Self { + pub const fn new(op: Instruction, arg: OpArgByte) -> Self { Self { op, arg } } } @@ -733,11 +766,13 @@ bitflags! { const TYPE_PARAMS = 0x10; } } + impl OpArgType for MakeFunctionFlags { #[inline(always)] fn from_op_arg(x: u32) -> Option { Self::from_bits(x as u8) } + #[inline(always)] fn to_op_arg(self) -> u32 { self.bits().into() @@ -770,6 +805,7 @@ pub enum ConstantData { impl PartialEq for ConstantData { fn eq(&self, other: &Self) -> bool { use ConstantData::*; + match (self, other) { (Integer { value: a }, Integer { value: b }) => a == b, // we want to compare floats *by actual value* - if we have the *exact same* float @@ -795,6 +831,7 @@ impl Eq for ConstantData {} impl hash::Hash for ConstantData { fn hash(&self, state: &mut H) { use ConstantData::*; + mem::discriminant(self).hash(state); match self { Integer { value } => value.hash(state), @@ -829,6 +866,7 @@ pub enum BorrowedConstant<'a, C: Constant> { } impl Copy for BorrowedConstant<'_, C> {} + impl Clone for BorrowedConstant<'_, C> { fn clone(&self) -> Self { *self @@ -845,7 +883,7 @@ impl BorrowedConstant<'_, C> { write!(f, "{}", if *value { "True" } else { "False" }) } BorrowedConstant::Str { value } => write!(f, "{value:?}"), - BorrowedConstant::Bytes { value } => write!(f, "b\"{}\"", value.escape_ascii()), + BorrowedConstant::Bytes { value } => write!(f, r#"b"{}""#, value.escape_ascii()), BorrowedConstant::Code { code } => write!(f, "{code:?}"), BorrowedConstant::Tuple { elements } => { write!(f, "(")?; @@ -864,8 +902,10 @@ impl BorrowedConstant<'_, C> { BorrowedConstant::Ellipsis => write!(f, "..."), } } + pub fn to_owned(self) -> ConstantData { use ConstantData::*; + match self { BorrowedConstant::Integer { value } => Integer { value: value.clone(), @@ -975,11 +1015,13 @@ impl OpArgType for UnpackExArgs { let [before, after, ..] = x.to_le_bytes(); Some(Self { before, after }) } + #[inline(always)] fn to_op_arg(self) -> u32 { u32::from_le_bytes([self.before, self.after, 0, 0]) } } + impl fmt::Display for UnpackExArgs { fn fmt(&self, f: &mut fmt::Formatter<'_>) -> fmt::Result { write!(f, "before: {}, after: {}", self.before, self.after) @@ -1208,7 +1250,7 @@ impl fmt::Display for CodeObject { impl Instruction { /// Gets the label stored inside this instruction, if it exists #[inline] - pub fn label_arg(&self) -> Option> { + pub const fn label_arg(&self) -> Option> { match self { Jump { target: l } | JumpIfTrue { target: l } @@ -1235,7 +1277,7 @@ impl Instruction { /// let jump_inst = Instruction::Jump { target: Arg::marker() }; /// assert!(jump_inst.unconditional_branch()) /// ``` - pub fn unconditional_branch(&self) -> bool { + pub const fn unconditional_branch(&self) -> bool { matches!( self, Jump { .. } @@ -1571,23 +1613,31 @@ impl Instruction { pub trait InstrDisplayContext { type Constant: Constant; + fn get_constant(&self, i: usize) -> &Self::Constant; + fn get_name(&self, i: usize) -> &str; + fn get_varname(&self, i: usize) -> &str; + fn get_cell_name(&self, i: usize) -> &str; } impl InstrDisplayContext for CodeObject { type Constant = C; + fn get_constant(&self, i: usize) -> &C { &self.constants[i] } + fn get_name(&self, i: usize) -> &str { self.names[i].as_ref() } + fn get_varname(&self, i: usize) -> &str { self.varnames[i].as_ref() } + fn get_cell_name(&self, i: usize) -> &str { self.cellvars .get(i) diff --git a/compiler/core/src/frozen.rs b/compiler/core/src/frozen.rs index 466a914f04..a79569ad7f 100644 --- a/compiler/core/src/frozen.rs +++ b/compiler/core/src/frozen.rs @@ -57,6 +57,7 @@ impl + ?Sized> FrozenLib { impl<'a, B: AsRef<[u8]> + ?Sized> IntoIterator for &'a FrozenLib { type Item = (&'a str, FrozenModule<&'a [u8]>); type IntoIter = FrozenModulesIter<'a>; + fn into_iter(self) -> Self::IntoIter { self.decode() } @@ -84,6 +85,7 @@ impl<'a> Iterator for FrozenModulesIter<'a> { (self.remaining as usize, Some(self.remaining as usize)) } } + impl ExactSizeIterator for FrozenModulesIter<'_> {} fn read_entry<'a>( diff --git a/compiler/core/src/marshal.rs b/compiler/core/src/marshal.rs index c3b482631d..ff82340c0e 100644 --- a/compiler/core/src/marshal.rs +++ b/compiler/core/src/marshal.rs @@ -75,8 +75,10 @@ enum Type { impl TryFrom for Type { type Error = MarshalError; + fn try_from(value: u8) -> Result { use Type::*; + Ok(match value { // b'0' => Null, b'N' => None, @@ -111,24 +113,31 @@ impl TryFrom for Type { pub trait Read { fn read_slice(&mut self, n: u32) -> Result<&[u8]>; + fn read_array(&mut self) -> Result<&[u8; N]> { self.read_slice(N as u32).map(|s| s.try_into().unwrap()) } + fn read_str(&mut self, len: u32) -> Result<&str> { Ok(std::str::from_utf8(self.read_slice(len)?)?) } + fn read_wtf8(&mut self, len: u32) -> Result<&Wtf8> { Wtf8::from_bytes(self.read_slice(len)?).ok_or(MarshalError::InvalidUtf8) } + fn read_u8(&mut self) -> Result { Ok(u8::from_le_bytes(*self.read_array()?)) } + fn read_u16(&mut self) -> Result { Ok(u16::from_le_bytes(*self.read_array()?)) } + fn read_u32(&mut self) -> Result { Ok(u32::from_le_bytes(*self.read_array()?)) } + fn read_u64(&mut self) -> Result { Ok(u64::from_le_bytes(*self.read_array()?)) } @@ -136,6 +145,7 @@ pub trait Read { pub(crate) trait ReadBorrowed<'a>: Read { fn read_slice_borrow(&mut self, n: u32) -> Result<&'a [u8]>; + fn read_str_borrow(&mut self, len: u32) -> Result<&'a str> { Ok(std::str::from_utf8(self.read_slice_borrow(len)?)?) } @@ -264,85 +274,117 @@ pub fn deserialize_code( pub trait MarshalBag: Copy { type Value; + type ConstantBag: ConstantBag; + fn make_bool(&self, value: bool) -> Self::Value; + fn make_none(&self) -> Self::Value; + fn make_ellipsis(&self) -> Self::Value; + fn make_float(&self, value: f64) -> Self::Value; + fn make_complex(&self, value: Complex64) -> Self::Value; + fn make_str(&self, value: &Wtf8) -> Self::Value; + fn make_bytes(&self, value: &[u8]) -> Self::Value; + fn make_int(&self, value: BigInt) -> Self::Value; + fn make_tuple(&self, elements: impl Iterator) -> Self::Value; + fn make_code( &self, code: CodeObject<::Constant>, ) -> Self::Value; + fn make_stop_iter(&self) -> Result; + fn make_list(&self, it: impl Iterator) -> Result; + fn make_set(&self, it: impl Iterator) -> Result; + fn make_frozenset(&self, it: impl Iterator) -> Result; + fn make_dict( &self, it: impl Iterator, ) -> Result; - type ConstantBag: ConstantBag; + fn constant_bag(self) -> Self::ConstantBag; } impl MarshalBag for Bag { type Value = Bag::Constant; + type ConstantBag = Self; + fn make_bool(&self, value: bool) -> Self::Value { self.make_constant::(BorrowedConstant::Boolean { value }) } + fn make_none(&self) -> Self::Value { self.make_constant::(BorrowedConstant::None) } + fn make_ellipsis(&self) -> Self::Value { self.make_constant::(BorrowedConstant::Ellipsis) } + fn make_float(&self, value: f64) -> Self::Value { self.make_constant::(BorrowedConstant::Float { value }) } + fn make_complex(&self, value: Complex64) -> Self::Value { self.make_constant::(BorrowedConstant::Complex { value }) } + fn make_str(&self, value: &Wtf8) -> Self::Value { self.make_constant::(BorrowedConstant::Str { value }) } + fn make_bytes(&self, value: &[u8]) -> Self::Value { self.make_constant::(BorrowedConstant::Bytes { value }) } + fn make_int(&self, value: BigInt) -> Self::Value { self.make_int(value) } + fn make_tuple(&self, elements: impl Iterator) -> Self::Value { self.make_tuple(elements) } + fn make_code( &self, code: CodeObject<::Constant>, ) -> Self::Value { self.make_code(code) } + fn make_stop_iter(&self) -> Result { Err(MarshalError::BadType) } + fn make_list(&self, _: impl Iterator) -> Result { Err(MarshalError::BadType) } + fn make_set(&self, _: impl Iterator) -> Result { Err(MarshalError::BadType) } + fn make_frozenset(&self, _: impl Iterator) -> Result { Err(MarshalError::BadType) } + fn make_dict( &self, _: impl Iterator, ) -> Result { Err(MarshalError::BadType) } - type ConstantBag = Self; + fn constant_bag(self) -> Self::ConstantBag { self } @@ -421,6 +463,7 @@ pub fn deserialize_value(rdr: &mut R, bag: Bag) -> Res pub trait Dumpable: Sized { type Error; type Constant: Constant; + fn with_dump(&self, f: impl FnOnce(DumpableValue<'_, Self>) -> R) -> Result; } @@ -462,6 +505,7 @@ impl<'a, C: Constant> From> for DumpableValue<'a, C> { impl Dumpable for C { type Error = Infallible; type Constant = Self; + #[inline(always)] fn with_dump(&self, f: impl FnOnce(DumpableValue<'_, Self>) -> R) -> Result { Ok(f(self.borrow_constant().into())) @@ -470,15 +514,19 @@ impl Dumpable for C { pub trait Write { fn write_slice(&mut self, slice: &[u8]); + fn write_u8(&mut self, v: u8) { self.write_slice(&v.to_le_bytes()) } + fn write_u16(&mut self, v: u16) { self.write_slice(&v.to_le_bytes()) } + fn write_u32(&mut self, v: u32) { self.write_slice(&v.to_le_bytes()) } + fn write_u64(&mut self, v: u64) { self.write_slice(&v.to_le_bytes()) } diff --git a/compiler/literal/src/escape.rs b/compiler/literal/src/escape.rs index 920e0a8cb1..6bdd94e986 100644 --- a/compiler/literal/src/escape.rs +++ b/compiler/literal/src/escape.rs @@ -8,26 +8,26 @@ pub enum Quote { impl Quote { #[inline] - pub const fn swap(self) -> Quote { + pub const fn swap(self) -> Self { match self { - Quote::Single => Quote::Double, - Quote::Double => Quote::Single, + Self::Single => Self::Double, + Self::Double => Self::Single, } } #[inline] pub const fn to_byte(&self) -> u8 { match self { - Quote::Single => b'\'', - Quote::Double => b'"', + Self::Single => b'\'', + Self::Double => b'"', } } #[inline] pub const fn to_char(&self) -> char { match self { - Quote::Single => '\'', - Quote::Double => '"', + Self::Single => '\'', + Self::Double => '"', } } } @@ -95,7 +95,7 @@ pub struct UnicodeEscape<'a> { impl<'a> UnicodeEscape<'a> { #[inline] - pub fn with_forced_quote(source: &'a Wtf8, quote: Quote) -> Self { + pub const fn with_forced_quote(source: &'a Wtf8, quote: Quote) -> Self { let layout = EscapeLayout { quote, len: None }; Self { source, layout } } @@ -109,7 +109,7 @@ impl<'a> UnicodeEscape<'a> { Self::with_preferred_quote(source, Quote::Single) } #[inline] - pub fn str_repr<'r>(&'a self) -> StrRepr<'r, 'a> { + pub const fn str_repr<'r>(&'a self) -> StrRepr<'r, 'a> { StrRepr(self) } } @@ -169,7 +169,7 @@ impl UnicodeEscape<'_> { }; let Some(new_len) = length_add(out_len, incr) else { #[cold] - fn stop( + const fn stop( single_count: usize, double_count: usize, preferred_quote: Quote, @@ -283,11 +283,11 @@ pub struct AsciiEscape<'a> { impl<'a> AsciiEscape<'a> { #[inline] - pub fn new(source: &'a [u8], layout: EscapeLayout) -> Self { + pub const fn new(source: &'a [u8], layout: EscapeLayout) -> Self { Self { source, layout } } #[inline] - pub fn with_forced_quote(source: &'a [u8], quote: Quote) -> Self { + pub const fn with_forced_quote(source: &'a [u8], quote: Quote) -> Self { let layout = EscapeLayout { quote, len: None }; Self { source, layout } } @@ -301,7 +301,7 @@ impl<'a> AsciiEscape<'a> { Self::with_preferred_quote(source, Quote::Single) } #[inline] - pub fn bytes_repr<'r>(&'a self) -> BytesRepr<'r, 'a> { + pub const fn bytes_repr<'r>(&'a self) -> BytesRepr<'r, 'a> { BytesRepr(self) } } @@ -329,7 +329,7 @@ impl AsciiEscape<'_> { let mut single_count = 0; let mut double_count = 0; - for ch in source.iter() { + for ch in source { let incr = match ch { b'\'' => { single_count += 1; @@ -343,7 +343,7 @@ impl AsciiEscape<'_> { }; let Some(new_len) = length_add(out_len, incr) else { #[cold] - fn stop( + const fn stop( single_count: usize, double_count: usize, preferred_quote: Quote, @@ -370,7 +370,7 @@ impl AsciiEscape<'_> { } } - fn escaped_char_len(ch: u8) -> usize { + const fn escaped_char_len(ch: u8) -> usize { match ch { b'\\' | b'\t' | b'\r' | b'\n' => 2, 0x20..=0x7e => 1, @@ -413,7 +413,7 @@ unsafe impl Escape for AsciiEscape<'_> { #[cold] fn write_body_slow(&self, formatter: &mut impl std::fmt::Write) -> std::fmt::Result { - for ch in self.source.iter() { + for ch in self.source { Self::write_char(*ch, self.layout().quote, formatter)?; } Ok(()) diff --git a/compiler/literal/src/float.rs b/compiler/literal/src/float.rs index 0c07d97c98..b1fc4607b5 100644 --- a/compiler/literal/src/float.rs +++ b/compiler/literal/src/float.rs @@ -44,7 +44,7 @@ fn format_inf(case: Case) -> String { inf.to_string() } -pub fn decimal_point_or_empty(precision: usize, alternate_form: bool) -> &'static str { +pub const fn decimal_point_or_empty(precision: usize, alternate_form: bool) -> &'static str { match (precision, alternate_form) { (0, true) => ".", _ => "", diff --git a/examples/call_between_rust_and_python.rs b/examples/call_between_rust_and_python.rs index 80bf65526a..dee1705847 100644 --- a/examples/call_between_rust_and_python.rs +++ b/examples/call_between_rust_and_python.rs @@ -89,7 +89,7 @@ python_person.name: {}", impl<'a> TryFromBorrowedObject<'a> for PythonPerson { fn try_from_borrowed_object(vm: &VirtualMachine, obj: &'a PyObject) -> PyResult { let name = obj.get_attr("name", vm)?.try_into_value::(vm)?; - Ok(PythonPerson { name }) + Ok(Self { name }) } } } diff --git a/src/settings.rs b/src/settings.rs index 00ee55bddd..23553aa5c0 100644 --- a/src/settings.rs +++ b/src/settings.rs @@ -26,8 +26,8 @@ impl FromStr for InstallPipMode { fn from_str(s: &str) -> Result { match s { - "ensurepip" => Ok(InstallPipMode::Ensurepip), - "get-pip" => Ok(InstallPipMode::GetPip), + "ensurepip" => Ok(Self::Ensurepip), + "get-pip" => Ok(Self::GetPip), _ => Err("--install-pip takes ensurepip or get-pip as first argument"), } } diff --git a/src/shell/helper.rs b/src/shell/helper.rs index 0fe2f5ca93..493dc57846 100644 --- a/src/shell/helper.rs +++ b/src/shell/helper.rs @@ -54,7 +54,7 @@ fn split_idents_on_dot(line: &str) -> Option<(usize, Vec)> { } impl<'vm> ShellHelper<'vm> { - pub fn new(vm: &'vm VirtualMachine, globals: PyDictRef) -> Self { + pub const fn new(vm: &'vm VirtualMachine, globals: PyDictRef) -> Self { ShellHelper { vm, globals } } diff --git a/stdlib/src/unicodedata.rs b/stdlib/src/unicodedata.rs index 6fbb385c4c..b97a4b0402 100644 --- a/stdlib/src/unicodedata.rs +++ b/stdlib/src/unicodedata.rs @@ -24,9 +24,7 @@ pub fn make_module(vm: &VirtualMachine) -> PyRef { "east_asian_width", "normalize", "mirrored", - ] - .into_iter() - { + ] { crate::vm::extend_module!(vm, &module, { attr => ucd.get_attr(attr, vm).unwrap(), }); diff --git a/vm/src/builtins/code.rs b/vm/src/builtins/code.rs index 43b6380d73..8286b63f64 100644 --- a/vm/src/builtins/code.rs +++ b/vm/src/builtins/code.rs @@ -107,6 +107,7 @@ impl<'a> AsBag for &'a Context { PyObjBag(self) } } + impl<'a> AsBag for &'a VirtualMachine { type Bag = PyObjBag<'a>; fn as_bag(self) -> PyObjBag<'a> { @@ -123,28 +124,27 @@ impl ConstantBag for PyObjBag<'_> { fn make_constant(&self, constant: BorrowedConstant<'_, C>) -> Self::Constant { let ctx = self.0; let obj = match constant { - bytecode::BorrowedConstant::Integer { value } => ctx.new_bigint(value).into(), - bytecode::BorrowedConstant::Float { value } => ctx.new_float(value).into(), - bytecode::BorrowedConstant::Complex { value } => ctx.new_complex(value).into(), - bytecode::BorrowedConstant::Str { value } if value.len() <= 20 => { + BorrowedConstant::Integer { value } => ctx.new_bigint(value).into(), + BorrowedConstant::Float { value } => ctx.new_float(value).into(), + BorrowedConstant::Complex { value } => ctx.new_complex(value).into(), + BorrowedConstant::Str { value } if value.len() <= 20 => { ctx.intern_str(value).to_object() } - bytecode::BorrowedConstant::Str { value } => ctx.new_str(value).into(), - bytecode::BorrowedConstant::Bytes { value } => ctx.new_bytes(value.to_vec()).into(), - bytecode::BorrowedConstant::Boolean { value } => ctx.new_bool(value).into(), - bytecode::BorrowedConstant::Code { code } => { - ctx.new_code(code.map_clone_bag(self)).into() - } - bytecode::BorrowedConstant::Tuple { elements } => { + BorrowedConstant::Str { value } => ctx.new_str(value).into(), + BorrowedConstant::Bytes { value } => ctx.new_bytes(value.to_vec()).into(), + BorrowedConstant::Boolean { value } => ctx.new_bool(value).into(), + BorrowedConstant::Code { code } => ctx.new_code(code.map_clone_bag(self)).into(), + BorrowedConstant::Tuple { elements } => { let elements = elements .iter() .map(|constant| self.make_constant(constant.borrow_constant()).0) .collect(); ctx.new_tuple(elements).into() } - bytecode::BorrowedConstant::None => ctx.none(), - bytecode::BorrowedConstant::Ellipsis => ctx.ellipsis.clone().into(), + BorrowedConstant::None => ctx.none(), + BorrowedConstant::Ellipsis => ctx.ellipsis.clone().into(), }; + Literal(obj) } @@ -172,7 +172,7 @@ pub trait IntoCodeObject { } impl IntoCodeObject for CodeObject { - fn into_code_object(self, _ctx: &Context) -> CodeObject { + fn into_code_object(self, _ctx: &Context) -> Self { self } } diff --git a/vm/src/builtins/dict.rs b/vm/src/builtins/dict.rs index e59aa5bcf7..690bdb829a 100644 --- a/vm/src/builtins/dict.rs +++ b/vm/src/builtins/dict.rs @@ -279,7 +279,7 @@ impl PyDict { if let OptionalArg::Present(dict_obj) = dict_obj { self.merge_object(dict_obj, vm)?; } - for (key, value) in kwargs.into_iter() { + for (key, value) in kwargs { self.entries.insert(vm, &key, value)?; } Ok(()) diff --git a/vm/src/builtins/genericalias.rs b/vm/src/builtins/genericalias.rs index 00bd65583d..aca07a63a0 100644 --- a/vm/src/builtins/genericalias.rs +++ b/vm/src/builtins/genericalias.rs @@ -338,7 +338,7 @@ fn subs_tvars( { // TypeVarTuple case - extend with tuple elements if let Ok(tuple) = substituted_arg.try_to_ref::(vm) { - for elem in tuple.iter() { + for elem in tuple { sub_args.push(elem.clone()); } continue; @@ -387,7 +387,7 @@ fn unpack_args(item: PyObjectRef, vm: &VirtualMachine) -> PyResult { if !has_ellipsis_at_end { // Safe to unpack - add all elements's PyList_SetSlice - for arg in tuple.iter() { + for arg in tuple { new_args.push(arg.clone()); } continue; @@ -484,7 +484,7 @@ pub fn subs_parameters( if unpack { // Handle unpacked TypeVarTuple's tuple_extend if let Ok(tuple) = substituted_arg.try_to_ref::(vm) { - for elem in tuple.iter() { + for elem in tuple { new_args.push(elem.clone()); } } else { @@ -572,7 +572,7 @@ impl Hashable for PyGenericAlias { impl GetAttr for PyGenericAlias { fn getattro(zelf: &Py, attr: &Py, vm: &VirtualMachine) -> PyResult { - for exc in ATTR_EXCEPTIONS.iter() { + for exc in &ATTR_EXCEPTIONS { if *(*exc) == attr.to_string() { return zelf.as_object().generic_getattr(attr, vm); } diff --git a/vm/src/builtins/namespace.rs b/vm/src/builtins/namespace.rs index 2c6b8e79d8..96f366e067 100644 --- a/vm/src/builtins/namespace.rs +++ b/vm/src/builtins/namespace.rs @@ -61,7 +61,7 @@ impl Initializer for PyNamespace { if !args.args.is_empty() { return Err(vm.new_type_error("no positional arguments expected")); } - for (name, value) in args.kwargs.into_iter() { + for (name, value) in args.kwargs { let name = vm.ctx.new_str(name); zelf.as_object().set_attr(&name, value, vm)?; } diff --git a/vm/src/builtins/slice.rs b/vm/src/builtins/slice.rs index f77c8cb8e8..cb36a0080f 100644 --- a/vm/src/builtins/slice.rs +++ b/vm/src/builtins/slice.rs @@ -228,7 +228,7 @@ impl Hashable for PySlice { }; let mut acc = XXPRIME_5; - for part in [zelf.start_ref(vm), &zelf.stop, zelf.step_ref(vm)].iter() { + for part in &[zelf.start_ref(vm), &zelf.stop, zelf.step_ref(vm)] { let lane = part.hash(vm)? as PyUHash; if lane == u64::MAX as PyUHash { return Ok(-1 as PyHash); diff --git a/vm/src/builtins/str.rs b/vm/src/builtins/str.rs index 1c38314afe..4fb5861973 100644 --- a/vm/src/builtins/str.rs +++ b/vm/src/builtins/str.rs @@ -1521,7 +1521,7 @@ impl PyStrRef { pub fn try_into_utf8(self, vm: &VirtualMachine) -> PyResult> { self.ensure_valid_utf8(vm)?; - Ok(unsafe { mem::transmute::, PyRef>(self) }) + Ok(unsafe { mem::transmute::>(self) }) } } diff --git a/vm/src/builtins/super.rs b/vm/src/builtins/super.rs index 2d7e48447f..de8c74101e 100644 --- a/vm/src/builtins/super.rs +++ b/vm/src/builtins/super.rs @@ -174,7 +174,7 @@ impl GetAttr for PySuper { .skip_while(|cls| !cls.is(&zelf.inner.read().typ)) .skip(1) // skip su->type (if any) .collect(); - for cls in mro.iter() { + for cls in &mro { if let Some(descr) = cls.get_direct_attr(name) { return vm .call_get_descriptor_specific( diff --git a/vm/src/builtins/traceback.rs b/vm/src/builtins/traceback.rs index 683bbf1654..b5a185a8ab 100644 --- a/vm/src/builtins/traceback.rs +++ b/vm/src/builtins/traceback.rs @@ -69,13 +69,13 @@ impl PyTraceback { } impl Constructor for PyTraceback { - type Args = (Option>, FrameRef, u32, usize); + type Args = (Option>, FrameRef, u32, usize); fn py_new(cls: PyTypeRef, args: Self::Args, vm: &VirtualMachine) -> PyResult { let (next, frame, lasti, lineno) = args; let lineno = OneIndexed::new(lineno) .ok_or_else(|| vm.new_value_error("lineno must be positive".to_owned()))?; - let tb = PyTraceback::new(next, frame, lasti, lineno); + let tb = Self::new(next, frame, lasti, lineno); tb.into_ref_with_type(vm, cls).map(Into::into) } } diff --git a/vm/src/builtins/tuple.rs b/vm/src/builtins/tuple.rs index 2c3255b249..b8daeacd96 100644 --- a/vm/src/builtins/tuple.rs +++ b/vm/src/builtins/tuple.rs @@ -231,13 +231,13 @@ impl PyTuple { } impl PyTuple> { - pub fn new_ref_typed(elements: Vec>, ctx: &Context) -> PyRef>> { + pub fn new_ref_typed(elements: Vec>, ctx: &Context) -> PyRef { // SAFETY: PyRef has the same layout as PyObjectRef unsafe { let elements: Vec = std::mem::transmute::>, Vec>(elements); let tuple = PyTuple::::new_ref(elements, ctx); - std::mem::transmute::, PyRef>>>(tuple) + std::mem::transmute::, PyRef>(tuple) } } } @@ -338,7 +338,7 @@ impl PyTuple { } fn _contains(&self, needle: &PyObject, vm: &VirtualMachine) -> PyResult { - for element in self.elements.iter() { + for element in &self.elements { if vm.identical_or_equal(element, needle)? { return Ok(true); } @@ -482,21 +482,21 @@ impl PyRef> { as TransmuteFromObject>::check(vm, elem)?; } // SAFETY: We just verified all elements are of type T - Ok(unsafe { std::mem::transmute::, PyRef>>>(self) }) + Ok(unsafe { std::mem::transmute::>>>(self) }) } } impl PyRef>> { pub fn into_untyped(self) -> PyRef { // SAFETY: PyTuple> has the same layout as PyTuple - unsafe { std::mem::transmute::>>, PyRef>(self) } + unsafe { std::mem::transmute::>(self) } } } impl Py>> { pub fn as_untyped(&self) -> &Py { // SAFETY: PyTuple> has the same layout as PyTuple - unsafe { std::mem::transmute::<&Py>>, &Py>(self) } + unsafe { std::mem::transmute::<&Self, &Py>(self) } } } diff --git a/vm/src/frame.rs b/vm/src/frame.rs index b3954b6f44..f88e30a220 100644 --- a/vm/src/frame.rs +++ b/vm/src/frame.rs @@ -341,7 +341,7 @@ impl ExecutingFrame<'_> { } #[inline(always)] - fn lasti(&self) -> u32 { + const fn lasti(&self) -> u32 { #[cfg(feature = "threading")] { self.state.lasti @@ -1313,7 +1313,7 @@ impl ExecutingFrame<'_> { // push a tuple of extracted attributes. if subject.is_instance(cls.as_ref(), vm)? { let mut extracted = vec![]; - for name in names.iter() { + for name in names { let name_str = name.downcast_ref::().unwrap(); let value = subject.get_attr(name_str, vm)?; extracted.push(value); @@ -2074,7 +2074,7 @@ impl ExecutingFrame<'_> { bytecode::TestOperator::NotIn => self._not_in(vm, &a, &b)?, bytecode::TestOperator::ExceptionMatch => { if let Some(tuple_of_exceptions) = b.downcast_ref::() { - for exception in tuple_of_exceptions.iter() { + for exception in tuple_of_exceptions { if !exception .is_subclass(vm.ctx.exceptions.base_exception_type.into(), vm)? { diff --git a/vm/src/stdlib/ast/python.rs b/vm/src/stdlib/ast/python.rs index 2a4f7cf157..d0b19d49da 100644 --- a/vm/src/stdlib/ast/python.rs +++ b/vm/src/stdlib/ast/python.rs @@ -71,7 +71,7 @@ pub(crate) mod _ast { let zelf = vm.ctx.new_base_object(cls, dict); // Initialize the instance with the provided arguments - NodeAst::__init__(zelf.clone(), args, vm)?; + Self::__init__(zelf.clone(), args, vm)?; Ok(zelf) } diff --git a/vm/src/stdlib/io.rs b/vm/src/stdlib/io.rs index 67ac51615a..1a35e1889e 100644 --- a/vm/src/stdlib/io.rs +++ b/vm/src/stdlib/io.rs @@ -3601,7 +3601,7 @@ mod _io { } #[pygetset] - fn line_buffering(&self) -> bool { + const fn line_buffering(&self) -> bool { false } } diff --git a/vm/src/stdlib/itertools.rs b/vm/src/stdlib/itertools.rs index 7653ac65a4..6a8d6e50a1 100644 --- a/vm/src/stdlib/itertools.rs +++ b/vm/src/stdlib/itertools.rs @@ -1381,7 +1381,7 @@ mod decl { } let mut pools: Vec = Vec::new(); - for element in zelf.pools.iter() { + for element in &zelf.pools { pools.push(element.clone().into_pytuple(vm).into()); } diff --git a/vm/src/stdlib/posix.rs b/vm/src/stdlib/posix.rs index 6220a15b8a..cf3fbb64a8 100644 --- a/vm/src/stdlib/posix.rs +++ b/vm/src/stdlib/posix.rs @@ -516,7 +516,7 @@ pub mod module { if reversed { funcs.reverse(); } - for func in funcs.into_iter() { + for func in funcs { if let Err(e) = func.call((), vm) { let exit = e.fast_isinstance(vm.ctx.exceptions.system_exit); vm.run_unraisable(e, Some("Exception ignored in".to_owned()), func); @@ -1228,7 +1228,7 @@ pub mod module { } #[pyfunction] - fn sync() { + const fn sync() { #[cfg(not(any(target_os = "redox", target_os = "android")))] unsafe { libc::sync(); @@ -1603,32 +1603,32 @@ pub mod module { } #[pyfunction(name = "WIFSIGNALED")] - fn wifsignaled(status: i32) -> bool { + const fn wifsignaled(status: i32) -> bool { libc::WIFSIGNALED(status) } #[pyfunction(name = "WIFSTOPPED")] - fn wifstopped(status: i32) -> bool { + const fn wifstopped(status: i32) -> bool { libc::WIFSTOPPED(status) } #[pyfunction(name = "WIFEXITED")] - fn wifexited(status: i32) -> bool { + const fn wifexited(status: i32) -> bool { libc::WIFEXITED(status) } #[pyfunction(name = "WTERMSIG")] - fn wtermsig(status: i32) -> i32 { + const fn wtermsig(status: i32) -> i32 { libc::WTERMSIG(status) } #[pyfunction(name = "WSTOPSIG")] - fn wstopsig(status: i32) -> i32 { + const fn wstopsig(status: i32) -> i32 { libc::WSTOPSIG(status) } #[pyfunction(name = "WEXITSTATUS")] - fn wexitstatus(status: i32) -> i32 { + const fn wexitstatus(status: i32) -> i32 { libc::WEXITSTATUS(status) } diff --git a/vm/src/stdlib/symtable.rs b/vm/src/stdlib/symtable.rs index 18c4836954..0ee137642c 100644 --- a/vm/src/stdlib/symtable.rs +++ b/vm/src/stdlib/symtable.rs @@ -69,7 +69,7 @@ mod symtable { } #[pymethod] - fn is_optimized(&self) -> bool { + const fn is_optimized(&self) -> bool { matches!( self.symtable.typ, CompilerScope::Function | CompilerScope::AsyncFunction diff --git a/vm/src/stdlib/thread.rs b/vm/src/stdlib/thread.rs index 6b751f3074..90e363774f 100644 --- a/vm/src/stdlib/thread.rs +++ b/vm/src/stdlib/thread.rs @@ -286,7 +286,7 @@ pub(crate) mod _thread { } #[pyfunction] - fn allocate_lock() -> Lock { + const fn allocate_lock() -> Lock { Lock { mu: RawMutex::INIT } } diff --git a/vm/src/stdlib/typing.rs b/vm/src/stdlib/typing.rs index 2491a5ce29..2c4517fc7b 100644 --- a/vm/src/stdlib/typing.rs +++ b/vm/src/stdlib/typing.rs @@ -165,7 +165,7 @@ pub(crate) mod decl { vm.ctx.empty_tuple.clone() }; - let ta = TypeAliasType::new(name, type_params, value); + let ta = Self::new(name, type_params, value); ta.into_ref_with_type(vm, cls).map(Into::into) } } diff --git a/vm/sre_engine/src/engine.rs b/vm/sre_engine/src/engine.rs index 92b62e9f63..9cc2e4788a 100644 --- a/vm/sre_engine/src/engine.rs +++ b/vm/sre_engine/src/engine.rs @@ -68,7 +68,7 @@ impl Marks { } } - pub fn last_index(&self) -> isize { + pub const fn last_index(&self) -> isize { self.last_index } @@ -1054,11 +1054,11 @@ impl MatchContext { &req.pattern_codes[self.code_position..] } - fn remaining_codes(&self, req: &Request<'_, S>) -> usize { + const fn remaining_codes(&self, req: &Request<'_, S>) -> usize { req.pattern_codes.len() - self.code_position } - fn remaining_chars(&self, req: &Request<'_, S>) -> usize { + const fn remaining_chars(&self, req: &Request<'_, S>) -> usize { req.end - self.cursor.position } @@ -1097,7 +1097,7 @@ impl MatchContext { self.peek_code(req, peek).try_into() } - fn skip_code(&mut self, skip: usize) { + const fn skip_code(&mut self, skip: usize) { self.code_position += skip; } @@ -1105,12 +1105,12 @@ impl MatchContext { self.skip_code(self.peek_code(req, peek) as usize + 1); } - fn at_beginning(&self) -> bool { + const fn at_beginning(&self) -> bool { // self.ctx().string_position == self.state().start self.cursor.position == 0 } - fn at_end(&self, req: &Request<'_, S>) -> bool { + const fn at_end(&self, req: &Request<'_, S>) -> bool { self.cursor.position == req.end } @@ -1144,7 +1144,7 @@ impl MatchContext { this == that } - fn can_success(&self, req: &Request<'_, S>) -> bool { + const fn can_success(&self, req: &Request<'_, S>) -> bool { if !self.toplevel { return true; } @@ -1163,12 +1163,12 @@ impl MatchContext { } #[must_use] - fn next_offset(&mut self, offset: usize, jump: Jump) -> Self { + const fn next_offset(&mut self, offset: usize, jump: Jump) -> Self { self.next_at(self.code_position + offset, jump) } #[must_use] - fn next_at(&mut self, code_position: usize, jump: Jump) -> Self { + const fn next_at(&mut self, code_position: usize, jump: Jump) -> Self { self.jump = jump; Self { code_position, diff --git a/vm/sre_engine/src/string.rs b/vm/sre_engine/src/string.rs index 9ca97069f8..0d3325b6a1 100644 --- a/vm/sre_engine/src/string.rs +++ b/vm/sre_engine/src/string.rs @@ -223,7 +223,7 @@ impl StrDrive for &Wtf8 { /// /// `bytes` must produce a valid UTF-8-like (UTF-8 or WTF-8) string #[inline] -unsafe fn next_code_point(ptr: &mut *const u8) -> u32 { +const unsafe fn next_code_point(ptr: &mut *const u8) -> u32 { // Decode UTF-8 let x = unsafe { **ptr }; *ptr = unsafe { ptr.offset(1) }; @@ -271,7 +271,7 @@ unsafe fn next_code_point(ptr: &mut *const u8) -> u32 { /// /// `bytes` must produce a valid UTF-8-like (UTF-8 or WTF-8) string #[inline] -unsafe fn next_code_point_reverse(ptr: &mut *const u8) -> u32 { +const unsafe fn next_code_point_reverse(ptr: &mut *const u8) -> u32 { // Decode UTF-8 *ptr = unsafe { ptr.offset(-1) }; let w = match unsafe { **ptr } { @@ -367,7 +367,7 @@ pub(crate) fn is_loc_word(ch: u32) -> bool { ch == '_' as u32 || is_loc_alnum(ch) } #[inline] -pub(crate) fn is_linebreak(ch: u32) -> bool { +pub(crate) const fn is_linebreak(ch: u32) -> bool { ch == '\n' as u32 } #[inline] @@ -433,7 +433,7 @@ pub(crate) fn is_uni_space(ch: u32) -> bool { ) } #[inline] -pub(crate) fn is_uni_linebreak(ch: u32) -> bool { +pub(crate) const fn is_uni_linebreak(ch: u32) -> bool { matches!( ch, 0x000A | 0x000B | 0x000C | 0x000D | 0x001C | 0x001D | 0x001E | 0x0085 | 0x2028 | 0x2029 diff --git a/wtf8/src/core_str_count.rs b/wtf8/src/core_str_count.rs index 9bc2d8d5b8..f25454d267 100644 --- a/wtf8/src/core_str_count.rs +++ b/wtf8/src/core_str_count.rs @@ -95,7 +95,7 @@ fn do_count_chars(s: &Wtf8) -> usize { // false), and bytes which are non-continuation bytes are left as `0x01` (e.g. // true) #[inline] -fn contains_non_continuation_byte(w: usize) -> usize { +const fn contains_non_continuation_byte(w: usize) -> usize { const LSB: usize = usize_repeat_u8(0x01); ((!w >> 7) | (w >> 6)) & LSB } @@ -103,7 +103,7 @@ fn contains_non_continuation_byte(w: usize) -> usize { // Morally equivalent to `values.to_ne_bytes().into_iter().sum::()`, but // more efficient. #[inline] -fn sum_bytes_in_usize(values: usize) -> usize { +const fn sum_bytes_in_usize(values: usize) -> usize { const LSB_SHORTS: usize = usize_repeat_u16(0x0001); const SKIP_BYTES: usize = usize_repeat_u16(0x00ff); diff --git a/wtf8/src/lib.rs b/wtf8/src/lib.rs index 01deb61f19..110abb8fa2 100644 --- a/wtf8/src/lib.rs +++ b/wtf8/src/lib.rs @@ -91,17 +91,17 @@ impl CodePoint { /// /// `value` must be less than or equal to 0x10FFFF. #[inline] - pub const unsafe fn from_u32_unchecked(value: u32) -> CodePoint { - CodePoint { value } + pub const unsafe fn from_u32_unchecked(value: u32) -> Self { + Self { value } } /// Creates a new `CodePoint` if the value is a valid code point. /// /// Returns `None` if `value` is above 0x10FFFF. #[inline] - pub const fn from_u32(value: u32) -> Option { + pub const fn from_u32(value: u32) -> Option { match value { - 0..=0x10FFFF => Some(CodePoint { value }), + 0..=0x10FFFF => Some(Self { value }), _ => None, } } @@ -110,8 +110,8 @@ impl CodePoint { /// /// Since all Unicode scalar values are code points, this always succeeds. #[inline] - pub const fn from_char(value: char) -> CodePoint { - CodePoint { + pub const fn from_char(value: char) -> Self { + Self { value: value as u32, } } @@ -212,6 +212,7 @@ impl PartialEq for CodePoint { self.to_u32() == *other as u32 } } + impl PartialEq for char { fn eq(&self, other: &CodePoint) -> bool { *self as u32 == other.to_u32() @@ -242,7 +243,7 @@ pub struct Wtf8Buf { impl ops::Deref for Wtf8Buf { type Target = Wtf8; - fn deref(&self) -> &Wtf8 { + fn deref(&self) -> &Self::Target { self.as_slice() } } @@ -283,14 +284,14 @@ impl fmt::Display for Wtf8Buf { impl Wtf8Buf { /// Creates a new, empty WTF-8 string. #[inline] - pub fn new() -> Wtf8Buf { - Wtf8Buf::default() + pub fn new() -> Self { + Self::default() } /// Creates a new, empty WTF-8 string with pre-allocated capacity for `capacity` bytes. #[inline] - pub fn with_capacity(capacity: usize) -> Wtf8Buf { - Wtf8Buf { + pub fn with_capacity(capacity: usize) -> Self { + Self { bytes: Vec::with_capacity(capacity), } } @@ -301,8 +302,8 @@ impl Wtf8Buf { /// /// `value` must contain valid WTF-8. #[inline] - pub const unsafe fn from_bytes_unchecked(value: Vec) -> Wtf8Buf { - Wtf8Buf { bytes: value } + pub const unsafe fn from_bytes_unchecked(value: Vec) -> Self { + Self { bytes: value } } /// Create a WTF-8 string from a WTF-8 byte vec. @@ -319,8 +320,8 @@ impl Wtf8Buf { /// /// Since WTF-8 is a superset of UTF-8, this always succeeds. #[inline] - pub fn from_string(string: String) -> Wtf8Buf { - Wtf8Buf { + pub fn from_string(string: String) -> Self { + Self { bytes: string.into_bytes(), } } @@ -333,8 +334,8 @@ impl Wtf8Buf { /// /// This is lossless: calling `.encode_wide()` on the resulting string /// will always return the original code units. - pub fn from_wide(v: &[u16]) -> Wtf8Buf { - let mut string = Wtf8Buf::with_capacity(v.len()); + pub fn from_wide(v: &[u16]) -> Self { + let mut string = Self::with_capacity(v.len()); for item in char::decode_utf16(v.iter().cloned()) { match item { Ok(ch) => string.push_char(ch), @@ -509,7 +510,7 @@ impl Wtf8Buf { /// If the contents are not well-formed UTF-8 /// (that is, if the string contains surrogates), /// the original WTF-8 string is returned instead. - pub fn into_string(self) -> Result { + pub fn into_string(self) -> Result { if self.is_utf8() { Ok(unsafe { String::from_utf8_unchecked(self.bytes) }) } else { @@ -541,9 +542,9 @@ impl Wtf8Buf { } /// Converts a `Box` into a `Wtf8Buf`. - pub fn from_box(boxed: Box) -> Wtf8Buf { + pub fn from_box(boxed: Box) -> Self { let bytes: Box<[u8]> = unsafe { mem::transmute(boxed) }; - Wtf8Buf { + Self { bytes: bytes.into_vec(), } } @@ -554,8 +555,8 @@ impl Wtf8Buf { /// This replaces surrogate code point pairs with supplementary code points, /// like concatenating ill-formed UTF-16 strings effectively would. impl FromIterator for Wtf8Buf { - fn from_iter>(iter: T) -> Wtf8Buf { - let mut string = Wtf8Buf::new(); + fn from_iter>(iter: T) -> Self { + let mut string = Self::new(); string.extend(iter); string } @@ -590,7 +591,7 @@ impl> Extend for Wtf8Buf { impl> FromIterator for Wtf8Buf { fn from_iter>(iter: T) -> Self { - let mut buf = Wtf8Buf::new(); + let mut buf = Self::new(); iter.into_iter().for_each(|w| buf.push_wtf8(w.as_ref())); buf } @@ -610,19 +611,19 @@ impl AsRef for Wtf8Buf { impl From for Wtf8Buf { fn from(s: String) -> Self { - Wtf8Buf::from_string(s) + Self::from_string(s) } } impl From<&str> for Wtf8Buf { fn from(s: &str) -> Self { - Wtf8Buf::from_string(s.to_owned()) + Self::from_string(s.to_owned()) } } impl From for Wtf8Buf { fn from(s: ascii::AsciiString) -> Self { - Wtf8Buf::from_string(s.into()) + Self::from_string(s.into()) } } @@ -635,8 +636,8 @@ pub struct Wtf8 { bytes: [u8], } -impl AsRef for Wtf8 { - fn as_ref(&self) -> &Wtf8 { +impl AsRef for Wtf8 { + fn as_ref(&self) -> &Self { self } } @@ -734,7 +735,7 @@ impl Wtf8 { /// /// Since WTF-8 is a superset of UTF-8, this always succeeds. #[inline] - pub fn new + ?Sized>(value: &S) -> &Wtf8 { + pub fn new + ?Sized>(value: &S) -> &Self { value.as_ref() } @@ -744,9 +745,9 @@ impl Wtf8 { /// /// `value` must contain valid WTF-8. #[inline] - pub const unsafe fn from_bytes_unchecked(value: &[u8]) -> &Wtf8 { + pub const unsafe fn from_bytes_unchecked(value: &[u8]) -> &Self { // SAFETY: start with &[u8], end with fancy &[u8] - unsafe { &*(value as *const [u8] as *const Wtf8) } + unsafe { &*(value as *const [u8] as *const Self) } } /// Creates a mutable WTF-8 slice from a mutable WTF-8 byte slice. @@ -754,9 +755,9 @@ impl Wtf8 { /// Since the byte slice is not checked for valid WTF-8, this functions is /// marked unsafe. #[inline] - const unsafe fn from_mut_bytes_unchecked(value: &mut [u8]) -> &mut Wtf8 { + const unsafe fn from_mut_bytes_unchecked(value: &mut [u8]) -> &mut Self { // SAFETY: start with &mut [u8], end with fancy &mut [u8] - unsafe { &mut *(value as *mut [u8] as *mut Wtf8) } + unsafe { &mut *(value as *mut [u8] as *mut Self) } } /// Create a WTF-8 slice from a WTF-8 byte slice. @@ -770,7 +771,7 @@ impl Wtf8 { let _ = Self::decode_surrogate(rest)?; rest = &rest[3..]; } - Some(unsafe { Wtf8::from_bytes_unchecked(b) }) + Some(unsafe { Self::from_bytes_unchecked(b) }) } fn decode_surrogate(b: &[u8]) -> Option { @@ -939,24 +940,24 @@ impl Wtf8 { /// Boxes this `Wtf8`. #[inline] - pub fn into_box(&self) -> Box { + pub fn into_box(&self) -> Box { let boxed: Box<[u8]> = self.bytes.into(); unsafe { mem::transmute(boxed) } } /// Creates a boxed, empty `Wtf8`. - pub fn empty_box() -> Box { - let boxed: Box<[u8]> = Default::default(); + pub fn empty_box() -> Box { + let boxed: Box<[u8]> = Box::default(); unsafe { mem::transmute(boxed) } } #[inline] - pub fn make_ascii_lowercase(&mut self) { + pub const fn make_ascii_lowercase(&mut self) { self.bytes.make_ascii_lowercase() } #[inline] - pub fn make_ascii_uppercase(&mut self) { + pub const fn make_ascii_uppercase(&mut self) { self.bytes.make_ascii_uppercase() } @@ -989,43 +990,43 @@ impl Wtf8 { self.bytes.eq_ignore_ascii_case(&other.bytes) } - pub fn split(&self, pat: &Wtf8) -> impl Iterator { + pub fn split(&self, pat: &Self) -> impl Iterator { self.as_bytes() .split_str(pat) - .map(|w| unsafe { Wtf8::from_bytes_unchecked(w) }) + .map(|w| unsafe { Self::from_bytes_unchecked(w) }) } - pub fn splitn(&self, n: usize, pat: &Wtf8) -> impl Iterator { + pub fn splitn(&self, n: usize, pat: &Self) -> impl Iterator { self.as_bytes() .splitn_str(n, pat) - .map(|w| unsafe { Wtf8::from_bytes_unchecked(w) }) + .map(|w| unsafe { Self::from_bytes_unchecked(w) }) } - pub fn rsplit(&self, pat: &Wtf8) -> impl Iterator { + pub fn rsplit(&self, pat: &Self) -> impl Iterator { self.as_bytes() .rsplit_str(pat) - .map(|w| unsafe { Wtf8::from_bytes_unchecked(w) }) + .map(|w| unsafe { Self::from_bytes_unchecked(w) }) } - pub fn rsplitn(&self, n: usize, pat: &Wtf8) -> impl Iterator { + pub fn rsplitn(&self, n: usize, pat: &Self) -> impl Iterator { self.as_bytes() .rsplitn_str(n, pat) - .map(|w| unsafe { Wtf8::from_bytes_unchecked(w) }) + .map(|w| unsafe { Self::from_bytes_unchecked(w) }) } pub fn trim(&self) -> &Self { let w = self.bytes.trim(); - unsafe { Wtf8::from_bytes_unchecked(w) } + unsafe { Self::from_bytes_unchecked(w) } } pub fn trim_start(&self) -> &Self { let w = self.bytes.trim_start(); - unsafe { Wtf8::from_bytes_unchecked(w) } + unsafe { Self::from_bytes_unchecked(w) } } pub fn trim_end(&self) -> &Self { let w = self.bytes.trim_end(); - unsafe { Wtf8::from_bytes_unchecked(w) } + unsafe { Self::from_bytes_unchecked(w) } } pub fn trim_start_matches(&self, f: impl Fn(CodePoint) -> bool) -> &Self { @@ -1064,23 +1065,23 @@ impl Wtf8 { self.trim_start_matches(&f).trim_end_matches(&f) } - pub fn find(&self, pat: &Wtf8) -> Option { + pub fn find(&self, pat: &Self) -> Option { memchr::memmem::find(self.as_bytes(), pat.as_bytes()) } - pub fn rfind(&self, pat: &Wtf8) -> Option { + pub fn rfind(&self, pat: &Self) -> Option { memchr::memmem::rfind(self.as_bytes(), pat.as_bytes()) } - pub fn find_iter(&self, pat: &Wtf8) -> impl Iterator { + pub fn find_iter(&self, pat: &Self) -> impl Iterator { memchr::memmem::find_iter(self.as_bytes(), pat.as_bytes()) } - pub fn rfind_iter(&self, pat: &Wtf8) -> impl Iterator { + pub fn rfind_iter(&self, pat: &Self) -> impl Iterator { memchr::memmem::rfind_iter(self.as_bytes(), pat.as_bytes()) } - pub fn contains(&self, pat: &Wtf8) -> bool { + pub fn contains(&self, pat: &Self) -> bool { self.bytes.contains_str(pat) } @@ -1109,32 +1110,32 @@ impl Wtf8 { } } - pub fn ends_with(&self, w: &Wtf8) -> bool { + pub fn ends_with(&self, w: &Self) -> bool { self.bytes.ends_with_str(w) } - pub fn starts_with(&self, w: &Wtf8) -> bool { + pub fn starts_with(&self, w: &Self) -> bool { self.bytes.starts_with_str(w) } - pub fn strip_prefix(&self, w: &Wtf8) -> Option<&Self> { + pub fn strip_prefix(&self, w: &Self) -> Option<&Self> { self.bytes .strip_prefix(w.as_bytes()) - .map(|w| unsafe { Wtf8::from_bytes_unchecked(w) }) + .map(|w| unsafe { Self::from_bytes_unchecked(w) }) } - pub fn strip_suffix(&self, w: &Wtf8) -> Option<&Self> { + pub fn strip_suffix(&self, w: &Self) -> Option<&Self> { self.bytes .strip_suffix(w.as_bytes()) - .map(|w| unsafe { Wtf8::from_bytes_unchecked(w) }) + .map(|w| unsafe { Self::from_bytes_unchecked(w) }) } - pub fn replace(&self, from: &Wtf8, to: &Wtf8) -> Wtf8Buf { + pub fn replace(&self, from: &Self, to: &Self) -> Wtf8Buf { let w = self.bytes.replace(from, to); unsafe { Wtf8Buf::from_bytes_unchecked(w) } } - pub fn replacen(&self, from: &Wtf8, to: &Wtf8, n: usize) -> Wtf8Buf { + pub fn replacen(&self, from: &Self, to: &Self, n: usize) -> Wtf8Buf { let w = self.bytes.replacen(from, to, n); unsafe { Wtf8Buf::from_bytes_unchecked(w) } } @@ -1159,11 +1160,11 @@ impl AsRef<[u8]> for Wtf8 { /// Panics when `begin` and `end` do not point to code point boundaries, /// or point beyond the end of the string. impl ops::Index> for Wtf8 { - type Output = Wtf8; + type Output = Self; #[inline] #[track_caller] - fn index(&self, range: ops::Range) -> &Wtf8 { + fn index(&self, range: ops::Range) -> &Self { // is_code_point_boundary checks that the index is in [0, .len()] if range.start <= range.end && is_code_point_boundary(self, range.start) @@ -1183,11 +1184,11 @@ impl ops::Index> for Wtf8 { /// Panics when `begin` is not at a code point boundary, /// or is beyond the end of the string. impl ops::Index> for Wtf8 { - type Output = Wtf8; + type Output = Self; #[inline] #[track_caller] - fn index(&self, range: ops::RangeFrom) -> &Wtf8 { + fn index(&self, range: ops::RangeFrom) -> &Self { // is_code_point_boundary checks that the index is in [0, .len()] if is_code_point_boundary(self, range.start) { unsafe { slice_unchecked(self, range.start, self.len()) } @@ -1204,11 +1205,11 @@ impl ops::Index> for Wtf8 { /// Panics when `end` is not at a code point boundary, /// or is beyond the end of the string. impl ops::Index> for Wtf8 { - type Output = Wtf8; + type Output = Self; #[inline] #[track_caller] - fn index(&self, range: ops::RangeTo) -> &Wtf8 { + fn index(&self, range: ops::RangeTo) -> &Self { // is_code_point_boundary checks that the index is in [0, .len()] if is_code_point_boundary(self, range.end) { unsafe { slice_unchecked(self, 0, range.end) } @@ -1219,10 +1220,10 @@ impl ops::Index> for Wtf8 { } impl ops::Index for Wtf8 { - type Output = Wtf8; + type Output = Self; #[inline] - fn index(&self, _range: ops::RangeFull) -> &Wtf8 { + fn index(&self, _range: ops::RangeFull) -> &Self { self } } @@ -1535,7 +1536,7 @@ impl From> for Box { impl From> for Box<[u8]> { fn from(w: Box) -> Self { - unsafe { Box::from_raw(Box::into_raw(w) as *mut [u8]) } + unsafe { Self::from_raw(Box::into_raw(w) as *mut [u8]) } } } @@ -1547,7 +1548,7 @@ impl From for Box { impl From> for Wtf8Buf { fn from(w: Box) -> Self { - Wtf8Buf::from_box(w) + Self::from_box(w) } } From 83a1076caf41f69deb1861884b02cc6cbb88e52a Mon Sep 17 00:00:00 2001 From: ShaharNaveh <50263213+ShaharNaveh@users.noreply.github.com> Date: Tue, 29 Jul 2025 09:59:12 +0200 Subject: [PATCH 2/4] Don't mark all fn as `const` --- vm/src/stdlib/posix.rs | 14 +++++++------- 1 file changed, 7 insertions(+), 7 deletions(-) diff --git a/vm/src/stdlib/posix.rs b/vm/src/stdlib/posix.rs index cf3fbb64a8..486764c105 100644 --- a/vm/src/stdlib/posix.rs +++ b/vm/src/stdlib/posix.rs @@ -1228,7 +1228,7 @@ pub mod module { } #[pyfunction] - const fn sync() { + fn sync() { #[cfg(not(any(target_os = "redox", target_os = "android")))] unsafe { libc::sync(); @@ -1603,32 +1603,32 @@ pub mod module { } #[pyfunction(name = "WIFSIGNALED")] - const fn wifsignaled(status: i32) -> bool { + fn wifsignaled(status: i32) -> bool { libc::WIFSIGNALED(status) } #[pyfunction(name = "WIFSTOPPED")] - const fn wifstopped(status: i32) -> bool { + fn wifstopped(status: i32) -> bool { libc::WIFSTOPPED(status) } #[pyfunction(name = "WIFEXITED")] - const fn wifexited(status: i32) -> bool { + fn wifexited(status: i32) -> bool { libc::WIFEXITED(status) } #[pyfunction(name = "WTERMSIG")] - const fn wtermsig(status: i32) -> i32 { + fn wtermsig(status: i32) -> i32 { libc::WTERMSIG(status) } #[pyfunction(name = "WSTOPSIG")] - const fn wstopsig(status: i32) -> i32 { + fn wstopsig(status: i32) -> i32 { libc::WSTOPSIG(status) } #[pyfunction(name = "WEXITSTATUS")] - const fn wexitstatus(status: i32) -> i32 { + fn wexitstatus(status: i32) -> i32 { libc::WEXITSTATUS(status) } From f26415e10236ce1685aa84adfdee740eeb0a43b0 Mon Sep 17 00:00:00 2001 From: ShaharNaveh <50263213+ShaharNaveh@users.noreply.github.com> Date: Tue, 29 Jul 2025 22:13:50 +0200 Subject: [PATCH 3/4] Revert changes to wtf8 --- wtf8/src/core_str_count.rs | 4 +- wtf8/src/lib.rs | 147 ++++++++++++++++++------------------- 2 files changed, 75 insertions(+), 76 deletions(-) diff --git a/wtf8/src/core_str_count.rs b/wtf8/src/core_str_count.rs index f25454d267..9bc2d8d5b8 100644 --- a/wtf8/src/core_str_count.rs +++ b/wtf8/src/core_str_count.rs @@ -95,7 +95,7 @@ fn do_count_chars(s: &Wtf8) -> usize { // false), and bytes which are non-continuation bytes are left as `0x01` (e.g. // true) #[inline] -const fn contains_non_continuation_byte(w: usize) -> usize { +fn contains_non_continuation_byte(w: usize) -> usize { const LSB: usize = usize_repeat_u8(0x01); ((!w >> 7) | (w >> 6)) & LSB } @@ -103,7 +103,7 @@ const fn contains_non_continuation_byte(w: usize) -> usize { // Morally equivalent to `values.to_ne_bytes().into_iter().sum::()`, but // more efficient. #[inline] -const fn sum_bytes_in_usize(values: usize) -> usize { +fn sum_bytes_in_usize(values: usize) -> usize { const LSB_SHORTS: usize = usize_repeat_u16(0x0001); const SKIP_BYTES: usize = usize_repeat_u16(0x00ff); diff --git a/wtf8/src/lib.rs b/wtf8/src/lib.rs index 110abb8fa2..01deb61f19 100644 --- a/wtf8/src/lib.rs +++ b/wtf8/src/lib.rs @@ -91,17 +91,17 @@ impl CodePoint { /// /// `value` must be less than or equal to 0x10FFFF. #[inline] - pub const unsafe fn from_u32_unchecked(value: u32) -> Self { - Self { value } + pub const unsafe fn from_u32_unchecked(value: u32) -> CodePoint { + CodePoint { value } } /// Creates a new `CodePoint` if the value is a valid code point. /// /// Returns `None` if `value` is above 0x10FFFF. #[inline] - pub const fn from_u32(value: u32) -> Option { + pub const fn from_u32(value: u32) -> Option { match value { - 0..=0x10FFFF => Some(Self { value }), + 0..=0x10FFFF => Some(CodePoint { value }), _ => None, } } @@ -110,8 +110,8 @@ impl CodePoint { /// /// Since all Unicode scalar values are code points, this always succeeds. #[inline] - pub const fn from_char(value: char) -> Self { - Self { + pub const fn from_char(value: char) -> CodePoint { + CodePoint { value: value as u32, } } @@ -212,7 +212,6 @@ impl PartialEq for CodePoint { self.to_u32() == *other as u32 } } - impl PartialEq for char { fn eq(&self, other: &CodePoint) -> bool { *self as u32 == other.to_u32() @@ -243,7 +242,7 @@ pub struct Wtf8Buf { impl ops::Deref for Wtf8Buf { type Target = Wtf8; - fn deref(&self) -> &Self::Target { + fn deref(&self) -> &Wtf8 { self.as_slice() } } @@ -284,14 +283,14 @@ impl fmt::Display for Wtf8Buf { impl Wtf8Buf { /// Creates a new, empty WTF-8 string. #[inline] - pub fn new() -> Self { - Self::default() + pub fn new() -> Wtf8Buf { + Wtf8Buf::default() } /// Creates a new, empty WTF-8 string with pre-allocated capacity for `capacity` bytes. #[inline] - pub fn with_capacity(capacity: usize) -> Self { - Self { + pub fn with_capacity(capacity: usize) -> Wtf8Buf { + Wtf8Buf { bytes: Vec::with_capacity(capacity), } } @@ -302,8 +301,8 @@ impl Wtf8Buf { /// /// `value` must contain valid WTF-8. #[inline] - pub const unsafe fn from_bytes_unchecked(value: Vec) -> Self { - Self { bytes: value } + pub const unsafe fn from_bytes_unchecked(value: Vec) -> Wtf8Buf { + Wtf8Buf { bytes: value } } /// Create a WTF-8 string from a WTF-8 byte vec. @@ -320,8 +319,8 @@ impl Wtf8Buf { /// /// Since WTF-8 is a superset of UTF-8, this always succeeds. #[inline] - pub fn from_string(string: String) -> Self { - Self { + pub fn from_string(string: String) -> Wtf8Buf { + Wtf8Buf { bytes: string.into_bytes(), } } @@ -334,8 +333,8 @@ impl Wtf8Buf { /// /// This is lossless: calling `.encode_wide()` on the resulting string /// will always return the original code units. - pub fn from_wide(v: &[u16]) -> Self { - let mut string = Self::with_capacity(v.len()); + pub fn from_wide(v: &[u16]) -> Wtf8Buf { + let mut string = Wtf8Buf::with_capacity(v.len()); for item in char::decode_utf16(v.iter().cloned()) { match item { Ok(ch) => string.push_char(ch), @@ -510,7 +509,7 @@ impl Wtf8Buf { /// If the contents are not well-formed UTF-8 /// (that is, if the string contains surrogates), /// the original WTF-8 string is returned instead. - pub fn into_string(self) -> Result { + pub fn into_string(self) -> Result { if self.is_utf8() { Ok(unsafe { String::from_utf8_unchecked(self.bytes) }) } else { @@ -542,9 +541,9 @@ impl Wtf8Buf { } /// Converts a `Box` into a `Wtf8Buf`. - pub fn from_box(boxed: Box) -> Self { + pub fn from_box(boxed: Box) -> Wtf8Buf { let bytes: Box<[u8]> = unsafe { mem::transmute(boxed) }; - Self { + Wtf8Buf { bytes: bytes.into_vec(), } } @@ -555,8 +554,8 @@ impl Wtf8Buf { /// This replaces surrogate code point pairs with supplementary code points, /// like concatenating ill-formed UTF-16 strings effectively would. impl FromIterator for Wtf8Buf { - fn from_iter>(iter: T) -> Self { - let mut string = Self::new(); + fn from_iter>(iter: T) -> Wtf8Buf { + let mut string = Wtf8Buf::new(); string.extend(iter); string } @@ -591,7 +590,7 @@ impl> Extend for Wtf8Buf { impl> FromIterator for Wtf8Buf { fn from_iter>(iter: T) -> Self { - let mut buf = Self::new(); + let mut buf = Wtf8Buf::new(); iter.into_iter().for_each(|w| buf.push_wtf8(w.as_ref())); buf } @@ -611,19 +610,19 @@ impl AsRef for Wtf8Buf { impl From for Wtf8Buf { fn from(s: String) -> Self { - Self::from_string(s) + Wtf8Buf::from_string(s) } } impl From<&str> for Wtf8Buf { fn from(s: &str) -> Self { - Self::from_string(s.to_owned()) + Wtf8Buf::from_string(s.to_owned()) } } impl From for Wtf8Buf { fn from(s: ascii::AsciiString) -> Self { - Self::from_string(s.into()) + Wtf8Buf::from_string(s.into()) } } @@ -636,8 +635,8 @@ pub struct Wtf8 { bytes: [u8], } -impl AsRef for Wtf8 { - fn as_ref(&self) -> &Self { +impl AsRef for Wtf8 { + fn as_ref(&self) -> &Wtf8 { self } } @@ -735,7 +734,7 @@ impl Wtf8 { /// /// Since WTF-8 is a superset of UTF-8, this always succeeds. #[inline] - pub fn new + ?Sized>(value: &S) -> &Self { + pub fn new + ?Sized>(value: &S) -> &Wtf8 { value.as_ref() } @@ -745,9 +744,9 @@ impl Wtf8 { /// /// `value` must contain valid WTF-8. #[inline] - pub const unsafe fn from_bytes_unchecked(value: &[u8]) -> &Self { + pub const unsafe fn from_bytes_unchecked(value: &[u8]) -> &Wtf8 { // SAFETY: start with &[u8], end with fancy &[u8] - unsafe { &*(value as *const [u8] as *const Self) } + unsafe { &*(value as *const [u8] as *const Wtf8) } } /// Creates a mutable WTF-8 slice from a mutable WTF-8 byte slice. @@ -755,9 +754,9 @@ impl Wtf8 { /// Since the byte slice is not checked for valid WTF-8, this functions is /// marked unsafe. #[inline] - const unsafe fn from_mut_bytes_unchecked(value: &mut [u8]) -> &mut Self { + const unsafe fn from_mut_bytes_unchecked(value: &mut [u8]) -> &mut Wtf8 { // SAFETY: start with &mut [u8], end with fancy &mut [u8] - unsafe { &mut *(value as *mut [u8] as *mut Self) } + unsafe { &mut *(value as *mut [u8] as *mut Wtf8) } } /// Create a WTF-8 slice from a WTF-8 byte slice. @@ -771,7 +770,7 @@ impl Wtf8 { let _ = Self::decode_surrogate(rest)?; rest = &rest[3..]; } - Some(unsafe { Self::from_bytes_unchecked(b) }) + Some(unsafe { Wtf8::from_bytes_unchecked(b) }) } fn decode_surrogate(b: &[u8]) -> Option { @@ -940,24 +939,24 @@ impl Wtf8 { /// Boxes this `Wtf8`. #[inline] - pub fn into_box(&self) -> Box { + pub fn into_box(&self) -> Box { let boxed: Box<[u8]> = self.bytes.into(); unsafe { mem::transmute(boxed) } } /// Creates a boxed, empty `Wtf8`. - pub fn empty_box() -> Box { - let boxed: Box<[u8]> = Box::default(); + pub fn empty_box() -> Box { + let boxed: Box<[u8]> = Default::default(); unsafe { mem::transmute(boxed) } } #[inline] - pub const fn make_ascii_lowercase(&mut self) { + pub fn make_ascii_lowercase(&mut self) { self.bytes.make_ascii_lowercase() } #[inline] - pub const fn make_ascii_uppercase(&mut self) { + pub fn make_ascii_uppercase(&mut self) { self.bytes.make_ascii_uppercase() } @@ -990,43 +989,43 @@ impl Wtf8 { self.bytes.eq_ignore_ascii_case(&other.bytes) } - pub fn split(&self, pat: &Self) -> impl Iterator { + pub fn split(&self, pat: &Wtf8) -> impl Iterator { self.as_bytes() .split_str(pat) - .map(|w| unsafe { Self::from_bytes_unchecked(w) }) + .map(|w| unsafe { Wtf8::from_bytes_unchecked(w) }) } - pub fn splitn(&self, n: usize, pat: &Self) -> impl Iterator { + pub fn splitn(&self, n: usize, pat: &Wtf8) -> impl Iterator { self.as_bytes() .splitn_str(n, pat) - .map(|w| unsafe { Self::from_bytes_unchecked(w) }) + .map(|w| unsafe { Wtf8::from_bytes_unchecked(w) }) } - pub fn rsplit(&self, pat: &Self) -> impl Iterator { + pub fn rsplit(&self, pat: &Wtf8) -> impl Iterator { self.as_bytes() .rsplit_str(pat) - .map(|w| unsafe { Self::from_bytes_unchecked(w) }) + .map(|w| unsafe { Wtf8::from_bytes_unchecked(w) }) } - pub fn rsplitn(&self, n: usize, pat: &Self) -> impl Iterator { + pub fn rsplitn(&self, n: usize, pat: &Wtf8) -> impl Iterator { self.as_bytes() .rsplitn_str(n, pat) - .map(|w| unsafe { Self::from_bytes_unchecked(w) }) + .map(|w| unsafe { Wtf8::from_bytes_unchecked(w) }) } pub fn trim(&self) -> &Self { let w = self.bytes.trim(); - unsafe { Self::from_bytes_unchecked(w) } + unsafe { Wtf8::from_bytes_unchecked(w) } } pub fn trim_start(&self) -> &Self { let w = self.bytes.trim_start(); - unsafe { Self::from_bytes_unchecked(w) } + unsafe { Wtf8::from_bytes_unchecked(w) } } pub fn trim_end(&self) -> &Self { let w = self.bytes.trim_end(); - unsafe { Self::from_bytes_unchecked(w) } + unsafe { Wtf8::from_bytes_unchecked(w) } } pub fn trim_start_matches(&self, f: impl Fn(CodePoint) -> bool) -> &Self { @@ -1065,23 +1064,23 @@ impl Wtf8 { self.trim_start_matches(&f).trim_end_matches(&f) } - pub fn find(&self, pat: &Self) -> Option { + pub fn find(&self, pat: &Wtf8) -> Option { memchr::memmem::find(self.as_bytes(), pat.as_bytes()) } - pub fn rfind(&self, pat: &Self) -> Option { + pub fn rfind(&self, pat: &Wtf8) -> Option { memchr::memmem::rfind(self.as_bytes(), pat.as_bytes()) } - pub fn find_iter(&self, pat: &Self) -> impl Iterator { + pub fn find_iter(&self, pat: &Wtf8) -> impl Iterator { memchr::memmem::find_iter(self.as_bytes(), pat.as_bytes()) } - pub fn rfind_iter(&self, pat: &Self) -> impl Iterator { + pub fn rfind_iter(&self, pat: &Wtf8) -> impl Iterator { memchr::memmem::rfind_iter(self.as_bytes(), pat.as_bytes()) } - pub fn contains(&self, pat: &Self) -> bool { + pub fn contains(&self, pat: &Wtf8) -> bool { self.bytes.contains_str(pat) } @@ -1110,32 +1109,32 @@ impl Wtf8 { } } - pub fn ends_with(&self, w: &Self) -> bool { + pub fn ends_with(&self, w: &Wtf8) -> bool { self.bytes.ends_with_str(w) } - pub fn starts_with(&self, w: &Self) -> bool { + pub fn starts_with(&self, w: &Wtf8) -> bool { self.bytes.starts_with_str(w) } - pub fn strip_prefix(&self, w: &Self) -> Option<&Self> { + pub fn strip_prefix(&self, w: &Wtf8) -> Option<&Self> { self.bytes .strip_prefix(w.as_bytes()) - .map(|w| unsafe { Self::from_bytes_unchecked(w) }) + .map(|w| unsafe { Wtf8::from_bytes_unchecked(w) }) } - pub fn strip_suffix(&self, w: &Self) -> Option<&Self> { + pub fn strip_suffix(&self, w: &Wtf8) -> Option<&Self> { self.bytes .strip_suffix(w.as_bytes()) - .map(|w| unsafe { Self::from_bytes_unchecked(w) }) + .map(|w| unsafe { Wtf8::from_bytes_unchecked(w) }) } - pub fn replace(&self, from: &Self, to: &Self) -> Wtf8Buf { + pub fn replace(&self, from: &Wtf8, to: &Wtf8) -> Wtf8Buf { let w = self.bytes.replace(from, to); unsafe { Wtf8Buf::from_bytes_unchecked(w) } } - pub fn replacen(&self, from: &Self, to: &Self, n: usize) -> Wtf8Buf { + pub fn replacen(&self, from: &Wtf8, to: &Wtf8, n: usize) -> Wtf8Buf { let w = self.bytes.replacen(from, to, n); unsafe { Wtf8Buf::from_bytes_unchecked(w) } } @@ -1160,11 +1159,11 @@ impl AsRef<[u8]> for Wtf8 { /// Panics when `begin` and `end` do not point to code point boundaries, /// or point beyond the end of the string. impl ops::Index> for Wtf8 { - type Output = Self; + type Output = Wtf8; #[inline] #[track_caller] - fn index(&self, range: ops::Range) -> &Self { + fn index(&self, range: ops::Range) -> &Wtf8 { // is_code_point_boundary checks that the index is in [0, .len()] if range.start <= range.end && is_code_point_boundary(self, range.start) @@ -1184,11 +1183,11 @@ impl ops::Index> for Wtf8 { /// Panics when `begin` is not at a code point boundary, /// or is beyond the end of the string. impl ops::Index> for Wtf8 { - type Output = Self; + type Output = Wtf8; #[inline] #[track_caller] - fn index(&self, range: ops::RangeFrom) -> &Self { + fn index(&self, range: ops::RangeFrom) -> &Wtf8 { // is_code_point_boundary checks that the index is in [0, .len()] if is_code_point_boundary(self, range.start) { unsafe { slice_unchecked(self, range.start, self.len()) } @@ -1205,11 +1204,11 @@ impl ops::Index> for Wtf8 { /// Panics when `end` is not at a code point boundary, /// or is beyond the end of the string. impl ops::Index> for Wtf8 { - type Output = Self; + type Output = Wtf8; #[inline] #[track_caller] - fn index(&self, range: ops::RangeTo) -> &Self { + fn index(&self, range: ops::RangeTo) -> &Wtf8 { // is_code_point_boundary checks that the index is in [0, .len()] if is_code_point_boundary(self, range.end) { unsafe { slice_unchecked(self, 0, range.end) } @@ -1220,10 +1219,10 @@ impl ops::Index> for Wtf8 { } impl ops::Index for Wtf8 { - type Output = Self; + type Output = Wtf8; #[inline] - fn index(&self, _range: ops::RangeFull) -> &Self { + fn index(&self, _range: ops::RangeFull) -> &Wtf8 { self } } @@ -1536,7 +1535,7 @@ impl From> for Box { impl From> for Box<[u8]> { fn from(w: Box) -> Self { - unsafe { Self::from_raw(Box::into_raw(w) as *mut [u8]) } + unsafe { Box::from_raw(Box::into_raw(w) as *mut [u8]) } } } @@ -1548,7 +1547,7 @@ impl From for Box { impl From> for Wtf8Buf { fn from(w: Box) -> Self { - Self::from_box(w) + Wtf8Buf::from_box(w) } } From 38e50afb000f45c3fb3710adde085ce1aca1528b Mon Sep 17 00:00:00 2001 From: ShaharNaveh <50263213+ShaharNaveh@users.noreply.github.com> Date: Wed, 30 Jul 2025 00:07:51 +0200 Subject: [PATCH 4/4] Trigger CI