Comments (8)
This id certainly an interesting idea. It would be incredibly neat if you where able to use pattern matching on terms.
The main problem I can think of is that this would require us to recursively check the type of every term that enters nif-land. Because there is no function that returns the absolute type of a term, we would need to use the enif_is_*
binary functions to determine the type of all terms. I don't know how expensive this would be, but it would at least require some thought.
from rustler.
I may have misunderstood a bit?
from rustler.
You could always submit a PR to the OTP Erlang project to add such a callback to get the type of a term as an enumeration. But yes, right now you'd need to use enif_is_*
as each branch is tested for as far as I recall.
from rustler.
we would need to use the
enif_is_*
binary functions to determine the type of all terms. I don't know how expensive this would be, but it would at least require some thought.
@hansihe yes this is exactly what I had in mind. I also share the cost concern associated with recursively testing each term.
The main thing is that I find that you will need to do this sort of thing in user code anyways. Here is an incomplete example for turning erlang terms to JSON: https://github.com/scrogson/fast_json/blob/master/native/fast_json/src/lib.rs.in#L88-L102
Obviously not the most ergonomic way to deal with this sort of thing.
Perhaps it could be apart of the NifEncoder
and NifDecoder
traits so that the conversion happens in the userland code?
match term.decode(env) {
Atom { ... } => ...,
...
}
You could always submit a PR to the OTP Erlang project to add such a callback to get the type of a term as an enumeration.
@OvermindDL1 can you expand on this?
from rustler.
@scrogson your use case is something I hadn't considered. I have mainly been using it for cases where you already knew the structure of the type you wanted to ship in and out of erlang-land.
An alternative to making all types dynamic would be to have a DynamicTerm
enum that a NifTerm
could decode into. The decoder would then do all of the work, and it would make things a lot less awkward for your code. At the same time there would be no overhead for normal usage, only for when you need the functionality. What do you think? I can have a go at implementing it if you are willing to try it out?
Edit: Missed your code example. That is pretty much exactly how what I wrote would look like.
from rustler.
@hansihe sounds good! Let me know when it's ready and I'll give it a shot.
from rustler.
Closed by #39
from rustler.
@OvermindDL1 can you expand on this?
The erlang compiler is open-source, you could submit a PR to add a function like nif_get_type
or so to the interface. :-)
from rustler.
Related Issues (20)
- NIF code trying to using MongoDB sync client hangs while trying to connect to DB HOT 2
- Check if Term is float HOT 5
- NifMap expansion error with rustler v0.27.0 HOT 2
- Update changelog for 0.27 HOT 1
- Store rustler template outside of priv/? HOT 2
- how to custom build target arch?
- can you support cargo-zigbuild? HOT 2
- `nif` macro fails with macro arguments HOT 2
- New Binary from Vec or Slice
- Support for enif_monitor_process HOT 1
- resource with lifetime
- Expose `enif_is_process_alive` HOT 4
- rustler errors on OTP26 HOT 1
- How to conveniently work with binaries as vectors of bytes?
- Why does Term::map_from_pairs return a NifResult?
- Support for fully custom `:load_from` for escripts HOT 8
- This OTP release uses the unsupported Erlang NIF version "2.17" HOT 1
- Test NifStruct derived traits
- Running `dialiyzer` with `rustler` 0.30.0 results in "The pattern can never match the type"" HOT 4
- Import structs from a library
Recommend Projects
-
React
A declarative, efficient, and flexible JavaScript library for building user interfaces.
-
Vue.js
🖖 Vue.js is a progressive, incrementally-adoptable JavaScript framework for building UI on the web.
-
Typescript
TypeScript is a superset of JavaScript that compiles to clean JavaScript output.
-
TensorFlow
An Open Source Machine Learning Framework for Everyone
-
Django
The Web framework for perfectionists with deadlines.
-
Laravel
A PHP framework for web artisans
-
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.
-
Visualization
Some thing interesting about visualization, use data art
-
Game
Some thing interesting about game, make everyone happy.
Recommend Org
-
Facebook
We are working to build community through open source technology. NB: members must have two-factor auth.
-
Microsoft
Open source projects and samples from Microsoft.
-
Google
Google ❤️ Open Source for everyone.
-
Alibaba
Alibaba Open Source for everyone
-
D3
Data-Driven Documents codes.
-
Tencent
China tencent open source team.
from rustler.