8000 Constify more · RustPython/RustPython@dc8fc74 · GitHub
[go: up one dir, main page]

Skip to content

Commit dc8fc74

Browse files
committed
Constify more
1 parent bd09e55 commit dc8fc74

File tree

8 files changed

+71
-39
lines changed

8 files changed

+71
-39
lines changed

common/src/cformat.rs

Lines changed: 22 additions & 9 deletions
Original file line numberDiff line numberDiff line change
@@ -1,6 +1,6 @@
11
//! Implementation of Printf-Style string formatting
22
//! as per the [Python Docs](https://docs.python.org/3/library/stdtypes.html#printf-style-string-formatting).
3-
use bitflags::bitflags;
3+
use bitflags::{bitflags, bitflags_match};
44
use itertools::Itertools;
55
use malachite_bigint::{BigInt, Sign};
66
use num_traits::Signed;
@@ -137,13 +137,12 @@ bitflags! {
137137
impl CConversionFlags {
138138
#[inline]
139139
pub fn sign_string(&self) -> &'static str {
140-
if self.contains(CConversionFlags::SIGN_CHAR) {
141-
"+"
142-
} else if self.contains(CConversionFlags::BLANK_SIGN) {
143-
" "
144-
} else {
145-
""
140+
bitflags_match!(*self, {
141+
Self::SIGN_CHAR => "+",
142+
Self::BLANK_SIGN => " ",
143+
_ => "",
146144
}
145+
)
147146
}
148147
}
149148

@@ -172,12 +171,15 @@ pub trait FormatChar: Copy + Into<CodePoint> + From<u8> {
172171

173172
impl FormatBuf for String {
174173
type Char = char;
174+
175175
fn chars(&self) -> impl Iterator<Item = Self::Char> {
176176
(**self).chars()
177177
}
178+
178179
fn len(&self) -> usize {
179180
self.len()
180181
}
182+
181183
fn concat(mut self, other: Self) -> Self {
182184
self.extend([other]);
183185
self
@@ -188,19 +190,23 @@ impl FormatChar for char {
188190
fn to_char_lossy(self) -> char {
189191
self
190192
}
193+
191194
fn eq_char(self, c: char) -> bool {
192195
self == c
193196
}
194197
}
195198

196199
impl FormatBuf for Wtf8Buf {
197200
type Char = CodePoint;
201+
198202
fn chars(&self) -> impl Iterator<Item = Self::Char> {
199203
self.code_points()
200204
}
205+
201206
fn len(&self) -> usize {
202207
(**self).len()
203208
}
209+
204210
fn concat(mut self, other: Self) -> Self {
205211
self.extend([other]);
206212
self
@@ -211,19 +217,23 @@ impl FormatChar for CodePoint {
211217
fn to_char_lossy(self) -> char {
212218
self.to_char_lossy()
213219
}
220+
214221
fn eq_char(self, c: char) -> bool {
215222
self == c
216223
}
217224
}
218225

219226
impl FormatBuf for Vec<u8> {
220227
type Char = u8;
228+
221229
fn chars(&self) -> impl Iterator<Item = Self::Char> {
222230
self.iter().copied()
223231
}
232+
224233
fn len(&self) -> usize {
225234
self.len()
226235
}
236+
227237
fn concat(mut self, other: Self) -> Self {
228238
self.extend(other);
229239
self
@@ -234,6 +244,7 @@ impl FormatChar for u8 {
234244
fn to_char_lossy(self) -> char {
235245
self.into()
236246
}
247+
237248
fn eq_char(self, c: char) -> bool {
238249
char::from(self) == c
239250
}
@@ -394,6 +405,7 @@ impl CFormatSpec {
394405
Some(&(CFormatQuantity::Amount(1).into())),
395406
)
396407
}
408+
397409
pub fn format_bytes(&self, bytes: &[u8]) -> Vec<u8> {
398410
let bytes = if let Some(CFormatPrecision::Quantity(CFormatQuantity::Amount(precision))) =
399411
self.precision
@@ -707,12 +719,12 @@ pub enum CFormatPart<T> {
707719

708720
impl<T> CFormatPart<T> {
709721
#[inline]
710-
pub fn is_specifier(&self) -> bool {
722+
pub const fn is_specifier(&self) -> bool {
711723
matches!(self, CFormatPart::Spec { .. })
712724
}
713725

714726
#[inline]
715-
pub fn has_key(&self) -> bool {
727+
pub const fn has_key(&self) -> bool {
716728
match self {
717729
CFormatPart::Spec(s) => s.mapping_key.is_some(),
718730
_ => false,
@@ -804,6 +816,7 @@ impl<S> CFormatStrOrBytes<S> {
804816
impl<S> IntoIterator for CFormatStrOrBytes<S> {
805817
type Item = (usize, CFormatPart<S>);
806818
type IntoIter = std::vec::IntoIter<Self::Item>;
819+
807820
fn into_iter(self) -> Self::IntoIter {
808821
self.parts.into_iter()
809822
}

common/src/encodings.rs

Lines changed: 3 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -121,6 +121,7 @@ impl ops::Add for StrSize {
121121
}
122122
}
123123
}
124+
124125
impl ops::AddAssign for StrSize {
125126
fn add_assign(&mut self, rhs: Self) {
126127
self.bytes += rhs.bytes;
@@ -133,6 +134,7 @@ struct DecodeError<'a> {
133134
rest: &'a [u8],
134135
err_len: Option<usize>,
135136
}
137+
136138
/// # Safety
137139
/// `v[..valid_up_to]` must be valid utf8
138140
unsafe fn make_decode_err(v: &[u8], valid_up_to: usize, err_len: Option<usize>) -> DecodeError<'_> {
@@ -152,6 +154,7 @@ enum HandleResult<'a> {
152154
reason: &'a str,
153155
},
154156
}
157+
155158
fn decode_utf8_compatible<Ctx, E, DecodeF, ErrF>(
156159
mut ctx: Ctx,
157160
errors: &E,

common/src/fileutils.rs

Lines changed: 2 additions & 1 deletion
Original file line numberDiff line numberDiff line change
@@ -256,7 +256,7 @@ pub mod windows {
256256
}
257257
}
258258

259-
fn attributes_to_mode(attr: u32) -> u16 {
259+
const fn attributes_to_mode(attr: u32) -> u16 {
260260
let mut m = 0;
261261
if attr & FILE_ATTRIBUTE_DIRECTORY != 0 {
262262
m |= libc::S_IFDIR | 0o111; // IFEXEC for user,group,other
@@ -362,6 +362,7 @@ pub mod windows {
362362
}
363363
}
364364
}
365+
365366
pub fn stat_basic_info_to_stat(info: &FILE_STAT_BASIC_INFORMATION) -> StatStruct {
366367
use windows_sys::Win32::Storage::FileSystem;
367368
use windows_sys::Win32::System::Ioctl;

common/src/float_ops.rs

Lines changed: 2 additions & 2 deletions
Original file line numberDiff line numberDiff line change
@@ -2,7 +2,7 @@ use malachite_bigint::{BigInt, ToBigInt};
22
use num_traits::{Float, Signed, ToPrimitive, Zero};
33
use std::f64;
44

5-
pub fn decompose_float(value: f64) -> (f64, i32) {
5+
pub const fn decompose_float(value: f64) -> (f64, i32) {
66
if 0.0 == value {
77
(0.0, 0i32)
88
} else {
@@ -63,7 +63,7 @@ pub fn gt_int(value: f64, other_int: &BigInt) -> bool {
6363
}
6464
}
6565

66-
pub fn div(v1: f64, v2: f64) -> Option<f64> {
66+
pub const fn div(v1: f64, v2: f64) -> Option<f64> {
6767
if v2 != 0.0 { Some(v1 / v2) } else { None }
6868
}
6969

derive-impl/src/pymodule.rs

Lines changed: 4 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -31,6 +31,7 @@ impl std::fmt::Display for AttrName {
3131

3232
impl FromStr for AttrName {
333 F43C 3
type Err = String;
34+
3435
fn from_str(s: &str) -> std::result::Result<Self, Self::Err> {
3536
Ok(match s {
3637
"pyfunction" => Self::Function,
@@ -404,20 +405,23 @@ struct AttributeItem {
404405

405406
impl ContentItem for FunctionItem {
406407
type AttrName = AttrName;
408+
407409
fn inner(&self) -> &ContentItemInner<AttrName> {
408410
&self.inner
409411
}
410412
}
411413

412414
impl ContentItem for ClassItem {
413415
type AttrName = AttrName;
416+
414417
fn inner(&self) -> &ContentItemInner<AttrName> {
415418
&self.inner
416419
}
417420
}
418421

419422
impl ContentItem for AttributeItem {
420423
type AttrName = AttrName;
424+
421425
fn inner(&self) -> &ContentItemInner<AttrName> {
422426
&self.inner
423427
}

derive-impl/src/util.rs

Lines changed: 12 additions & 3 deletions
Original file line numberDiff line numberDiff line change
@@ -277,6 +277,7 @@ impl ItemMeta for SimpleItemMeta {
277277
fn from_inner(inner: ItemMetaInner) -> Self {
278278
Self(inner)
279279
}
280+
280281
fn inner(&self) -> &ItemMetaInner {
281282
&self.0
282283
}
@@ -290,6 +291,7 @@ impl ItemMeta for ModuleItemMeta {
290291
fn from_inner(inner: ItemMetaInner) -> Self {
291292
Self(inner)
292293
}
294+
293295
fn inner(&self) -> &ItemMetaInner {
294296
&self.0
295297
}
@@ -299,6 +301,7 @@ impl ModuleItemMeta {
299301
pub fn sub(&self) -> Result<bool> {
300302
self.inner()._bool("sub")
301303
}
304+
302305
pub fn with(&self) -> Result<Vec<&syn::Path>> {
303306
let mut withs = Vec::new();
304307
let Some(nested) = self.inner()._optional_list("with")? else {
@@ -322,6 +325,7 @@ impl ItemMeta for AttrItemMeta {
322325
fn from_inner(inner: ItemMetaInner) -> Self {
323326
Self(inner)
324327
}
328+
325329
fn inner(&self) -> &ItemMetaInner {
326330
&self.0
327331
}
@@ -344,6 +348,7 @@ impl ItemMeta for ClassItemMeta {
344348
fn from_inner(inner: ItemMetaInner) -> Self {
345349
Self(inner)
346350
}
351+
347352
fn inner(&self) -> &ItemMetaInner {
348353
&self.0
349354
}
@@ -446,6 +451,7 @@ impl ItemMeta for ExceptionItemMeta {
446451
fn from_inner(inner: ItemMetaInner) -> Self {
447452
Self(ClassItemMeta(inner))
448453
}
454+
449455
fn inner(&self) -> &ItemMetaInner {
450456
&self.0.0
451457
}
@@ -515,8 +521,8 @@ impl AttributeExt for Attribute {
515521
let name = self.get_ident().unwrap().to_string();
516522
e.combine(err_span!(
517523
self,
518-
"#[{name} = \"...\"] cannot be a name/value, you probably meant \
519-
#[{name}(name = \"...\")]",
524+
r##"#[{name} = "..."] cannot be a name/value, you probably meant \
525+
#[{name}(name = "...")]"##,
520526
));
521527
e
522528
})?;
@@ -620,13 +626,15 @@ pub(crate) fn pyexception_ident_and_attrs(item: &syn::Item) -> Result<(&Ident, &
620626

621627
pub(crate) trait ErrorVec: Sized {
622628
fn into_error(self) -> Option<syn::Error>;
629+
623630
fn into_result(self) -> Result<()> {
624631
if let Some(error) = self.into_error() {
625632
Err(error)
626633
} else {
627634
Ok(())
628635
}
629636
}
637+
630638
fn ok_or_push<T>(&mut self, r: Result<T>) -> Option<T>;
631639
}
632640

@@ -642,6 +650,7 @@ impl ErrorVec for Vec<syn::Error> {
642650
None
643651
}
644652
}
653+
645654
fn ok_or_push<T>(&mut self, r: Result<T>) -> Option<T> {
646655
match r {
647656
Ok(v) => Some(v),
@@ -732,7 +741,7 @@ fn func_sig(sig: &Signature) -> String {
732741
return Some("$self".to_owned());
733742
}
734743
if ident == "vm" {
735-
unreachable!("type &VirtualMachine(`{}`) must be filtered already", ty);
744+
unreachable!("type &VirtualMachine(`{ty}`) must be filtered already");
736745
}
737746
Some(ident)
738747
})

wtf8/src/core_str_count.rs

Lines changed: 1 addition & 1 deletion
Original file line numberDiff line numberDiff line change
@@ -157,6 +157,6 @@ unsafe fn slice_as_chunks_unchecked<T, const N: usize>(slice: &[T]) -> &[[T; N]]
157157
unsafe { std::slice::from_raw_parts(slice.as_ptr().cast(), new_len) }
158158
}
159159

160-
fn unlikely(x: bool) -> bool {
160+
const fn unlikely(x: bool) -> bool {
161161
x
162162
}

0 commit comments

Comments
 (0)
0