143
143
#![ allow( missing_docs) ]
144
144
#![ stable( feature = "rust1" , since = "1.0.0" ) ]
145
145
146
+ use core:: alloc:: Allocator ;
146
147
use core:: fmt;
147
148
use core:: iter:: { FusedIterator , InPlaceIterable , SourceIter , TrustedLen } ;
148
149
use core:: mem:: { self , swap, ManuallyDrop } ;
149
150
use core:: num:: NonZeroUsize ;
150
151
use core:: ops:: { Deref , DerefMut } ;
151
152
use core:: ptr;
152
153
154
+ use crate :: alloc:: Global ;
153
155
use crate :: collections:: TryReserveError ;
154
156
use crate :: slice;
155
157
use crate :: vec:: { self , AsVecIntoIter , Vec } ;
@@ -271,8 +273,11 @@ mod tests;
271
273
/// [peek\_mut]: BinaryHeap::peek_mut
272
274
#[ stable( feature = "rust1" , since = "1.0.0" ) ]
273
275
#[ cfg_attr( not( test) , rustc_diagnostic_item = "BinaryHeap" ) ]
274
- pub struct BinaryHeap < T > {
275
- data : Vec < T > ,
276
+ pub struct BinaryHeap <
277
+ T ,
278
+ #[ unstable( feature = "allocator_api" , issue = "32838" ) ] A : Allocator = Global ,
279
+ > {
280
+ data : Vec < T , A > ,
276
281
}
277
282
278
283
/// Structure wrapping a mutable reference to the greatest item on a
@@ -283,22 +288,26 @@ pub struct BinaryHeap<T> {
283
288
///
284
289
/// [`peek_mut`]: BinaryHeap::peek_mut
285
290
#[ stable( feature = "binary_heap_peek_mut" , since = "1.12.0" ) ]
286
- pub struct PeekMut < ' a , T : ' a + Ord > {
287
- heap : & ' a mut BinaryHeap < T > ,
291
+ pub struct PeekMut <
292
+ ' a ,
293
+ T : ' a + Ord ,
294
+ #[ unstable( feature = "allocator_api" , issue = "32838" ) ] A : Allocator = Global ,
295
+ > {
296
+ heap : & ' a mut BinaryHeap < T , A > ,
288
297
// If a set_len + sift_down are required, this is Some. If a &mut T has not
289
298
// yet been exposed to peek_mut()'s caller, it's None.
290
299
original_len : Option < NonZeroUsize > ,
291
300
}
292
301
293
302
#[ stable( feature = "collection_debug" , since = "1.17.0" ) ]
294
- impl < T : Ord + fmt:: Debug > fmt:: Debug for PeekMut < ' _ , T > {
303
+ impl < T : Ord + fmt:: Debug , A : Allocator > fmt:: Debug for PeekMut < ' _ , T , A > {
295
304
fn fmt ( & self , f : & mut fmt:: Formatter < ' _ > ) -> fmt:: Result {
296
305
f. debug_tuple ( "PeekMut" ) . field ( & self . heap . data [ 0 ] ) . finish ( )
297
306
}
298
307
}
299
308
300
309
#[ stable( feature = "binary_heap_peek_mut" , since = "1.12.0" ) ]
301
- impl < T : Ord > Drop for PeekMut < ' _ , T > {
310
+ impl < T : Ord , A : Allocator > Drop for PeekMut < ' _ , T , A > {
302
311
fn drop ( & mut self ) {
303
312
if let Some ( original_len) = self . original_len {
304
313
// SAFETY: That's how many elements were in the Vec at the time of
@@ -315,7 +324,7 @@ impl<T: Ord> Drop for PeekMut<'_, T> {
315
324
}
316
325
317
326
#[ stable( feature = "binary_heap_peek_mut" , since = "1.12.0" ) ]
318
- impl < T : Ord > Deref for PeekMut < ' _ , T > {
327
+ impl < T : Ord , A : Allocator > Deref for PeekMut < ' _ , T , A > {
319
328
type Target = T ;
320
329
fn deref ( & self ) -> & T {
321
330
debug_assert ! ( !self . heap. is_empty( ) ) ;
@@ -325,7 +334,7 @@ impl<T: Ord> Deref for PeekMut<'_, T> {
325
334
}
326
335
327
336
#[ stable( feature = "binary_heap_peek_mut" , since = "1.12.0" ) ]
328
- impl < T : Ord > DerefMut for PeekMut < ' _ , T > {
337
+ impl < T : Ord , A : Allocator > DerefMut for PeekMut < ' _ , T , A > {
329
338
fn deref_mut ( & mut self ) -> & mut T {
330
339
debug_assert ! ( !self . heap. is_empty( ) ) ;
331
340
@@ -353,10 +362,10 @@ impl<T: Ord> DerefMut for PeekMut<'_, T> {
353
362
}
354
363
}
355
364
356
- impl < ' a , T : Ord > PeekMut < ' a , T > {
365
+ impl < ' a , T : Ord , A : Allocator > PeekMut < ' a , T , A > {
357
366
/// Removes the peeked value from the heap and returns it.
358
367
#[ stable( feature = "binary_heap_peek_mut_pop" , since = "1.18.0" ) ]
359
- pub fn pop ( mut this : PeekMut < ' a , T > ) -> T {
368
+ pub fn pop ( mut this : PeekMut < ' a , T , A > ) -> T {
360
369
if let Some ( original_len) = this. original_len . take ( ) {
361
370
// SAFETY: This is how many elements were in the Vec at the time of
362
371
// the BinaryHeap::peek_mut call.
@@ -371,7 +380,7 @@ impl<'a, T: Ord> PeekMut<'a, T> {
371
380
}
372
381
373
382
#[ stable( feature = "rust1" , since = "1.0.0" ) ]
374
- impl < T : Clone > Clone for BinaryHeap < T > {
383
+ impl < T : Clone , A : Allocator + Clone > Clone for BinaryHeap < T , A > {
375
384
fn clone ( & self ) -> Self {
376
385
BinaryHeap { data : self . data . clone ( ) }
377
386
}
@@ -391,18 +400,22 @@ impl<T: Ord> Default for BinaryHeap<T> {
391
400
}
392
401
393
402
#[ stable( feature = "binaryheap_debug" , since = "1.4.0" ) ]
394
- impl < T : fmt:: Debug > fmt:: Debug for BinaryHeap < T > {
403
+ impl < T : fmt:: Debug , A : Allocator > fmt:: Debug for BinaryHeap < T , A > {
395
404
fn fmt ( & self , f : & mut fmt:: Formatter < ' _ > ) -> fmt:: Result {
396
405
f. debug_list ( ) . entries ( self . iter ( ) ) . finish ( )
397
406
}
398
407
}
399
408
400
- struct RebuildOnDrop < ' a , T : Ord > {
401
- heap : & ' a mut BinaryHeap < T > ,
409
+ struct RebuildOnDrop <
410
+ ' a ,
411
+ T : Ord ,
412
+ #[ unstable( feature = "allocator_api" , issue = "32838" ) ] A : Allocator = Global ,
413
+ > {
414
+ heap : & ' a mut BinaryHeap < T , A > ,
402
415
rebuild_from : usize ,
403
416
}
404
417
405
- impl < ' a , T : Ord > Drop for RebuildOnDrop < ' a , T > {
418
+ impl < T : Ord , A : Allocator > Drop for RebuildOnDrop < ' _ , T , A > {
406
419
fn drop ( & mut self ) {
407
420
self . heap . rebuild_tail ( self . rebuild_from ) ;
408
421
}
@@ -446,6 +459,52 @@ impl<T: Ord> BinaryHeap<T> {
446
459
pub fn with_capacity ( capacity : usize ) -> BinaryHeap < T > {
447
460
BinaryHeap { data : Vec :: with_capacity ( capacity) }
448
461
}
462
+ }
463
+
464
+ impl < T : Ord , A : Allocator > BinaryHeap < T , A > {
465
+ /// Creates an empty `BinaryHeap` as a max-heap, using `A` as allocator.
466
+ ///
467
+ /// # Examples
468
+ ///
469
+ /// Basic usage:
470
+ ///
471
+ /// ```
472
+ /// #![feature(allocator_api)]
473
+ ///
474
+ /// use std::alloc::System;
475
+ /// use std::collections::BinaryHeap;
476
+ /// let mut heap = BinaryHeap::new_in(System);
477
+ /// heap.push(4);
478
+ /// ```
479
+ #[ unstable( feature = "allocator_api" , issue = "32838" ) ]
480
+ #[ must_use]
481
+ pub fn new_in ( alloc : A ) -> BinaryHeap < T , A > {
482
+ BinaryHeap { data : Vec :: new_in ( alloc) }
483
+ }
484
+
485
+ /// Creates an empty `BinaryHeap` with at least the specified capacity, using `A` as allocator.
486
+ ///
487
+ /// The binary heap will be able to hold at least `capacity` elements without
488
+ /// reallocating. This method is allowed to allocate for more elements than
489
+ /// `capacity`. If `capacity` is 0, the binary heap will not allocate.
490
+ ///
491
+ /// # Examples
492
+ ///
493
+ /// Basic usage:
494
+ ///
495
+ /// ```
496
+ /// #![feature(allocator_api)]
497
+ ///
498
+ /// use std::alloc::System;
499
+ /// use std::collections::BinaryHeap;
500
+ /// let mut heap = BinaryHeap::with_capacity_in(10, System);
501
+ /// heap.push(4);
502
+ /// ```
503
+ #[ unstable( feature = "allocator_api" , issue = "32838" ) ]
504
+ #[ must_use]
505
+ pub fn with_capacity_in ( capacity : usize , alloc : A ) -> BinaryHeap < T , A > {
506
+ BinaryHeap { data : Vec :: with_capacity_in ( capacity, alloc) }
507
+ }
449
508
450
509
/// Returns a mutable reference to the greatest item in the binary heap, or
451
510
/// `None` if it is empty.
@@ -478,7 +537,7 @@ impl<T: Ord> BinaryHeap<T> {
478
537
/// If the item is modified then the worst case time complexity is *O*(log(*n*)),
479
538
/// otherwise it's *O*(1).
480
539
#[ stable( feature = "binary_heap_peek_mut" , since = "1.12.0" ) ]
481
- pub fn peek_mut ( & mut self ) -> Option < PeekMut < ' _ , T > > {
540
+ pub fn peek_mut ( & mut self ) -> Option < PeekMut < ' _ , T , A > > {
482
541
if self . is_empty ( ) { None } else { Some ( PeekMut { heap : self , original_len : None } ) }
483
542
}
484
543
@@ -573,7 +632,7 @@ impl<T: Ord> BinaryHeap<T> {
573
632
/// ```
574
633
#[ must_use = "`self` will be dropped if the result is not used" ]
575
634
#[ stable( feature = "binary_heap_extras_15" , since = "1.5.0" ) ]
576
- pub fn into_sorted_vec ( mut self ) -> Vec < T > {
635
+ pub fn into_sorted_vec ( mut self ) -> Vec < T , A > {
577
636
let mut end = self . len ( ) ;
578
637
while end > 1 {
579
638
end -= 1 ;
@@ -831,7 +890,7 @@ impl<T: Ord> BinaryHeap<T> {
831
890
/// ```
832
891
#[ inline]
833
892
#[ unstable( feature = "binary_heap_drain_sorted" , issue = "59278" ) ]
834
- pub fn drain_sorted ( & mut self ) -> DrainSorted < ' _ , T > {
893
+ pub fn drain_sorted ( & mut self ) -> DrainSorted < ' _ , T , A > {
835
894
DrainSorted { inner : self }
836
895
}
837
896
@@ -874,7 +933,7 @@ impl<T: Ord> BinaryHeap<T> {
874
933
}
875
934
}
876
935
877
- impl < T > BinaryHeap < T > {
936
+ impl < T , A : Allocator > BinaryHeap < T , A > {
878
937
/// Returns an iterator visiting all values in the underlying vector, in
879
938
/// arbitrary order.
880
939
///
@@ -911,7 +970,7 @@ impl<T> BinaryHeap<T> {
911
970
/// assert_eq!(heap.into_iter_sorted().take(2).collect::<Vec<_>>(), [5, 4]);
912
971
/// ```
913
972
#[ unstable( feature = "binary_heap_into_iter_sorted" , issue = "59278" ) ]
914
- pub fn into_iter_sorted ( self ) -> IntoIterSorted < T > {
973
+ pub fn into_iter_sorted ( self ) -> IntoIterSorted < T , A > {
915
974
IntoIterSorted { inner : self }
916
975
}
917
976
@@ -1178,10 +1237,17 @@ impl<T> BinaryHeap<T> {
1178
1237
/// ```
1179
1238
#[ must_use = "`self` will be dropped if the result is not used" ]
1180
1239
#[ stable( feature = "binary_heap_extras_15" , since = "1.5.0" ) ]
1181
- pub fn into_vec ( self ) -> Vec < T > {
1240
+ pub fn into_vec ( self ) -> Vec < T , A > {
1182
1241
self . into ( )
1183
1242
}
1184
1243
1244
+ /// Returns a reference to the underlying allocator.
1245
+ #[ unstable( feature = "allocator_api" , issue = "32838" ) ]
1246
+ #[ inline]
1247
+ pub fn allocator ( & self ) -> & A {
1248
+ self . data . allocator ( )
1249
+ }
1250
+
1185
1251
/// Returns the length of the binary heap.
1186
1252
///
1187
1253
/// # Examples
@@ -1249,7 +1315,7 @@ impl<T> BinaryHeap<T> {
1249
1315
/// ```
1250
1316
#[ inline]
1251
1317
#[ stable( feature = "drain" , since = "1.6.0" ) ]
1252
- pub fn drain ( & mut self ) -> Drain < ' _ , T > {
1318
+ pub fn drain ( & mut self ) -> Drain < ' _ , T , A > {
1253
1319
Drain { iter : self . data . drain ( ..) }
1254
1320
}
1255
1321
@@ -1419,19 +1485,30 @@ impl<T> FusedIterator for Iter<'_, T> {}
1419
1485
/// [`into_iter`]: BinaryHeap::into_iter
1420
1486
#[ stable( feature = "rust1" , since = "1.0.0" ) ]
1421
1487
#[ derive( Clone ) ]
1422
- pub struct IntoIter < T > {
1423
- iter : vec:: IntoIter < T > ,
1488
+ pub struct IntoIter <
1489
+ T ,
1490
+ #[ unstable( feature = "allocator_api" , issue = "32838" ) ] A : Allocator = Global ,
1491
+ > {
1492
+ iter : vec:: IntoIter < T , A > ,
1493
+ }
1494
+
1495
+ impl < T , A : Allocator > IntoIter < T , A > {
1496
+ /// Returns a reference to the underlying allocator.
1497
+ #[ unstable( feature = "allocator_api" , issue = "32838" ) ]
1498
+ pub fn allocator ( & self ) -> & A {
1499
+ self . iter . allocator ( )
1500
+ }
1424
1501
}
1425
1502
1426
1503
#[ stable( feature = "collection_debug" , since = "1.17.0" ) ]
1427
- impl < T : fmt:: Debug > fmt:: Debug for IntoIter < T > {
1504
+ impl < T : fmt:: Debug , A : Allocator > fmt:: Debug for IntoIter < T , A > {
1428
1505
fn fmt ( & self , f : & mut fmt:: Formatter < ' _ > ) -> fmt:: Result {
1429
1506
f. debug_tuple ( "IntoIter" ) . field ( & self . iter . as_slice ( ) ) . finish ( )
1430
1507
}
1431
1508
}
1432
1509
1433
1510
#[ stable( feature = "rust1" , since = "1.0.0" ) ]
1434
- impl < T > Iterator for IntoIter < T > {
1511
+ impl < T , A : Allocator > Iterator for IntoIter < T , A > {
1435
1512
type Item = T ;
1436
1513
1437
1514
#[ inline]
@@ -1446,22 +1523,22 @@ impl<T> Iterator for IntoIter<T> {
1446
1523
}
1447
1524
1448
1525
#[ stable( feature = "rust1" , since = "1.0.0" ) ]
1449
- impl < T > DoubleEndedIterator for IntoIter < T > {
1526
+ impl < T , A : Allocator > DoubleEndedIterator for IntoIter < T , A > {
1450
1527
#[ inline]
1451
1528
fn next_back ( & mut self ) -> Option < T > {
1452
1529
self . iter . next_back ( )
1453
1530
}
1454
1531
}
1455
1532
1456
1533
#[ stable( feature = "rust1" , since = "1.0.0" ) ]
1457
- impl < T > ExactSizeIterator for IntoIter < T > {
1534
+ impl < T , A : Allocator > ExactSizeIterator for IntoIter < T , A > {
1458
1535
fn is_empty ( & self ) -> bool {
1459
1536
self . iter . is_empty ( )
1460
1537
}
1461
1538
}
1462
1539
1463
1540
#[ stable( feature = "fused" , since = "1.26.0" ) ]
1464
- impl < T > FusedIterator for IntoIter < T > { }
1541
+ impl < T , A : Allocator > FusedIterator for IntoIter < T , A > { }
1465
1542
1466
1543
#[ stable( feature = "default_iters" , since = "1.70.0" ) ]
1467
1544
impl < T > Default for IntoIter < T > {
@@ -1481,8 +1558,8 @@ impl<T> Default for IntoIter<T> {
1481
1558
// also refer to the vec::in_place_collect module documentation to get an overview
1482
1559
#[ unstable( issue = "none" , feature = "inplace_iteration" ) ]
1483
1560
#[ doc( hidden) ]
1484
- unsafe impl < T > SourceIter for IntoIter < T > {
1485
- type Source = IntoIter < T > ;
1561
+ unsafe impl < T , A : Allocator > SourceIter for IntoIter < T , A > {
1562
+ type Source = IntoIter < T , A > ;
1486
1563
1487
1564
#[ inline]
1488
1565
unsafe fn as_inner ( & mut self ) -> & mut Self :: Source {
@@ -1492,7 +1569,7 @@ unsafe impl<T> SourceIter for IntoIter<T> {
1492
1569
1493
1570
#[ unstable( issue = "none" , feature = "inplace_iteration" ) ]
1494
1571
#[ doc( hidden) ]
1495
- unsafe impl < I > InPlaceIterable for IntoIter < I > { }
1572
+ unsafe impl < I , A : Allocator > InPlaceIterable for IntoIter < I , A > { }
1496
1573
1497
1574
unsafe impl < I > AsVecIntoIter for IntoIter < I > {
1498
1575
type Item = I ;
@@ -1505,12 +1582,23 @@ unsafe impl<I> AsVecIntoIter for IntoIter<I> {
1505
1582
#[ must_use = "iterators are lazy and do nothing unless consumed" ]
1506
1583
#[ unstable( feature = "binary_heap_into_iter_sorted" , issue = "59278" ) ]
1507
1584
#[ derive( Clone , Debug ) ]
1508
- pub struct IntoIterSorted < T > {
1509
- inner : BinaryHeap < T > ,
1585
+ pub struct IntoIterSorted <
1586
+ T ,
1587
+ #[ unstable( feature = "allocator_api" , issue = "32838" ) ] A : Allocator = Global ,
1588
+ > {
1589
+ inner : BinaryHeap < T , A > ,
1590
+ }
1591
+
1592
+ impl < T , A : Allocator > IntoIterSorted < T , A > {
1593
+ /// Returns a reference to the underlying allocator.
1594
+ #[ unstable( feature = "allocator_api" , issue = "32838" ) ]
1595
+ pub fn allocator ( & self ) -> & A {
1596
+ self . inner . allocator ( )
1597
+ }
1510
1598
}
1511
1599
1512
1600
#[ unstable( feature = "binary_heap_into_iter_sorted" , issue = "59278" ) ]
1513
- impl < T : Ord > Iterator for IntoIterSorted < T > {
1601
+ impl < T : Ord , A : Allocator > Iterator for IntoIterSorted < T , A > {
1514
1602
type Item = T ;
1515
1603
1516
1604
#[ inline]
@@ -1526,13 +1614,13 @@ impl<T: Ord> Iterator for IntoIterSorted<T> {
1526
1614
}
1527
1615
1528
1616
#[ unstable( feature = "binary_heap_into_iter_sorted" , issue = "59278" ) ]
1529
- impl < T : Ord > ExactSizeIterator for IntoIterSorted < T > { }
1617
+ impl < T : Ord , A : Allocator > ExactSizeIterator for IntoIterSorted < T , A > { }
1530
1618
1531
1619
#[ unstable( feature = "binary_heap_into_iter_sorted" , issue = "59278" ) ]
1532
- impl < T : Ord > FusedIterator for IntoIterSorted < T > { }
1620
+ impl < T : Ord , A : Allocator > FusedIterator for IntoIterSorted < T , A > { }
1533
1621
1534
1622
#[ unstable( feature = "trusted_len" , issue = "37572" ) ]
1535
- unsafe impl < T : Ord > TrustedLen for IntoIterSorted < T > { }
1623
+ unsafe impl < T : Ord , A : Allocator > TrustedLen for IntoIterSorted < T , A > { }
1536
1624
1537
1625
/// A draining iterator over the elements of a `BinaryHeap`.
1538
1626
///
@@ -1542,12 +1630,24 @@ unsafe impl<T: Ord> TrustedLen for IntoIterSorted<T> {}
1542
1630
/// [`drain`]: BinaryHeap::drain
1543
1631
#[ stable( feature = "drain" , since = "1.6.0" ) ]
1544
1632
#[ derive( Debug ) ]
1545
- pub struct Drain < ' a , T : ' a > {
1546
- iter : vec:: Drain < ' a , T > ,
1633
+ pub struct Drain <
1634
+ ' a ,
1635
+ T : ' a ,
1636
+ #[ unstable( feature = "allocator_api" , issue = "32838" ) ] A : Allocator = Global ,
1637
+ > {
1638
+ iter : vec:: Drain < ' a , T , A > ,
1639
+ }
1640
+
1641
+ impl < T , A : Allocator > Drain < ' _ , T , A > {
1642
+ /// Returns a reference to the underlying allocator.
1643
+ #[ unstable( feature = "allocator_api" , issue = "32838" ) ]
1644
+ pub fn allocator ( & self ) -> & A {
1645
+ self . iter . allocator ( )
1646
+ }
1547
1647
}
1548
1648
1549
1649
#[ stable( feature = "drain" , since = "1.6.0" ) ]
1550
- impl < T > Iterator for Drain < ' _ , T > {
1650
+ impl < T , A : Allocator > Iterator for Drain < ' _ , T , A > {
1551
1651
type Item = T ;
1552
1652
1553
1653
#[ inline]
@@ -1562,22 +1662,22 @@ impl<T> Iterator for Drain<'_, T> {
1562
1662
}
1563
1663
1564
1664
#[ stable( feature = "drain" , since = "1.6.0" ) ]
1565
- impl < T > DoubleEndedIterator for Drain < ' _ , T > {
1665
+ impl < T , A : Allocator > DoubleEndedIterator for Drain < ' _ , T , A > {
1566
1666
#[ inline]
1567
1667
fn next_back ( & mut self ) -> Option < T > {
1568
1668
self . iter . next_back ( )
1569
1669
}
1570
1670
}
1571
1671
1572
1672
#[ stable( feature = "drain" , since = "1.6.0" ) ]
1573
- impl < T > ExactSizeIterator for Drain < ' _ , T > {
1673
+ impl < T , A : Allocator > ExactSizeIterator for Drain < ' _ , T , A > {
1574
1674
fn is_empty ( & self ) -> bool {
1575
1675
self . iter . is_empty ( )
1576
1676
}
1577
1677
}
1578
1678
1579
1679
#[ stable( feature = "fused" , since = "1.26.0" ) ]
1580
- impl < T > FusedIterator for Drain < ' _ , T > { }
1680
+ impl < T , A : Allocator > FusedIterator for Drain < ' _ , T , A > { }
1581
1681
1582
1682
/// A draining iterator over the elements of a `BinaryHeap`.
1583
1683
///
@@ -1587,17 +1687,29 @@ impl<T> FusedIterator for Drain<'_, T> {}
1587
1687
/// [`drain_sorted`]: BinaryHeap::drain_sorted
1588
1688
#[ unstable( feature = "binary_heap_drain_sorted" , issue = "59278" ) ]
1589
1689
#[ derive( Debug ) ]
1590
- pub struct DrainSorted < ' a , T : Ord > {
1591
- inner : & ' a mut BinaryHeap < T > ,
1690
+ pub struct DrainSorted <
1691
+ ' a ,
1692
+ T : Ord ,
1693
+ #[ unstable( feature = "allocator_api" , issue = "32838" ) ] A : Allocator = Global ,
1694
+ > {
1695
+ inner : & ' a mut BinaryHeap < T , A > ,
1696
+ }
1697
+
1698
+ impl < ' a , T : Ord , A : Allocator > DrainSorted < ' a , T , A > {
1699
+ /// Returns a reference to the underlying allocator.
1700
+ #[ unstable( feature = "allocator_api" , issue = "32838" ) ]
1701
+ pub fn allocator ( & self ) -> & A {
1702
+ self . inner . allocator ( )
1703
+ }
1592
1704
}
1593
1705
1594
1706
#[ unstable( feature = "binary_heap_drain_sorted" , issue = "59278" ) ]
1595
- impl < ' a , T : Ord > Drop for DrainSorted < ' a , T > {
1707
+ impl < ' a , T : Ord , A : Allocator > Drop for DrainSorted < ' a , T , A > {
1596
1708
/// Removes heap elements in heap order.
1597
1709
fn drop ( & mut self ) {
1598
- struct DropGuard < ' r , ' a , T : Ord > ( & ' r mut DrainSorted < ' a , T > ) ;
1710
+ struct DropGuard < ' r , ' a , T : Ord , A : Allocator > ( & ' r mut DrainSorted < ' a , T , A > ) ;
1599
1711
1600
- impl < ' r , ' a , T : Ord > Drop for DropGuard < ' r , ' a , T > {
1712
+ impl < ' r , ' a , T : Ord , A : Allocator > Drop for DropGuard < ' r , ' a , T , A > {
1601
1713
fn drop ( & mut self ) {
1602
1714
while self . 0 . inner . pop ( ) . is_some ( ) { }
1603
1715
}
@@ -1612,7 +1724,7 @@ impl<'a, T: Ord> Drop for DrainSorted<'a, T> {
1612
1724
}
1613
1725
1614
1726
#[ unstable( feature = "binary_heap_drain_sorted" , issue = "59278" ) ]
1615
- impl < T : Ord > Iterator for DrainSorted < ' _ , T > {
1727
+ impl < T : Ord , A : Allocator > Iterator for DrainSorted < ' _ , T , A > {
1616
1728
type Item = T ;
1617
1729
1618
1730
#[ inline]
@@ -1628,20 +1740,20 @@ impl<T: Ord> Iterator for DrainSorted<'_, T> {
1628
1740
}
1629
1741
1630
1742
#[ unstable( feature = "binary_heap_drain_sorted" , issue = "59278" ) ]
1631
- impl < T : Ord > ExactSizeIterator for DrainSorted < ' _ , T > { }
1743
+ impl < T : Ord , A : Allocator > ExactSizeIterator for DrainSorted < ' _ , T , A > { }
1632
1744
1633
1745
#[ unstable( feature = "binary_heap_drain_sorted" , issue = "59278" ) ]
1634
- impl < T : Ord > FusedIterator for DrainSorted < ' _ , T > { }
1746
+ impl < T : Ord , A : Allocator > FusedIterator for DrainSorted < ' _ , T , A > { }
1635
1747
1636
1748
#[ unstable( feature = "trusted_len" , issue = "37572" ) ]
1637
- unsafe impl < T : Ord > TrustedLen for DrainSorted < ' _ , T > { }
1749
+ unsafe impl < T : Ord , A : Allocator > TrustedLen for DrainSorted < ' _ , T , A > { }
1638
1750
1639
1751
#[ stable( feature = "binary_heap_extras_15" , since = "1.5.0" ) ]
1640
- impl < T : Ord > From < Vec < T > > for BinaryHeap < T > {
1752
+ impl < T : Ord , A : Allocator > From < Vec < T , A > > for BinaryHeap < T , A > {
1641
1753
/// Converts a `Vec<T>` into a `BinaryHeap<T>`.
1642
1754
///
1643
1755
/// This conversion happens in-place, and has *O*(*n*) time complexity.
1644
- fn from ( vec : Vec < T > ) -> BinaryHeap < T > {
1756
+ fn from ( vec : Vec < T , A > ) -> BinaryHeap < T , A > {
1645
1757
let mut heap = BinaryHeap { data : vec } ;
1646
1758
heap. rebuild ( ) ;
1647
1759
heap
@@ -1665,12 +1777,12 @@ impl<T: Ord, const N: usize> From<[T; N]> for BinaryHeap<T> {
1665
1777
}
1666
1778
1667
1779
#[ stable( feature = "binary_heap_extras_15" , since = "1.5.0" ) ]
1668
- impl < T > From < BinaryHeap < T > > for Vec < T > {
1780
+ impl < T , A : Allocator > From < BinaryHeap < T , A > > for Vec < T , A > {
1669
1781
/// Converts a `BinaryHeap<T>` into a `Vec<T>`.
1670
1782
///
1671
1783
/// This conversion requires no data movement or allocation, and has
1672
1784
/// constant time complexity.
1673
- fn from ( heap : BinaryHeap < T > ) -> Vec < T > {
1785
+ fn from ( heap : BinaryHeap < T , A > ) -> Vec < T , A > {
1674
1786
heap. data
1675
1787
}
1676
1788
}
@@ -1683,9 +1795,9 @@ impl<T: Ord> FromIterator<T> for BinaryHeap<T> {
1683
1795
}
1684
1796
1685
1797
#[ stable( feature = "rust1" , since = "1.0.0" ) ]
1686
- impl < T > IntoIterator for BinaryHeap < T > {
1798
+ impl < T , A : Allocator > IntoIterator for BinaryHeap < T , A > {
1687
1799
type Item = T ;
1688
- type IntoIter = IntoIter < T > ;
1800
+ type IntoIter = IntoIter < T , A > ;
1689
1801
1690
1802
/// Creates a consuming iterator, that is, one that moves each value out of
1691
1803
/// the binary heap in arbitrary order. The binary heap cannot be used
@@ -1705,13 +1817,13 @@ impl<T> IntoIterator for BinaryHeap<T> {
1705
1817
/// println!("{x}");
1706
1818
/// }
1707
1819
/// ```
1708
- fn into_iter ( self ) -> IntoIter < T > {
1820
+ fn into_iter ( self ) -> IntoIter < T , A > {
1709
1821
IntoIter { iter : self . data . into_iter ( ) }
1710
1822
}
1711
1823
}
1712
1824
1713
1825
#[ stable( feature = "rust1" , since = "1.0.0" ) ]
1714
- impl < ' a , T > IntoIterator for & ' a BinaryHeap < T > {
1826
+ impl < ' a , T , A : Allocator > IntoIterator for & ' a BinaryHeap < T , A > {
1715
1827
type Item = & ' a T ;
1716
1828
type IntoIter = Iter < ' a , T > ;
1717
1829
@@ -1721,7 +1833,7 @@ impl<'a, T> IntoIterator for &'a BinaryHeap<T> {
1721
1833
}
1722
1834
1723
1835
#[ stable( feature = "rust1" , since = "1.0.0" ) ]
1724
- impl < T : Ord > Extend < T > for BinaryHeap < T > {
1836
+ impl < T : Ord , A : Allocator > Extend < T > for BinaryHeap < T , A > {
1725
1837
#[ inline]
1726
1838
fn extend < I : IntoIterator < Item = T > > ( & mut self , iter : I ) {
1727
1839
let guard = RebuildOnDrop { rebuild_from : self . len ( ) , heap : self } ;
@@ -1740,7 +1852,7 @@ impl<T: Ord> Extend<T> for BinaryHeap<T> {
1740
1852
}
1741
1853
1742
1854
#[ stable( feature = "extend_ref" , since = "1.2.0" ) ]
1743
- impl < ' a , T : ' a + Ord + Copy > Extend < & ' a T > for BinaryHeap < T > {
1855
+ impl < ' a , T : ' a + Ord + Copy , A : Allocator > Extend < & ' a T > for BinaryHeap < T , A > {
1744
1856
fn extend < I : IntoIterator < Item = & ' a T > > ( & mut self , iter : I ) {
1745
1857
self . extend ( iter. into_iter ( ) . cloned ( ) ) ;
1746
1858
}
0 commit comments