From 9471a9ab917410cea2ccdfc402688371fe766809 Mon Sep 17 00:00:00 2001 From: gnzlbg Date: Thu, 14 Mar 2019 18:24:42 +0100 Subject: [PATCH 1/2] Note the layout of pointers to T:Sized matches that of C pointers Closes #99 . --- reference/src/layout/pointers.md | 4 ++++ 1 file changed, 4 insertions(+) diff --git a/reference/src/layout/pointers.md b/reference/src/layout/pointers.md index ef9abdb0..196ce1c4 100644 --- a/reference/src/layout/pointers.md +++ b/reference/src/layout/pointers.md @@ -31,6 +31,10 @@ and are at least one word. The layouts of `&T`, `&mut T` and `*T` are the same. +If `T` is sized, references and pointers to `T` have a size and alignment of one +word and have therefore the same layout as C pointers - they can be used in C FFI +where pointers are expected. + We do not make any guarantees about the layout of multi-trait objects `&(dyn T + U)` or references to other dynamically sized types, other than that they are at least word-aligned, and have size at least one word. From 228a0fd72039af0d291668c64291fbeb684a2fd6 Mon Sep 17 00:00:00 2001 From: gnzlbg Date: Fri, 15 Mar 2019 10:09:17 +0100 Subject: [PATCH 2/2] Add warning about reference validity --- reference/src/layout/pointers.md | 8 ++++++-- 1 file changed, 6 insertions(+), 2 deletions(-) diff --git a/reference/src/layout/pointers.md b/reference/src/layout/pointers.md index 196ce1c4..ac97112f 100644 --- a/reference/src/layout/pointers.md +++ b/reference/src/layout/pointers.md @@ -32,8 +32,12 @@ and are at least one word. The layouts of `&T`, `&mut T` and `*T` are the same. If `T` is sized, references and pointers to `T` have a size and alignment of one -word and have therefore the same layout as C pointers - they can be used in C FFI -where pointers are expected. +word and have therefore the same layout as C pointers. + +> **warning**: while the layout of references and pointers is compatible with +> the layout of C pointers, references come with a _validity_ invariant that +> does not allow them to be used when they could be `NULL`, unaligned, dangling, +> or, in the case of `&mut T`, aliasing. We do not make any guarantees about the layout of multi-trait objects `&(dyn T + U)` or references to other dynamically sized types,