mirror of
https://github.com/RustPython/RustPython.git
synced 2026-06-02 19:39:49 +09:00
Update some stuff for inline const & associated type bounds
This commit is contained in:
@@ -331,12 +331,15 @@ pub mod levenshtein {
|
||||
/// ```
|
||||
#[macro_export]
|
||||
macro_rules! ascii {
|
||||
($x:literal) => {{
|
||||
const STR: &str = $x;
|
||||
const _: () = if !STR.is_ascii() {
|
||||
panic!("ascii!() argument is not an ascii string");
|
||||
($x:expr $(,)?) => {{
|
||||
let s = const {
|
||||
let s: &str = $x;
|
||||
if !s.is_ascii() {
|
||||
panic!("ascii!() argument is not an ascii string");
|
||||
}
|
||||
s
|
||||
};
|
||||
unsafe { $crate::vendored::ascii::AsciiStr::from_ascii_unchecked(STR.as_bytes()) }
|
||||
unsafe { $crate::vendored::ascii::AsciiStr::from_ascii_unchecked(s.as_bytes()) }
|
||||
}};
|
||||
}
|
||||
pub use ascii;
|
||||
|
||||
@@ -102,8 +102,7 @@ impl FrozenLib<Vec<u8>> {
|
||||
/// Encode the given iterator of frozen modules into a compressed vector of bytes
|
||||
pub fn encode<'a, I, B: AsRef<[u8]>>(lib: I) -> FrozenLib<Vec<u8>>
|
||||
where
|
||||
I: IntoIterator<Item = (&'a str, FrozenModule<B>)>,
|
||||
I::IntoIter: ExactSizeIterator + Clone,
|
||||
I: IntoIterator<Item = (&'a str, FrozenModule<B>), IntoIter: ExactSizeIterator + Clone>,
|
||||
{
|
||||
let iter = lib.into_iter();
|
||||
let mut bytes = Vec::new();
|
||||
|
||||
@@ -728,11 +728,8 @@ impl ExactSizeIterator for DictIter<'_> {
|
||||
}
|
||||
|
||||
#[pyclass]
|
||||
trait DictView: PyPayload + PyClassDef + Iterable + Representable
|
||||
where
|
||||
Self::ReverseIter: PyPayload,
|
||||
{
|
||||
type ReverseIter;
|
||||
trait DictView: PyPayload + PyClassDef + Iterable + Representable {
|
||||
type ReverseIter: PyPayload;
|
||||
|
||||
fn dict(&self) -> &PyDictRef;
|
||||
fn item(vm: &VirtualMachine, key: PyObjectRef, value: PyObjectRef) -> PyObjectRef;
|
||||
|
||||
@@ -55,19 +55,14 @@ const fn zst_ref_out_of_thin_air<T: 'static>(x: T) -> &'static T {
|
||||
// would never get called anyway if we consider this semantically a Box::leak(Box::new(x))-type
|
||||
// operation. if T isn't zero-sized, we don't have to worry about it because we'll fail to compile.
|
||||
std::mem::forget(x);
|
||||
trait Zst: Sized + 'static {
|
||||
const THIN_AIR: &'static Self = {
|
||||
if std::mem::size_of::<Self>() == 0 {
|
||||
// SAFETY: we just confirmed that Self is zero-sized, so we can
|
||||
// pull a value of it out of thin air.
|
||||
unsafe { std::ptr::NonNull::<Self>::dangling().as_ref() }
|
||||
} else {
|
||||
panic!("can't use a non-zero-sized type here")
|
||||
}
|
||||
};
|
||||
const {
|
||||
if std::mem::size_of::<T>() != 0 {
|
||||
panic!("can't use a non-zero-sized type here")
|
||||
}
|
||||
// SAFETY: we just confirmed that T is zero-sized, so we can
|
||||
// pull a value of it out of thin air.
|
||||
unsafe { std::ptr::NonNull::<T>::dangling().as_ref() }
|
||||
}
|
||||
impl<T: 'static> Zst for T {}
|
||||
<T as Zst>::THIN_AIR
|
||||
}
|
||||
|
||||
/// Get the [`STATIC_FUNC`](IntoPyNativeFn::STATIC_FUNC) of the passed function. The same
|
||||
|
||||
@@ -228,12 +228,8 @@ mod sealed {
|
||||
}
|
||||
|
||||
/// A sealed marker trait for `DictKey` types that always become an exact instance of `str`
|
||||
pub trait InternableString
|
||||
where
|
||||
Self: sealed::SealedInternable + ToPyObject + AsRef<Self::Interned>,
|
||||
Self::Interned: MaybeInternedString,
|
||||
{
|
||||
type Interned: ?Sized;
|
||||
pub trait InternableString: sealed::SealedInternable + ToPyObject + AsRef<Self::Interned> {
|
||||
type Interned: MaybeInternedString + ?Sized;
|
||||
fn into_pyref_exact(self, str_type: PyTypeRef) -> PyRefExact<PyStr>;
|
||||
}
|
||||
|
||||
|
||||
@@ -1,4 +1,4 @@
|
||||
use std::{fmt, marker::PhantomData};
|
||||
use std::fmt;
|
||||
|
||||
use crate::{
|
||||
object::{
|
||||
@@ -16,25 +16,18 @@ pub(in crate::object) struct PyObjVTable {
|
||||
}
|
||||
|
||||
impl PyObjVTable {
|
||||
pub fn of<T: PyObjectPayload>() -> &'static Self {
|
||||
struct Helper<T: PyObjectPayload>(PhantomData<T>);
|
||||
trait VtableHelper {
|
||||
const VTABLE: PyObjVTable;
|
||||
pub const fn of<T: PyObjectPayload>() -> &'static Self {
|
||||
&PyObjVTable {
|
||||
drop_dealloc: drop_dealloc_obj::<T>,
|
||||
debug: debug_obj::<T>,
|
||||
trace: const {
|
||||
if T::IS_TRACE {
|
||||
Some(try_trace_obj::<T>)
|
||||
} else {
|
||||
None
|
||||
}
|
||||
},
|
||||
}
|
||||
impl<T: PyObjectPayload> VtableHelper for Helper<T> {
|
||||
const VTABLE: PyObjVTable = PyObjVTable {
|
||||
drop_dealloc: drop_dealloc_obj::<T>,
|
||||
debug: debug_obj::<T>,
|
||||
trace: {
|
||||
if T::IS_TRACE {
|
||||
Some(try_trace_obj::<T>)
|
||||
} else {
|
||||
None
|
||||
}
|
||||
},
|
||||
};
|
||||
}
|
||||
&Helper::<T>::VTABLE
|
||||
}
|
||||
}
|
||||
|
||||
|
||||
@@ -133,13 +133,10 @@ impl VirtualMachine {
|
||||
let import_func = ctx.none();
|
||||
let profile_func = RefCell::new(ctx.none());
|
||||
let trace_func = RefCell::new(ctx.none());
|
||||
// hack to get around const array repeat expressions, rust issue #79270
|
||||
const NONE: Option<PyObjectRef> = None;
|
||||
// putting it in a const optimizes better, prevents linear initialization of the array
|
||||
#[allow(clippy::declare_interior_mutable_const)]
|
||||
const SIGNAL_HANDLERS: RefCell<[Option<PyObjectRef>; signal::NSIG]> =
|
||||
RefCell::new([NONE; signal::NSIG]);
|
||||
let signal_handlers = Some(Box::new(SIGNAL_HANDLERS));
|
||||
let signal_handlers = Some(Box::new(
|
||||
// putting it in a const optimizes better, prevents linear initialization of the array
|
||||
const { RefCell::new([const { None }; signal::NSIG]) },
|
||||
));
|
||||
|
||||
let module_inits = stdlib::get_module_inits();
|
||||
|
||||
|
||||
Reference in New Issue
Block a user