From 2d49dcd66ecd6595af4084a97c409271f62857a7 Mon Sep 17 00:00:00 2001 From: Noah <33094578+coolreader18@users.noreply.github.com> Date: Sun, 30 Aug 2020 14:44:10 -0500 Subject: [PATCH] Make PyClassDef::BASE a function --- derive/src/pyclass.rs | 7 +++++-- vm/src/pyobject.rs | 23 ++++------------------- 2 files changed, 9 insertions(+), 21 deletions(-) diff --git a/derive/src/pyclass.rs b/derive/src/pyclass.rs index ea951701f..9b0be8574 100644 --- a/derive/src/pyclass.rs +++ b/derive/src/pyclass.rs @@ -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!() }; diff --git a/vm/src/pyobject.rs b/vm/src/pyobject.rs index b4b4768c7..753b1c496 100644 --- a/vm/src/pyobject.rs +++ b/vm/src/pyobject.rs @@ -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 PyClassDef for PyRef @@ -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 {