Skip to content

Commit cf83008

Browse files
authored
no_std for wtf8, sre_engine, compiler-core, literal (#7051)
* `no_std` for wtf8 * `no_std` for sre_engine, compiler-core, literal
1 parent ea352cc commit cf83008

File tree

12 files changed

+53
-23
lines changed

12 files changed

+53
-23
lines changed

crates/compiler-core/src/bytecode.rs

Lines changed: 2 additions & 1 deletion
Original file line numberDiff line numberDiff line change
@@ -6,7 +6,7 @@ use crate::{
66
varint::{read_varint, read_varint_with_start, write_varint, write_varint_with_start},
77
{OneIndexed, SourceLocation},
88
};
9-
use alloc::{collections::BTreeSet, fmt, vec::Vec};
9+
use alloc::{borrow::ToOwned, boxed::Box, collections::BTreeSet, fmt, string::String, vec::Vec};
1010
use bitflags::bitflags;
1111
use core::{hash, mem, ops::Deref};
1212
use itertools::Itertools;
@@ -804,6 +804,7 @@ impl<C: Constant> fmt::Debug for CodeObject<C> {
804804
#[cfg(test)]
805805
mod tests {
806806
use super::*;
807+
use alloc::{vec, vec::Vec};
807808

808809
#[test]
809810
fn test_exception_table_encode_decode() {

crates/compiler-core/src/frozen.rs

Lines changed: 1 addition & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -1,5 +1,6 @@
11
use crate::bytecode::*;
22
use crate::marshal::{self, Read, ReadBorrowed, Write};
3+
use alloc::vec::Vec;
34

45
/// A frozen module. Holds a frozen code object and whether it is part of a package
56
#[derive(Copy, Clone)]

crates/compiler-core/src/lib.rs

Lines changed: 1 addition & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -1,3 +1,4 @@
1+
#![no_std]
12
#![doc(html_logo_url = "https://raw.githubusercontent.com/RustPython/RustPython/main/logo.png")]
23
#![doc(html_root_url = "https://docs.rs/rustpython-compiler-core/")]
34

crates/compiler-core/src/marshal.rs

Lines changed: 1 addition & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -1,4 +1,5 @@
11
use crate::{OneIndexed, SourceLocation, bytecode::*};
2+
use alloc::{boxed::Box, vec::Vec};
23
use core::convert::Infallible;
34
use malachite_bigint::{BigInt, Sign};
45
use num_complex::Complex64;

crates/compiler-core/src/varint.rs

Lines changed: 2 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -3,6 +3,8 @@
33
//! Uses 6-bit chunks with a continuation bit (0x40) to encode integers.
44
//! Used for exception tables and line number tables.
55
6+
use alloc::vec::Vec;
7+
68
/// Write a variable-length unsigned integer using 6-bit chunks.
79
/// Returns the number of bytes written.
810
#[inline]

crates/literal/src/complex.rs

Lines changed: 2 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -1,4 +1,6 @@
11
use crate::float;
2+
use alloc::borrow::ToOwned;
3+
use alloc::string::{String, ToString};
24

35
/// Convert a complex number to a string.
46
pub fn to_string(re: f64, im: f64) -> String {

crates/literal/src/escape.rs

Lines changed: 18 additions & 13 deletions
Original file line numberDiff line numberDiff line change
@@ -1,3 +1,4 @@
1+
use alloc::string::String;
12
use rustpython_wtf8::{CodePoint, Wtf8};
23

34
#[derive(Debug, PartialEq, Eq, Copy, Clone, Hash, is_macro::Is)]
@@ -55,9 +56,9 @@ pub unsafe trait Escape {
5556
/// # Safety
5657
///
5758
/// This string must only contain printable characters.
58-
unsafe fn write_source(&self, formatter: &mut impl std::fmt::Write) -> core::fmt::Result;
59-
fn write_body_slow(&self, formatter: &mut impl std::fmt::Write) -> core::fmt::Result;
60-
fn write_body(&self, formatter: &mut impl std::fmt::Write) -> core::fmt::Result {
59+
unsafe fn write_source(&self, formatter: &mut impl core::fmt::Write) -> core::fmt::Result;
60+
fn write_body_slow(&self, formatter: &mut impl core::fmt::Write) -> core::fmt::Result;
61+
fn write_body(&self, formatter: &mut impl core::fmt::Write) -> core::fmt::Result {
6162
if self.changed() {
6263
self.write_body_slow(formatter)
6364
} else {
@@ -117,7 +118,7 @@ impl<'a> UnicodeEscape<'a> {
117118
pub struct StrRepr<'r, 'a>(&'r UnicodeEscape<'a>);
118119

119120
impl StrRepr<'_, '_> {
120-
pub fn write(&self, formatter: &mut impl std::fmt::Write) -> core::fmt::Result {
121+
pub fn write(&self, formatter: &mut impl core::fmt::Write) -> core::fmt::Result {
121122
let quote = self.0.layout().quote.to_char();
122123
formatter.write_char(quote)?;
123124
self.0.write_body(formatter)?;
@@ -216,7 +217,7 @@ impl UnicodeEscape<'_> {
216217
fn write_char(
217218
ch: CodePoint,
218219
quote: Quote,
219-
formatter: &mut impl std::fmt::Write,
220+
formatter: &mut impl core::fmt::Write,
220221
) -> core::fmt::Result {
221222
let Some(ch) = ch.to_char() else {
222223
return write!(formatter, "\\u{:04x}", ch.to_u32());
@@ -260,15 +261,15 @@ unsafe impl Escape for UnicodeEscape<'_> {
260261
&self.layout
261262
}
262263

263-
unsafe fn write_source(&self, formatter: &mut impl std::fmt::Write) -> core::fmt::Result {
264+
unsafe fn write_source(&self, formatter: &mut impl core::fmt::Write) -> core::fmt::Result {
264265
formatter.write_str(unsafe {
265266
// SAFETY: this function must be called only when source is printable characters (i.e. no surrogates)
266-
std::str::from_utf8_unchecked(self.source.as_bytes())
267+
core::str::from_utf8_unchecked(self.source.as_bytes())
267268
})
268269
}
269270

270271
#[cold]
271-
fn write_body_slow(&self, formatter: &mut impl std::fmt::Write) -> core::fmt::Result {
272+
fn write_body_slow(&self, formatter: &mut impl core::fmt::Write) -> core::fmt::Result {
272273
for ch in self.source.code_points() {
273274
Self::write_char(ch, self.layout().quote, formatter)?;
274275
}
@@ -378,7 +379,11 @@ impl AsciiEscape<'_> {
378379
}
379380
}
380381

381-
fn write_char(ch: u8, quote: Quote, formatter: &mut impl std::fmt::Write) -> core::fmt::Result {
382+
fn write_char(
383+
ch: u8,
384+
quote: Quote,
385+
formatter: &mut impl core::fmt::Write,
386+
) -> core::fmt::Result {
382387
match ch {
383388
b'\t' => formatter.write_str("\\t"),
384389
b'\n' => formatter.write_str("\\n"),
@@ -404,15 +409,15 @@ unsafe impl Escape for AsciiEscape<'_> {
404409
&self.layout
405410
}
406411

407-
unsafe fn write_source(&self, formatter: &mut impl std::fmt::Write) -> core::fmt::Result {
412+
unsafe fn write_source(&self, formatter: &mut impl core::fmt::Write) -> core::fmt::Result {
408413
formatter.write_str(unsafe {
409414
// SAFETY: this function must be called only when source is printable ascii characters
410-
std::str::from_utf8_unchecked(self.source)
415+
core::str::from_utf8_unchecked(self.source)
411416
})
412417
}
413418

414419
#[cold]
415-
fn write_body_slow(&self, formatter: &mut impl std::fmt::Write) -> core::fmt::Result {
420+
fn write_body_slow(&self, formatter: &mut impl core::fmt::Write) -> core::fmt::Result {
416421
for ch in self.source {
417422
Self::write_char(*ch, self.layout().quote, formatter)?;
418423
}
@@ -423,7 +428,7 @@ unsafe impl Escape for AsciiEscape<'_> {
423428
pub struct BytesRepr<'r, 'a>(&'r AsciiEscape<'a>);
424429

425430
impl BytesRepr<'_, '_> {
426-
pub fn write(&self, formatter: &mut impl std::fmt::Write) -> core::fmt::Result {
431+
pub fn write(&self, formatter: &mut impl core::fmt::Write) -> core::fmt::Result {
427432
let quote = self.0.layout().quote.to_char();
428433
formatter.write_char('b')?;
429434
formatter.write_char(quote)?;

crates/literal/src/float.rs

Lines changed: 4 additions & 1 deletion
Original file line numberDiff line numberDiff line change
@@ -1,6 +1,9 @@
11
use crate::format::Case;
2+
use alloc::borrow::ToOwned;
3+
use alloc::format;
4+
use alloc::string::{String, ToString};
5+
use core::f64;
26
use num_traits::{Float, Zero};
3-
use std::f64;
47

58
pub fn parse_str(literal: &str) -> Option<f64> {
69
parse_inner(literal.trim().as_bytes())

crates/literal/src/lib.rs

Lines changed: 4 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -1,3 +1,7 @@
1+
#![no_std]
2+
3+
extern crate alloc;
4+
15
pub mod char;
26
pub mod complex;
37
pub mod escape;

crates/sre_engine/src/engine.rs

Lines changed: 1 addition & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -6,6 +6,7 @@ use crate::string::{
66
};
77

88
use super::{MAXREPEAT, SreAtCode, SreCatCode, SreInfo, SreOpcode, StrDrive, StringCursor};
9+
use alloc::{vec, vec::Vec};
910
use core::{convert::TryFrom, ptr::null};
1011
use optional::Optioned;
1112

0 commit comments

Comments
 (0)