Skip to content

Commit 8cac433

Browse files
authored
Constify functions & General nitpicks (#5858)
* General cleanups * Add some newlines * Use bitlag match * More constify * Don't convert to str twice * constify more * Use match directly instead of if & match! * Constify more * Constify more * more consts * Constify more * Constify more * Don't use bitflags_match macro
1 parent e41d7f5 commit 8cac433

39 files changed

+273
-141
lines changed

common/src/borrow.rs

Lines changed: 3 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -56,6 +56,7 @@ impl<'a, T: ?Sized> BorrowedValue<'a, T> {
5656

5757
impl<T: ?Sized> Deref for BorrowedValue<'_, T> {
5858
type Target = T;
59+
5960
fn deref(&self) -> &T {
6061
match self {
6162
Self::Ref(r) => r,
@@ -81,6 +82,7 @@ pub enum BorrowedValueMut<'a, T: ?Sized> {
8182
WriteLock(PyRwLockWriteGuard<'a, T>),
8283
MappedWriteLock(PyMappedRwLockWriteGuard<'a, T>),
8384
}
85+
8486
impl_from!('a, T, BorrowedValueMut<'a, T>,
8587
RefMut(&'a mut T),
8688
MuLock(PyMutexGuard<'a, T>),
@@ -108,6 +110,7 @@ impl<'a, T: ?Sized> BorrowedValueMut<'a, T> {
108110

109111
impl<T: ?Sized> Deref for BorrowedValueMut<'_, T> {
110112
type Target = T;
113+
111114
fn deref(&self) -> &T {
112115
match self {
113116
Self::RefMut(r) => r,

common/src/boxvec.rs

Lines changed: 10 additions & 6 deletions
Original file line numberDiff line numberDiff line change
@@ -46,25 +46,25 @@ impl<T> BoxVec<T> {
4646
}
4747

4848
#[inline]
49-
pub fn len(&self) -> usize {
49+
pub const fn len(&self) -> usize {
5050
self.len
5151
}
5252

5353
#[inline]
54-
pub fn is_empty(&self) -> bool {
54+
pub const fn is_empty(&self) -> bool {
5555
self.len() == 0
5656
}
5757

5858
#[inline]
59-
pub fn capacity(&self) -> usize {
59+
pub const fn capacity(&self) -> usize {
6060
self.xs.len()
6161
}
6262

63-
pub fn is_full(&self) -> bool {
63+
pub const fn is_full(&self) -> bool {
6464
self.len() == self.capacity()
6565
}
6666

67-
pub fn remaining_capacity(&self) -> usize {
67+
pub const fn remaining_capacity(&self) -> usize {
6868
self.capacity() - self.len()
6969
}
7070

@@ -336,6 +336,7 @@ impl<T> BoxVec<T> {
336336

337337
impl<T> Deref for BoxVec<T> {
338338
type Target = [T];
339+
339340
#[inline]
340341
fn deref(&self) -> &[T] {
341342
unsafe { slice::from_raw_parts(self.as_ptr(), self.len()) }
@@ -354,6 +355,7 @@ impl<T> DerefMut for BoxVec<T> {
354355
impl<'a, T> IntoIterator for &'a BoxVec<T> {
355356
type Item = &'a T;
356357
type IntoIter = slice::Iter<'a, T>;
358+
357359
fn into_iter(self) -> Self::IntoIter {
358360
self.iter()
359361
}
@@ -363,6 +365,7 @@ impl<'a, T> IntoIterator for &'a BoxVec<T> {
363365
impl<'a, T> IntoIterator for &'a mut BoxVec<T> {
364366
type Item = &'a mut T;
365367
type IntoIter = slice::IterMut<'a, T>;
368+
366369
fn into_iter(self) -> Self::IntoIter {
367370
self.iter_mut()
368371
}
@@ -374,6 +377,7 @@ impl<'a, T> IntoIterator for &'a mut BoxVec<T> {
374377
impl<T> IntoIterator for BoxVec<T> {
375378
type Item = T;
376379
type IntoIter = IntoIter<T>;
380+
377381
fn into_iter(self) -> IntoIter<T> {
378382
IntoIter { index: 0, v: self }
379383
}
@@ -672,7 +676,7 @@ pub struct CapacityError<T = ()> {
672676

673677
impl<T> CapacityError<T> {
674678
/// Create a new `CapacityError` from `element`.
675-
pub fn new(element: T) -> CapacityError<T> {
679+
pub const fn new(element: T) -> CapacityError<T> {
676680
CapacityError { element }
677681
}
678682

common/src/cformat.rs

Lines changed: 21 additions & 6 deletions
Original file line numberDiff line numberDiff line change
@@ -76,8 +76,9 @@ pub enum CFloatType {
7676
}
7777

7878
impl CFloatType {
79-
fn case(self) -> Case {
79+
const fn case(self) -> Case {
8080
use CFloatType::*;
81+
8182
match self {
8283
ExponentLower | PointDecimalLower | GeneralLower => Case::Lower,
8384
ExponentUpper | PointDecimalUpper | GeneralUpper => Case::Upper,
@@ -100,7 +101,7 @@ pub enum CFormatType {
100101
}
101102

102103
impl CFormatType {
103-
pub fn to_char(self) -> char {
104+
pub const fn to_char(self) -> char {
104105
match self {
105106
CFormatType::Number(x) => x as u8 as char,
106107
CFormatType::Float(x) => x as u8 as char,
@@ -136,9 +137,9 @@ bitflags! {
136137
impl CConversionFlags {
137138
#[inline]
138139
pub fn sign_string(&self) -> &'static str {
139-
if self.contains(CConversionFlags::SIGN_CHAR) {
140+
if self.contains(Self::SIGN_CHAR) {
140141
"+"
141-
} else if self.contains(CConversionFlags::BLANK_SIGN) {
142+
} else if self.contains(Self::BLANK_SIGN) {
142143
" "
143144
} else {
144145
""
@@ -171,12 +172,15 @@ pub trait FormatChar: Copy + Into<CodePoint> + From<u8> {
171172

172173
impl FormatBuf for String {
173174
type Char = char;
175+
174176
fn chars(&self) -> impl Iterator<Item = Self::Char> {
175177
(**self).chars()
176178
}
179+
177180
fn len(&self) -> usize {
178181
self.len()
179182
}
183+
180184
fn concat(mut self, other: Self) -> Self {
181185
self.extend([other]);
182186
self
@@ -187,19 +191,23 @@ impl FormatChar for char {
187191
fn to_char_lossy(self) -> char {
188192
self
189193
}
194+
190195
fn eq_char(self, c: char) -> bool {
191196
self == c
192197
}
193198
}
194199

195200
impl FormatBuf for Wtf8Buf {
196201
type Char = CodePoint;
202+
197203
fn chars(&self) -> impl Iterator<Item = Self::Char> {
198204
self.code_points()
199205
}
206+
200207
fn len(&self) -> usize {
201208
(**self).len()
202209
}
210+
203211
fn concat(mut self, other: Self) -> Self {
204212
self.extend([other]);
205213
self
@@ -210,19 +218,23 @@ impl FormatChar for CodePoint {
210218
fn to_char_lossy(self) -> char {
211219
self.to_char_lossy()
212220
}
221+
213222
fn eq_char(self, c: char) -> bool {
214223
self == c
215224
}
216225
}
217226

218227
impl FormatBuf for Vec<u8> {
219228
type Char = u8;
229+
220230
fn chars(&self) -> impl Iterator<Item = Self::Char> {
221231
self.iter().copied()
222232
}
233+
223234
fn len(&self) -> usize {
224235
self.len()
225236
}
237+
226238
fn concat(mut self, other: Self) -> Self {
227239
self.extend(other);
228240
self
@@ -233,6 +245,7 @@ impl FormatChar for u8 {
233245
fn to_char_lossy(self) -> char {
234246
self.into()
235247
}
248+
236249
fn eq_char(self, c: char) -> bool {
237250
char::from(self) == c
238251
}
@@ -393,6 +406,7 @@ impl CFormatSpec {
393406
Some(&(CFormatQuantity::Amount(1).into())),
394407
)
395408
}
409+
396410
pub fn format_bytes(&self, bytes: &[u8]) -> Vec<u8> {
397411
let bytes = if let Some(CFormatPrecision::Quantity(CFormatQuantity::Amount(precision))) =
398412
self.precision
@@ -706,12 +720,12 @@ pub enum CFormatPart<T> {
706720

707721
impl<T> CFormatPart<T> {
708722
#[inline]
709-
pub fn is_specifier(&self) -> bool {
723+
pub const fn is_specifier(&self) -> bool {
710724
matches!(self, CFormatPart::Spec { .. })
711725
}
712726

713727
#[inline]
714-
pub fn has_key(&self) -> bool {
728+
pub const fn has_key(&self) -> bool {
715729
match self {
716730
CFormatPart::Spec(s) => s.mapping_key.is_some(),
717731
_ => false,
@@ -803,6 +817,7 @@ impl<S> CFormatStrOrBytes<S> {
803817
impl<S> IntoIterator for CFormatStrOrBytes<S> {
804818
type Item = (usize, CFormatPart<S>);
805819
type IntoIter = std::vec::IntoIter<Self::Item>;
820+
806821
fn into_iter(self) -> Self::IntoIter {
807822
self.parts.into_iter()
808823
}

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

common/src/format.rs

Lines changed: 3 additions & 2 deletions
Original file line numberDiff line numberDiff line change
@@ -392,7 +392,7 @@ impl FormatSpec {
392392
}
393393
}
394394

395-
fn get_separator_interval(&self) -> usize {
395+
const fn get_separator_interval(&self) -> usize {
396396
match self.format_type {
397397
Some(FormatType::Binary | FormatType::Octal | FormatType::Hex(_)) => 4,
398398
Some(FormatType::Decimal | FormatType::Number(_) | FormatType::FixedPoint(_)) => 3,
@@ -677,7 +677,7 @@ struct AsciiStr<'a> {
677677
}
678678

679679
impl<'a> AsciiStr<'a> {
680-
fn new(inner: &'a str) -> Self {
680+
const fn new(inner: &'a str) -> Self {
681681
Self { inner }
682682
}
683683
}
@@ -690,6 +690,7 @@ impl CharLen for AsciiStr<'_> {
690690

691691
impl Deref for AsciiStr<'_> {
692692
type Target = str;
693+
693694
fn deref(&self) -> &Self::Target {
694695
self.inner
695696
}

common/src/hash.rs

Lines changed: 5 additions & 4 deletions
Original file line numberDiff line numberDiff line change
@@ -32,6 +32,7 @@ pub struct HashSecret {
3232

3333
impl BuildHasher for HashSecret {
3434
type Hasher = SipHasher24;
35+
3536
fn build_hasher(&self) -> Self::Hasher {
3637
SipHasher24::new_with_keys(self.k0, self.k1)
3738
}
@@ -80,7 +81,7 @@ impl HashSecret {
8081
}
8182

8283
#[inline]
83-
pub fn hash_pointer(value: usize) -> PyHash {
84+
pub const fn hash_pointer(value: usize) -> PyHash {
8485
// TODO: 32bit?
8586
let hash = (value >> 4) | value;
8687
hash as _
@@ -140,17 +141,17 @@ pub fn hash_bigint(value: &BigInt) -> PyHash {
140141
}
141142

142143
#[inline]
143-
pub fn hash_usize(data: usize) -> PyHash {
144+
pub const fn hash_usize(data: usize) -> PyHash {
144145
fix_sentinel(mod_int(data as i64))
145146
}
146147

147148
#[inline(always)]
148-
pub fn fix_sentinel(x: PyHash) -> PyHash {
149+
pub const fn fix_sentinel(x: PyHash) -> PyHash {
149150
if x == SENTINEL { -2 } else { x }
150151
}
151152

152153
#[inline]
153-
pub fn mod_int(value: i64) -> PyHash {
154+
pub const fn mod_int(value: i64) -> PyHash {
154155
value % MODULUS as i64
155156
}
156157

common/src/int.rs

Lines changed: 1 addition & 1 deletion
Original file line numberDiff line numberDiff line change
@@ -128,7 +128,7 @@ pub fn bytes_to_int(lit: &[u8], mut base: u32) -> Option<BigInt> {
128128
}
129129

130130
#[inline]
131-
pub fn detect_base(c: &u8) -> Option<u32> {
131+
pub const fn detect_base(c: &u8) -> Option<u32> {
132132
let base = match c {
133133
b'x' | b'X' => 16,
134134
b'b' | b'B' => 2,

0 commit comments

Comments
 (0)