Skip to content
New issue

Have a question about this project? Sign up for a free GitHub account to open an issue and contact its maintainers and the community.

By clicking “Sign up for GitHub”, you agree to our terms of service and privacy statement. We’ll occasionally send you account related emails.

Already on GitHub? Sign in to your account

rustler: add a Rust type ErlOption<T> #507

Merged
merged 2 commits into from
May 25, 2023

Conversation

tatsuya6502
Copy link
Contributor

This PR fixes #500 by adding a Rust typeErlOption<T> to the Rustler crate.

ErlOption<T> is a wrapper around Option<T>. It implements Rustler's Encoder and Decoder, and serializes None into undefined atom instead of nil. The undefined atom is commonly used in Erlang libraries to express the absence of a value.

For convenience, ErlOption<T> implements some traits from std, so that it will be easy to get a reference of enclosing Option<T> or convert from/to Option<T>. Here are the examples from its doc:

use rustler::ErlOption;

// Create new `ErlOption<i32>` values via convenient functions.
let _ = ErlOption::some(1); // Wraps `Some(1)`.
let _ = ErlOption::<i32>::none();

// Convert Option<i32> values to ErlOption<i32> values.
let _ = ErlOption::from(Some(2));
let _: ErlOption<_> = Some(3).into();
let _: ErlOption<i32> = None.into();

// Get a reference of enclosing Option<T> from an ErlOption<T>.
let _: &Option<i32> = ErlOption::some(4).as_ref();

// Get a mutable reference of enclosing Option<T> from an ErlOption<T>.
let _: &mut Option<i32> = ErlOption::some(5).as_mut();

// Convert an ErlOption<i32> value to an Option<i32> value.
let _: Option<i32> = ErlOption::some(6).into();

// Compare ErlOption<T> with Option<T>.
assert_eq!(ErlOption::some(7), Some(7));
assert!(ErlOption::some(8) > Some(7));

// Call Option<T>'s methods on an ErlOption<T> via Deref and DerefMut.
assert!(ErlOption::some(9).is_some());
assert_eq!(ErlOption::some(10).unwrap(), 10);
assert_eq!(ErlOption::some(12).map(|v| v + 1), ErlOption::some(13));

@evnu evnu requested a review from a team December 11, 2022 20:58
@filmor
Copy link
Member

filmor commented Dec 12, 2022

Maybe, instead of incorporating many small adjustments to the current error-handling, we could try to come up with a system that allows for all of these in one go, e.g. by extending the nif macro. I don't have a proposal for that, yet. @hansihe Do you remember what you had in mind for #2? :)

@tatsuya6502
Copy link
Contributor Author

ping @filmor @hansihe

@tatsuya6502
Copy link
Contributor Author

@filmor @hansihe — Can you please decide and tell me what I should do with this PR? Thanks!

@tatsuya6502
Copy link
Contributor Author

ping @filmor @hansihe

`ErlOption<T>` is a wrapper around `Option<T>` and it serializes `None` into
`undefined` atom. `undefined` is commonly used in Erlang libraries to express
the absence of a value.
@filmor filmor merged commit d8aa66d into rusterlium:master May 25, 2023
@tatsuya6502 tatsuya6502 deleted the erl-option branch May 25, 2023 09:12
Sign up for free to join this conversation on GitHub. Already have an account? Sign in to comment
Labels
None yet
Projects
None yet
Development

Successfully merging this pull request may close these issues.

An optional crate feature to switch from Elixir nil to Erlang undefined?
2 participants