no_std for wtf8, sre_engine, compiler-core, literal (#7051)

* `no_std` for wtf8

* `no_std` for sre_engine, compiler-core, literal
This commit is contained in:
Jeong, YunWon
2026-02-09 01:04:54 +09:00
committed by GitHub
parent ea352ccdae
commit cf83008d33
12 changed files with 53 additions and 23 deletions

View File

@@ -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<C: Constant> fmt::Debug for CodeObject<C> {
#[cfg(test)]
mod tests {
use super::*;
use alloc::{vec, vec::Vec};
#[test]
fn test_exception_table_encode_decode() {

View File

@@ -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)]

View File

@@ -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/")]

View File

@@ -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;

View File

@@ -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]

View File

@@ -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 {

View File

@@ -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)?;

View File

@@ -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<f64> {
parse_inner(literal.trim().as_bytes())

View File

@@ -1,3 +1,7 @@
#![no_std]
extern crate alloc;
pub mod char;
pub mod complex;
pub mod escape;

View File

@@ -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;

View File

@@ -1,3 +1,7 @@
#![no_std]
extern crate alloc;
pub mod constants;
pub mod engine;
pub mod string;

View File

@@ -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<str> 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))
}
}