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

Add safe r_length() function #23

Closed
wants to merge 2 commits into from
Closed

Conversation

romainfrancois
Copy link
Contributor

Follow up from posit-dev/positron#354 (comment)

I think usize is the right return although it causes some casting to be needed here and there

@DavisVaughan
Copy link
Contributor

I have a slight preference for using a signed integer as the return value because in C code I somewhat frequently initialize a size value to -1 to indicate "this will eventually be filled in with something else depending on the code path taken below". @lionel- any thoughts?

@romainfrancois
Copy link
Contributor Author

romainfrancois commented Jun 9, 2023

That makes sense. I'll revert to using isize and probably can keep it labelled as R_xlen_t anyway

#[doc = "R_xlen_t is defined as int on 32-bit platforms, and\n that confuses Rust. Keeping it always as ptrdiff_t works\n fine even on 32-bit.\n <div rustbindgen replaces=\"R_xlen_t\"></div>"]
pub type R_xlen_t = isize;

Copy link
Contributor

@lionel- lionel- left a comment

Choose a reason for hiding this comment

The reason will be displayed to describe this comment to others. Learn more.

In general I have a strong preference for always using signed sizes to avoid coercions with mixed type arithmetic (less of an issue in Rust?) and the surprising behaviour when substracting two unsigned types.

See discussion in https://stackoverflow.com/a/34642843/1725177 and Bjarne Stroutrup's advice in https://www.youtube.com/watch?v=Puio5dly9N8:

  • Never make signed an unsigned.
  • You don't need the extra bit.
  • It doesn't matter that something can't be negative.

@@ -19,6 +19,7 @@ use harp::utils::r_is_data_frame;
use harp::utils::r_is_matrix;
use harp::utils::r_is_simple_vector;
use harp::utils::r_typeof;
use harp::utils::r_xlength;
Copy link
Contributor

@lionel- lionel- Jun 9, 2023

Choose a reason for hiding this comment

The reason will be displayed to describe this comment to others. Learn more.

I think this could be called r_usize()? (or r_ssize() if signed, as in the rlang C lib - edit: oh I see from your comment that this is r_isize() in rust)

Copy link
Contributor

Choose a reason for hiding this comment

The reason will be displayed to describe this comment to others. Learn more.

actually it's r_length() in rlang, sorry for the confusion

r_ssize r_length(r_obj* x);

Copy link
Contributor Author

Choose a reason for hiding this comment

The reason will be displayed to describe this comment to others. Learn more.

We might want to keep _size() for size of object in bytes: posit-dev/positron#478

@romainfrancois romainfrancois changed the title Add safe r_length() function that returns usize Add safe r_length() function Jun 9, 2023
@romainfrancois
Copy link
Contributor Author

@DavisVaughan should we close this one, since it overlaps with #26 ? This PR was mostly a follow up to this comment posit-dev/positron#354 (comment)

@DavisVaughan
Copy link
Contributor

Yea I think if it is ok with you, I'll probably end up adding it with or as a follow up to #26

@lionel- lionel- deleted the feature/harp/r_xlength branch June 29, 2023 13:47
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.

3 participants