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

Simplify some error handling #105

Merged
merged 2 commits into from
Jun 27, 2024
Merged

Simplify some error handling #105

merged 2 commits into from
Jun 27, 2024

Conversation

faern
Copy link
Member

@faern faern commented Jun 27, 2024

This is in preparation of ditching error-chain. We had pretty tangled errors. Lots of our own errors are just stringly typed errors (ensure! kind of stuff).

This PR just massage away some errors.

  • Types that implemented TryCopyTo but actually had no code path leading to an error was changed into implementations of the infallible CopyTo
  • The internal compare_cstr_safe was changed to panic instead of returning an error. This because it only deals with parsing C strings returned by the system. Since the system should return C strings, it's something really bad going on with the system if they don't null-terminate their strings. I regard this as a case for panic rather than user error handling.

This change is Reviewable

@faern faern requested a review from dlon June 27, 2024 10:46
@faern faern force-pushed the simplify-some-errors branch from 986ea21 to 76df50a Compare June 27, 2024 11:28
@faern faern force-pushed the simplify-some-errors branch from 76df50a to 1e585d3 Compare June 27, 2024 11:48
Copy link
Member

@dlon dlon left a comment

Choose a reason for hiding this comment

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

:lgtm:

Reviewed 7 of 7 files at r1, all commit messages.
Reviewable status: :shipit: complete! all files reviewed, all discussions resolved


src/lib.rs line 162 at r1 (raw file):

    // SAFETY: We point to the same memory region as `c_chars`,
    // which is a valid slice, so it's guaranteed to be safe
    let c_chars_u8 = unsafe { slice::from_raw_parts(c_chars_ptr, c_chars.len()) };

I'm curious how you feel about simply casting this from *const [i8] to *const [u8]:

let c_chars_u8 = unsafe { &*(c_chars as *const [i8] as *const [u8]) };

I'm sure their bits will be identical.

Copy link
Member Author

@faern faern left a comment

Choose a reason for hiding this comment

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

Reviewable status: :shipit: complete! all files reviewed, all discussions resolved


src/lib.rs line 162 at r1 (raw file):

Previously, dlon (David Lönnhager) wrote…

I'm curious how you feel about simply casting this from *const [i8] to *const [u8]:

let c_chars_ut = unsafe { &*(c_chars as *const [i8] as *const [u8]) };

I'm sure their bits will be identical.

As discussed on slack: That is not sound. because the memory layout of &[T] can't be assumed to be the same as &[U]. https://users.rust-lang.org/t/how-to-convert-i8-to-u8/16308/11

@faern faern merged commit 4d9c96d into main Jun 27, 2024
10 checks passed
@faern faern deleted the simplify-some-errors branch June 27, 2024 13:04
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.

2 participants