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

Adding toBytes and fromBytes to Nat32 #269

Open
wants to merge 3 commits into
base: master
Choose a base branch
from

Conversation

skilesare
Copy link

I'm not sure how to go about testing, but I tried to add tests as well. This same pattern can be followed for the hard edged NatXz. The Nat to bytes will need a bit more finesse than I have at the moment.

@nomeata
Copy link
Contributor

nomeata commented Jun 17, 2021

Thanks!

We probably want to indicate the endianness in these functions, i.e. Nat32.toBytesLE and Nat32.toBytesBE or something like that.

Also, the fromBytes function should probably do some bound checking, and return an ?Nat32 to indicate failure?

Comment on lines +149 to +152
[ Prim.natToNat8(Prim.nat32ToNat((x >> 24) & (255))),
Prim.natToNat8(Prim.nat32ToNat((x >> 16) & (255))),
Prim.natToNat8(Prim.nat32ToNat((x >> 8) & (255))),
Prim.natToNat8(Prim.nat32ToNat((x & 255))) ];
Copy link
Contributor

Choose a reason for hiding this comment

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

Nit:

Suggested change
[ Prim.natToNat8(Prim.nat32ToNat((x >> 24) & (255))),
Prim.natToNat8(Prim.nat32ToNat((x >> 16) & (255))),
Prim.natToNat8(Prim.nat32ToNat((x >> 8) & (255))),
Prim.natToNat8(Prim.nat32ToNat((x & 255))) ];
[ Prim.natToNat8(Prim.nat32ToNat(x >> 24 & 255)),
Prim.natToNat8(Prim.nat32ToNat(x >> 16 & 255)),
Prim.natToNat8(Prim.nat32ToNat(x >> 8 & 255)),
Prim.natToNat8(Prim.nat32ToNat(x & 255)) ];

Comment on lines +157 to +160
(Prim.natToNat32(Prim.nat8ToNat(x[0])) << 24) +
(Prim.natToNat32(Prim.nat8ToNat(x[1])) << 16) +
(Prim.natToNat32(Prim.nat8ToNat(x[2])) << 8) +
(Prim.natToNat32(Prim.nat8ToNat(x[3])));
Copy link
Contributor

Choose a reason for hiding this comment

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

Suggested change
(Prim.natToNat32(Prim.nat8ToNat(x[0])) << 24) +
(Prim.natToNat32(Prim.nat8ToNat(x[1])) << 16) +
(Prim.natToNat32(Prim.nat8ToNat(x[2])) << 8) +
(Prim.natToNat32(Prim.nat8ToNat(x[3])));
Prim.natToNat32(Prim.nat8ToNat(x[0])) << 24 +
Prim.natToNat32(Prim.nat8ToNat(x[1])) << 16 +
Prim.natToNat32(Prim.nat8ToNat(x[2])) << 8 +
Prim.natToNat32(Prim.nat8ToNat(x[3]));

@@ -2,6 +2,7 @@
///
/// Most operations are available as built-in operators (e.g. `1 + 1`).
import Nat "Nat";
import Nat8 "Nat8";
Copy link
Contributor

Choose a reason for hiding this comment

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

Is this module actually used?

@Brycewetzel
Copy link

still ok?

@ggreif ggreif force-pushed the master branch 2 times, most recently from d52aecd to 08507fc Compare October 21, 2022 12:22
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.

4 participants