From a96977fdb9de57047b074be95dd2bad4c384cc1a Mon Sep 17 00:00:00 2001 From: "Jeong, YunWon" Date: Mon, 9 Feb 2026 00:10:19 +0900 Subject: [PATCH 1/2] `no_std` for wtf8 --- crates/wtf8/src/lib.rs | 21 +++++++++++++-------- 1 file changed, 13 insertions(+), 8 deletions(-) diff --git a/crates/wtf8/src/lib.rs b/crates/wtf8/src/lib.rs index 01deb61f19b..6614ca83572 100644 --- a/crates/wtf8/src/lib.rs +++ b/crates/wtf8/src/lib.rs @@ -33,8 +33,17 @@ //! [WTF-8]: https://simonsapin.github.io/wtf-8 //! [`OsStr`]: std::ffi::OsStr +#![no_std] #![allow(clippy::precedence, clippy::match_overlapping_arm)] +extern crate alloc; + +use alloc::borrow::{Cow, ToOwned}; +use alloc::boxed::Box; +use alloc::collections::TryReserveError; +use alloc::string::String; +use alloc::vec::Vec; +use core::borrow::Borrow; use core::fmt; use core::hash::{Hash, Hasher}; use core::iter::FusedIterator; @@ -46,10 +55,6 @@ use core_char::MAX_LEN_UTF8; use core_char::{MAX_LEN_UTF16, encode_utf8_raw, encode_utf16_raw, len_utf8}; use core_str::{next_code_point, next_code_point_reverse}; use itertools::{Either, Itertools}; -use std::borrow::{Borrow, Cow}; -use std::collections::TryReserveError; -use std::string::String; -use std::vec::Vec; use bstr::{ByteSlice, ByteVec}; @@ -665,7 +670,7 @@ impl PartialEq for Wtf8 { impl fmt::Debug for Wtf8 { fn fmt(&self, formatter: &mut fmt::Formatter<'_>) -> fmt::Result { fn write_str_escaped(f: &mut fmt::Formatter<'_>, s: &str) -> fmt::Result { - use std::fmt::Write; + use core::fmt::Write; for c in s.chars().flat_map(|c| c.escape_debug()) { f.write_char(c)? } @@ -765,7 +770,7 @@ impl Wtf8 { #[inline] pub fn from_bytes(b: &[u8]) -> Option<&Self> { let mut rest = b; - while let Err(e) = std::str::from_utf8(rest) { + while let Err(e) = core::str::from_utf8(rest) { rest = &rest[e.valid_up_to()..]; let _ = Self::decode_surrogate(rest)?; rest = &rest[3..]; @@ -899,7 +904,7 @@ impl Wtf8 { { self.chunks().flat_map(move |chunk| match chunk { Wtf8Chunk::Utf8(s) => Either::Left(f(s).map_into()), - Wtf8Chunk::Surrogate(c) => Either::Right(std::iter::once(c)), + Wtf8Chunk::Surrogate(c) => Either::Right(core::iter::once(c)), }) } @@ -1469,7 +1474,7 @@ impl<'a> Iterator for Wtf8Chunks<'a> { } None => { let s = - unsafe { str::from_utf8_unchecked(std::mem::take(&mut self.wtf8).as_bytes()) }; + unsafe { str::from_utf8_unchecked(core::mem::take(&mut self.wtf8).as_bytes()) }; (!s.is_empty()).then_some(Wtf8Chunk::Utf8(s)) } } From 2c5ece945b463b2725296fc078e935875e18568e Mon Sep 17 00:00:00 2001 From: "Jeong, YunWon" Date: Mon, 9 Feb 2026 00:23:46 +0900 Subject: [PATCH 2/2] `no_std` for sre_engine, compiler-core, literal --- crates/compiler-core/src/bytecode.rs | 3 ++- crates/compiler-core/src/frozen.rs | 1 + crates/compiler-core/src/lib.rs | 1 + crates/compiler-core/src/marshal.rs | 1 + crates/compiler-core/src/varint.rs | 2 ++ crates/literal/src/complex.rs | 2 ++ crates/literal/src/escape.rs | 31 ++++++++++++++++------------ crates/literal/src/float.rs | 5 ++++- crates/literal/src/lib.rs | 4 ++++ crates/sre_engine/src/engine.rs | 1 + crates/sre_engine/src/lib.rs | 4 ++++ 11 files changed, 40 insertions(+), 15 deletions(-) diff --git a/crates/compiler-core/src/bytecode.rs b/crates/compiler-core/src/bytecode.rs index 07df2c994f2..a6d19795076 100644 --- a/crates/compiler-core/src/bytecode.rs +++ b/crates/compiler-core/src/bytecode.rs @@ -6,7 +6,7 @@ use crate::{ varint::{read_varint, read_varint_with_start, write_varint, write_varint_with_start}, {OneIndexed, SourceLocation}, }; -use alloc::{collections::BTreeSet, fmt, vec::Vec}; +use alloc::{borrow::ToOwned, boxed::Box, collections::BTreeSet, fmt, string::String, vec::Vec}; use bitflags::bitflags; use core::{hash, mem, ops::Deref}; use itertools::Itertools; @@ -804,6 +804,7 @@ impl fmt::Debug for CodeObject { #[cfg(test)] mod tests { use super::*; + use alloc::{vec, vec::Vec}; #[test] fn test_exception_table_encode_decode() { diff --git a/crates/compiler-core/src/frozen.rs b/crates/compiler-core/src/frozen.rs index a79569ad7fb..81530610d0e 100644 --- a/crates/compiler-core/src/frozen.rs +++ b/crates/compiler-core/src/frozen.rs @@ -1,5 +1,6 @@ use crate::bytecode::*; use crate::marshal::{self, Read, ReadBorrowed, Write}; +use alloc::vec::Vec; /// A frozen module. Holds a frozen code object and whether it is part of a package #[derive(Copy, Clone)] diff --git a/crates/compiler-core/src/lib.rs b/crates/compiler-core/src/lib.rs index 151d4cdea52..245713d1a14 100644 --- a/crates/compiler-core/src/lib.rs +++ b/crates/compiler-core/src/lib.rs @@ -1,3 +1,4 @@ +#![no_std] #![doc(html_logo_url = "https://raw.githubusercontent.com/RustPython/RustPython/main/logo.png")] #![doc(html_root_url = "https://docs.rs/rustpython-compiler-core/")] diff --git a/crates/compiler-core/src/marshal.rs b/crates/compiler-core/src/marshal.rs index fa568226aa1..11df127920a 100644 --- a/crates/compiler-core/src/marshal.rs +++ b/crates/compiler-core/src/marshal.rs @@ -1,4 +1,5 @@ use crate::{OneIndexed, SourceLocation, bytecode::*}; +use alloc::{boxed::Box, vec::Vec}; use core::convert::Infallible; use malachite_bigint::{BigInt, Sign}; use num_complex::Complex64; diff --git a/crates/compiler-core/src/varint.rs b/crates/compiler-core/src/varint.rs index db8eb88b0e3..f1ea6b17ec0 100644 --- a/crates/compiler-core/src/varint.rs +++ b/crates/compiler-core/src/varint.rs @@ -3,6 +3,8 @@ //! Uses 6-bit chunks with a continuation bit (0x40) to encode integers. //! Used for exception tables and line number tables. +use alloc::vec::Vec; + /// Write a variable-length unsigned integer using 6-bit chunks. /// Returns the number of bytes written. #[inline] diff --git a/crates/literal/src/complex.rs b/crates/literal/src/complex.rs index 076f2807c99..c91d1c1439e 100644 --- a/crates/literal/src/complex.rs +++ b/crates/literal/src/complex.rs @@ -1,4 +1,6 @@ use crate::float; +use alloc::borrow::ToOwned; +use alloc::string::{String, ToString}; /// Convert a complex number to a string. pub fn to_string(re: f64, im: f64) -> String { diff --git a/crates/literal/src/escape.rs b/crates/literal/src/escape.rs index 72ceaf60d5b..1099c0a02bc 100644 --- a/crates/literal/src/escape.rs +++ b/crates/literal/src/escape.rs @@ -1,3 +1,4 @@ +use alloc::string::String; use rustpython_wtf8::{CodePoint, Wtf8}; #[derive(Debug, PartialEq, Eq, Copy, Clone, Hash, is_macro::Is)] @@ -55,9 +56,9 @@ pub unsafe trait Escape { /// # Safety /// /// This string must only contain printable characters. - unsafe fn write_source(&self, formatter: &mut impl std::fmt::Write) -> core::fmt::Result; - fn write_body_slow(&self, formatter: &mut impl std::fmt::Write) -> core::fmt::Result; - fn write_body(&self, formatter: &mut impl std::fmt::Write) -> core::fmt::Result { + unsafe fn write_source(&self, formatter: &mut impl core::fmt::Write) -> core::fmt::Result; + fn write_body_slow(&self, formatter: &mut impl core::fmt::Write) -> core::fmt::Result; + fn write_body(&self, formatter: &mut impl core::fmt::Write) -> core::fmt::Result { if self.changed() { self.write_body_slow(formatter) } else { @@ -117,7 +118,7 @@ impl<'a> UnicodeEscape<'a> { pub struct StrRepr<'r, 'a>(&'r UnicodeEscape<'a>); impl StrRepr<'_, '_> { - pub fn write(&self, formatter: &mut impl std::fmt::Write) -> core::fmt::Result { + pub fn write(&self, formatter: &mut impl core::fmt::Write) -> core::fmt::Result { let quote = self.0.layout().quote.to_char(); formatter.write_char(quote)?; self.0.write_body(formatter)?; @@ -216,7 +217,7 @@ impl UnicodeEscape<'_> { fn write_char( ch: CodePoint, quote: Quote, - formatter: &mut impl std::fmt::Write, + formatter: &mut impl core::fmt::Write, ) -> core::fmt::Result { let Some(ch) = ch.to_char() else { return write!(formatter, "\\u{:04x}", ch.to_u32()); @@ -260,15 +261,15 @@ unsafe impl Escape for UnicodeEscape<'_> { &self.layout } - unsafe fn write_source(&self, formatter: &mut impl std::fmt::Write) -> core::fmt::Result { + unsafe fn write_source(&self, formatter: &mut impl core::fmt::Write) -> core::fmt::Result { formatter.write_str(unsafe { // SAFETY: this function must be called only when source is printable characters (i.e. no surrogates) - std::str::from_utf8_unchecked(self.source.as_bytes()) + core::str::from_utf8_unchecked(self.source.as_bytes()) }) } #[cold] - fn write_body_slow(&self, formatter: &mut impl std::fmt::Write) -> core::fmt::Result { + fn write_body_slow(&self, formatter: &mut impl core::fmt::Write) -> core::fmt::Result { for ch in self.source.code_points() { Self::write_char(ch, self.layout().quote, formatter)?; } @@ -378,7 +379,11 @@ impl AsciiEscape<'_> { } } - fn write_char(ch: u8, quote: Quote, formatter: &mut impl std::fmt::Write) -> core::fmt::Result { + fn write_char( + ch: u8, + quote: Quote, + formatter: &mut impl core::fmt::Write, + ) -> core::fmt::Result { match ch { b'\t' => formatter.write_str("\\t"), b'\n' => formatter.write_str("\\n"), @@ -404,15 +409,15 @@ unsafe impl Escape for AsciiEscape<'_> { &self.layout } - unsafe fn write_source(&self, formatter: &mut impl std::fmt::Write) -> core::fmt::Result { + unsafe fn write_source(&self, formatter: &mut impl core::fmt::Write) -> core::fmt::Result { formatter.write_str(unsafe { // SAFETY: this function must be called only when source is printable ascii characters - std::str::from_utf8_unchecked(self.source) + core::str::from_utf8_unchecked(self.source) }) } #[cold] - fn write_body_slow(&self, formatter: &mut impl std::fmt::Write) -> core::fmt::Result { + fn write_body_slow(&self, formatter: &mut impl core::fmt::Write) -> core::fmt::Result { for ch in self.source { Self::write_char(*ch, self.layout().quote, formatter)?; } @@ -423,7 +428,7 @@ unsafe impl Escape for AsciiEscape<'_> { pub struct BytesRepr<'r, 'a>(&'r AsciiEscape<'a>); impl BytesRepr<'_, '_> { - pub fn write(&self, formatter: &mut impl std::fmt::Write) -> core::fmt::Result { + pub fn write(&self, formatter: &mut impl core::fmt::Write) -> core::fmt::Result { let quote = self.0.layout().quote.to_char(); formatter.write_char('b')?; formatter.write_char(quote)?; diff --git a/crates/literal/src/float.rs b/crates/literal/src/float.rs index 79c655487cb..0fc51782438 100644 --- a/crates/literal/src/float.rs +++ b/crates/literal/src/float.rs @@ -1,6 +1,9 @@ use crate::format::Case; +use alloc::borrow::ToOwned; +use alloc::format; +use alloc::string::{String, ToString}; +use core::f64; use num_traits::{Float, Zero}; -use std::f64; pub fn parse_str(literal: &str) -> Option { parse_inner(literal.trim().as_bytes()) diff --git a/crates/literal/src/lib.rs b/crates/literal/src/lib.rs index 29971070129..a863dd87738 100644 --- a/crates/literal/src/lib.rs +++ b/crates/literal/src/lib.rs @@ -1,3 +1,7 @@ +#![no_std] + +extern crate alloc; + pub mod char; pub mod complex; pub mod escape; diff --git a/crates/sre_engine/src/engine.rs b/crates/sre_engine/src/engine.rs index 249b599b030..36a7a9d485e 100644 --- a/crates/sre_engine/src/engine.rs +++ b/crates/sre_engine/src/engine.rs @@ -6,6 +6,7 @@ use crate::string::{ }; use super::{MAXREPEAT, SreAtCode, SreCatCode, SreInfo, SreOpcode, StrDrive, StringCursor}; +use alloc::{vec, vec::Vec}; use core::{convert::TryFrom, ptr::null}; use optional::Optioned; diff --git a/crates/sre_engine/src/lib.rs b/crates/sre_engine/src/lib.rs index 08c21de9df8..df598974ba9 100644 --- a/crates/sre_engine/src/lib.rs +++ b/crates/sre_engine/src/lib.rs @@ -1,3 +1,7 @@ +#![no_std] + +extern crate alloc; + pub mod constants; pub mod engine; pub mod string;