diff --git a/benches/microbenchmarks.rs b/benches/microbenchmarks.rs index 505e0819f8..377196e8bd 100644 --- a/benches/microbenchmarks.rs +++ b/benches/microbenchmarks.rs @@ -3,9 +3,7 @@ use criterion::{ criterion_group, criterion_main, BatchSize, BenchmarkGroup, BenchmarkId, Criterion, Throughput, }; use rustpython_compiler::Mode; -use rustpython_vm::ItemProtocol; -use rustpython_vm::PyResult; -use rustpython_vm::{InitParameter, Interpreter, PySettings}; +use rustpython_vm::{utils::ascii, InitParameter, Interpreter, ItemProtocol, PyResult, PySettings}; use std::path::{Path, PathBuf}; use std::{ffi, fs, io}; @@ -133,7 +131,7 @@ fn bench_rustpy_code(group: &mut BenchmarkGroup, bench: &MicroBenchmar scope .locals .set_item( - vm.ctx.new_ascii_literal(crate::utils::ascii!("ITERATIONS")), + vm.ctx.new_ascii_literal(ascii!("ITERATIONS")), vm.ctx.new_int(idx), vm, ) diff --git a/vm/src/lib.rs b/vm/src/lib.rs index 6a301e278b..4c8e15c255 100644 --- a/vm/src/lib.rs +++ b/vm/src/lib.rs @@ -40,7 +40,7 @@ pub use rustpython_derive::*; // This is above everything else so that the defined macros are available everywhere #[macro_use] -pub mod macros; +pub(crate) mod macros; mod anystr; pub mod builtins; diff --git a/vm/src/macros.rs b/vm/src/macros.rs index 6997e9231f..ae3dcffb22 100644 --- a/vm/src/macros.rs +++ b/vm/src/macros.rs @@ -249,3 +249,13 @@ cfg_if::cfg_if! { } } } + +macro_rules! ascii { + ($x:literal) => {{ + const _: () = { + ["not ascii"][!rustpython_vm::utils::bytes_is_ascii($x) as usize]; + }; + unsafe { ascii::AsciiStr::from_ascii_unchecked($x.as_bytes()) } + }}; +} +pub use ascii; diff --git a/vm/src/utils.rs b/vm/src/utils.rs index d0f65de272..953eb8932c 100644 --- a/vm/src/utils.rs +++ b/vm/src/utils.rs @@ -1,11 +1,13 @@ -use crate::builtins::{pystr::PyStr, PyFloat}; -use crate::exceptions::IntoPyException; use crate::{ + builtins::{pystr::PyStr, PyFloat}, + exceptions::IntoPyException, IntoPyObject, PyObjectRef, PyRef, PyResult, PyValue, TryFromObject, TypeProtocol, VirtualMachine, }; use num_traits::ToPrimitive; +pub use crate::macros::ascii; + pub enum Either { A(A), B(B), @@ -127,8 +129,7 @@ impl ToCString for PyStr { } } -#[allow(dead_code)] -pub(crate) const fn bytes_is_ascii(x: &str) -> bool { +pub const fn bytes_is_ascii(x: &str) -> bool { let x = x.as_bytes(); let mut i = 0; while i < x.len() { @@ -139,14 +140,3 @@ pub(crate) const fn bytes_is_ascii(x: &str) -> bool { } true } - -macro_rules! ascii { - ($x:literal) => {{ - const _: () = { - ["not ascii"][!crate::utils::bytes_is_ascii($x) as usize]; - }; - unsafe { ascii::AsciiStr::from_ascii_unchecked($x.as_bytes()) } - }}; -} - -pub(crate) use ascii;