15 releases (4 breaking)

new 0.11.0 Apr 27, 2024
0.9.1 Apr 17, 2024
0.7.10 Mar 24, 2024
0.7.8 Apr 11, 2020
0.7.5 Jun 21, 2018

#3 in #polymorphism

Download history 11/week @ 2024-02-26 56/week @ 2024-03-18 98/week @ 2024-03-25 148/week @ 2024-04-01 179/week @ 2024-04-08 517/week @ 2024-04-15

997 downloads per month
Used in eventor

MIT/Apache

25KB
200 lines

elicit-rs

License

Licensed under either of

at your option.

Contribution

Unless you explicitly state otherwise, any contribution intentionally submitted for inclusion in the work by you, as defined in the Apache-2.0 license, shall be dual licensed as above, without any additional terms or conditions.


lib.rs:

lib.rs

Examples

Elicit

pub(crate) mod mine {
    use elicit::{elicit_define, Elicit};

    #[elicit_define(mine_elicit)]
    pub(crate) trait Mine {
        fn action(&self) -> i32;
        fn action_mut(&mut self) -> i32;
    }

    // pub(crate) mine_elicit::author as elicit_author;
    pub(crate) use mine_elicit::user as elicit_user;

    #[derive(Debug, Default, Clone, Elicit)]
    #[elicit_mod_author(mine_elicit::author)]
    pub(crate) struct MineX {}

    impl Mine for MineX {
        fn action(&self) -> i32 {
            0i32
        }
        fn action_mut(&mut self) -> i32 {
            0i32
        }
    }

    #[derive(Debug, Clone, Elicit)]
    #[elicit_mod_author(mine_elicit::author)]
    // #[elicit_from_self_field(_fsf)] // here
    pub(crate) struct MineY {
        #[elicit_from_self_field] // or here
        _fsf: mine_elicit::author::ElicitFromSelfField,
        i: i32,
    }

    impl MineY {
        pub(crate) fn new(a: i32) -> Self {
            MineY {
                _fsf: Default::default(),
                i: a,
            }
        }
        ///
        ///
        /// fn evil
        ///
        /// It is not possible to suppress calls to _weak_assign within
        /// the same module.
        ///
        #[allow(box_pointers, dead_code)]
        pub(crate) fn evil(&mut self) -> elicit::Result<()> {
            use mine_elicit::author::*;
            use std::{cell::RefCell, rc::Rc};
            self._weak_assign(Rc::<RefCell<Box<dyn ElicitBase>>>::downgrade(
                &Rc::new(RefCell::new(Box::<MineX>::default())),
            ))
        }
    }

    impl Mine for MineY {
        fn action(&self) -> i32 {
            self.i
        }
        fn action_mut(&mut self) -> i32 {
            self.i += 1;
            self.i
        }
    }
}

pub(crate) fn fire() -> elicit::Result<()> {
    use mine::elicit_user::Elicit as MineElicit;
    use mine::{MineX, MineY};

    let mut e: MineElicit;

    e = MineElicit::new(MineX::default())?;

    e.try_with(|m| -> elicit::Result<()> {
        println!("{:?}", m);
        assert!(m.action() == 0);
        Ok(())
    })?;

    let y = MineY::new(1);

    // eprintln!("{:?}", y.evil());

    e = MineElicit::new(y)?;

    e.try_with_mut(|m| -> elicit::Result<()> {
        println!("{:?}", m);
        assert!(m.action_mut() == 2);
        Ok(())
    })?;

    Ok(())
}

fire().expect("Doc-tests");

Dependencies

~0.3–5.5MB
~21K SLoC