Commit c7125ba
authored
Rollup merge of rust-lang#91884 - woppopo:const_box, r=oli-obk
Constify `Box<T, A>` methods
Tracking issue: none yet
Most of the methods bounded on `~const`. `intrinsics::const_eval_select` is used for handling an allocation error.
<details><summary>Constified API</summary>
```rust
impl<T, A: Allocator> Box<T, A> {
pub const fn new_in(x: T, alloc: A) -> Self
where
A: ~const Allocator + ~const Drop;
pub const fn try_new_in(x: T, alloc: A) -> Result<Self, AllocError>
where
T: ~const Drop,
A: ~const Allocator + ~const Drop;
pub const fn new_uninit_in(alloc: A) -> Box<mem::MaybeUninit<T>, A>
where
A: ~const Allocator + ~const Drop;
pub const fn try_new_uninit_in(alloc: A) -> Result<Box<mem::MaybeUninit<T>, A>, AllocError>
where
A: ~const Allocator + ~const Drop;
pub const fn new_zeroed_in(alloc: A) -> Box<mem::MaybeUninit<T>, A>
where
A: ~const Allocator + ~const Drop;
pub const fn try_new_zeroed_in(alloc: A) -> Result<Box<mem::MaybeUninit<T>, A>, AllocError>
where
A: ~const Allocator + ~const Drop;
pub const fn pin_in(x: T, alloc: A) -> Pin<Self>
where
A: 'static,
A: 'static + ~const Allocator + ~const Drop,
pub const fn into_boxed_slice(boxed: Self) -> Box<[T], A>;
pub const fn into_inner(boxed: Self) -> T
where
Self: ~const Drop,
}
impl<T, A: Allocator> Box<MaybeUninit<T>, A> {
pub const unsafe fn assume_init(self) -> Box<T, A>;
pub const fn write(mut boxed: Self, value: T) -> Box<T, A>;
pub const unsafe fn from_raw_in(raw: *mut T, alloc: A) -> Self;
pub const fn into_raw_with_allocator(b: Self) -> (*mut T, A);
pub const fn into_unique(b: Self) -> (Unique<T>, A);
pub const fn allocator(b: &Self) -> &A;
pub const fn leak<'a>(b: Self) -> &'a mut T
where
A: 'a;
pub const fn into_pin(boxed: Self) -> Pin<Self>
where
A: 'static;
}
unsafe impl<#[may_dangle] T: ?Sized, A: Allocator> const Drop for Box<T, A>;
impl<T: ?Sized, A: Allocator> const From<Box<T, A>> for Pin<Box<T, A>>
where
A: 'static;
impl<T: ?Sized, A: Allocator> const Deref for Box<T, A>;
impl<T: ?Sized, A: Allocator> const DerefMut for Box<T, A>;
impl<T: ?Sized, A: Allocator> const Unpin for Box<T, A> where A: 'static;
```
</details>
<details><summary>Example</summary>
```rust
pub struct ConstAllocator;
unsafe impl const Allocator for ConstAllocator {
fn allocate(&self, layout: Layout) -> Result<NonNull<[u8]>, AllocError> {
unsafe {
let ptr = core::intrinsics::const_allocate(layout.size(), layout.align());
Ok(NonNull::new_unchecked(ptr as *mut [u8; 0] as *mut [u8]))
}
}
unsafe fn deallocate(&self, _ptr: NonNull<u8>, _layout: Layout) {
/* do nothing */
}
fn allocate_zeroed(&self, layout: Layout) -> Result<NonNull<[u8]>, AllocError> {
self.allocate(layout)
}
unsafe fn grow(
&self,
_ptr: NonNull<u8>,
_old_layout: Layout,
_new_layout: Layout,
) -> Result<NonNull<[u8]>, AllocError> {
unimplemented!()
}
unsafe fn grow_zeroed(
&self,
_ptr: NonNull<u8>,
_old_layout: Layout,
_new_layout: Layout,
) -> Result<NonNull<[u8]>, AllocError> {
unimplemented!()
}
unsafe fn shrink(
&self,
_ptr: NonNull<u8>,
_old_layout: Layout,
_new_layout: Layout,
) -> Result<NonNull<[u8]>, AllocError> {
unimplemented!()
}
fn by_ref(&self) -> &Self
where
Self: Sized,
{
self
}
}
#[test]
fn const_box() {
const VALUE: u32 = {
let mut boxed = Box::new_in(1u32, ConstAllocator);
assert!(*boxed == 1);
*boxed = 42;
assert!(*boxed == 42);
*boxed
};
assert!(VALUE == 42);
}
```
</details>5 files changed
+225
-32
lines changed| Original file line number | Diff line number | Diff line change | |
|---|---|---|---|
| |||
323 | 323 | | |
324 | 324 | | |
325 | 325 | | |
| 326 | + | |
326 | 327 | | |
327 | 328 | | |
328 | 329 | | |
329 | 330 | | |
330 | 331 | | |
331 | | - | |
| 332 | + | |
| 333 | + | |
| 334 | + | |
| 335 | + | |
332 | 336 | | |
333 | 337 | | |
334 | 338 | | |
335 | 339 | | |
336 | | - | |
| 340 | + | |
337 | 341 | | |
338 | 342 | | |
339 | 343 | | |
| |||
361 | 365 | | |
362 | 366 | | |
363 | 367 | | |
| 368 | + | |
364 | 369 | | |
365 | 370 | | |
366 | 371 | | |
367 | | - | |
368 | | - | |
369 | | - | |
| 372 | + | |
| 373 | + | |
| 374 | + | |
370 | 375 | | |
| 376 | + | |
| 377 | + | |
| 378 | + | |
| 379 | + | |
| 380 | + | |
| 381 | + | |
| 382 | + | |
| 383 | + | |
371 | 384 | | |
372 | 385 | | |
373 | 386 | | |
| |||
| Original file line number | Diff line number | Diff line change | |
|---|---|---|---|
| |||
346 | 346 | | |
347 | 347 | | |
348 | 348 | | |
| 349 | + | |
349 | 350 | | |
350 | 351 | | |
351 | | - | |
| 352 | + | |
| 353 | + | |
| 354 | + | |
| 355 | + | |
352 | 356 | | |
353 | 357 | | |
354 | 358 | | |
| |||
372 | 376 | | |
373 | 377 | | |
374 | 378 | | |
| 379 | + | |
375 | 380 | | |
376 | | - | |
| 381 | + | |
| 382 | + | |
| 383 | + | |
| 384 | + | |
| 385 | + | |
377 | 386 | | |
378 | 387 | | |
379 | 388 | | |
| |||
402 | 411 | | |
403 | 412 | | |
404 | 413 | | |
| 414 | + | |
405 | 415 | | |
406 | 416 | | |
407 | 417 | | |
408 | | - | |
| 418 | + | |
| 419 | + | |
| 420 | + | |
| 421 | + | |
409 | 422 | | |
410 | 423 | | |
411 | 424 | | |
| |||
439 | 452 | | |
440 | 453 | | |
441 | 454 | | |
442 | | - | |
| 455 | + | |
| 456 | + | |
| 457 | + | |
| 458 | + | |
| 459 | + | |
443 | 460 | | |
444 | 461 | | |
445 | 462 | | |
| |||
466 | 483 | | |
467 | 484 | | |
468 | 485 | | |
| 486 | + | |
469 | 487 | | |
470 | 488 | | |
471 | 489 | | |
472 | | - | |
| 490 | + | |
| 491 | + | |
| 492 | + | |
| 493 | + | |
473 | 494 | | |
474 | 495 | | |
475 | 496 | | |
| |||
503 | 524 | | |
504 | 525 | | |
505 | 526 | | |
506 | | - | |
| 527 | + | |
| 528 | + | |
| 529 | + | |
| 530 | + | |
| 531 | + | |
507 | 532 | | |
508 | 533 | | |
509 | 534 | | |
| |||
513 | 538 | | |
514 | 539 | | |
515 | 540 | | |
| 541 | + | |
516 | 542 | | |
517 | 543 | | |
518 | | - | |
| 544 | + | |
519 | 545 | | |
520 | | - | |
| 546 | + | |
521 | 547 | | |
522 | | - | |
| 548 | + | |
523 | 549 | | |
524 | 550 | | |
525 | 551 | | |
526 | 552 | | |
527 | 553 | | |
528 | 554 | | |
529 | | - | |
| 555 | + | |
| 556 | + | |
530 | 557 | | |
531 | 558 | | |
532 | 559 | | |
| |||
543 | 570 | | |
544 | 571 | | |
545 | 572 | | |
| 573 | + | |
546 | 574 | | |
547 | | - | |
| 575 | + | |
| 576 | + | |
| 577 | + | |
| 578 | + | |
548 | 579 | | |
549 | 580 | | |
550 | 581 | | |
| |||
758 | 789 | | |
759 | 790 | | |
760 | 791 | | |
| 792 | + | |
761 | 793 | | |
762 | | - | |
| 794 | + | |
763 | 795 | | |
764 | 796 | | |
765 | 797 | | |
| |||
792 | 824 | | |
793 | 825 | | |
794 | 826 | | |
| 827 | + | |
795 | 828 | | |
796 | | - | |
| 829 | + | |
797 | 830 | | |
798 | 831 | | |
799 | 832 | | |
| |||
938 | 971 | | |
939 | 972 | | |
940 | 973 | | |
| 974 | + | |
941 | 975 | | |
942 | | - | |
| 976 | + | |
943 | 977 | | |
944 | 978 | | |
945 | 979 | | |
| |||
1035 | 1069 | | |
1036 | 1070 | | |
1037 | 1071 | | |
| 1072 | + | |
1038 | 1073 | | |
1039 | | - | |
| 1074 | + | |
1040 | 1075 | | |
1041 | 1076 | | |
1042 | 1077 | | |
| |||
1046 | 1081 | | |
1047 | 1082 | | |
1048 | 1083 | | |
| 1084 | + | |
1049 | 1085 | | |
1050 | 1086 | | |
1051 | | - | |
| 1087 | + | |
1052 | 1088 | | |
1053 | 1089 | | |
1054 | 1090 | | |
| |||
1064 | 1100 | | |
1065 | 1101 | | |
1066 | 1102 | | |
| 1103 | + | |
1067 | 1104 | | |
1068 | | - | |
| 1105 | + | |
1069 | 1106 | | |
1070 | 1107 | | |
1071 | 1108 | | |
| |||
1105 | 1142 | | |
1106 | 1143 | | |
1107 | 1144 | | |
| 1145 | + | |
1108 | 1146 | | |
1109 | | - | |
| 1147 | + | |
1110 | 1148 | | |
1111 | 1149 | | |
1112 | 1150 | | |
| |||
1119 | 1157 | | |
1120 | 1158 | | |
1121 | 1159 | | |
1122 | | - | |
| 1160 | + | |
| 1161 | + | |
1123 | 1162 | | |
1124 | 1163 | | |
1125 | 1164 | | |
| |||
1131 | 1170 | | |
1132 | 1171 | | |
1133 | 1172 | | |
1134 | | - | |
| 1173 | + | |
| 1174 | + | |
1135 | 1175 | | |
1136 | 1176 | | |
1137 | 1177 | | |
| |||
1341 | 1381 | | |
1342 | 1382 | | |
1343 | 1383 | | |
1344 | | - | |
| 1384 | + | |
| 1385 | + | |
1345 | 1386 | | |
1346 | 1387 | | |
1347 | 1388 | | |
| |||
1720 | 1761 | | |
1721 | 1762 | | |
1722 | 1763 | | |
1723 | | - | |
| 1764 | + | |
| 1765 | + | |
1724 | 1766 | | |
1725 | 1767 | | |
1726 | 1768 | | |
| |||
1729 | 1771 | | |
1730 | 1772 | | |
1731 | 1773 | | |
1732 | | - | |
| 1774 | + | |
| 1775 | + | |
1733 | 1776 | | |
1734 | 1777 | | |
1735 | 1778 | | |
| |||
1908 | 1951 | | |
1909 | 1952 | | |
1910 | 1953 | | |
1911 | | - | |
| 1954 | + | |
| 1955 | + | |
1912 | 1956 | | |
1913 | 1957 | | |
1914 | 1958 | | |
| |||
| Original file line number | Diff line number | Diff line change | |
|---|---|---|---|
| |||
96 | 96 | | |
97 | 97 | | |
98 | 98 | | |
| 99 | + | |
| 100 | + | |
99 | 101 | | |
100 | 102 | | |
| 103 | + | |
| 104 | + | |
| 105 | + | |
| 106 | + | |
| 107 | + | |
| 108 | + | |
| 109 | + | |
101 | 110 | | |
| 111 | + | |
| 112 | + | |
102 | 113 | | |
103 | 114 | | |
104 | 115 | | |
| |||
134 | 145 | | |
135 | 146 | | |
136 | 147 | | |
| 148 | + | |
137 | 149 | | |
| 150 | + | |
| 151 | + | |
| 152 | + | |
138 | 153 | | |
| 154 | + | |
139 | 155 | | |
140 | 156 | | |
141 | 157 | | |
| |||
0 commit comments