Skip to content

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 {
        pub fn Symbol() {
            super();
        }

        /// Empty, Foo, FooBar or FooQux
        pub fn name(&self) -> String {
            "".into()
        }

        pub fn base_example(&self) -> String {
            "from base".into()
        }

        pub fn x(&self) -> f64 {
            0.0
        }
    }

    struct Foo: Symbol {
        pub fn Foo() {
            super();
        }

        pub override fn name(&self) -> String {
            "Foo".into()
        }
    }

    struct FooBar: Foo {
        pub fn FooBar() {
            super();
        }

        pub override fn name(&self) -> String {
            "FooBar".into()
        }

        pub override fn base_example(&self) -> String {
            format!("from bar; {}", super.base_example())
        }
    }
    
    struct FooBarBar: FooBar {
        let _x: f64 = 0.0;
        let ref _y: String = "".into();

        pub fn FooBarBar(x: f64, y: &str) {
            super();
            self.set__x(x);
            self.set__y(y.into());
        }

        pub override fn name(&self) -> String {
            "FooBarBar".into()
        }

        pub override fn x(&self) -> f64 {
            self._x()
        }

        pub override fn base_example(&self) -> String {
            format!("from {}; {}", self._y(), super.base_example())
        }
    }

    struct FooQux: Foo {
        pub fn FooQux() {
            super();
        }

        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 FooBarBar {
    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 {
                if let __data__::__variant_FooBar::FooBarBar(_o) = &_o.__variant {
                    Ok(FooBarBar(FooBar(Foo(Symbol(v.0.clone())))))
                } else {
                    Err(crate::SModelError::Contravariant)
                }
            } 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 FooBarBar {
    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 {
                if let __data__::__variant_FooBar::FooBarBar(_o) = &_o.__variant {
                    Ok(FooBarBar(FooBar(Foo(Symbol(v.0 .0.clone())))))
                } else {
                    Err(crate::SModelError::Contravariant)
                }
            } 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())
    }
}
impl TryFrom<FooBar> for FooBarBar {
    type Error = crate::SModelError;
    fn try_from(v: FooBar) -> Result<Self, Self::Error> {
        if let __data__::__variant_Symbol::Foo(_o) = &v.0 .0 .0.upgrade().unwrap().__variant {
            if let __data__::__variant_Foo::FooBar(_o) = &_o.__variant {
                if let __data__::__variant_FooBar::FooBarBar(_o) = &_o.__variant {
                    Ok(FooBarBar(FooBar(Foo(Symbol(v.0 .0 .0.clone())))))
                } else {
                    Err(crate::SModelError::Contravariant)
                }
            } else {
                Err(crate::SModelError::Contravariant)
            }
        } else {
            Err(crate::SModelError::Contravariant)
        }
    }
}
#[derive(Clone, PartialEq, Hash)]
struct FooBarBar(FooBar);

