-
Notifications
You must be signed in to change notification settings - Fork 9
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
Conversation
31b1566
to
8766ccc
Compare
- 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
8766ccc
to
79bcbe2
Compare
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) { |
There was a problem hiding this comment.
Choose a reason for hiding this comment
The reason will be displayed to describe this comment to others. Learn more.
What is rustfmt
, amirite?
There was a problem hiding this comment.
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); |
There was a problem hiding this comment.
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.
There was a problem hiding this comment.
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. |
There was a problem hiding this comment.
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 🎵
There was a problem hiding this comment.
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, |
There was a problem hiding this comment.
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?
There was a problem hiding this comment.
Choose a reason for hiding this comment
The reason will be displayed to describe this comment to others. Learn more.
Is it ok now?
Released in |
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 ofbools
being larger than one byte. In practice though, this doesn't happen. So right now I made the actual implementation generic over any sizebool
, 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.