-
Notifications
You must be signed in to change notification settings - Fork 0
Auto generated code
Matheus Dias de Souza edited this page May 1, 2024
·
5 revisions
It is easier to understand what SModel does by examining what it generates.
Given:
smodel! {
mod smodel = crate;
type Arena = Arena;
/// My unified data type.
struct Symbol {
let x: f64 = 0.0;
let ref y: String = "".into();
pub fn Symbol() {
super();
}
/// Empty, Foo, FooBar or FooQux
pub fn name(&self) -> String {
"".into()
}
pub fn base_example(&self) -> String {
"from base".into()
}
}
struct Foo: Symbol {
pub fn Foo() {
super();
}
pub override fn name(&self) -> String {
"Foo".into()
}
}
struct FooBar: Foo {
pub fn FooBar() {
super();
}
#[inheritdoc]
pub override fn name(&self) -> String {
"FooBar".into()
}
pub override fn base_example(&self) -> String {
format!("from bar; {}", super.base_example())
}
}
struct FooQux: Foo {
pub fn FooQux() {
super();
}
#[inheritdoc]
pub override fn name(&self) -> String {
"FooQux".into()
}
}
}It expands to:
// Recursive expansion of smodel! macro
// =====================================
type Arena = crate::Arena<__data__::Symbol>;
#[doc = " My unified data type."]
#[derive(Clone)]
struct Symbol(::std::rc::Weak<__data__::Symbol>);
impl PartialEq for Symbol {
fn eq(&self, other: &Self) -> bool {
self.0.ptr_eq(&other.0)
}
}
impl ::std::hash::Hash for Symbol {
fn hash<H: ::std::hash::Hasher>(&self, state: &mut H) {
self.0.as_ptr().hash(state)
}
}
impl TryFrom<Symbol> for Foo {
type Error = crate::SModelError;
fn try_from(v: Symbol) -> Result<Self, Self::Error> {
if let __data__::__variant_Symbol::Foo(_o) = &v.0.upgrade().unwrap().__variant {
Ok(Foo(Symbol(v.0.clone())))
} else {
Err(crate::SModelError::Contravariant)
}
}
}
impl TryFrom<Symbol> for FooBar {
type Error = crate::SModelError;
fn try_from(v: Symbol) -> Result<Self, Self::Error> {
if let __data__::__variant_Symbol::Foo(_o) = &v.0.upgrade().unwrap().__variant {
if let __data__::__variant_Foo::FooBar(_o) = &_o.__variant {
Ok(FooBar(Foo(Symbol(v.0.clone()))))
} else {
Err(crate::SModelError::Contravariant)
}
} else {
Err(crate::SModelError::Contravariant)
}
}
}
impl TryFrom<Symbol> for FooQux {
type Error = crate::SModelError;
fn try_from(v: Symbol) -> Result<Self, Self::Error> {
if let __data__::__variant_Symbol::Foo(_o) = &v.0.upgrade().unwrap().__variant {
if let __data__::__variant_Foo::FooQux(_o) = &_o.__variant {
Ok(FooQux(Foo(Symbol(v.0.clone()))))
} else {
Err(crate::SModelError::Contravariant)
}
} else {
Err(crate::SModelError::Contravariant)
}
}
}
#[derive(Clone, PartialEq, Hash)]
struct Foo(Symbol);
impl ::std::ops::Deref for Foo {
type Target = Symbol;
fn deref(&self) -> &Self::Target {
&self.0
}
}
impl From<Foo> for Symbol {
fn from(v: Foo) -> Self {
Symbol(v.0 .0.clone())
}
}
impl TryFrom<Foo> for FooBar {
type Error = crate::SModelError;
fn try_from(v: Foo) -> Result<Self, Self::Error> {
if let __data__::__variant_Symbol::Foo(_o) = &v.0 .0.upgrade().unwrap().__variant {
if let __data__::__variant_Foo::FooBar(_o) = &_o.__variant {
Ok(FooBar(Foo(Symbol(v.0 .0.clone()))))
} else {
Err(crate::SModelError::Contravariant)
}
} else {
Err(crate::SModelError::Contravariant)
}
}
}
impl TryFrom<Foo> for FooQux {
type Error = crate::SModelError;
fn try_from(v: Foo) -> Result<Self, Self::Error> {
if let __data__::__variant_Symbol::Foo(_o) = &v.0 .0.upgrade().unwrap().__variant {
if let __data__::__variant_Foo::FooQux(_o) = &_o.__variant {
Ok(FooQux(Foo(Symbol(v.0 .0.clone()))))
} else {
Err(crate::SModelError::Contravariant)
}
} else {
Err(crate::SModelError::Contravariant)
}
}
}
#[derive(Clone, PartialEq, Hash)]
struct FooBar(Foo);
impl ::std::ops::Deref for FooBar {
type Target = Foo;
fn deref(&self) -> &Self::Target {
&self.0
}
}
impl From<FooBar> for Foo {
fn from(v: FooBar) -> Self {
Foo(v.0 .0.clone())
}
}
impl From<FooBar> for Symbol {
fn from(v: FooBar) -> Self {
Symbol(v.0 .0 .0.clone())
}
}
#[derive(Clone, PartialEq, Hash)]
struct FooQux(Foo);
impl ::std::ops::Deref for FooQux {
type Target = Foo;
fn deref(&self) -> &Self::Target {
&self.0
}
}
impl From<FooQux> for Foo {
fn from(v: FooQux) -> Self {
Foo(v.0 .0.clone())
}
}
impl From<FooQux> for Symbol {
fn from(v: FooQux) -> Self {
Symbol(v.0 .0 .0.clone())
}
}
impl Symbol {
fn x(&self) -> f64 {
self.0.upgrade().unwrap().x.get()
}
fn set_x(&self, v: f64) {
self.0.upgrade().unwrap().x.set(v);
}
fn y(&self) -> String {
self.0.upgrade().unwrap().y.borrow().clone()
}
fn set_y(&self, v: String) {
self.0.upgrade().unwrap().y.replace(v);
}
fn __ctor(&self) {}
pub fn new(arena: &Arena) -> Self {
let __cto1 = Symbol(
arena
.allocate(__data__::Symbol {
x: ::std::cell::Cell::new(0.0),
y: ::std::cell::RefCell::new("".into()),
__variant: __data__::__variant_Symbol::__NoSubmeaning,
})
.clone(),
);
__cto1.__ctor();
__cto1
}
fn __nd_name(&self) -> String {
"".into()
}
fn __nd_base_example(&self) -> String {
"from base".into()
}
#[doc = " Empty, Foo, FooBar or FooQux"]
pub fn name(&self) -> String {
if self.is::<Foo>() {
return Foo(self.clone()).name();
}
self.__nd_name()
}
pub fn base_example(&self) -> String {
if self.is::<Foo>() {
if self.is::<FooBar>() {
return FooBar(Foo(self.clone())).base_example();
}
}
self.__nd_base_example()
}
pub fn to<T: TryFrom<Symbol, Error = crate::SModelError>>(
&self,
) -> Result<T, crate::SModelError> {
T::try_from(self.clone())
}
pub fn is<T: TryFrom<Symbol, Error = crate::SModelError>>(&self) -> bool {
T::try_from(self.clone()).is_ok()
}
}
impl Foo {
fn __ctor(&self) {}
pub fn new(arena: &Arena) -> Self {
let __cto1 = Foo(Symbol(
arena
.allocate(__data__::Symbol {
x: ::std::cell::Cell::new(0.0),
y: ::std::cell::RefCell::new("".into()),
__variant: __data__::__variant_Symbol::Foo(::std::rc::Rc::new(
__data__::Foo {
__variant: __data__::__variant_Foo::__NoSubmeaning,
},
)),
})
.clone(),
));
Symbol::__ctor(&__cto1.0);
__cto1.__ctor();
__cto1
}
fn __nd_name(&self) -> String {
"Foo".into()
}
pub fn name(&self) -> String {
if self.is::<FooBar>() {
return FooBar(self.clone()).name();
} else if self.is::<FooQux>() {
return FooQux(self.clone()).name();
}
self.__nd_name()
}
pub fn to<T: TryFrom<Foo, Error = crate::SModelError>>(
&self,
) -> Result<T, crate::SModelError> {
T::try_from(self.clone())
}
pub fn is<T: TryFrom<Foo, Error = crate::SModelError>>(&self) -> bool {
T::try_from(self.clone()).is_ok()
}
}
impl FooBar {
fn __ctor(&self) {}
pub fn new(arena: &Arena) -> Self {
let __cto1 = FooBar(Foo(Symbol(
arena
.allocate(__data__::Symbol {
x: ::std::cell::Cell::new(0.0),
y: ::std::cell::RefCell::new("".into()),
__variant: __data__::__variant_Symbol::Foo(::std::rc::Rc::new(
__data__::Foo {
__variant: __data__::__variant_Foo::FooBar(::std::rc::Rc::new(
__data__::FooBar {
__variant: __data__::__variant_FooBar::__NoSubmeaning,
},
)),
},
)),
})
.clone(),
)));
Foo::__ctor(&__cto1.0);
__cto1.__ctor();
__cto1
}
fn __nd_name(&self) -> String {
"FooBar".into()
}
fn __nd_base_example(&self) -> String {
{
let res = $crate::fmt::format(builtin #format_args(
"from bar; {}",
Symbol::__nd_base_example(&self.0 .0),
));
res
}
}
pub fn name(&self) -> String {
self.__nd_name()
}
pub fn base_example(&self) -> String {
self.__nd_base_example()
}
pub fn to<T: TryFrom<FooBar, Error = crate::SModelError>>(
&self,
) -> Result<T, crate::SModelError> {
T::try_from(self.clone())
}
pub fn is<T: TryFrom<FooBar, Error = crate::SModelError>>(&self) -> bool {
T::try_from(self.clone()).is_ok()
}
}
impl FooQux {
fn __ctor(&self) {}
pub fn new(arena: &Arena) -> Self {
let __cto1 = FooQux(Foo(Symbol(
arena
.allocate(__data__::Symbol {
x: ::std::cell::Cell::new(0.0),
y: ::std::cell::RefCell::new("".into()),
__variant: __data__::__variant_Symbol::Foo(::std::rc::Rc::new(
__data__::Foo {
__variant: __data__::__variant_Foo::FooQux(::std::rc::Rc::new(
__data__::FooQux {
__variant: __data__::__variant_FooQux::__NoSubmeaning,
},
)),
},
)),
})
.clone(),
)));
Foo::__ctor(&__cto1.0);
__cto1.__ctor();
__cto1
}
fn __nd_name(&self) -> String {
"FooQux".into()
}
pub fn name(&self) -> String {
self.__nd_name()
}
pub fn to<T: TryFrom<FooQux, Error = crate::SModelError>>(
&self,
) -> Result<T, crate::SModelError> {
T::try_from(self.clone())
}
pub fn is<T: TryFrom<FooQux, Error = crate::SModelError>>(&self) -> bool {
T::try_from(self.clone()).is_ok()
}
}
#[allow(non_camel_case_types)]
mod __data__ {
use super::*;
pub enum __variant_Symbol {
Foo(::std::rc::Rc<Foo>),
__NoSubmeaning,
}
pub struct Symbol {
pub x: ::std::cell::Cell<f64>,
pub y: ::std::cell::RefCell<String>,
pub __variant: __variant_Symbol,
}
pub enum __variant_Foo {
FooBar(::std::rc::Rc<FooBar>),
FooQux(::std::rc::Rc<FooQux>),
__NoSubmeaning,
}
pub struct Foo {
pub __variant: __variant_Foo,
}
pub enum __variant_FooBar {
__NoSubmeaning,
}
pub struct FooBar {
pub __variant: __variant_FooBar,
}
pub enum __variant_FooQux {
__NoSubmeaning,
}
pub struct FooQux {
pub __variant: __variant_FooQux,
}
}