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 byte conversions #72

Draft
wants to merge 2 commits into
base: main
Choose a base branch
from
Draft

Add byte conversions #72

wants to merge 2 commits into from

Conversation

hecatia-elegua
Copy link
Owner

@hecatia-elegua hecatia-elegua commented Aug 9, 2023

Closes #33.
this is for testing big endian:

cargo miri run --target mips64-unknown-linux-gnuabi64 --example readme
cargo miri run --target mips64-unknown-linux-gnuabi64 --example nested_tuples_and_arrays

&self or self?
can this rotate_left-then-slice-try_into setup be easier?

also this will probably not stay in bitsize_internal, I still don't believe everyone needs this...
also useful as a trait, I mean it's basically adapted from here / here.

  • use a trait

@pickx
Copy link
Collaborator

pickx commented Aug 11, 2023

this seems to work from the very basic tests I did

I asked around and if the bounds check of try_into() don't get removed (which they should if the compiler can prove it), we can unwrap_unchecked() to force it out.

in the target_endian = "big" case, can we just return full[size_difference..].try_into().unwrap() and avoid the rotate?

&self or self?

if we go by the API guidelines it should take &self (for non-Copy types, into_ is used for consuming conversions, to_ for non-consuming).

why does this not work for non-repr enums?

this could be a good place to use enum.value() so that something like Subclass::from(42).to_ne_bytes() would return the expected value.

@hecatia-elegua
Copy link
Owner Author

can we just return full[size_difference..]

👍

&self (for non-Copy types

right, it was about Copy

why does this not work for non-repr enums?

rust doesn't know that the enum is u32, so it will not allow transmuting unless you define the repr

Subclass::from(42).to_ne_bytes() would return the expected value.

it returns [3, 0, 0, 0] on lil, [0, 0, 0, 3] on big endian right now, which is correct, right?

#[cfg(target_endian = "little")]
{
// [1 1 1 1 1] 0 0 0
return full[..MIN_BYTES].try_into().unwrap();
Copy link
Collaborator

Choose a reason for hiding this comment

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

is this just transmute_copy(self)? because it's already in little endian?

Copy link
Owner Author

Choose a reason for hiding this comment

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

that's true, that's also how the impl started.
though it's a bit more explicit like this, but I might change it back when doing the other methods

@pickx
Copy link
Collaborator

pickx commented Aug 11, 2023

rust doesn't know that the enum is u32, so it will not allow transmuting unless you define the repr

ah, I see.

Subclass::from(42).to_ne_bytes() would return the expected value.

it returns [3, 0, 0, 0] on lil, [0, 0, 0, 3] on big endian right now, which is correct, right?

I would expect [42, 0, 0, 0] / [0, 0, 0, 42] here

@hecatia-elegua
Copy link
Owner Author

hecatia-elegua commented Aug 11, 2023

I would expect [42, 0, 0, 0] / [0, 0, 0, 42] here

that's Subclass2 :)
oh wait you mean Subclass2 doesn't work? you're right, meaning we would need to handle enums differently

@pickx
Copy link
Collaborator

pickx commented Aug 11, 2023

I would expect [42, 0, 0, 0] / [0, 0, 0, 42] here

that's Subclass2 :) oh wait you mean Subclass2 doesn't work? you're right, meaning we would need to handle enums differently

oops, yeah. I meant Subclass2 and it does behave like Subclass (discarding the value of the fallback), unexpectedly

@hecatia-elegua
Copy link
Owner Author

@pickx any non c-like enum or struct containing a non c-like enum can't be transmuted since it contains discriminant and value...
my only idea rn is to add your .value() idea to all items and transmute what it gives back, which would make readonly fields unsafe when we support those.. or you just can't convert stuff with readonly fields, uuhhh

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.

pack/unpack a bilge struct to/from &[u8]
2 participants