Skip to content

Commit ee7c676

Browse files
authored
Merge pull request #1392 from joshlf/joshlf-patch-1
Specify bit validity and padding of some types
2 parents cef4aaf + 13b5af8 commit ee7c676

File tree

4 files changed

+27
-0
lines changed

4 files changed

+27
-0
lines changed

src/types/boolean.md

+6
Original file line numberDiff line numberDiff line change
@@ -92,6 +92,12 @@ boolean type for its operands, they evaluate using the rules of [boolean logic].
9292
* `a < b` is the same as `!(a >= b)`
9393
* `a <= b` is the same as `a == b | a < b`
9494

95+
## Bit validity
96+
97+
The single byte of a `bool` is guaranteed to be initialized (in other words,
98+
`transmute::<bool, u8>(...)` is always sound -- but since some bit patterns
99+
are invalid `bool`s, the inverse is not always sound).
100+
95101
[boolean logic]: https://en.wikipedia.org/wiki/Boolean_algebra
96102
[enumerated type]: enum.md
97103
[expressions]: ../expressions.md

src/types/numeric.md

+5
Original file line numberDiff line numberDiff line change
@@ -45,3 +45,8 @@ within an object along with one byte past the end.
4545
> `isize` are either 32-bit or 64-bit. As a consequence, 16-bit
4646
> pointer support is limited and may require explicit care and acknowledgment
4747
> from a library to support.
48+
49+
## Bit validity
50+
51+
For every numeric type, `T`, the bit validity of `T` is equivalent to the bit
52+
validity of `[u8; size_of::<T>()]`. An uninitialized byte is not a valid `u8`.

src/types/pointer.md

+10
Original file line numberDiff line numberDiff line change
@@ -50,6 +50,16 @@ Raw pointers can be created directly using [`core::ptr::addr_of!`] for `*const`
5050

5151
The standard library contains additional 'smart pointer' types beyond references and raw pointers.
5252

53+
## Bit validity
54+
55+
Despite pointers and references being similar to `usize`s in the machine code emitted on most platforms,
56+
the semantics of transmuting a reference or pointer type to a non-pointer type is currently undecided.
57+
Thus, it may not be valid to transmute a pointer or reference type, `P`, to a `[u8; size_of::<P>()]`.
58+
59+
For thin raw pointers (i.e., for `P = *const T` or `P = *mut T` for `T: Sized`),
60+
the inverse direction (transmuting from an integer or array of integers to `P`) is always valid.
61+
However, the pointer produced via such a transmutation may not be dereferenced (not even if `T` has size zero).
62+
5363
[`core::ptr::addr_of!`]: ../../core/ptr/macro.addr_of.html
5464
[`core::ptr::addr_of_mut!`]: ../../core/ptr/macro.addr_of_mut.html
5565
[Interior mutability]: ../interior-mutability.md

src/types/textual.md

+6
Original file line numberDiff line numberDiff line change
@@ -17,6 +17,12 @@ is valid UTF-8. Calling a `str` method with a non-UTF-8 buffer can cause
1717
Since `str` is a [dynamically sized type], it can only be instantiated through a
1818
pointer type, such as `&str`.
1919

20+
## Bit validity
21+
22+
Every byte of a `char` is guaranteed to be initialized (in other words,
23+
`transmute::<char, [u8; size_of::<char>()]>(...)` is always sound -- but since
24+
some bit patterns are invalid `char`s, the inverse is not always sound).
25+
2026
[Unicode scalar value]: http://www.unicode.org/glossary/#unicode_scalar_value
2127
[Undefined Behavior]: ../behavior-considered-undefined.md
2228
[dynamically sized type]: ../dynamically-sized-types.md

0 commit comments

Comments
 (0)