Macro class

Source
class!() { /* proc-macro */ }
Expand description

need to wrap struct and implements.

using #[keep] for method make that method keep in the original impl.

methods without #[keep] will be put into trait __XXX__.

and macro will auto generate a new function which return Pin<Box>.

expression in the method will be converted.

instead of use self, using this.

self will be convert to use unsafe { self.__real__.as_ref().unwrap() }.

self_mut will be convert to use unsafe { self.__real__.as_mut().unwrap() }.

_super will be convert to use self.__prototype__.

_super_mut will be convert to use unsafe { self.__prototype__.as_mut().get_unchecked_mut() }.

An example to use this macro:

class! {
    struct Example {
        data: String    
    }
    impl Example {
        #[keep]
        fn with() -> Pin<Box<Self>> where Self: Sized {
            Self::new("example".to_string())
        }
         
        fn set_data(&mut self, data: String) {
            this.data = data;
        }
 
        fn get_data(&self) -> String {
            this.data.clone()
        }
    }
    impl Something for Example {
        //do something
    }
}
class! {
    extends Example;
    struct Sub { }
    impl Sub { }
}

the struct will become:

struct Example {
    __real__: *mut dyn __Example__,
    _pinned: ::std::marker::PhantomPinned,
    data: String
}
struct Sub {
    __prototype__: ::std::pin::Pin<Box<Example>>
    __real__: *mut dyn __Sub__,
    _pinned: ::std::marker::PhantomPinned,
}