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 boolean transmutation module #11

Merged
merged 2 commits into from
Dec 15, 2017
Merged

Conversation

Enet4
Copy link
Collaborator

@Enet4 Enet4 commented Dec 13, 2017

This PR is actually a bit tricky, and contains things that maybe I could reconsider before merging. Currently Rust does not specify the size of bool, which in turn leads to the theoretical possibility of bools being larger than one byte. In practice though, this doesn't happen. So right now I made the actual implementation generic over any size bool, but made the tests assume that they're 1-byte sized. ¯\_(ツ)_/¯

This also adds a new InvalidValues error reason. It is not exactly for size guard errors, but making heavier modifications to the error type requires a breaking change, which I have plans to do later in order to deal with #9.

- add error reason for invalid values (should be refactored in next major release)
- add guarded_transmute_bool_* variants which check for valid boolean values
- add respective tests
src/bool.rs Outdated
#[inline]
fn bytes_are_bool(v: &[u8]) -> bool {
let sizeof_bool: usize = align_of::<bool>();
for c in v.chunks(sizeof_bool).filter(|c| c.len() == sizeof_bool) {
Copy link
Owner

Choose a reason for hiding this comment

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

What is rustfmt, amirite?

Copy link
Collaborator Author

Choose a reason for hiding this comment

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

Ain't nobody got time fo' that. $ rustfmt src/bool.rs

src/bool.rs Outdated
fn bytes_are_bool(v: &[u8]) -> bool {
let sizeof_bool: usize = align_of::<bool>();
for c in v.chunks(sizeof_bool).filter(|c| c.len() == sizeof_bool) {
let (l, r) = c.split_at(sizeof_bool - 1);
Copy link
Owner

Choose a reason for hiding this comment

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

The entirety of these functions is the same, apart from picking {L,M}SB. I feel like this would work as well, and better express what's happening:

let (msbs, lsb) = if cfg!(target_endian = "little") {
    (c[1..], c)
} else {
    c.split_at(sizeof_bool - 1)
};
if lsb[0] > 1 || msbs.iter().any(|x| *x != 0) {

Also, you have an unhealthy obsession with naming verbols for that they are rather than for what they represent (l, r) mean naught, (l, m) or (lsb, msb) or sth would be 👍er.

Copy link
Collaborator Author

Choose a reason for hiding this comment

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

Ok, I took care of that. Glad the compiler's smart enough to optimize the if-else statement.

That last bit's subjective, but fine. I improved the variable naming as well.

src/lib.rs Outdated
//!
//! Those functions are exactly as safe as the data passed to them - creating a null pointer,
//! The base functions in this crate are not inherently safe, but just guarded against common simple mistakes
//! (like trying to create an 8-byte type from 7 bytes.
Copy link
Owner

Choose a reason for hiding this comment

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

🎵 You missed a paren and you liked it 🎵

Copy link
Collaborator Author

Choose a reason for hiding this comment

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

🎵 I've got a lovely bunch of parentheses. 🎵

🎵 There they are, in a new commit. 🎵

src/lib.rs Outdated
@@ -76,7 +79,8 @@ pub use self::error::{ErrorReason, Error};
pub use self::pod::{PodTransmutable, guarded_transmute_pod_many_permissive, guarded_transmute_pod_vec_permissive, guarded_transmute_pod_many_pedantic,
guarded_transmute_pod_pedantic, guarded_transmute_pod_vec_pedantic, guarded_transmute_pod_many, guarded_transmute_pod,
guarded_transmute_pod_vec};

pub use self::bool::{guarded_transmute_bool_permissive, guarded_transmute_bool_pedantic,
Copy link
Owner

Choose a reason for hiding this comment

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

Are these sorted from longest to shortest?

Copy link
Collaborator Author

Choose a reason for hiding this comment

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

Is it ok now?

- refactor bytes_are_bool into a single fn
- rustfmt
- fix docs
- rearrange re-exports
@nabijaczleweli nabijaczleweli merged commit 39f68d9 into master Dec 15, 2017
@nabijaczleweli nabijaczleweli deleted the transmute_bool branch December 15, 2017 15:30
@nabijaczleweli
Copy link
Owner

Released in v0.6.0.

Sign up for free to join this conversation on GitHub. Already have an account? Sign in to comment
Projects
None yet
Development

Successfully merging this pull request may close these issues.

2 participants