@@ -20,79 +20,18 @@ mod slice;
2020mod uninit;
2121mod zero_sized;
2222
23+ mod capacity;
24+
2325#[ cfg( any( doc, feature = "alloc" ) ) ]
2426pub use heap:: Heap ;
2527
2628pub use slice:: UninitSlice ;
2729pub use uninit:: UninitBuffer ;
2830pub use zero_sized:: ZeroSized ;
2931
30- #[ cold]
31- #[ inline( never) ]
32- #[ cfg( feature = "nightly" ) ]
33- const fn capacity_calculation_overflow ( ) -> ! { panic ! ( "Tried to calculate the current capacity, but overflowed" ) }
34-
35- #[ cold]
36- #[ inline( never) ]
37- fn fixed_capacity_reserve_error ( capacity : usize , new_capacity : usize ) -> ! {
38- panic ! (
39- "Tried to reserve {}, but used a fixed capacity storage of {}" ,
40- new_capacity, capacity
41- )
42- }
43-
44- #[ cfg( not( any(
45- target_pointer_width = "8" ,
46- target_pointer_width = "16" ,
47- target_pointer_width = "32" ,
48- target_pointer_width = "64"
49- ) ) ) ]
50- compile_error ! ( "Cannot correctly calculate capacity on an 128-bit or larger architecture" ) ;
51-
52- const fn capacity ( old_capacity : usize , size_self : usize , size_other : usize ) -> usize {
53- #[ cfg( target_pointer_width = "8" ) ]
54- type PointerNext = u16 ;
55- #[ cfg( target_pointer_width = "16" ) ]
56- type PointerNext = u32 ;
57- #[ cfg( target_pointer_width = "32" ) ]
58- type PointerNext = u64 ;
59- #[ cfg( target_pointer_width = "64" ) ]
60- type PointerNext = u128 ;
61-
62- let size = ( old_capacity as PointerNext ) * ( size_self as PointerNext ) / ( size_other as PointerNext ) ;
63-
64- #[ cfg( not( feature = "nightly" ) ) ]
65- {
66- [ size as usize ] [ ( size > usize:: MAX as PointerNext ) as usize ]
67- }
68-
69- #[ cfg( feature = "nightly" ) ]
70- {
71- if size > usize:: MAX as PointerNext {
72- capacity_calculation_overflow ( )
73- }
74-
75- size as usize
76- }
77- }
78-
7932/// A [`Storage`] that can only contain initialized `Storage::Item`
8033pub unsafe trait StorageInit < T > : Storage < T > { }
8134
82- /// Check if type `U` smaller than `T` and less aligned than `T`
83- pub const fn is_compatible < T , U > ( ) -> bool {
84- use core:: mem:: { align_of, size_of} ;
85-
86- size_of :: < T > ( ) >= size_of :: < U > ( ) && align_of :: < T > ( ) >= align_of :: < U > ( )
87- }
88-
89- /// Check if type `U` is layout identical to `T`
90- pub const fn is_identical < T , U > ( ) -> bool {
91- use core:: mem:: { align_of, size_of} ;
92-
93- size_of :: < T > ( ) == size_of :: < U > ( ) && align_of :: < T > ( ) == align_of :: < U > ( )
94- }
95-
9635/// A type that can hold `T`s, and potentially
9736/// reserve space for more `Self::Items`s
9837pub unsafe trait Storage < T > {
@@ -143,7 +82,12 @@ pub unsafe trait Storage<T> {
14382}
14483
14584/// A storage that can be initially created with a given capacity
146- pub trait StorageWithCapacity < T > : Storage < T > + Default {
85+ ///
86+ /// # Safety
87+ ///
88+ /// The storage must have a capacity of at least `capacity` after
89+ /// `StorageWithCapacity::with_capacity` is called.
90+ pub unsafe trait StorageWithCapacity < T > : Storage < T > + Default {
14791 /// Creates a new storage with at least the given storage capacity
14892 fn with_capacity ( capacity : usize ) -> Self ;
14993
@@ -193,7 +137,7 @@ unsafe impl<T, S: ?Sized + Storage<T>> Storage<T> for Box<S> {
193137}
194138
195139#[ cfg( feature = "alloc" ) ]
196- impl < T , S : ?Sized + StorageWithCapacity < T > > StorageWithCapacity < T > for Box < S > {
140+ unsafe impl < T , S : ?Sized + StorageWithCapacity < T > > StorageWithCapacity < T > for Box < S > {
197141 fn with_capacity ( capacity : usize ) -> Self { Box :: new ( S :: with_capacity ( capacity) ) }
198142
199143 #[ doc( hidden) ]
0 commit comments