mirror of
https://github.com/RustPython/RustPython.git
synced 2026-10-28 18:59:04 +08:00
Make PyClassDef::BASE a function
This commit is contained in:
2 files changed
+9
-21
No files matched your search
@@ -177,8 +177,11 @@ fn generate_class_def(
|
||||
}
|
||||
|
||||
let base_class = if is_pystruct {
|
||||
let baseclassdef = quote!(::rustpython_vm::pyobject::BaseClassDef);
|
||||
quote!(const BASE: #baseclassdef = #baseclassdef::tuple();)
|
||||
quote! {
|
||||
fn base_class(ctx: &::rustpython_vm::pyobject::PyContext) -> ::rustpython_vm::obj::objtype::PyClassRef {
|
||||
ctx.types.tuple_type.clone()
|
||||
}
|
||||
}
|
||||
} else {
|
||||
quote!()
|
||||
};
|
||||
|
||||
+4
-19
@@ -1260,29 +1260,14 @@ where
|
||||
}
|
||||
}
|
||||
|
||||
// can be extended in the future to use some sort of pyclass() attr instead of make_class_with_base
|
||||
pub struct BaseClassDef {
|
||||
is_tuple: bool,
|
||||
}
|
||||
impl BaseClassDef {
|
||||
pub const fn tuple() -> Self {
|
||||
BaseClassDef { is_tuple: true }
|
||||
}
|
||||
pub fn get_class(&self, ctx: &PyContext) -> PyClassRef {
|
||||
if self.is_tuple {
|
||||
ctx.types.tuple_type.clone()
|
||||
} else {
|
||||
ctx.types.object_type.clone()
|
||||
}
|
||||
}
|
||||
}
|
||||
|
||||
pub trait PyClassDef {
|
||||
const NAME: &'static str;
|
||||
const MODULE_NAME: Option<&'static str>;
|
||||
const TP_NAME: &'static str;
|
||||
const DOC: Option<&'static str> = None;
|
||||
const BASE: BaseClassDef = BaseClassDef { is_tuple: false };
|
||||
fn base_class(ctx: &PyContext) -> PyClassRef {
|
||||
ctx.types.object_type.clone()
|
||||
}
|
||||
}
|
||||
|
||||
impl<T> PyClassDef for PyRef<T>
|
||||
@@ -1326,7 +1311,7 @@ pub trait PyClassImpl: PyClassDef {
|
||||
}
|
||||
|
||||
fn make_class(ctx: &PyContext) -> PyClassRef {
|
||||
Self::make_class_with_base(ctx, Self::NAME, Self::BASE.get_class(ctx))
|
||||
Self::make_class_with_base(ctx, Self::NAME, Self::base_class(ctx))
|
||||
}
|
||||
|
||||
fn make_class_with_base(ctx: &PyContext, name: &str, base: PyClassRef) -> PyClassRef {
|
||||
|
||||
Reference in new issue
Block a user