From 79e4d28c82bc06da43fa2f757abe822fdfd2899b Mon Sep 17 00:00:00 2001 From: yvt Date: Mon, 21 Mar 2022 20:18:33 +0900 Subject: [PATCH] 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]: https://github.com/rust-lang/rust/pull/94657 --- doc/toolchain_limitations.md | 29 -------------- src/r3_core/src/bind/sorter.rs | 14 +++---- src/r3_core/src/kernel/cfg.rs | 10 ++--- src/r3_core/src/lib.rs | 1 + src/r3_core/src/utils/binary_heap/helpers.rs | 42 +++----------------- src/r3_kernel/src/cfg/interrupt.rs | 7 +--- src/r3_kernel/src/lib.rs | 1 + 7 files changed, 19 insertions(+), 85 deletions(-) diff --git a/doc/toolchain_limitations.md b/doc/toolchain_limitations.md index 04e4992a53..7c0ad29053 100644 --- a/doc/toolchain_limitations.md +++ b/doc/toolchain_limitations.md @@ -376,21 +376,6 @@ const fn f(_: usize) {} ``` -### `[tag:const_slice_get]` `<[T]>::get` is not `const fn` - -```rust -assert!(matches!(b"a".get(0), Some(b'a'))); -assert!(matches!(b"a".get(1), None)); -``` - -```rust,compile_fail,E0015 -// `assert!` is used here due to [ref:const_assert_eq] -// `matches!` is used here due to [ref:option_const_partial_eq] -const _: () = assert!(matches!(b"a".get(0), Some(b'a'))); -const _: () = assert!(matches!(b"a".get(1), None)); -``` - - ### `[tag:const_slice_sort_unstable]` `<[T]>::sort_unstable*` is not `const fn` ```rust @@ -442,20 +427,6 @@ const _: () = { Ok::<(), ()>(()).map(identity); }; ``` -### `[tag:const_slice_get_unchecked]` `<[T]>::get_unchecked` is not `const fn` - -```rust -assert!(unsafe { *b"a".get_unchecked(0) } == b'a'); -``` - -```rust,compile_fail,E0015 -// `assert!` is used here due to [ref:const_assert_eq] -// error[E0015]: cannot call non-const fn `core::slice:::: -// get_unchecked::` in constants -const _: () = assert!(unsafe { *b"a".get_unchecked(0) } == b'a'); -``` - - ### `[tag:const_slice_iter]` `<[T]>::iter` is not `const fn` ```rust diff --git a/src/r3_core/src/bind/sorter.rs b/src/r3_core/src/bind/sorter.rs index 565ba8c2be..cc9f17194a 100644 --- a/src/r3_core/src/bind/sorter.rs +++ b/src/r3_core/src/bind/sorter.rs @@ -378,17 +378,16 @@ pub(super) const fn sort_bindings( SuccessorIterState::BindInitToBorrowingUser(bind_i, bind_user_i) => { let cb = self.cb.borrow(); let bind_users = cb.bind_users(bind_i); - // `[T]::get` is not `const fn` yet [ref:const_slice_get] - if bind_user_i < bind_users.len() { + if let Some(bind_user) = bind_users.get(bind_user_i) { self.st = SuccessorIterState::BindInitToBorrowingUser( bind_i, bind_user_i + 1, ); if matches!( - bind_users[bind_user_i].1, + bind_user.1, BindBorrowType::Borrow | BindBorrowType::BorrowMut ) { - return Some(match bind_users[bind_user_i].0 { + return Some(match bind_user.0 { BindUsage::Executable => Vertex::Executable, BindUsage::Bind(user_bind_i) => Vertex::BindInit(user_bind_i), }); @@ -434,17 +433,16 @@ pub(super) const fn sort_bindings( SuccessorIterState::BindDisownToTakingUser(bind_i, bind_user_i) => { let cb = self.cb.borrow(); let bind_users = cb.bind_users(bind_i); - // `[T]::get` is not `const fn` yet [ref:const_slice_get] - if bind_user_i < bind_users.len() { + if let Some(bind_user) = bind_users.get(bind_user_i) { self.st = SuccessorIterState::BindDisownToTakingUser(bind_i, bind_user_i + 1); if matches!( - bind_users[bind_user_i].1, + bind_user.1, BindBorrowType::Take | BindBorrowType::TakeRef | BindBorrowType::TakeMut ) { - return Some(match bind_users[bind_user_i].0 { + return Some(match bind_user.0 { BindUsage::Executable => Vertex::Executable, BindUsage::Bind(user_bind_i) => Vertex::BindInit(user_bind_i), }); diff --git a/src/r3_core/src/kernel/cfg.rs b/src/r3_core/src/kernel/cfg.rs index 7c53406bf2..0bb87f35a3 100644 --- a/src/r3_core/src/kernel/cfg.rs +++ b/src/r3_core/src/kernel/cfg.rs @@ -212,12 +212,10 @@ impl<'c, C: raw_cfg::CfgBase> Cfg<'c, C> { let mut i = 0; while i < self.interrupt_lines.len() { let interrupt_line = &self.interrupt_lines[i]; - // `<[T]>::get` is not `const fn` yet [ref:const_slice_get] - let start = if interrupt_line.num < C::System::CFG_INTERRUPT_HANDLERS.len() { - C::System::CFG_INTERRUPT_HANDLERS[interrupt_line.num] - } else { - None - }; + let start = C::System::CFG_INTERRUPT_HANDLERS + .get(interrupt_line.num) + .copied() + .flatten(); self.raw.interrupt_line_define( raw_cfg::InterruptLineDescriptor { phantom: Init::INIT, diff --git a/src/r3_core/src/lib.rs b/src/r3_core/src/lib.rs index 62c0336158..8abc6e5c3e 100644 --- a/src/r3_core/src/lib.rs +++ b/src/r3_core/src/lib.rs @@ -23,6 +23,7 @@ #![feature(maybe_uninit_slice)] #![feature(const_nonnull_new)] #![feature(const_result_drop)] +#![feature(const_slice_index)] #![feature(const_option_ext)] #![feature(const_ptr_offset)] #![feature(const_trait_impl)] diff --git a/src/r3_core/src/utils/binary_heap/helpers.rs b/src/r3_core/src/utils/binary_heap/helpers.rs index 5a6829ddd0..48fd703bea 100644 --- a/src/r3_core/src/utils/binary_heap/helpers.rs +++ b/src/r3_core/src/utils/binary_heap/helpers.rs @@ -20,7 +20,7 @@ impl<'a, T> Hole<'a, T> { pub(super) const unsafe fn new(data: &'a mut [T], pos: usize) -> Self { debug_assert!(pos < data.len()); // SAFE: pos should be inside the slice - let elt = unsafe { ptr::read(data.get_unchecked2(pos)) }; + let elt = unsafe { ptr::read(data.get_unchecked(pos)) }; Hole { data, elt: ManuallyDrop::new(elt), @@ -52,7 +52,7 @@ impl<'a, T> Hole<'a, T> { pub(super) const unsafe fn get(&self, index: usize) -> &T { debug_assert!(index != self.pos); debug_assert!(index < self.data.len()); - unsafe { self.data.get_unchecked2(index) } + unsafe { self.data.get_unchecked(index) } } /// Returns a mutable reference to the element at `index`. @@ -62,7 +62,7 @@ impl<'a, T> Hole<'a, T> { pub(super) const unsafe fn get_mut(&mut self, index: usize) -> &mut T { debug_assert!(index != self.pos); debug_assert!(index < self.data.len()); - unsafe { self.data.get_unchecked_mut2(index) } + unsafe { self.data.get_unchecked_mut(index) } } /// Move hole to new location @@ -73,8 +73,8 @@ impl<'a, T> Hole<'a, T> { debug_assert!(index != self.pos); debug_assert!(index < self.data.len()); unsafe { - let index_ptr: *const _ = self.data.get_unchecked2(index); - let hole_ptr = self.data.get_unchecked_mut2(self.pos); + let index_ptr: *const _ = self.data.get_unchecked(index); + let hole_ptr = self.data.get_unchecked_mut(self.pos); ptr::copy_nonoverlapping(index_ptr, hole_ptr, 1); } self.pos = index; @@ -87,37 +87,7 @@ impl const Drop for Hole<'_, T> { // fill the hole again unsafe { let pos = self.pos; - ptr::copy_nonoverlapping(&*self.elt, self.data.get_unchecked_mut2(pos), 1); + ptr::copy_nonoverlapping(&*self.elt, self.data.get_unchecked_mut(pos), 1); } } } - -// `[T]::get_unchecked` is not `const fn` yet [ref:const_slice_get_unchecked] -trait GetUnchecked { - type Element; - unsafe fn get_unchecked2(&self, i: usize) -> &Self::Element; -} - -impl const GetUnchecked for [Element] { - type Element = Element; - - #[inline] - unsafe fn get_unchecked2(&self, i: usize) -> &Self::Element { - unsafe { &*self.as_ptr().add(i) } - } -} - -// `[T]::get_unchecked_mut` is not `const fn` yet [ref:const_slice_get_unchecked] -trait GetUncheckedMut { - type Element; - unsafe fn get_unchecked_mut2(&mut self, i: usize) -> &mut Self::Element; -} - -impl const GetUncheckedMut for [Element] { - type Element = Element; - - #[inline] - unsafe fn get_unchecked_mut2(&mut self, i: usize) -> &mut Self::Element { - unsafe { &mut *self.as_mut_ptr().add(i) } - } -} diff --git a/src/r3_kernel/src/cfg/interrupt.rs b/src/r3_kernel/src/cfg/interrupt.rs index bb0dd2809b..78ee6459f4 100644 --- a/src/r3_kernel/src/cfg/interrupt.rs +++ b/src/r3_kernel/src/cfg/interrupt.rs @@ -112,11 +112,6 @@ impl InterruptHandlerTable { /// specified interrupt number. #[inline] pub const fn get(&self, line: InterruptNum) -> Option { - // `<[T]>::get` is not `const fn` yet [ref:const_slice_get] - if line < self.storage.len() { - self.storage[line] - } else { - None - } + self.storage.get(line).copied().flatten() } } diff --git a/src/r3_kernel/src/lib.rs b/src/r3_kernel/src/lib.rs index bba8b422c9..6b3118a14d 100644 --- a/src/r3_kernel/src/lib.rs +++ b/src/r3_kernel/src/lib.rs @@ -9,6 +9,7 @@ #![feature(generic_const_exprs)] #![feature(const_refs_to_cell)] #![feature(maybe_uninit_slice)] +#![feature(const_slice_index)] #![feature(const_option_ext)] #![feature(const_ptr_offset)] #![feature(const_trait_impl)]