expose ascii! macro as pub

This commit is contained in:
Jeong YunWon
2021-09-26 12:20:40 +09:00
parent 2d727abfbb
commit 5fab0771ae
4 changed files with 18 additions and 20 deletions

View File

@@ -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<WallTime>, 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,
)

View File

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

View File

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

View File

@@ -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, B> {
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;