forked from Rust-related/RustPython
allow AsciiStr/AsciiString -> PyStr conversion
This commit is contained in:
@@ -1113,19 +1113,13 @@ fn cert_to_py(vm: &VirtualMachine, cert: &X509Ref, binary: bool) -> PyResult {
|
||||
.iter()
|
||||
.filter_map(|gen_name| {
|
||||
if let Some(email) = gen_name.email() {
|
||||
Some(
|
||||
vm.new_tuple((vm.ctx.new_ascii_literal(ascii!("email")), email))
|
||||
.into(),
|
||||
)
|
||||
Some(vm.new_tuple((ascii!("email"), email)).into())
|
||||
} else if let Some(dnsname) = gen_name.dnsname() {
|
||||
Some(
|
||||
vm.new_tuple((vm.ctx.new_ascii_literal(ascii!("DNS")), dnsname))
|
||||
.into(),
|
||||
)
|
||||
Some(vm.new_tuple((ascii!("DNS"), dnsname)).into())
|
||||
} else if let Some(ip) = gen_name.ipaddress() {
|
||||
Some(
|
||||
vm.new_tuple((
|
||||
vm.ctx.new_ascii_literal(ascii!("IP Address")),
|
||||
ascii!("IP Address"),
|
||||
String::from_utf8_lossy(ip).into_owned(),
|
||||
))
|
||||
.into(),
|
||||
|
||||
@@ -18,6 +18,7 @@ use crate::{
|
||||
IdProtocol, ItemProtocol, PyClassDef, PyClassImpl, PyComparisonValue, PyContext, PyObjectRef,
|
||||
PyRef, PyResult, PyValue, TryIntoRef, TypeProtocol, VirtualMachine,
|
||||
};
|
||||
use ascii::{AsciiStr, AsciiString};
|
||||
use bstr::ByteSlice;
|
||||
use itertools::Itertools;
|
||||
use num_traits::ToPrimitive;
|
||||
@@ -117,15 +118,21 @@ where
|
||||
}
|
||||
}
|
||||
|
||||
impl From<AsciiString> for PyStr {
|
||||
fn from(s: AsciiString) -> Self {
|
||||
unsafe { Self::new_ascii_unchecked(s.into()) }
|
||||
}
|
||||
}
|
||||
|
||||
impl From<String> for PyStr {
|
||||
fn from(s: String) -> PyStr {
|
||||
fn from(s: String) -> Self {
|
||||
s.into_boxed_str().into()
|
||||
}
|
||||
}
|
||||
|
||||
impl From<Box<str>> for PyStr {
|
||||
#[inline]
|
||||
fn from(value: Box<str>) -> PyStr {
|
||||
fn from(value: Box<str>) -> Self {
|
||||
// doing the check is ~10x faster for ascii, and is actually only 2% slower worst case for
|
||||
// non-ascii; see https://github.com/RustPython/RustPython/pull/2586#issuecomment-844611532
|
||||
let is_ascii = value.is_ascii();
|
||||
@@ -153,6 +160,13 @@ impl fmt::Display for PyStr {
|
||||
}
|
||||
}
|
||||
|
||||
impl TryIntoRef<PyStr> for AsciiString {
|
||||
#[inline]
|
||||
fn try_into_ref(self, vm: &VirtualMachine) -> PyResult<PyRef<PyStr>> {
|
||||
Ok(unsafe { PyStr::new_ascii_unchecked(self.into()) }.into_ref(vm))
|
||||
}
|
||||
}
|
||||
|
||||
impl TryIntoRef<PyStr> for String {
|
||||
#[inline]
|
||||
fn try_into_ref(self, vm: &VirtualMachine) -> PyResult<PyRef<PyStr>> {
|
||||
@@ -1318,6 +1332,12 @@ impl IntoPyObject for &String {
|
||||
}
|
||||
}
|
||||
|
||||
impl IntoPyObject for &AsciiStr {
|
||||
fn into_pyobject(self, vm: &VirtualMachine) -> PyObjectRef {
|
||||
vm.ctx.new_ascii_literal(self)
|
||||
}
|
||||
}
|
||||
|
||||
type SplitArgs<'a> = anystr::SplitArgs<'a, PyStrRef>;
|
||||
|
||||
#[derive(FromArgs)]
|
||||
|
||||
@@ -242,7 +242,7 @@ fn sys_exc_info(vm: &VirtualMachine) -> (PyObjectRef, PyObjectRef, PyObjectRef)
|
||||
|
||||
fn sys_git_info(vm: &VirtualMachine) -> PyTupleRef {
|
||||
vm.new_tuple((
|
||||
vm.ctx.new_ascii_literal(ascii!("RustPython")),
|
||||
ascii!("RustPython"),
|
||||
version::get_git_identifier(),
|
||||
version::get_git_revision(),
|
||||
))
|
||||
|
||||
Reference in New Issue
Block a user