8000 Use `const fn` where possible by ShaharNaveh · Pull Request #5894 · RustPython/RustPython · GitHub
[go: up one dir, main page]

Skip to content

Use const fn where possible #5894

New issue

Have a question about this project? Sign up for a free GitHub account to open an issue and contact its maintainers and the community.

By clicking “Sign up for GitHub”, you agree to our terms of service and privacy statement. We’ll occasionally send you account related emails.

Already on GitHub? Sign in to your account

Merged
merged 17 commits into from
Jul 4, 2025
Prev Previous commit
Next Next commit
common
  • Loading branch information
ShaharNaveh committed Jul 3, 2025
commit 5f790b8a5313843b7839ebd5d514af92daa50a41
2 changes: 1 addition & 1 deletion common/src/cformat.rs
Original file line number Diff line number Diff line change
Expand Up @@ -136,7 +136,7 @@ bitflags! {

impl CConversionFlags {
#[inline]
pub fn sign_string(&self) -> &'static str {
pub const fn sign_string(&self) -> &'static str {
if self.contains(Self::SIGN_CHAR) {
"+"
} else if self.contains(Self::BLANK_SIGN) {
Expand Down
2 changes: 1 addition & 1 deletion common/src/encodings.rs
Original file line number Diff line number Diff line change
Expand Up @@ -137,7 +137,7 @@ struct DecodeError<'a> {

/// # Safety
/// `v[..valid_up_to]` must be valid utf8
unsafe fn make_decode_err(v: &[u8], valid_up_to: usize, err_len: Option<usize>) -> DecodeError<'_> {
const unsafe fn make_decode_err(v: &[u8], valid_up_to: usize, err_len: Option<usize>) -> DecodeError<'_> {
let (valid_prefix, rest) = unsafe { v.split_at_unchecked(valid_up_to) };
let valid_prefix = unsafe { core::str::from_utf8_unchecked(valid_prefix) };
DecodeError {
Expand Down
4 changes: 2 additions & 2 deletions common/src/hash.rs
Original file line number Diff line number Diff line change
Expand Up @@ -164,7 +164,7 @@ pub fn lcg_urandom(mut x: u32, buf: &mut [u8]) {
}

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

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

#[inline]
pub fn hash_object_id(p: usize) -> PyHash {
pub const fn hash_object_id(p: usize) -> PyHash {
fix_sentinel(hash_object_id_raw(p))
}

Expand Down
14 changes: 7 additions & 7 deletions common/src/linked_list.rs
Original file line number Diff line number Diff line change
Expand Up @@ -193,7 +193,7 @@ impl<L: Link> LinkedList<L, L::Target> {
// }

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

impl<T: Link> LinkedList<T, T::Target> {
pub fn drain_filter<F>(&mut self, filter: F) -> DrainFilter<'_, T, F>
pub const fn drain_filter<F>(&mut self, filter: F) -> DrainFilter<'_, T, F>
where
F: FnMut(&mut T::Target) -> bool,
{
Expand Down Expand Up @@ -323,7 +323,7 @@ where

impl<T> Pointers<T> {
/// Create a new set of empty pointers
pub fn new() -> Pointers<T> {
pub const fn new() -> Pointers<T> {
Pointers {
inner: UnsafeCell::new(PointersInner {
prev: None,
Expand All @@ -333,15 +333,15 @@ impl<T> Pointers<T> {
}
}

fn get_prev(&self) -> Option<NonNull<T>> {
const fn get_prev(&self) -> Option<NonNull<T>> {
// SAFETY: prev is the first field in PointersInner, which is #[repr(C)].
unsafe {
let inner = self.inner.get();
let prev = inner as *const Option<NonNull<T>>;
ptr::read(prev)
}
}
fn get_next(&self) -> Option<NonNull<T>> {
const fn get_next(&self) -> Option<NonNull<T>> {
// SAFETY: next is the second field in PointersInner, which is #[repr(C)].
unsafe {
let inner = self.inner.get();
Expand All @@ -351,15 +351,15 @@ impl<T> Pointers<T> {
}
}

fn set_prev(&mut self, value: Option<NonNull<T>>) {
const fn set_prev(&mut self, value: Option<NonNull<T>>) {
// SAFETY: prev is the first field in PointersInner, which is #[repr(C)].
unsafe {
let inner = self.inner.get();
let prev = inner as *mut Option<NonNull<T>>;
ptr::write(prev, value);
}
}
fn set_next(&mut self, value: Option<NonNull<T>>) {
const fn set_next(&mut self, value: Option<NonNull<T>>) {
// SAFETY: next is the second field in PointersInner, which is #[repr(C)].
unsafe {
let inner = self.inner.get();
Expand Down
2 changes: 1 addition & 1 deletion common/src/lock/thread_mutex.rs
Original file line number Diff line number Diff line change
Expand Up @@ -78,7 +78,7 @@ pub struct ThreadMutex<R: RawMutex, G: GetThreadId, T: ?Sized> {
}

impl<R: RawMutex, G: GetThreadId, T> ThreadMutex<R, G, T> {
pub fn new(val: T) -> Self {
pub const fn new(val: T) -> Self {
ThreadMutex {
raw: RawThreadMutex::INIT,
data: UnsafeCell::new(val),
Expand Down
0