diff options
| author | EFanZh <efanzh@gmail.com> | 2024-12-15 15:44:56 +0800 |
|---|---|---|
| committer | EFanZh <efanzh@gmail.com> | 2024-12-15 15:44:56 +0800 |
| commit | b5ea631fbd547c922a2b9fe09e1ae3794d4195ad (patch) | |
| tree | 0d11922fae633912e6b65dc0c8557e7a28bcde1b /library/core/src | |
| parent | 4790a435cbcb55c94ccdef51bf7a9b2e55824528 (diff) | |
| download | rust-b5ea631fbd547c922a2b9fe09e1ae3794d4195ad.tar.gz rust-b5ea631fbd547c922a2b9fe09e1ae3794d4195ad.zip | |
Asserts the maximum value that can be returned from `Vec::len`
Diffstat (limited to 'library/core/src')
| -rw-r--r-- | library/core/src/mem/mod.rs | 11 |
1 files changed, 11 insertions, 0 deletions
diff --git a/library/core/src/mem/mod.rs b/library/core/src/mem/mod.rs index 78ad6880709..57acc9dcd6e 100644 --- a/library/core/src/mem/mod.rs +++ b/library/core/src/mem/mod.rs @@ -1241,6 +1241,17 @@ pub trait SizedTypeProperties: Sized { #[doc(hidden)] #[unstable(feature = "sized_type_properties", issue = "none")] const LAYOUT: Layout = Layout::new::<Self>(); + + /// The largest safe length for a `[Self]`. + /// + /// Anything larger than this would make `size_of_val` overflow `isize::MAX`, + /// which is never allowed for a single object. + #[doc(hidden)] + #[unstable(feature = "sized_type_properties", issue = "none")] + const MAX_SLICE_LEN: usize = match size_of::<Self>() { + 0 => usize::MAX, + n => (isize::MAX as usize) / n, + }; } #[doc(hidden)] #[unstable(feature = "sized_type_properties", issue = "none")] |
