Giter Site home page Giter Site logo

Comments (7)

Hodkinson avatar Hodkinson commented on June 2, 2024 1

My bad - I had forgotten that I had defined the assert_eq call myself, I had thought this was within rhai. I patched the assert_eq call to handle this extra type and now it works as expected. Thank you for the quick response. 🙏🏻

from rhai.

schungx avatar schungx commented on June 2, 2024

Well, you know that but Rhai doesn't. Unless you define the == operator for that enum type, Rhai can only assume that one opaque object is not the same as another...

So the solution is to define the comparison operators on your type.

from rhai.

Hodkinson avatar Hodkinson commented on June 2, 2024

I did that: here's the full macro I used:

macro_rules! create_enum_module {
    ($module:ident : $typ:ty => $($variant:ident),+) => {
        #[export_module]
        pub mod $module {
            $(
                #[allow(non_upper_case_globals)]
                pub const $variant: $typ = <$typ>::$variant;
            )*
            
            #[rhai_fn(global, name = "to_string", name = "to_debug", pure)]
            pub fn to_string(my_enum: &mut $typ) -> String {
                format!("{my_enum:?}")
            }
            
            #[rhai_fn(global, name = "==", pure)]
            pub fn eq(my_enum: &mut $typ, my_enum2: $typ) -> bool {
                my_enum == &my_enum2
            }
            
            #[rhai_fn(global, name = "!=", pure)]
            pub fn neq(my_enum: &mut $typ, my_enum2: $typ) -> bool {
                my_enum != &my_enum2
            }
        }
    };
}

from rhai.

Hodkinson avatar Hodkinson commented on June 2, 2024

I also tried defining the comparison operators directly on the enum type itself but I had the same error. Although the == comparison works when they are defined, using within assert_eq fails.

from rhai.

schungx avatar schungx commented on June 2, 2024

May I ask whether you derived PartialEq for your type?

EDIT: Probably yes since you can use == in Rust ..

from rhai.

schungx avatar schungx commented on June 2, 2024

Can you post your exact aasert_eq call?

It seems like you are comparing with the results of a script call.

from rhai.

Hodkinson avatar Hodkinson commented on June 2, 2024

I tried various ways of calling it, always the same result:

assert_eq!(MyEnum::Foo,MyEnum::Foo);
assert_eq!(MyEnum::Foo,m.method_that_returns_enum_foo());

Edit: I might have found the issue, am checking now

from rhai.

Related Issues (20)

Recommend Projects

  • React photo React

    A declarative, efficient, and flexible JavaScript library for building user interfaces.

  • Vue.js photo Vue.js

    🖖 Vue.js is a progressive, incrementally-adoptable JavaScript framework for building UI on the web.

  • Typescript photo Typescript

    TypeScript is a superset of JavaScript that compiles to clean JavaScript output.

  • TensorFlow photo TensorFlow

    An Open Source Machine Learning Framework for Everyone

  • Django photo Django

    The Web framework for perfectionists with deadlines.

  • D3 photo D3

    Bring data to life with SVG, Canvas and HTML. 📊📈🎉

Recommend Topics

  • javascript

    JavaScript (JS) is a lightweight interpreted programming language with first-class functions.

  • web

    Some thing interesting about web. New door for the world.

  • server

    A server is a program made to process requests and deliver data to clients.

  • Machine learning

    Machine learning is a way of modeling and interpreting data that allows a piece of software to respond intelligently.

  • Game

    Some thing interesting about game, make everyone happy.

Recommend Org

  • Facebook photo Facebook

    We are working to build community through open source technology. NB: members must have two-factor auth.

  • Microsoft photo Microsoft

    Open source projects and samples from Microsoft.

  • Google photo Google

    Google ❤️ Open Source for everyone.

  • D3 photo D3

    Data-Driven Documents codes.