Skip to content

Commit ec5b6b4

Browse files
authored
Merge pull request #3143 from youknowone/expose-ascii
expose ascii! macro as pub
2 parents 2d727ab + 5fab077 commit ec5b6b4

File tree

4 files changed

+18
-20
lines changed

4 files changed

+18
-20
lines changed

benches/microbenchmarks.rs

+2-4
Original file line numberDiff line numberDiff line change
@@ -3,9 +3,7 @@ use criterion::{
33
criterion_group, criterion_main, BatchSize, BenchmarkGroup, BenchmarkId, Criterion, Throughput,
44
};
55
use rustpython_compiler::Mode;
6-
use rustpython_vm::ItemProtocol;
7-
use rustpython_vm::PyResult;
8-
use rustpython_vm::{InitParameter, Interpreter, PySettings};
6+
use rustpython_vm::{utils::ascii, InitParameter, Interpreter, ItemProtocol, PyResult, PySettings};
97
use std::path::{Path, PathBuf};
108
use std::{ffi, fs, io};
119

@@ -133,7 +131,7 @@ fn bench_rustpy_code(group: &mut BenchmarkGroup<WallTime>, bench: &MicroBenchmar
133131
scope
134132
.locals
135133
.set_item(
136-
vm.ctx.new_ascii_literal(crate::utils::ascii!("ITERATIONS")),
134+
vm.ctx.new_ascii_literal(ascii!("ITERATIONS")),
137135
vm.ctx.new_int(idx),
138136
vm,
139137
)

vm/src/lib.rs

+1-1
Original file line numberDiff line numberDiff line change
@@ -40,7 +40,7 @@ pub use rustpython_derive::*;
4040

4141
// This is above everything else so that the defined macros are available everywhere
4242
#[macro_use]
43-
pub mod macros;
43+
pub(crate) mod macros;
4444

4545
mod anystr;
4646
pub mod builtins;

vm/src/macros.rs

+10
Original file line numberDiff line numberDiff line change
@@ -249,3 +249,13 @@ cfg_if::cfg_if! {
249249
}
250250
}
251251
}
252+
253+
macro_rules! ascii {
254+
($x:literal) => {{
255+
const _: () = {
256+
["not ascii"][!rustpython_vm::utils::bytes_is_ascii($x) as usize];
257+
};
258+
unsafe { ascii::AsciiStr::from_ascii_unchecked($x.as_bytes()) }
259+
}};
260+
}
261+
pub use ascii;

vm/src/utils.rs

+5-15
Original file line numberDiff line numberDiff line change
@@ -1,11 +1,13 @@
1-
use crate::builtins::{pystr::PyStr, PyFloat};
2-
use crate::exceptions::IntoPyException;
31
use crate::{
2+
builtins::{pystr::PyStr, PyFloat},
3+
exceptions::IntoPyException,
44
IntoPyObject, PyObjectRef, PyRef, PyResult, PyValue, TryFromObject, TypeProtocol,
55
VirtualMachine,
66
};
77
use num_traits::ToPrimitive;
88

9+
pub use crate::macros::ascii;
10+
911
pub enum Either<A, B> {
1012
A(A),
1113
B(B),
@@ -127,8 +129,7 @@ impl ToCString for PyStr {
127129
}
128130
}
129131

130-
#[allow(dead_code)]
131-
pub(crate) const fn bytes_is_ascii(x: &str) -> bool {
132+
pub const fn bytes_is_ascii(x: &str) -> bool {
132133
let x = x.as_bytes();
133134
let mut i = 0;
134135
while i < x.len() {
@@ -139,14 +140,3 @@ pub(crate) const fn bytes_is_ascii(x: &str) -> bool {
139140
}
140141
true
141142
}
142-
143-
macro_rules! ascii {
144-
($x:literal) => {{
145-
const _: () = {
146-
["not ascii"][!crate::utils::bytes_is_ascii($x) as usize];
147-
};
148-
unsafe { ascii::AsciiStr::from_ascii_unchecked($x.as_bytes()) }
149-
}};
150-
}
151-
152-
pub(crate) use ascii;

0 commit comments

Comments
 (0)