impl ::std::ops::Deref for FooBarBar {
    type Target = FooBar;
    fn deref(&self) -> &Self::Target {
        &self.0
    }
}
impl From<FooBarBar> for FooBar {
    fn from(v: FooBarBar) -> Self {
        FooBar(v.0 .0.clone())
    }
}
impl From<FooBarBar> for Foo {
    fn from(v: FooBarBar) -> Self {
        Foo(v.0 .0 .0.clone())
    }
}
impl From<FooBarBar> for Symbol {
    fn from(v: FooBarBar) -> Self {
        Symbol(v.0 .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 __ctor(&self) {}

    pub fn new(arena: &Arena) -> Self {
        let __cto1 = Symbol(
            arena
                .allocate(__data__::Symbol {
                    __variant: __data__::__variant_Symbol::__Nothing,
                })
                .clone(),
        );
        __cto1.__ctor();
        __cto1
    }
    fn __nd_name(&self) -> String {
        "".into()
    }
    fn __nd_base_example(&self) -> String {
        "from base".into()
    }
    fn __nd_x(&self) -> f64 {
        0.0
    }
    #[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 x(&self) -> f64 {
        if self.is::<Foo>() {
            if self.is::<FooBar>() {
                if self.is::<FooBarBar>() {
                    return FooBarBar(FooBar(Foo(self.clone()))).x();
                }
            }
        }
        self.__nd_x()
    }
    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 {
                    __variant: __data__::__variant_Symbol::Foo(::std::rc::Rc::new(
                        __data__::Foo {
                            __variant: __data__::__variant_Foo::__Nothing,
                        },
                    )),
                })
                .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 {
                    __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::__Nothing,
                                },
                            )),
                        },
                    )),
                })
                .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 {
        if self.is::<FooBarBar>() {
            return FooBarBar(self.clone()).name();
        }
        self.__nd_name()
    }
    pub fn base_example(&self) -> String {
        if self.is::<FooBarBar>() {
            return FooBarBar(self.clone()).base_example();
        }
        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 FooBarBar {
    #[allow(non_snake_case)]
    fn _x(&self) -> f64 {
        (if let __data__::__variant_Symbol::Foo(o) =
            &self.0 .0 .0 .0.upgrade().unwrap().__variant
        {
            (if let __data__::__variant_Foo::FooBar(o) = &o.__variant {
                (if let __data__::__variant_FooBar::FooBarBar(o) = &o.__variant {
                    &o._x
                } else {
                    {
                        #[cold]
                        #[track_caller]
                        #[inline(never)]
                        const fn panic_cold_explicit() -> ! {
                            $crate::panicking::panic_explicit()
                        }
                        panic_cold_explicit();
                    }
                })
            } else {
                {
                    #[cold]
                    #[track_caller]
                    #[inline(never)]
                    const fn panic_cold_explicit() -> ! {
                        $crate::panicking::panic_explicit()
                    }
                    panic_cold_explicit();
                }
            })
        } else {
            {
                #[cold]
                #[track_caller]
                #[inline(never)]
                const fn panic_cold_explicit() -> ! {
                    $crate::panicking::panic_explicit()
                }
                panic_cold_explicit();
            }
        })
        .get()
    }
    #[allow(non_snake_case)]
    fn set__x(&self, v: f64) {
        (if let __data__::__variant_Symbol::Foo(o) =
            &self.0 .0 .0 .0.upgrade().unwrap().__variant
        {
            (if let __data__::__variant_Foo::FooBar(o) = &o.__variant {
                (if let __data__::__variant_FooBar::FooBarBar(o) = &o.__variant {
                    &o._x
                } else {
                    {
                        #[cold]
                        #[track_caller]
                        #[inline(never)]
                        const fn panic_cold_explicit() -> ! {
                            $crate::panicking::panic_explicit()
                        }
                        panic_cold_explicit();
                    }
                })
            } else {
                {
                    #[cold]
                    #[track_caller]
                    #[inline(never)]
                    const fn panic_cold_explicit() -> ! {
                        $crate::panicking::panic_explicit()
                    }
                    panic_cold_explicit();
                }
            })
        } else {
            {
                #[cold]
                #[track_caller]
                #[inline(never)]
                const fn panic_cold_explicit() -> ! {
                    $crate::panicking::panic_explicit()
                }
                panic_cold_explicit();
            }
        })
        .set(v);
    }
    #[allow(non_snake_case)]
    fn _y(&self) -> String {
        (if let __data__::__variant_Symbol::Foo(o) =
            &self.0 .0 .0 .0.upgrade().unwrap().__variant
        {
            (if let __data__::__variant_Foo::FooBar(o) = &o.__variant {
                (if let __data__::__variant_FooBar::FooBarBar(o) = &o.__variant {
                    &o._y
                } else {
                    {
                        #[cold]
                        #[track_caller]
                        #[inline(never)]
                        const fn panic_cold_explicit() -> ! {
                            $crate::panicking::panic_explicit()
                        }
                        panic_cold_explicit();
                    }
                })
            } else {
                {
                    #[cold]
                    #[track_caller]
                    #[inline(never)]
                    const fn panic_cold_explicit() -> ! {
                        $crate::panicking::panic_explicit()
                    }
                    panic_cold_explicit();
                }
            })
        } else {
            {
                #[cold]
                #[track_caller]
                #[inline(never)]
                const fn panic_cold_explicit() -> ! {
                    $crate::panicking::panic_explicit()
                }
                panic_cold_explicit();
            }
        })
        .borrow()
        .clone()
    }
    #[allow(non_snake_case)]
    fn set__y(&self, v: String) {
        (if let __data__::__variant_Symbol::Foo(o) =
            &self.0 .0 .0 .0.upgrade().unwrap().__variant
        {
            (if let __data__::__variant_Foo::FooBar(o) = &o.__variant {
                (if let __data__::__variant_FooBar::FooBarBar(o) = &o.__variant {
                    &o._y
                } else {
                    {
                        #[cold]
                        #[track_caller]
                        #[inline(never)]
                        const fn panic_cold_explicit() -> ! {
                            $crate::panicking::panic_explicit()
                        }
                        panic_cold_explicit();
                    }
                })
            } else {
                {
                    #[cold]
                    #[track_caller]
                    #[inline(never)]
                    const fn panic_cold_explicit() -> ! {
                        $crate::panicking::panic_explicit()
                    }
                    panic_cold_explicit();
                }
            })
        } else {
            {
                #[cold]
                #[track_caller]
                #[inline(never)]
                const fn panic_cold_explicit() -> ! {
                    $crate::panicking::panic_explicit()
                }
                panic_cold_explicit();
            }
        })
        .replace(v);
    }
    fn __ctor(&self, x: f64, y: &str) {
        self.set__x(x);
        self.set__y(y.into());
    }
    pub fn new(arena: &Arena, x: f64, y: &str) -> Self {
        let __cto1 = FooBarBar(FooBar(Foo(Symbol(
            arena
                .allocate(__data__::Symbol {
                    __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::FooBarBar(
                                        ::std::rc::Rc::new(__data__::FooBarBar {
                                            _x: ::std::cell::Cell::new(0.0),
                                            _y: ::std::cell::RefCell::new("".into()),
                                            __variant: __data__::__variant_FooBarBar::__Nothing,
                                        }),
                                    ),
                                },
                            )),
                        },
                    )),
                })
                .clone(),
        ))));
        FooBar::__ctor(&__cto1.0);
        __cto1.__ctor(x, y);
        __cto1
    }
    fn __nd_name(&self) -> String {
        "FooBarBar".into()
    }
    fn __nd_x(&self) -> f64 {
        self._x()
    }
    fn __nd_base_example(&self) -> String {
        {
            let res = $crate::fmt::format(builtin #format_args(
                "from {}; {}",
                self._y(),
                FooBar::__nd_base_example(&self.0),
            ));
            res
        }
    }
    pub fn name(&self) -> String {
        self.__nd_name()
    }
    pub fn x(&self) -> f64 {
        self.__nd_x()
    }
    pub fn base_example(&self) -> String {
        self.__nd_base_example()
    }
    pub fn to<T: TryFrom<FooBarBar, Error = crate::SModelError>>(
        &self,
    ) -> Result<T, crate::SModelError> {
        T::try_from(self.clone())
    }
    pub fn is<T: TryFrom<FooBarBar, 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 {
                    __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::__Nothing,
                                },
                            )),
                        },
                    )),
                })
                .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, non_snake_case)]
mod __data__ {
    use super::*;
    pub enum __variant_Symbol {
        Foo(::std::rc::Rc<Foo>),
        __Nothing,
    }
    pub struct Symbol {
        pub __variant: __variant_Symbol,
    }
    pub enum __variant_Foo {
        FooBar(::std::rc::Rc<FooBar>),
        FooQux(::std::rc::Rc<FooQux>),
        __Nothing,
    }
    pub struct Foo {
        pub __variant: __variant_Foo,
    }
    pub enum __variant_FooBar {
        FooBarBar(::std::rc::Rc<FooBarBar>),
        __Nothing,
    }
    pub struct FooBar {
        pub __variant: __variant_FooBar,
    }
    pub enum __variant_FooBarBar {
        __Nothing,
    }
    pub struct FooBarBar {
        pub _x: ::std::cell::Cell<f64>,
        pub _y: ::std::cell::RefCell<String>,
        pub __variant: __variant_FooBarBar,
    }
    pub enum __variant_FooQux {
        __Nothing,
    }
    pub struct FooQux {
        pub __variant: __variant_FooQux,
    }
}

Clone this wiki locally