Skip to content

Commit 28673da

Browse files
committed
Guarantee that isize/usize are at least 16-bit wide
1 parent 97cbda4 commit 28673da

File tree

1 file changed

+15
-1
lines changed

1 file changed

+15
-1
lines changed

reference/src/layout/scalars.md

Lines changed: 15 additions & 1 deletion
Original file line numberDiff line numberDiff line change
@@ -35,7 +35,8 @@ of `char` is _implementation-defined_.
3535

3636
The `isize` and `usize` types are pointer-sized signed and unsigned integers.
3737
They have the same layout as the [pointer types] for which the pointee is
38-
`Sized`, and are layout compatible with C's `uintptr_t` and `intptr_t` types.
38+
`Sized`, and are layout compatible with C's `uintptr_t` and `intptr_t` types,
39+
and are therefore at least 16-bit wide.
3940

4041
> **Note**: Rust's `usize` and C's `unsigned` types are **not** equivalent. C's
4142
> `unsigned` is at least as large as a short, allowed to have padding bits, etc.
@@ -59,6 +60,19 @@ They have the same layout as the [pointer types] for which the pointee is
5960
> These limits have not gone through the RFC process and are not guaranteed to
6061
> hold.
6162
63+
<details><summary><b>Rationale</b></summary>
64+
65+
C99 [7.18.2.4](https://port70.net/~nsz/c/c99/n1256.html#7.18.2.4) "Limits of
66+
integer types capable of holding object pointers" provides the following minimum
67+
range of values that the pointer-holding integer types must support:
68+
69+
* `intptr_t`: `[INTPTR_MIN, INTPTR_MAX] = [-(2^15 - 1), 2^15 - 1]`
70+
* `uintptr_t`: `[0, UINTPTR_MAX] = [0, 2^16 - 1]`
71+
72+
That is, `isize`/`usize` are at least 16-bit wide.
73+
74+
</details>
75+
6276
[pointer types]: ./pointers.md
6377

6478
## Fixed-width integer types

0 commit comments

Comments
 (0)