@@ -83,7 +83,6 @@ use crate::{
83
83
clone:: CloneIn ,
84
84
collections:: CollectionAllocErr ,
85
85
raw_vec:: RawVec ,
86
- UncheckedResultExt ,
87
86
} ;
88
87
use core:: {
89
88
any:: Any ,
@@ -184,8 +183,12 @@ impl<T, A: AllocRef> Box<T, A> {
184
183
/// ```
185
184
#[ allow( clippy:: inline_always) ]
186
185
#[ inline( always) ]
187
- pub fn new_in ( x : T , a : A ) -> Self {
188
- unsafe { Self :: try_new_in ( x, a) . unwrap_unchecked ( ) }
186
+ pub fn new_in ( x : T , a : A ) -> Self
187
+ where
188
+ A : AllocRef < Error = !> ,
189
+ {
190
+ let Ok ( b) = Self :: try_new_in ( x, a) ;
191
+ b
189
192
}
190
193
191
194
/// Tries to allocate memory with the given allocator and then places `x` into it.
@@ -234,8 +237,12 @@ impl<T, A: AllocRef> Box<T, A> {
234
237
/// ```
235
238
#[ allow( clippy:: inline_always) ]
236
239
#[ inline( always) ]
237
- pub fn new_uninit_in ( a : A ) -> Box < mem:: MaybeUninit < T > , A > {
238
- unsafe { Self :: try_new_uninit_in ( a) . unwrap_unchecked ( ) }
240
+ pub fn new_uninit_in ( a : A ) -> Box < mem:: MaybeUninit < T > , A >
241
+ where
242
+ A : AllocRef < Error = !> ,
243
+ {
244
+ let Ok ( b) = Self :: try_new_uninit_in ( a) ;
245
+ b
239
246
}
240
247
241
248
/// Tries to construct a new box with uninitialized contents in a specified allocator.
@@ -271,8 +278,12 @@ impl<T, A: AllocRef> Box<T, A> {
271
278
/// `Unpin`, then `x` will be pinned in memory and unable to be moved.
272
279
#[ allow( clippy:: inline_always) ]
273
280
#[ inline( always) ]
274
- pub fn pin_in ( x : T , a : A ) -> Pin < Self > {
275
- unsafe { Self :: try_pin_in ( x, a) . unwrap_unchecked ( ) }
281
+ pub fn pin_in ( x : T , a : A ) -> Pin < Self >
282
+ where
283
+ A : AllocRef < Error = !> ,
284
+ {
285
+ let Ok ( b) = Self :: try_pin_in ( x, a) ;
286
+ b
276
287
}
277
288
278
289
/// Constructs a new `Pin<Box<T, A>>` with the specified allocator. If `T` does not implement
@@ -314,7 +325,7 @@ impl<T> Box<[T]> {
314
325
}
315
326
316
327
#[ allow( clippy:: use_self) ]
317
- impl < T , A : AllocRef > Box < [ T ] , A > {
328
+ impl < T , A : AllocRef < Error = ! > > Box < [ T ] , A > {
318
329
/// Construct a new boxed slice with uninitialized contents with the spoecified allocator.
319
330
///
320
331
/// # Example
@@ -338,9 +349,17 @@ impl<T, A: AllocRef> Box<[T], A> {
338
349
#[ allow( clippy:: inline_always) ]
339
350
#[ inline( always) ]
340
351
pub fn new_uninit_slice_in ( len : usize , a : A ) -> Box < [ mem:: MaybeUninit < T > ] , A > {
341
- unsafe { Self :: try_new_uninit_slice_in ( len, a) . unwrap_unchecked ( ) }
352
+ match Self :: try_new_uninit_slice_in ( len, a) {
353
+ Ok ( b) => b,
354
+ Err ( CollectionAllocErr :: CapacityOverflow ) => capacity_overflow ( ) ,
355
+ #[ allow( unreachable_patterns) ] // TODO rustc bug?
356
+ Err ( CollectionAllocErr :: AllocError { inner : e, .. } ) => e,
357
+ }
342
358
}
359
+ }
343
360
361
+ #[ allow( clippy:: use_self) ]
362
+ impl < T , A : AllocRef > Box < [ T ] , A > {
344
363
/// Tries to construct a new boxed slice with uninitialized contents with the spoecified
345
364
/// allocator.
346
365
///
@@ -747,7 +766,7 @@ unsafe impl<#[may_dangle] T: ?Sized, A: DeallocRef> Drop for Box<T, A> {
747
766
impl < T , A > Default for Box < T , A >
748
767
where
749
768
T : Default ,
750
- A : Default + AllocRef ,
769
+ A : Default + AllocRef < Error = ! > ,
751
770
{
752
771
#[ must_use]
753
772
fn default ( ) -> Self {
@@ -756,7 +775,7 @@ where
756
775
}
757
776
758
777
#[ allow( clippy:: use_self) ]
759
- impl < T , A > Default for Box < [ T ] , A >
778
+ impl < T , A : AllocRef < Error = ! > > Default for Box < [ T ] , A >
760
779
where
761
780
A : Default + AllocRef ,
762
781
{
@@ -773,7 +792,7 @@ unsafe fn from_boxed_utf8_unchecked<A: DeallocRef>(v: Box<[u8], A>) -> Box<str,
773
792
}
774
793
775
794
#[ allow( clippy:: use_self) ]
776
- impl < A > Default for Box < str , A >
795
+ impl < A : AllocRef < Error = ! > > Default for Box < str , A >
777
796
where
778
797
A : Default + AllocRef ,
779
798
{
@@ -783,7 +802,7 @@ where
783
802
}
784
803
}
785
804
786
- impl < T : Clone , A > Clone for Box < T , A >
805
+ impl < T : Clone , A : AllocRef < Error = ! > > Clone for Box < T , A >
787
806
where
788
807
A : AllocRef ,
789
808
A :: BuildAlloc : Clone ,
@@ -846,7 +865,10 @@ where
846
865
impl < T : Clone , A : AllocRef , B : AllocRef > CloneIn < B > for Box < T , A > {
847
866
type Cloned = Box < T , B > ;
848
867
849
- fn clone_in ( & self , a : B ) -> Self :: Cloned {
868
+ fn clone_in ( & self , a : B ) -> Self :: Cloned
869
+ where
870
+ B : AllocRef < Error = !> ,
871
+ {
850
872
Box :: new_in ( self . as_ref ( ) . clone ( ) , a)
851
873
}
852
874
@@ -947,7 +969,7 @@ impl<T: ?Sized + Hasher, A: DeallocRef> Hasher for Box<T, A> {
947
969
}
948
970
}
949
971
950
- impl < T , A > From < T > for Box < T , A >
972
+ impl < T , A : AllocRef < Error = ! > > From < T > for Box < T , A >
951
973
where
952
974
A : Default + AllocRef ,
953
975
{
@@ -983,7 +1005,7 @@ impl<T: ?Sized, A: DeallocRef> From<Box<T, A>> for Pin<Box<T, A>> {
983
1005
#[ allow( clippy:: use_self) ]
984
1006
impl < T : Copy , A > From < & [ T ] > for Box < [ T ] , A >
985
1007
where
986
- A : Default + AllocRef ,
1008
+ A : Default + AllocRef < Error = ! > ,
987
1009
{
988
1010
/// Converts a `&[T]` into a `Box<[T], B>`
989
1011
///
@@ -1012,7 +1034,7 @@ where
1012
1034
#[ allow( clippy:: use_self) ]
1013
1035
impl < A > From < & str > for Box < str , A >
1014
1036
where
1015
- A : Default + AllocRef ,
1037
+ A : Default + AllocRef < Error = ! > ,
1016
1038
{
1017
1039
/// Converts a `&str` into a `Box<str>`
1018
1040
///
@@ -1272,7 +1294,7 @@ impl_dispatch_from_dyn!(std::alloc::System);
1272
1294
#[ allow( clippy:: items_after_statements) ]
1273
1295
impl < T : Clone , A : Clone > Clone for Box < [ T ] , A >
1274
1296
where
1275
- A : AllocRef ,
1297
+ A : AllocRef < Error = ! > ,
1276
1298
A :: BuildAlloc : Clone ,
1277
1299
{
1278
1300
fn clone ( & self ) -> Self {
@@ -1383,3 +1405,10 @@ impl<F: ?Sized + Future + Unpin, A: DeallocRef> Future for Box<F, A> {
1383
1405
F :: poll ( Pin :: new ( & mut * self ) , cx)
1384
1406
}
1385
1407
}
1408
+
1409
+ // One central function responsible for reporting capacity overflows. This'll
1410
+ // ensure that the code generation related to these panics is minimal as there's
1411
+ // only one location which panics rather than a bunch throughout the module.
1412
+ fn capacity_overflow ( ) -> ! {
1413
+ panic ! ( "capacity overflow" ) ;
1414
+ }
0 commit comments