@@ -1203,7 +1203,7 @@ pub fn merge_sort<T, CmpF, ElemAllocF, ElemDeallocF, RunAllocF, RunDeallocF>(
1203
1203
// `is_less` panics. When merging two sorted runs, this buffer holds a copy of the shorter run,
1204
1204
// which will always have length at most `len / 2`.
1205
1205
let buf = BufGuard :: new ( len / 2 , elem_alloc_fn, elem_dealloc_fn) ;
1206
- let buf_ptr = buf. buf_ptr ;
1206
+ let buf_ptr = buf. buf_ptr . as_ptr ( ) ;
1207
1207
1208
1208
let mut runs = RunVec :: new ( run_alloc_fn, run_dealloc_fn) ;
1209
1209
@@ -1298,7 +1298,7 @@ pub fn merge_sort<T, CmpF, ElemAllocF, ElemDeallocF, RunAllocF, RunDeallocF>(
1298
1298
where
1299
1299
ElemDeallocF : Fn ( * mut T , usize ) ,
1300
1300
{
1301
- buf_ptr : * mut T ,
1301
+ buf_ptr : ptr :: NonNull < T > ,
1302
1302
capacity : usize ,
1303
1303
elem_dealloc_fn : ElemDeallocF ,
1304
1304
}
@@ -1315,7 +1315,11 @@ pub fn merge_sort<T, CmpF, ElemAllocF, ElemDeallocF, RunAllocF, RunDeallocF>(
1315
1315
where
1316
1316
ElemAllocF : Fn ( usize ) -> * mut T ,
1317
1317
{
1318
- Self { buf_ptr : elem_alloc_fn ( len) , capacity : len, elem_dealloc_fn }
1318
+ Self {
1319
+ buf_ptr : ptr:: NonNull :: new ( elem_alloc_fn ( len) ) . unwrap ( ) ,
1320
+ capacity : len,
1321
+ elem_dealloc_fn,
1322
+ }
1319
1323
}
1320
1324
}
1321
1325
@@ -1324,7 +1328,7 @@ pub fn merge_sort<T, CmpF, ElemAllocF, ElemDeallocF, RunAllocF, RunDeallocF>(
1324
1328
ElemDeallocF : Fn ( * mut T , usize ) ,
1325
1329
{
1326
1330
fn drop ( & mut self ) {
1327
- ( self . elem_dealloc_fn ) ( self . buf_ptr , self . capacity ) ;
1331
+ ( self . elem_dealloc_fn ) ( self . buf_ptr . as_ptr ( ) , self . capacity ) ;
1328
1332
}
1329
1333
}
1330
1334
@@ -1333,7 +1337,7 @@ pub fn merge_sort<T, CmpF, ElemAllocF, ElemDeallocF, RunAllocF, RunDeallocF>(
1333
1337
RunAllocF : Fn ( usize ) -> * mut TimSortRun ,
1334
1338
RunDeallocF : Fn ( * mut TimSortRun , usize ) ,
1335
1339
{
1336
- buf_ptr : * mut TimSortRun ,
1340
+ buf_ptr : ptr :: NonNull < TimSortRun > ,
1337
1341
capacity : usize ,
1338
1342
len : usize ,
1339
1343
run_alloc_fn : RunAllocF ,
@@ -1350,7 +1354,7 @@ pub fn merge_sort<T, CmpF, ElemAllocF, ElemDeallocF, RunAllocF, RunDeallocF>(
1350
1354
const START_RUN_CAPACITY : usize = 16 ;
1351
1355
1352
1356
Self {
1353
- buf_ptr : run_alloc_fn ( START_RUN_CAPACITY ) ,
1357
+ buf_ptr : ptr :: NonNull :: new ( run_alloc_fn ( START_RUN_CAPACITY ) ) . unwrap ( ) ,
1354
1358
capacity : START_RUN_CAPACITY ,
1355
1359
len : 0 ,
1356
1360
run_alloc_fn,
@@ -1361,23 +1365,23 @@ pub fn merge_sort<T, CmpF, ElemAllocF, ElemDeallocF, RunAllocF, RunDeallocF>(
1361
1365
fn push ( & mut self , val : TimSortRun ) {
1362
1366
if self . len == self . capacity {
1363
1367
let old_capacity = self . capacity ;
1364
- let old_buf_ptr = self . buf_ptr ;
1368
+ let old_buf_ptr = self . buf_ptr . as_ptr ( ) ;
1365
1369
1366
1370
self . capacity = self . capacity * 2 ;
1367
- self . buf_ptr = ( self . run_alloc_fn ) ( self . capacity ) ;
1371
+ self . buf_ptr = ptr :: NonNull :: new ( ( self . run_alloc_fn ) ( self . capacity ) ) . unwrap ( ) ;
1368
1372
1369
1373
// SAFETY: buf_ptr new and old were correctly allocated and old_buf_ptr has
1370
1374
// old_capacity valid elements.
1371
1375
unsafe {
1372
- ptr:: copy_nonoverlapping ( old_buf_ptr, self . buf_ptr , old_capacity) ;
1376
+ ptr:: copy_nonoverlapping ( old_buf_ptr, self . buf_ptr . as_ptr ( ) , old_capacity) ;
1373
1377
}
1374
1378
1375
1379
( self . run_dealloc_fn ) ( old_buf_ptr, old_capacity) ;
1376
1380
}
1377
1381
1378
1382
// SAFETY: The invariant was just checked.
1379
1383
unsafe {
1380
- self . buf_ptr . add ( self . len ) . write ( val) ;
1384
+ self . buf_ptr . as_ptr ( ) . add ( self . len ) . write ( val) ;
1381
1385
}
1382
1386
self . len += 1 ;
1383
1387
}
@@ -1390,7 +1394,7 @@ pub fn merge_sort<T, CmpF, ElemAllocF, ElemDeallocF, RunAllocF, RunDeallocF>(
1390
1394
// SAFETY: buf_ptr needs to be valid and len invariant upheld.
1391
1395
unsafe {
1392
1396
// the place we are taking from.
1393
- let ptr = self . buf_ptr . add ( index) ;
1397
+ let ptr = self . buf_ptr . as_ptr ( ) . add ( index) ;
1394
1398
1395
1399
// Shift everything down to fill in that spot.
1396
1400
ptr:: copy ( ptr. add ( 1 ) , ptr, self . len - index - 1 ) ;
@@ -1400,7 +1404,7 @@ pub fn merge_sort<T, CmpF, ElemAllocF, ElemDeallocF, RunAllocF, RunDeallocF>(
1400
1404
1401
1405
fn as_slice ( & self ) -> & [ TimSortRun ] {
1402
1406
// SAFETY: Safe as long as buf_ptr is valid and len invariant was upheld.
1403
- unsafe { & * ptr:: slice_from_raw_parts ( self . buf_ptr , self . len ) }
1407
+ unsafe { & * ptr:: slice_from_raw_parts ( self . buf_ptr . as_ptr ( ) , self . len ) }
1404
1408
}
1405
1409
1406
1410
fn len ( & self ) -> usize {
@@ -1419,7 +1423,7 @@ pub fn merge_sort<T, CmpF, ElemAllocF, ElemDeallocF, RunAllocF, RunDeallocF>(
1419
1423
if index < self . len {
1420
1424
// SAFETY: buf_ptr and len invariant must be upheld.
1421
1425
unsafe {
1422
- return & * ( self . buf_ptr . add ( index) ) ;
1426
+ return & * ( self . buf_ptr . as_ptr ( ) . add ( index) ) ;
1423
1427
}
1424
1428
}
1425
1429
@@ -1436,7 +1440,7 @@ pub fn merge_sort<T, CmpF, ElemAllocF, ElemDeallocF, RunAllocF, RunDeallocF>(
1436
1440
if index < self . len {
1437
1441
// SAFETY: buf_ptr and len invariant must be upheld.
1438
1442
unsafe {
1439
- return & mut * ( self . buf_ptr . add ( index) ) ;
1443
+ return & mut * ( self . buf_ptr . as_ptr ( ) . add ( index) ) ;
1440
1444
}
1441
1445
}
1442
1446
@@ -1452,7 +1456,7 @@ pub fn merge_sort<T, CmpF, ElemAllocF, ElemDeallocF, RunAllocF, RunDeallocF>(
1452
1456
fn drop ( & mut self ) {
1453
1457
// As long as TimSortRun is Copy we don't need to drop them individually but just the
1454
1458
// whole allocation.
1455
- ( self . run_dealloc_fn ) ( self . buf_ptr , self . capacity ) ;
1459
+ ( self . run_dealloc_fn ) ( self . buf_ptr . as_ptr ( ) , self . capacity ) ;
1456
1460
}
1457
1461
}
1458
1462
}
0 commit comments