-
Notifications
You must be signed in to change notification settings - Fork 41
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
Remove TryFrom/Into conversion of HashMap, and implement into_hashmap() and from_hashmap() #254
Remove TryFrom/Into conversion of HashMap, and implement into_hashmap() and from_hashmap() #254
Conversation
Do you need some help with this? |
@andy-thomason
|
I think this is ready for review now. I removed many integrated tests related to |
There was a problem hiding this comment.
Choose a reason for hiding this comment
The reason will be displayed to describe this comment to others. Learn more.
Looks good to me in principle. I have a few minor comments.
/// assert_eq!(names, vec!["a", "b"]); | ||
/// } | ||
/// ``` | ||
pub fn from_hashmap<K>(val: HashMap<K, Robj>) -> Result<Self> |
There was a problem hiding this comment.
Choose a reason for hiding this comment
The reason will be displayed to describe this comment to others. Learn more.
Should this take a reference? Otherwise, maybe mention in line 66 that the HashMap is consumed?
There was a problem hiding this comment.
Choose a reason for hiding this comment
The reason will be displayed to describe this comment to others. Learn more.
Maybe I am missing something, but why not make it a TryFrom
implementation?
There was a problem hiding this comment.
Choose a reason for hiding this comment
The reason will be displayed to describe this comment to others. Learn more.
@clauswilke
I think the result List
should own Robj
s in the HashMap
, so HashMap
should be consumed.
@Ilia-Kosenkov
Because the conversion is lossy. Please refer to #230 (comment) for the related discussions.
There was a problem hiding this comment.
Choose a reason for hiding this comment
The reason will be displayed to describe this comment to others. Learn more.
@yutannihilation That's fine. Then I'd say so explicitly in the docstring.
There was a problem hiding this comment.
Choose a reason for hiding this comment
The reason will be displayed to describe this comment to others. Learn more.
OK I see. If we do not have it (or have it planned), it may be useful to have trait for lossy conversions instead of multiplying from_name
and to_name
functions.
extendr-api/src/wrapper/list.rs
Outdated
|
||
/// Convert a List into a HashMap, consuming the list. | ||
/// | ||
/// - If there are some duplicated name of elements, only one of those will be preserved. |
There was a problem hiding this comment.
Choose a reason for hiding this comment
The reason will be displayed to describe this comment to others. Learn more.
"names"
extendr-api/src/wrapper/list.rs
Outdated
/// Convert a List into a HashMap, consuming the list. | ||
/// | ||
/// - If there are some duplicated name of elements, only one of those will be preserved. | ||
/// - If an element doesn't have the name, an empty string (i.e. `""`) will be the key. |
There was a problem hiding this comment.
Choose a reason for hiding this comment
The reason will be displayed to describe this comment to others. Learn more.
"doesn't have a name, ... will be used as the key"
Also mention what happens when there are multiple elements without name?
expect_error(list_str_hash(20:30), "expected a list") | ||
expect_error(list_str_hash(NA), "expected a list") | ||
expect_error(list_str_hash(e), "expected a list") | ||
# TODO |
There was a problem hiding this comment.
Choose a reason for hiding this comment
The reason will be displayed to describe this comment to others. Learn more.
Can we add some tests here before merging, or is there a reason to defer?
There was a problem hiding this comment.
Choose a reason for hiding this comment
The reason will be displayed to describe this comment to others. Learn more.
This TODO
doesn't mean we need to add some tests for list. As I commented above, I intentionally removed integrated tests because this is no longer what happens inside the [#extendr]
macro, so I think unit tests are enough.
If this comment is confusing, I'll remove this.
expect_error(try_list_str_hash(20:30), "Expected List got Integer") | ||
expect_error(try_list_str_hash(NA), "Expected List got Logical") | ||
expect_error(try_list_str_hash(e), "Expected List got Environment") | ||
# TODO |
There was a problem hiding this comment.
Choose a reason for hiding this comment
The reason will be displayed to describe this comment to others. Learn more.
See above.
CHANGELOG.md
Outdated
- Remove `TryFrom` conversions between `Robj` and `HashMap` for consistency. Use explicit conversions by `List::into_hashmap()` and `List::from_hashmap()` instead. | ||
|
There was a problem hiding this comment.
Choose a reason for hiding this comment
The reason will be displayed to describe this comment to others. Learn more.
I think changelog should be in past simple. I suggest to slightly rephrase it to
"Removed TryFrom
conversions between Robj
and HashMap
for consistency. List::into_hashmap()
and List::from_hashmap()
should be used instead."
Thanks, I addressed the comments. |
@andy-thomason |
Close #230
<List>::from_hashmap()
<List>::into_hashmap()
<HashMap<>>::from_list()
<HashMap<>>::into_list()