Skip to content

Commit 79e4d28

Browse files
committed
refactor: use #![feature(const_slice_index)]
This feature, which covers the `const fn` version of `<[T]>:: get[_unchecked][_mut]`, was added by [rust-lang/rust#94657][1]. [1]: rust-lang/rust#94657
1 parent 630ba7e commit 79e4d28

File tree

7 files changed

+19
-85
lines changed

7 files changed

+19
-85
lines changed

doc/toolchain_limitations.md

Lines changed: 0 additions & 29 deletions
Original file line numberDiff line numberDiff line change
@@ -376,21 +376,6 @@ const fn f(_: usize) {}
376376
```
377377

378378

379-
### `[tag:const_slice_get]` `<[T]>::get` is not `const fn`
380-
381-
```rust
382-
assert!(matches!(b"a".get(0), Some(b'a')));
383-
assert!(matches!(b"a".get(1), None));
384-
```
385-
386-
```rust,compile_fail,E0015
387-
// `assert!` is used here due to [ref:const_assert_eq]
388-
// `matches!` is used here due to [ref:option_const_partial_eq]
389-
const _: () = assert!(matches!(b"a".get(0), Some(b'a')));
390-
const _: () = assert!(matches!(b"a".get(1), None));
391-
```
392-
393-
394379
### `[tag:const_slice_sort_unstable]` `<[T]>::sort_unstable*` is not `const fn`
395380

396381
```rust
@@ -442,20 +427,6 @@ const _: () = { Ok::<(), ()>(()).map(identity); };
442427
```
443428

444429

445-
### `[tag:const_slice_get_unchecked]` `<[T]>::get_unchecked` is not `const fn`
446-
447-
```rust
448-
assert!(unsafe { *b"a".get_unchecked(0) } == b'a');
449-
```
450-
451-
```rust,compile_fail,E0015
452-
// `assert!` is used here due to [ref:const_assert_eq]
453-
// error[E0015]: cannot call non-const fn `core::slice::<impl [u8]>::
454-
// get_unchecked::<usize>` in constants
455-
const _: () = assert!(unsafe { *b"a".get_unchecked(0) } == b'a');
456-
```
457-
458-
459430
### `[tag:const_slice_iter]` `<[T]>::iter` is not `const fn`
460431

461432
```rust

src/r3_core/src/bind/sorter.rs

Lines changed: 6 additions & 8 deletions
Original file line numberDiff line numberDiff line change
@@ -378,17 +378,16 @@ pub(super) const fn sort_bindings<Callback, SorterUseInfoList, VertexList>(
378378
SuccessorIterState::BindInitToBorrowingUser(bind_i, bind_user_i) => {
379379
let cb = self.cb.borrow();
380380
let bind_users = cb.bind_users(bind_i);
381-
// `[T]::get` is not `const fn` yet [ref:const_slice_get]
382-
if bind_user_i < bind_users.len() {
381+
if let Some(bind_user) = bind_users.get(bind_user_i) {
383382
self.st = SuccessorIterState::BindInitToBorrowingUser(
384383
bind_i,
385384
bind_user_i + 1,
386385
);
387386
if matches!(
388-
bind_users[bind_user_i].1,
387+
bind_user.1,
389388
BindBorrowType::Borrow | BindBorrowType::BorrowMut
390389
) {
391-
return Some(match bind_users[bind_user_i].0 {
390+
return Some(match bind_user.0 {
392391
BindUsage::Executable => Vertex::Executable,
393392
BindUsage::Bind(user_bind_i) => Vertex::BindInit(user_bind_i),
394393
});
@@ -434,17 +433,16 @@ pub(super) const fn sort_bindings<Callback, SorterUseInfoList, VertexList>(
434433
SuccessorIterState::BindDisownToTakingUser(bind_i, bind_user_i) => {
435434
let cb = self.cb.borrow();
436435
let bind_users = cb.bind_users(bind_i);
437-
// `[T]::get` is not `const fn` yet [ref:const_slice_get]
438-
if bind_user_i < bind_users.len() {
436+
if let Some(bind_user) = bind_users.get(bind_user_i) {
439437
self.st =
440438
SuccessorIterState::BindDisownToTakingUser(bind_i, bind_user_i + 1);
441439
if matches!(
442-
bind_users[bind_user_i].1,
440+
bind_user.1,
443441
BindBorrowType::Take
444442
| BindBorrowType::TakeRef
445443
| BindBorrowType::TakeMut
446444
) {
447-
return Some(match bind_users[bind_user_i].0 {
445+
return Some(match bind_user.0 {
448446
BindUsage::Executable => Vertex::Executable,
449447
BindUsage::Bind(user_bind_i) => Vertex::BindInit(user_bind_i),
450448
});

src/r3_core/src/kernel/cfg.rs

Lines changed: 4 additions & 6 deletions
Original file line numberDiff line numberDiff line change
@@ -212,12 +212,10 @@ impl<'c, C: raw_cfg::CfgBase> Cfg<'c, C> {
212212
let mut i = 0;
213213
while i < self.interrupt_lines.len() {
214214
let interrupt_line = &self.interrupt_lines[i];
215-
// `<[T]>::get` is not `const fn` yet [ref:const_slice_get]
216-
let start = if interrupt_line.num < C::System::CFG_INTERRUPT_HANDLERS.len() {
217-
C::System::CFG_INTERRUPT_HANDLERS[interrupt_line.num]
218-
} else {
219-
None
220-
};
215+
let start = C::System::CFG_INTERRUPT_HANDLERS
216+
.get(interrupt_line.num)
217+
.copied()
218+
.flatten();
221219
self.raw.interrupt_line_define(
222220
raw_cfg::InterruptLineDescriptor {
223221
phantom: Init::INIT,

src/r3_core/src/lib.rs

Lines changed: 1 addition & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -23,6 +23,7 @@
2323
#![feature(maybe_uninit_slice)]
2424
#![feature(const_nonnull_new)]
2525
#![feature(const_result_drop)]
26+
#![feature(const_slice_index)]
2627
#![feature(const_option_ext)]
2728
#![feature(const_ptr_offset)]
2829
#![feature(const_trait_impl)]

src/r3_core/src/utils/binary_heap/helpers.rs

Lines changed: 6 additions & 36 deletions
Original file line numberDiff line numberDiff line change
@@ -20,7 +20,7 @@ impl<'a, T> Hole<'a, T> {
2020
pub(super) const unsafe fn new(data: &'a mut [T], pos: usize) -> Self {
2121
debug_assert!(pos < data.len());
2222
// SAFE: pos should be inside the slice
23-
let elt = unsafe { ptr::read(data.get_unchecked2(pos)) };
23+
let elt = unsafe { ptr::read(data.get_unchecked(pos)) };
2424
Hole {
2525
data,
2626
elt: ManuallyDrop::new(elt),
@@ -52,7 +52,7 @@ impl<'a, T> Hole<'a, T> {
5252
pub(super) const unsafe fn get(&self, index: usize) -> &T {
5353
debug_assert!(index != self.pos);
5454
debug_assert!(index < self.data.len());
55-
unsafe { self.data.get_unchecked2(index) }
55+
unsafe { self.data.get_unchecked(index) }
5656
}
5757

5858
/// Returns a mutable reference to the element at `index`.
@@ -62,7 +62,7 @@ impl<'a, T> Hole<'a, T> {
6262
pub(super) const unsafe fn get_mut(&mut self, index: usize) -> &mut T {
6363
debug_assert!(index != self.pos);
6464
debug_assert!(index < self.data.len());
65-
unsafe { self.data.get_unchecked_mut2(index) }
65+
unsafe { self.data.get_unchecked_mut(index) }
6666
}
6767

6868
/// Move hole to new location
@@ -73,8 +73,8 @@ impl<'a, T> Hole<'a, T> {
7373
debug_assert!(index != self.pos);
7474
debug_assert!(index < self.data.len());
7575
unsafe {
76-
let index_ptr: *const _ = self.data.get_unchecked2(index);
77-
let hole_ptr = self.data.get_unchecked_mut2(self.pos);
76+
let index_ptr: *const _ = self.data.get_unchecked(index);
77+
let hole_ptr = self.data.get_unchecked_mut(self.pos);
7878
ptr::copy_nonoverlapping(index_ptr, hole_ptr, 1);
7979
}
8080
self.pos = index;
@@ -87,37 +87,7 @@ impl<T> const Drop for Hole<'_, T> {
8787
// fill the hole again
8888
unsafe {
8989
let pos = self.pos;
90-
ptr::copy_nonoverlapping(&*self.elt, self.data.get_unchecked_mut2(pos), 1);
90+
ptr::copy_nonoverlapping(&*self.elt, self.data.get_unchecked_mut(pos), 1);
9191
}
9292
}
9393
}
94-
95-
// `[T]::get_unchecked<usize>` is not `const fn` yet [ref:const_slice_get_unchecked]
96-
trait GetUnchecked {
97-
type Element;
98-
unsafe fn get_unchecked2(&self, i: usize) -> &Self::Element;
99-
}
100-
101-
impl<Element> const GetUnchecked for [Element] {
102-
type Element = Element;
103-
104-
#[inline]
105-
unsafe fn get_unchecked2(&self, i: usize) -> &Self::Element {
106-
unsafe { &*self.as_ptr().add(i) }
107-
}
108-
}
109-
110-
// `[T]::get_unchecked_mut<usize>` is not `const fn` yet [ref:const_slice_get_unchecked]
111-
trait GetUncheckedMut {
112-
type Element;
113-
unsafe fn get_unchecked_mut2(&mut self, i: usize) -> &mut Self::Element;
114-
}
115-
116-
impl<Element> const GetUncheckedMut for [Element] {
117-
type Element = Element;
118-
119-
#[inline]
120-
unsafe fn get_unchecked_mut2(&mut self, i: usize) -> &mut Self::Element {
121-
unsafe { &mut *self.as_mut_ptr().add(i) }
122-
}
123-
}

src/r3_kernel/src/cfg/interrupt.rs

Lines changed: 1 addition & 6 deletions
Original file line numberDiff line numberDiff line change
@@ -112,11 +112,6 @@ impl InterruptHandlerTable {
112112
/// specified interrupt number.
113113
#[inline]
114114
pub const fn get(&self, line: InterruptNum) -> Option<InterruptHandlerFn> {
115-
// `<[T]>::get` is not `const fn` yet [ref:const_slice_get]
116-
if line < self.storage.len() {
117-
self.storage[line]
118-
} else {
119-
None
120-
}
115+
self.storage.get(line).copied().flatten()
121116
}
122117
}

src/r3_kernel/src/lib.rs

Lines changed: 1 addition & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -9,6 +9,7 @@
99
#![feature(generic_const_exprs)]
1010
#![feature(const_refs_to_cell)]
1111
#![feature(maybe_uninit_slice)]
12+
#![feature(const_slice_index)]
1213
#![feature(const_option_ext)]
1314
#![feature(const_ptr_offset)]
1415
#![feature(const_trait_impl)]

0 commit comments

Comments
 (0)