diff options
| author | Ralf Jung <post@ralfj.de> | 2024-06-08 11:26:10 +0200 |
|---|---|---|
| committer | Ralf Jung <post@ralfj.de> | 2024-07-10 18:01:06 +0200 |
| commit | 287b66b0b5cc06b9c0acb01d8a7b0db9ddf764ee (patch) | |
| tree | b117ac8233cfb690f6dd89ccd8df83a6e052bfc2 /library/core | |
| parent | cd3d98b3bef709c1f42b1964ba7a3ea371f95773 (diff) | |
size_of_val_raw: for length 0 this is safe to call
Diffstat (limited to 'library/core')
| -rw-r--r-- | library/core/src/alloc/layout.rs | 2 | ||||
| -rw-r--r-- | library/core/src/mem/mod.rs | 8 |
2 files changed, 10 insertions, 0 deletions
diff --git a/library/core/src/alloc/layout.rs b/library/core/src/alloc/layout.rs index 0b92767c932..e96a41422a2 100644 --- a/library/core/src/alloc/layout.rs +++ b/library/core/src/alloc/layout.rs @@ -183,6 +183,8 @@ impl Layout { /// - a [slice], then the length of the slice tail must be an initialized /// integer, and the size of the *entire value* /// (dynamic tail length + statically sized prefix) must fit in `isize`. + /// For the special case where the dynamic tail length is 0, this function + /// is safe to call. /// - a [trait object], then the vtable part of the pointer must point /// to a valid vtable for the type `T` acquired by an unsizing coercion, /// and the size of the *entire value* diff --git a/library/core/src/mem/mod.rs b/library/core/src/mem/mod.rs index dd4b6e82343..9bb4ba922cd 100644 --- a/library/core/src/mem/mod.rs +++ b/library/core/src/mem/mod.rs @@ -359,6 +359,12 @@ pub const fn size_of_val<T: ?Sized>(val: &T) -> usize { /// - a [slice], then the length of the slice tail must be an initialized /// integer, and the size of the *entire value* /// (dynamic tail length + statically sized prefix) must fit in `isize`. +/// For the special case where the dynamic tail length is 0, this function +/// is safe to call. +// NOTE: the reason this is safe is that if an overflow were to occur already with size 0, +// then we would stop compilation as even the "statically known" part of the type would +// already be too big (or the call may be in dead code and optimized away, but then it +// doesn't matter). /// - a [trait object], then the vtable part of the pointer must point /// to a valid vtable acquired by an unsizing coercion, and the size /// of the *entire value* (dynamic tail length + statically sized prefix) @@ -506,6 +512,8 @@ pub const fn align_of_val<T: ?Sized>(val: &T) -> usize { /// - a [slice], then the length of the slice tail must be an initialized /// integer, and the size of the *entire value* /// (dynamic tail length + statically sized prefix) must fit in `isize`. +/// For the special case where the dynamic tail length is 0, this function +/// is safe to call. /// - a [trait object], then the vtable part of the pointer must point /// to a valid vtable acquired by an unsizing coercion, and the size /// of the *entire value* (dynamic tail length + statically sized prefix) |
