Skip to content

Commit 694fe50

Browse files
authored
Use const fn where possible (RustPython#5894)
1 parent 5b20bb8 commit 694fe50

Some content is hidden

Large Commits have some content hidden by default. Use the searchbox below for content that may be hidden.

64 files changed

+200
-191
lines changed

common/src/cformat.rs

Lines changed: 1 addition & 1 deletion
Original file line numberDiff line numberDiff line change
@@ -136,7 +136,7 @@ bitflags! {
136136

137137
impl CConversionFlags {
138138
#[inline]
139-
pub fn sign_string(&self) -> &'static str {
139+
pub const fn sign_string(&self) -> &'static str {
140140
if self.contains(Self::SIGN_CHAR) {
141141
"+"
142142
} else if self.contains(Self::BLANK_SIGN) {

common/src/encodings.rs

Lines changed: 5 additions & 1 deletion
Original file line numberDiff line numberDiff line change
@@ -137,7 +137,11 @@ struct DecodeError<'a> {
137137

138138
/// # Safety
139139
/// `v[..valid_up_to]` must be valid utf8
140-
unsafe fn make_decode_err(v: &[u8], valid_up_to: usize, err_len: Option<usize>) -> DecodeError<'_> {
140+
const unsafe fn make_decode_err(
141+
v: &[u8],
142+
valid_up_to: usize,
143+
err_len: Option<usize>,
144+
) -> DecodeError<'_> {
141145
let (valid_prefix, rest) = unsafe { v.split_at_unchecked(valid_up_to) };
142146
let valid_prefix = unsafe { core::str::from_utf8_unchecked(valid_prefix) };
143147
DecodeError {

common/src/hash.rs

Lines changed: 2 additions & 2 deletions
Original file line numberDiff line numberDiff line change
@@ -164,7 +164,7 @@ pub fn lcg_urandom(mut x: u32, buf: &mut [u8]) {
164164
}
165165

166166
#[inline]
167-
pub fn hash_object_id_raw(p: usize) -> PyHash {
167+
pub const fn hash_object_id_raw(p: usize) -> PyHash {
168168
// TODO: Use commented logic when below issue resolved.
169169
// Ref: https://github.com/RustPython/RustPython/pull/3951#issuecomment-1193108966
170170

@@ -175,7 +175,7 @@ pub fn hash_object_id_raw(p: usize) -> PyHash {
175175
}
176176

177177
#[inline]
178-
pub fn hash_object_id(p: usize) -> PyHash {
178+
pub const fn hash_object_id(p: usize) -> PyHash {
179179
fix_sentinel(hash_object_id_raw(p))
180180
}
181181

common/src/linked_list.rs

Lines changed: 7 additions & 7 deletions
Original file line numberDiff line numberDiff line change
@@ -193,7 +193,7 @@ impl<L: Link> LinkedList<L, L::Target> {
193193
// }
194194

195195
/// Returns whether the linked list does not contain any node
196-
pub fn is_empty(&self) -> bool {
196+
pub const fn is_empty(&self) -> bool {
197197
self.head.is_none()
198198
// if self.head.is_some() {
199199
// return false;
@@ -284,7 +284,7 @@ pub struct DrainFilter<'a, T: Link, F> {
284284
}
285285

286286
impl<T: Link> LinkedList<T, T::Target> {
287-
pub fn drain_filter<F>(&mut self, filter: F) -> DrainFilter<'_, T, F>
287+
pub const fn drain_filter<F>(&mut self, filter: F) -> DrainFilter<'_, T, F>
288288
where
289289
F: FnMut(&mut T::Target) -> bool,
290290
{
@@ -323,7 +323,7 @@ where
323323

324324
impl<T> Pointers<T> {
325325
/// Create a new set of empty pointers
326-
pub fn new() -> Self {
326+
pub const fn new() -> Self {
327327
Self {
328328
inner: UnsafeCell::new(PointersInner {
329329
prev: None,
@@ -333,15 +333,15 @@ impl<T> Pointers<T> {
333333
}
334334
}
335335

336-
fn get_prev(&self) -> Option<NonNull<T>> {
336+
const fn get_prev(&self) -> Option<NonNull<T>> {
337337
// SAFETY: prev is the first field in PointersInner, which is #[repr(C)].
338338
unsafe {
339339
let inner = self.inner.get();
340340
let prev = inner as *const Option<NonNull<T>>;
341341
ptr::read(prev)
342342
}
343343
}
344-
fn get_next(&self) -> Option<NonNull<T>> {
344+
const fn get_next(&self) -> Option<NonNull<T>> {
345345
// SAFETY: next is the second field in PointersInner, which is #[repr(C)].
346346
unsafe {
347347
let inner = self.inner.get();
@@ -351,15 +351,15 @@ impl<T> Pointers<T> {
351351
}
352352
}
353353

354-
fn set_prev(&mut self, value: Option<NonNull<T>>) {
354+
const fn set_prev(&mut self, value: Option<NonNull<T>>) {
355355
// SAFETY: prev is the first field in PointersInner, which is #[repr(C)].
356356
unsafe {
357357
let inner = self.inner.get();
358358
let prev = inner as *mut Option<NonNull<T>>;
359359
ptr::write(prev, value);
360360
}
361361
}
362-
fn set_next(&mut self, value: Option<NonNull<T>>) {
362+
const fn set_next(&mut self, value: Option<NonNull<T>>) {
363363
// SAFETY: next is the second field in PointersInner, which is #[repr(C)].
364364
unsafe {
365365
let inner = self.inner.get();

common/src/lock/thread_mutex.rs

Lines changed: 1 addition & 1 deletion
Original file line numberDiff line numberDiff line change
@@ -78,7 +78,7 @@ pub struct ThreadMutex<R: RawMutex, G: GetThreadId, T: ?Sized> {
7878
}
7979

8080
impl<R: RawMutex, G: GetThreadId, T> ThreadMutex<R, G, T> {
81-
pub fn new(val: T) -> Self {
81+
pub const fn new(val: T) -> Self {
8282
Self {
8383
raw: RawThreadMutex::INIT,
8484
data: UnsafeCell::new(val),

compiler/codegen/src/compile.rs

Lines changed: 2 additions & 2 deletions
Original file line numberDiff line numberDiff line change
@@ -282,7 +282,7 @@ impl Default for PatternContext {
282282
}
283283

284284
impl PatternContext {
285-
pub fn new() -> Self {
285+
pub const fn new() -> Self {
286286
Self {
287287
stores: Vec::new(),
288288
allow_irrefutable: false,
@@ -4118,7 +4118,7 @@ impl Compiler<'_> {
41184118
code.current_block = block;
41194119
}
41204120

4121-
fn set_source_range(&mut self, range: TextRange) {
4121+
const fn set_source_range(&mut self, range: TextRange) {
41224122
self.current_source_range = range;
41234123
}
41244124

compiler/codegen/src/string_parser.rs

Lines changed: 1 addition & 1 deletion
Original file line numberDiff line numberDiff line change
@@ -28,7 +28,7 @@ struct StringParser {
2828
}
2929

3030
impl StringParser {
31-
fn new(source: Box<str>, flags: AnyStringFlags) -> Self {
31+
const fn new(source: Box<str>, flags: AnyStringFlags) -> Self {
3232
Self {
3333
source,
3434
cursor: 0,

compiler/codegen/src/symboltable.rs

Lines changed: 3 additions & 3 deletions
Original file line numberDiff line numberDiff line change
@@ -162,18 +162,18 @@ impl Symbol {
162162
}
163163
}
164164

165-
pub fn is_global(&self) -> bool {
165+
pub const fn is_global(&self) -> bool {
166166
matches!(
167167
self.scope,
168168
SymbolScope::GlobalExplicit | SymbolScope::GlobalImplicit
169169
)
170170
}
171171

172-
pub fn is_local(&self) -> bool {
172+
pub const fn is_local(&self) -> bool {
173173
matches!(self.scope, SymbolScope::Local | SymbolScope::Cell)
174174
}
175175

176-
pub fn is_bound(&self) -> bool {
176+
pub const fn is_bound(&self) -> bool {
177177
self.flags.intersects(SymbolFlags::BOUND)
178178
}
179179
}

compiler/codegen/src/unparse.rs

Lines changed: 2 additions & 2 deletions
Original file line numberDiff line numberDiff line change
@@ -32,7 +32,7 @@ struct Unparser<'a, 'b, 'c> {
3232
source: &'c SourceCode<'c>,
3333
}
3434
impl<'a, 'b, 'c> Unparser<'a, 'b, 'c> {
35-
fn new(f: &'b mut fmt::Formatter<'a>, source: &'c SourceCode<'c>) -> Self {
35+
const fn new(f: &'b mut fmt::Formatter<'a>, source: &'c SourceCode<'c>) -> Self {
3636
Unparser { f, source }
3737
}
3838

@@ -602,7 +602,7 @@ pub struct UnparseExpr<'a> {
602602
source: &'a SourceCode<'a>,
603603
}
604604

605-
pub fn unparse_expr<'a>(expr: &'a Expr, source: &'a SourceCode<'a>) -> UnparseExpr<'a> {
605+
pub const fn unparse_expr<'a>(expr: &'a Expr, source: &'a SourceCode<'a>) -> UnparseExpr<'a> {
606606
UnparseExpr { expr, source }
607607
}
608608

compiler/src/lib.rs

Lines changed: 1 addition & 1 deletion
Original file line numberDiff line numberDiff line change
@@ -62,7 +62,7 @@ impl CompileError {
6262
}
6363
}
6464

65-
pub fn python_location(&self) -> (usize, usize) {
65+
pub const fn python_location(&self) -> (usize, usize) {
6666
match self {
6767
Self::Codegen(codegen_error) => {
6868
if let Some(location) = &codegen_error.location {

0 commit comments

Comments
 (0)