Skip to content
Prev Previous commit
Next Next commit
stdlib
  • Loading branch information
ShaharNaveh committed Jul 3, 2025
commit 8aef1a3e38fa6bef8de660c206956d53e71cce29
14 changes: 7 additions & 7 deletions stdlib/src/array.rs
Original file line number Diff line number Diff line change
Expand Up @@ -89,26 +89,26 @@ mod array {
}
}

fn typecode(&self) -> char {
const fn typecode(&self) -> char {
match self {
$(ArrayContentType::$n(_) => $c,)*
}
}

fn typecode_str(&self) -> &'static str {
const fn typecode_str(&self) -> &'static str {
match self {
$(ArrayContentType::$n(_) => $scode,)*
}
}

fn itemsize_of_typecode(c: char) -> Option<usize> {
const fn itemsize_of_typecode(c: char) -> Option<usize> {
match c {
$($c => Some(std::mem::size_of::<$t>()),)*
_ => None,
}
}

fn itemsize(&self) -> usize {
const fn itemsize(&self) -> usize {
match self {
$(ArrayContentType::$n(_) => std::mem::size_of::<$t>(),)*
}
Expand Down Expand Up @@ -554,11 +554,11 @@ mod array {
(f64, f64_try_into_from_object, f64_swap_bytes, PyFloat::from),
);

fn f32_swap_bytes(x: f32) -> f32 {
const fn f32_swap_bytes(x: f32) -> f32 {
f32::from_bits(x.to_bits().swap_bytes())
}

fn f64_swap_bytes(x: f64) -> f64 {
const fn f64_swap_bytes(x: f64) -> f64 {
f64::from_bits(x.to_bits().swap_bytes())
}

Expand Down Expand Up @@ -1557,7 +1557,7 @@ mod array {
_ => None,
}
}
fn item_size(self) -> usize {
const fn item_size(self) -> usize {
match self {
Self::Int8 { .. } => 1,
Self::Int16 { .. } | Self::Utf16 { .. } => 2,
Expand Down
4 changes: 2 additions & 2 deletions stdlib/src/binascii.rs
Original file line number Diff line number Diff line change
Expand Up @@ -160,7 +160,7 @@ mod decl {
})
}

fn unhex_nibble(c: u8) -> Option<u8> {
const fn unhex_nibble(c: u8) -> Option<u8> {
match c {
b'0'..=b'9' => Some(c - b'0'),
b'a'..=b'f' => Some(c - b'a' + 10),
Expand Down Expand Up @@ -810,7 +810,7 @@ mod decl {
vm: &VirtualMachine,
) -> PyResult<Vec<u8>> {
#[inline]
fn uu_b2a(num: u8, backtick: bool) -> u8 {
const fn uu_b2a(num: u8, backtick: bool) -> u8 {
if backtick && num == 0 {
0x60
} else {
Expand Down
16 changes: 8 additions & 8 deletions stdlib/src/compression.rs
Original file line number Diff line number Diff line change
Expand Up @@ -66,7 +66,7 @@ impl DecompressFlushKind for () {
const SYNC: Self = ();
}

pub fn flush_sync<T: DecompressFlushKind>(_final_chunk: bool) -> T {
pub const fn flush_sync<T: DecompressFlushKind>(_final_chunk: bool) -> T {
T::SYNC
}

Expand All @@ -76,13 +76,13 @@ pub struct Chunker<'a> {
data2: &'a [u8],
}
impl<'a> Chunker<'a> {
pub fn new(data: &'a [u8]) -> Self {
pub const fn new(data: &'a [u8]) -> Self {
Self {
data1: data,
data2: &[],
}
}
pub fn chain(data1: &'a [u8], data2: &'a [u8]) -> Self {
pub const fn chain(data1: &'a [u8], data2: &'a [u8]) -> Self {
if data1.is_empty() {
Self {
data1: data2,
Expand All @@ -92,10 +92,10 @@ impl<'a> Chunker<'a> {
Self { data1, data2 }
}
}
pub fn len(&self) -> usize {
pub const fn len(&self) -> usize {
self.data1.len() + self.data2.len()
}
pub fn is_empty(&self) -> bool {
pub const fn is_empty(&self) -> bool {
self.data1.is_empty()
}
pub fn to_vec(&self) -> Vec<u8> {
Expand Down Expand Up @@ -216,7 +216,7 @@ pub struct CompressState<C: Compressor> {
}

impl<C: Compressor> CompressState<C> {
pub fn new(compressor: C) -> Self {
pub const fn new(compressor: C) -> Self {
Self {
compressor: Some(compressor),
}
Expand Down Expand Up @@ -293,15 +293,15 @@ impl<D: Decompressor> DecompressState<D> {
}
}

pub fn eof(&self) -> bool {
pub const fn eof(&self) -> bool {
self.eof
}

pub fn unused_data(&self) -> PyBytesRef {
self.unused_data.clone()
}

pub fn needs_input(&self) -> bool {
pub const fn needs_input(&self) -> bool {
self.needs_input
}

Expand Down
12 changes: 6 additions & 6 deletions stdlib/src/csv.rs
Original file line number Diff line number Diff line change
Expand Up @@ -84,11 +84,11 @@ mod _csv {
Some(vm.ctx.new_str(format!("{}", self.quotechar? as char)))
}
#[pygetset]
fn doublequote(&self) -> bool {
const fn doublequote(&self) -> bool {
self.doublequote
}
#[pygetset]
fn skipinitialspace(&self) -> bool {
const fn skipinitialspace(&self) -> bool {
self.skipinitialspace
}
#[pygetset]
Expand All @@ -108,7 +108,7 @@ mod _csv {
Some(vm.ctx.new_str(format!("{}", self.escapechar? as char)))
}
#[pygetset(name = "strict")]
fn get_strict(&self) -> bool {
const fn get_strict(&self) -> bool {
self.strict
}
}
Expand Down Expand Up @@ -659,7 +659,7 @@ mod _csv {
}

impl FormatOptions {
fn update_py_dialect(&self, mut res: PyDialect) -> PyDialect {
const fn update_py_dialect(&self, mut res: PyDialect) -> PyDialect {
macro_rules! check_and_fill {
($res:ident, $e:ident) => {{
if let Some(t) = self.$e {
Expand Down Expand Up @@ -916,7 +916,7 @@ mod _csv {
self.state.lock().line_num
}
#[pygetset]
fn dialect(&self, _vm: &VirtualMachine) -> PyDialect {
const fn dialect(&self, _vm: &VirtualMachine) -> PyDialect {
self.dialect
}
}
Expand Down Expand Up @@ -1066,7 +1066,7 @@ mod _csv {
#[pyclass]
impl Writer {
#[pygetset(name = "dialect")]
fn get_dialect(&self, _vm: &VirtualMachine) -> PyDialect {
const fn get_dialect(&self, _vm: &VirtualMachine) -> PyDialect {
self.dialect
}
#[pymethod]
Expand Down
4 changes: 2 additions & 2 deletions stdlib/src/faulthandler.rs
Original file line number Diff line number Diff line change
Expand Up @@ -39,7 +39,7 @@ mod decl {
}

#[pyfunction]
fn enable(_args: EnableArgs) {
const fn enable(_args: EnableArgs) {
// TODO
}

Expand All @@ -57,7 +57,7 @@ mod decl {
}

#[pyfunction]
fn register(_args: RegisterArgs) {
const fn register(_args: RegisterArgs) {
// TODO
}
}
6 changes: 3 additions & 3 deletions stdlib/src/hashlib.rs
Original file line number Diff line number Diff line change
Expand Up @@ -190,7 +190,7 @@ pub mod _hashlib {
}

#[pygetset]
fn digest_size(&self) -> usize {
const fn digest_size(&self) -> usize {
0
}

Expand Down Expand Up @@ -317,7 +317,7 @@ pub mod _hashlib {
b: ArgStrOrBytesLike,
vm: &VirtualMachine,
) -> PyResult<PyObjectRef> {
fn is_str(arg: &ArgStrOrBytesLike) -> bool {
const fn is_str(arg: &ArgStrOrBytesLike) -> bool {
matches!(arg, ArgStrOrBytesLike::Str(_))
}

Expand Down Expand Up @@ -381,7 +381,7 @@ pub mod _hashlib {
self.inner.update(data);
}

fn block_size(&self) -> usize {
const fn block_size(&self) -> usize {
self.block_size
}

Expand Down
2 changes: 1 addition & 1 deletion stdlib/src/json/machinery.rs
Original file line number Diff line number Diff line change
Expand Up @@ -119,7 +119,7 @@ enum StrOrChar<'a> {
Char(CodePoint),
}
impl StrOrChar<'_> {
fn len(&self) -> usize {
const fn len(&self) -> usize {
match self {
StrOrChar::Str(s) => s.len(),
StrOrChar::Char(c) => c.len_wtf8(),
Expand Down
4 changes: 2 additions & 2 deletions stdlib/src/posixsubprocess.rs
Original file line number Diff line number Diff line change
Expand Up @@ -116,7 +116,7 @@ struct CharPtrSlice<'a> {
}

impl CharPtrSlice<'_> {
fn as_ptr(&self) -> *const *const libc::c_char {
const fn as_ptr(&self) -> *const *const libc::c_char {
self.slice.as_ptr()
}
}
Expand Down Expand Up @@ -174,7 +174,7 @@ enum ExecErrorContext {
}

impl ExecErrorContext {
fn as_msg(&self) -> &'static str {
const fn as_msg(&self) -> &'static str {
match self {
ExecErrorContext::NoExec => "noexec",
ExecErrorContext::ChDir => "noexec:chdir",
Expand Down
2 changes: 1 addition & 1 deletion stdlib/src/pystruct.rs
Original file line number Diff line number Diff line change
Expand Up @@ -315,7 +315,7 @@ pub(crate) mod _struct {
// seems weird that this is part of the "public" API, but whatever
// TODO: implement a format code->spec cache like CPython does?
#[pyfunction]
fn _clearcache() {}
const fn _clearcache() {}

#[pyattr(name = "error")]
fn error_type(vm: &VirtualMachine) -> PyTypeRef {
Expand Down
2 changes: 1 addition & 1 deletion stdlib/src/select.rs
Original file line number Diff line number Diff line change
Expand Up @@ -24,7 +24,7 @@ mod platform {
pub use libc::{FD_ISSET, FD_SET, FD_SETSIZE, FD_ZERO, fd_set, select, timeval};
pub use std::os::unix::io::RawFd;

pub fn check_err(x: i32) -> bool {
pub const fn check_err(x: i32) -> bool {
x < 0
}
}
Expand Down
4 changes: 2 additions & 2 deletions stdlib/src/syslog.rs
Original file line number Diff line number Diff line change
Expand Up @@ -135,13 +135,13 @@ mod syslog {

#[inline]
#[pyfunction(name = "LOG_MASK")]
fn log_mask(pri: i32) -> i32 {
const fn log_mask(pri: i32) -> i32 {
pri << 1
}

#[inline]
#[pyfunction(name = "LOG_UPTO")]
fn log_upto(pri: i32) -> i32 {
const fn log_upto(pri: i32) -> i32 {
(1 << (pri + 1)) - 1
}
}
2 changes: 1 addition & 1 deletion stdlib/src/unicodedata.rs
Original file line number Diff line number Diff line change
Expand Up @@ -83,7 +83,7 @@ mod unicodedata {
}

impl Ucd {
pub fn new(unic_version: UnicodeVersion) -> Self {
pub const fn new(unic_version: UnicodeVersion) -> Self {
Self { unic_version }
}

Expand Down