From 78118208d4280655b059a48264cb331d80c5cc3d Mon Sep 17 00:00:00 2001 From: Jonathan Spira Date: Sat, 7 Jun 2025 16:39:53 -0400 Subject: [PATCH 1/2] &str and &[u8] have the same layout --- src/dynamically-sized-types.md | 5 +++-- src/type-layout.md | 3 ++- src/types/textual.md | 3 ++- 3 files changed, 7 insertions(+), 4 deletions(-) diff --git a/src/dynamically-sized-types.md b/src/dynamically-sized-types.md index 28c398b7d..7434cbb77 100644 --- a/src/dynamically-sized-types.md +++ b/src/dynamically-sized-types.md @@ -5,7 +5,7 @@ r[dynamic-sized.intro] Most types have a fixed size that is known at compile time and implement the trait [`Sized`][sized]. A type with a size that is known only at run-time is called a _dynamically sized type_ (_DST_) or, informally, an unsized type. -[Slices] and [trait objects] are two examples of DSTs. r[dynamic-sized.restriction] @@ -14,7 +14,7 @@ Such types can only be used in certain cases: r[dynamic-sized.pointer-types] * [Pointer types] to DSTs are sized but have twice the size of pointers to sized types - * Pointers to slices also store the number of elements of the slice. + * Pointers to slices and `str` also store the number of elements of the slice. * Pointers to trait objects also store a pointer to a vtable. r[dynamic-sized.question-sized] @@ -38,6 +38,7 @@ r[dynamic-sized.struct-field] [sized]: special-types-and-traits.md#sized [Slices]: types/slice.md +[str]: types/textual.md [trait objects]: types/trait-object.md [Pointer types]: types/pointer.md [Variables]: variables.md diff --git a/src/type-layout.md b/src/type-layout.md index cefa1dc11..c17c24d42 100644 --- a/src/type-layout.md +++ b/src/type-layout.md @@ -89,7 +89,7 @@ Pointers to unsized types are sized. The size and alignment is guaranteed to be at least equal to the size and alignment of a pointer. > [!NOTE] -> Though you should not rely on this, all pointers to DSTs are currently twice the size of the size of `usize` and have the same alignment. +> Though you should not rely on this, all pointers to DSTs have the same alignment as usize. r[layout.array] ## Array Layout @@ -110,6 +110,7 @@ r[layout.str] ## `str` Layout String slices are a UTF-8 representation of characters that have the same layout as slices of type `[u8]`. +A reference `&str` has the same layout as a reference `&[u8]`. r[layout.tuple] ## Tuple Layout diff --git a/src/types/textual.md b/src/types/textual.md index a6ad38ffc..32722e720 100644 --- a/src/types/textual.md +++ b/src/types/textual.md @@ -23,7 +23,8 @@ is valid UTF-8. Calling a `str` method with a non-UTF-8 buffer can cause r[type.text.str-unsized] Since `str` is a [dynamically sized type], it can only be instantiated through a -pointer type, such as `&str`. +pointer type, such as `&str`. The layout of `&str` is the same as the layout of +`&[u8]`. r[type.text.layout] ## Layout and bit validity From 1a7fa481efe24a4ffa86adecc36b482d46bceec2 Mon Sep 17 00:00:00 2001 From: Jonathan Spira Date: Sat, 7 Jun 2025 16:58:01 -0400 Subject: [PATCH 2/2] removed different edit --- src/type-layout.md | 2 +- 1 file changed, 1 insertion(+), 1 deletion(-) diff --git a/src/type-layout.md b/src/type-layout.md index c17c24d42..d05d2cfc5 100644 --- a/src/type-layout.md +++ b/src/type-layout.md @@ -89,7 +89,7 @@ Pointers to unsized types are sized. The size and alignment is guaranteed to be at least equal to the size and alignment of a pointer. > [!NOTE] -> Though you should not rely on this, all pointers to DSTs have the same alignment as usize. +> Though you should not rely on this, all pointers to DSTs are currently twice the size of the size of `usize` and have the same alignment. r[layout.array] ## Array Layout