56
56
#[ cfg( not( no_global_oom_handling) ) ]
57
57
use core:: cmp;
58
58
use core:: cmp:: Ordering ;
59
- use core:: fmt;
60
59
use core:: hash:: { Hash , Hasher } ;
61
60
#[ cfg( not( no_global_oom_handling) ) ]
62
61
use core:: iter;
@@ -65,6 +64,7 @@ use core::mem::{self, ManuallyDrop, MaybeUninit, SizedTypeProperties};
65
64
use core:: ops:: { self , Index , IndexMut , Range , RangeBounds } ;
66
65
use core:: ptr:: { self , NonNull } ;
67
66
use core:: slice:: { self , SliceIndex } ;
67
+ use core:: { fmt, intrinsics} ;
68
68
69
69
#[ unstable( feature = "extract_if" , reason = "recently added" , issue = "43244" ) ]
70
70
pub use self :: extract_if:: ExtractIf ;
@@ -2680,7 +2680,14 @@ impl<T, A: Allocator> Vec<T, A> {
2680
2680
#[ rustc_const_unstable( feature = "const_vec_string_slice" , issue = "129041" ) ]
2681
2681
#[ rustc_confusables( "length" , "size" ) ]
2682
2682
pub const fn len ( & self ) -> usize {
2683
- self . len
2683
+ let len = self . len ;
2684
+
2685
+ // SAFETY: The maximum capacity of `Vec<T>` is `isize::MAX` bytes, so the maximum value can
2686
+ // be returned is `usize::checked_div(mem::size_of::<T>()).unwrap_or(usize::MAX)`, which
2687
+ // matches the definition of `T::MAX_SLICE_LEN`.
2688
+ unsafe { intrinsics:: assume ( len <= T :: MAX_SLICE_LEN ) } ;
2689
+
2690
+ len
2684
2691
}
2685
2692
2686
2693
/// Returns `true` if the vector contains no elements.
0 commit comments