Git Product home page Git Product logo

Comments (5)

evnu avatar evnu commented on June 2, 2024 1

Is there any particular reason, why Term::get_type() is implemented using the various is_* methods

enif_term_type was introduced with OTP 22.0, so it was not around when get_type() was implemented.

from rustler.

filmor avatar filmor commented on June 2, 2024
  • You should not use unwrap in "production" code. It will not crash your code but only because Rustler explicitly handles panics
  • Instead, you can use if let or match (or unwrap_or, there are plenty of options) to first try to decode as integer and, if that fails, decode as float instead
  • @ all: Is there any particular reason, why Term::get_type() is implemented using the various is_* methods (which map to enif_is_*)? Erlang's built in enif_term_type can actually distinguish floats and integers, changing the behaviour of get_type in this way now would be a breaking change, though.

from rustler.

juanazam avatar juanazam commented on June 2, 2024

@filmor Thanks so much for your reply! will implement this update!

from rustler.

filmor avatar filmor commented on June 2, 2024

Then I'll add it. @rusterlium/core do you think it's fine to break API here by adjusting the enum or should I add a new function?

from rustler.

evnu avatar evnu commented on June 2, 2024

https://github.com/orgs/rusterlium/teams/core do you think it's fine to break API here by adjusting the enum or should I add a new function?

I'd tend to deprecate this function and schedule for removal in 1.0, and add a new function instead.

Maybe impl From<Term> for TermType would be the way to go here. That allows us to keep the deprecated function around and avoid thinking of a new name. Alternatively, Term::as_type should fit as well, as this is a cheap function.

I imagine the possibility of doing something like:

rustler_codegen can help you here by deriving that decoder above automatically. NifUntaggedEnum implements Decode and Encode for an enum. Example:

#[derive(NifUntaggedEnum)]
enum MyEnum {
  Float(f64),
  Integer(i64)
}

#[rustler::nif]
fn my_nif(my_enum: MyEnum) -> MyEnum {
  my_enum // this is a variant of MyEnum, which was already decoded and which will be encoded after it was returned
}

(Disclaimer: I did not compile the code above :))

from rustler.

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.