Skip to content

Commit afc66ce

Browse files
committed
feat(core): use ~const in supertraits
`[ref:const_supertraits]` has been resolved by [rust-lang/rust#93429][1]. [1]: rust-lang/rust#93429
1 parent c4cfc44 commit afc66ce

File tree

88 files changed

+108
-231
lines changed

Some content is hidden

Large Commits have some content hidden by default. Use the searchbox below for content that may be hidden.

88 files changed

+108
-231
lines changed

doc/toolchain_limitations.md

Lines changed: 0 additions & 16 deletions
Original file line numberDiff line numberDiff line change
@@ -158,22 +158,6 @@ const fn clone_projection<T: ~const Trait>(p: &T::Proj) -> T::Proj {
158158
```
159159

160160

161-
### `[tag:const_supertraits]` Supertraits can't have `~const`
162-
163-
*Upstream PR:* [rust-lang/rust#93429](https://github.com/rust-lang/rust/pull/93429) might resolve this
164-
165-
```rust
166-
#![feature(const_trait_impl)]
167-
trait Trait: Clone {}
168-
```
169-
170-
```rust,compile_fail
171-
#![feature(const_trait_impl)]
172-
// error: `~const` is not allowed here
173-
trait Trait: ~const Clone {}
174-
```
175-
176-
177161
### `[tag:impl_block_const_bounds]` The trait bounds of an `impl` block can't include `~const`
178162

179163
```rust,compile_fail

examples/basic_wio_terminal/src/main.rs

Lines changed: 1 addition & 1 deletion
Original file line numberDiff line numberDiff line change
@@ -701,7 +701,7 @@ mod queue {
701701
impl<System: SupportedSystem, T: Init + Copy + Send + 'static> Queue<System, T> {
702702
pub const fn new<C>(cfg: &mut Cfg<C>) -> Self
703703
where
704-
C: ~const traits::CfgBase<System = System> + ~const traits::CfgMutex,
704+
C: ~const traits::CfgMutex<System = System>,
705705
{
706706
Self {
707707
st: StaticMutex::define()

src/r3/src/bind/mod.rs

Lines changed: 5 additions & 9 deletions
Original file line numberDiff line numberDiff line change
@@ -28,8 +28,7 @@
2828
/// # type Objects = ();
2929
/// const fn configure_app<C>(cfg: &mut Cfg<C>)
3030
/// where
31-
/// C: ~const traits::CfgBase<System = System> +
32-
/// ~const traits::CfgTask +
31+
/// C: ~const traits::CfgTask<System = System> +
3332
/// ~const traits::CfgTimer,
3433
/// {
3534
/// // Create a binding and give the timer an exclusive access
@@ -97,8 +96,7 @@
9796
/// # type Objects = ();
9897
/// const fn configure_app<C>(cfg: &mut Cfg<C>)
9998
/// where
100-
/// C: ~const traits::CfgBase<System = System> +
101-
/// ~const traits::CfgTask +
99+
/// C: ~const traits::CfgTask<System = System> +
102100
/// ~const traits::CfgTimer,
103101
/// {
104102
/// let count = bind((), || 0).finish(cfg);
@@ -119,7 +117,7 @@
119117
/// ```
120118
)]
121119
#![doc = include_str!("../common.md")]
122-
use r3_core::kernel::{cfg, raw_cfg};
120+
use r3_core::kernel::cfg;
123121

124122
pub use r3_core::bind::{
125123
fn_bind_map, Bind, BindBorrow, BindBorrowMut, BindDefiner, BindRef, BindTable, BindTake,
@@ -196,8 +194,7 @@ pub const fn bind_uninit<'pool, T, C>(
196194
) -> Bind<'pool, C::System, core::mem::MaybeUninit<T>>
197195
where
198196
T: 'static,
199-
// `~const CfgBase` not implied due to [ref:const_supertraits]
200-
C: ~const raw_cfg::CfgBase + ~const cfg::CfgStatic,
197+
C: ~const cfg::CfgStatic,
201198
{
202199
// Safety: `MaybeUninit` is safe to leave uninitialized
203200
unsafe { Bind::define().uninit_unchecked().finish(cfg) }
@@ -234,8 +231,7 @@ where
234231
pub const fn bind_default<'pool, T, C>(cfg: &mut cfg::Cfg<'pool, C>) -> Bind<'pool, C::System, T>
235232
where
236233
T: Default + 'static,
237-
// `~const CfgBase` not implied due to [ref:const_supertraits]
238-
C: ~const raw_cfg::CfgBase + ~const cfg::CfgStatic,
234+
C: ~const cfg::CfgStatic,
239235
{
240236
Bind::define().init(Default::default).finish(cfg)
241237
}

src/r3/src/sync/mutex.rs

Lines changed: 2 additions & 5 deletions
Original file line numberDiff line numberDiff line change
@@ -62,8 +62,7 @@ pub struct GenericMutex<Cell, Mutex> {
6262
///
6363
/// const fn configure_app<C>(cfg: &mut Cfg<C>) -> Objects
6464
/// where
65-
/// C: ~const traits::CfgBase<System = System> +
66-
/// ~const traits::CfgTask +
65+
/// C: ~const traits::CfgTask<System = System> +
6766
/// ~const traits::CfgMutex,
6867
/// {
6968
/// StaticTask::define()
@@ -272,9 +271,7 @@ where
272271
System: traits::KernelMutex + traits::KernelStatic,
273272
{
274273
/// Complete the definition of a mutex, returning a reference to the mutex.
275-
// `CfgMutex` can't have `~const CfgBase` as a supertrait because of
276-
// [ref:const_supertraits], hence we need to specify `~const CfgBase` here
277-
pub const fn finish<C: ~const traits::CfgMutex<System = System> + ~const traits::CfgBase>(
274+
pub const fn finish<C: ~const traits::CfgMutex<System = System>>(
278275
self,
279276
cfg: &mut Cfg<C>,
280277
) -> StaticMutex<System, Source::Target>

src/r3/src/sync/recursive_mutex.rs

Lines changed: 2 additions & 5 deletions
Original file line numberDiff line numberDiff line change
@@ -56,8 +56,7 @@ pub struct GenericRecursiveMutex<Cell, Mutex> {
5656
///
5757
/// const fn configure_app<C>(cfg: &mut Cfg<C>) -> Objects
5858
/// where
59-
/// C: ~const traits::CfgBase<System = System> +
60-
/// ~const traits::CfgTask +
59+
/// C: ~const traits::CfgTask<System = System> +
6160
/// ~const traits::CfgMutex,
6261
/// {
6362
/// StaticTask::define()
@@ -310,9 +309,7 @@ where
310309
System: traits::KernelMutex + traits::KernelStatic,
311310
{
312311
/// Complete the definition of a mutex, returning a reference to the mutex.
313-
// `CfgMutex` can't have `~const CfgBase` as a supertrait because of
314-
// [ref:const_supertraits], hence we need to specify `~const CfgBase` here
315-
pub const fn finish<C: ~const traits::CfgMutex<System = System> + ~const traits::CfgBase, T>(
312+
pub const fn finish<C: ~const traits::CfgMutex<System = System>, T>(
316313
self,
317314
cfg: &mut Cfg<C>,
318315
) -> StaticRecursiveMutex<System, T>

src/r3_core/CHANGELOG.md

Lines changed: 1 addition & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -10,6 +10,7 @@ and this project adheres to [Semantic Versioning](http://semver.org/spec/v2.0.0.
1010
### Added
1111

1212
- The new blanket-implemented `CfgStatic` trait can be used to simplify some trait bounds of configuration functions.
13+
- The `Cfg*` traits now include `~const` in their supertraits ([rust-lang/rust#93429](https://github.com/rust-lang/rust/pull/93429)), making `~const CfgBase` trait bound unnecessary if it's implied by others.
1314

1415
### Fixed
1516

src/r3_core/src/bind.rs

Lines changed: 2 additions & 4 deletions
Original file line numberDiff line numberDiff line change
@@ -686,8 +686,7 @@ impl<'pool, System, T> DivideBind<'pool, System, T> {
686686
///
687687
/// const fn configure_app<C>(cfg: &mut Cfg<C>)
688688
/// where
689-
// `~const CfgBase` not implied due to [ref:const_supertraits]
690-
/// C: ~const traits::CfgBase + ~const traits::CfgStatic,
689+
/// C: ~const traits::CfgStatic,
691690
/// {
692691
/// let values = Bind::define().init(|| (12, 34)).finish(cfg);
693692
/// let (value0, value1) = values.unzip();
@@ -818,8 +817,7 @@ impl<'pool, const LEN: usize, System, T> const UnzipBind for Bind<'pool, System,
818817
///
819818
/// const fn configure_app<C>(cfg: &mut Cfg<C>)
820819
/// where
821-
/// C: ~const traits::CfgBase +
822-
/// ~const traits::CfgTask,
820+
/// C: ~const traits::CfgTask,
823821
/// C::System: traits::KernelStatic,
824822
/// {
825823
/// let foo = Bind::define().init(|| {

src/r3_core/src/bind/sorter.rs

Lines changed: 4 additions & 6 deletions
Original file line numberDiff line numberDiff line change
@@ -51,7 +51,7 @@
5151
// restricted to "comptime" anymore
5252
use core::{
5353
marker::Destruct,
54-
ops::{Deref, DerefMut, Index, IndexMut},
54+
ops::{Index, IndexMut},
5555
};
5656

5757
use super::{BindBorrowType, BindUsage};
@@ -149,8 +149,8 @@ pub(super) const fn sort_bindings<Callback, SorterUseInfoList, VertexList>(
149149
temp_vertices: &mut VertexList,
150150
) where
151151
Callback: ~const SorterCallback,
152-
SorterUseInfoList: ~const VecLike<Element = SorterUseInfo> + ~const Deref + ~const DerefMut,
153-
VertexList: ~const VecLike<Element = Vertex> + ~const Deref + ~const DerefMut,
152+
SorterUseInfoList: ~const VecLike<Element = SorterUseInfo>,
153+
VertexList: ~const VecLike<Element = Vertex>,
154154
{
155155
// Preconditions
156156
let num_binds = cb.num_binds();
@@ -605,9 +605,7 @@ where
605605
Graph::SuccessorIter<'a>: ~const MyIterator + ~const Destruct,
606606
VertexRef: Copy,
607607
VertexRefLessThan: ~const FnMut(&VertexRef, &VertexRef) -> bool,
608-
// `~const Deref[Mut]` isn't implied because of
609-
// [ref:veclike_const_supertrait]
610-
ReadyVertexQueue: ~const VecLike<Element = VertexRef> + ~const Deref + ~const DerefMut,
608+
ReadyVertexQueue: ~const VecLike<Element = VertexRef>,
611609
for<'index> VertexInfoMap: ~const Index<&'index VertexRef, Output = TopologicalSortVertexInfo>
612610
+ ~const IndexMut<&'index VertexRef>,
613611
OutputSink: ~const TopologicalSortOutputSink<VertexRef>,

src/r3_core/src/kernel/cfg.rs

Lines changed: 2 additions & 4 deletions
Original file line numberDiff line numberDiff line change
@@ -742,8 +742,7 @@ macro array_item_from_fn($(
742742
/// #
743743
/// const fn configure<C>(cfg: &mut C)
744744
/// where
745-
// `~const CfgBase` not implied due to [ref:const_supertraits]
746-
/// C: ~const traits::CfgBase + ~const traits::CfgStatic,
745+
/// C: ~const traits::CfgStatic,
747746
/// {
748747
/// todo!()
749748
/// }
@@ -764,8 +763,7 @@ macro array_item_from_fn($(
764763
/// todo!()
765764
/// }
766765
/// ```
767-
// The supertrait can't be `~const` due to [ref:const_supertraits]
768-
pub trait CfgStatic: raw_cfg::CfgBase<System: KernelStatic> {}
766+
pub trait CfgStatic: ~const raw_cfg::CfgBase<System: KernelStatic> {}
769767

770768
impl<C> const CfgStatic for C
771769
where

src/r3_core/src/kernel/raw_cfg.rs

Lines changed: 6 additions & 12 deletions
Original file line numberDiff line numberDiff line change
@@ -81,8 +81,7 @@ pub unsafe trait CfgBase {
8181
/// [2]: crate::kernel::cfg::KernelStatic
8282
/// [3]: self#stability
8383
/// [4]: self#safety
84-
// The supertrait can't be `~const` due to [ref:const_supertraits]
85-
pub unsafe trait CfgTask: CfgBase {
84+
pub unsafe trait CfgTask: ~const CfgBase {
8685
fn task_define<Properties: ~const Bag>(
8786
&mut self,
8887
descriptor: TaskDescriptor<Self::System>,
@@ -115,8 +114,7 @@ pub struct TaskDescriptor<System> {
115114
/// [2]: crate::kernel::cfg::KernelStatic
116115
/// [3]: self#stability
117116
/// [4]: self#safety
118-
// The supertrait can't be `~const` due to [ref:const_supertraits]
119-
pub unsafe trait CfgEventGroup: CfgBase<System: raw::KernelEventGroup> {
117+
pub unsafe trait CfgEventGroup: ~const CfgBase<System: raw::KernelEventGroup> {
120118
fn event_group_define<Properties: ~const Bag>(
121119
&mut self,
122120
descriptor: EventGroupDescriptor<Self::System>,
@@ -147,8 +145,7 @@ pub struct EventGroupDescriptor<System> {
147145
/// [2]: crate::kernel::cfg::KernelStatic
148146
/// [3]: self#stability
149147
/// [4]: self#safety
150-
// The supertrait can't be `~const` due to [ref:const_supertraits]
151-
pub unsafe trait CfgMutex: CfgBase<System: raw::KernelMutex> {
148+
pub unsafe trait CfgMutex: ~const CfgBase<System: raw::KernelMutex> {
152149
fn mutex_define<Properties: ~const Bag>(
153150
&mut self,
154151
descriptor: MutexDescriptor<Self::System>,
@@ -178,8 +175,7 @@ pub struct MutexDescriptor<System> {
178175
/// [2]: crate::kernel::cfg::KernelStatic
179176
/// [3]: self#stability
180177
/// [4]: self#safety
181-
// The supertrait can't be `~const` due to [ref:const_supertraits]
182-
pub unsafe trait CfgSemaphore: CfgBase<System: raw::KernelSemaphore> {
178+
pub unsafe trait CfgSemaphore: ~const CfgBase<System: raw::KernelSemaphore> {
183179
fn semaphore_define<Properties: ~const Bag>(
184180
&mut self,
185181
descriptor: SemaphoreDescriptor<Self::System>,
@@ -211,8 +207,7 @@ pub struct SemaphoreDescriptor<System> {
211207
/// [2]: crate::kernel::cfg::KernelStatic
212208
/// [3]: self#stability
213209
/// [4]: self#safety
214-
// The supertrait can't be `~const` due to [ref:const_supertraits]
215-
pub unsafe trait CfgTimer: CfgBase<System: raw::KernelTimer> {
210+
pub unsafe trait CfgTimer: ~const CfgBase<System: raw::KernelTimer> {
216211
fn timer_define<Properties: ~const Bag>(
217212
&mut self,
218213
descriptor: TimerDescriptor<Self::System>,
@@ -245,8 +240,7 @@ pub struct TimerDescriptor<System> {
245240
/// [2]: crate::kernel::cfg::KernelStatic
246241
/// [3]: self#stability
247242
/// [4]: self#safety
248-
// The supertrait can't be `~const` due to [ref:const_supertraits]
249-
pub unsafe trait CfgInterruptLine: CfgBase<System: raw::KernelInterruptLine> {
243+
pub unsafe trait CfgInterruptLine: ~const CfgBase<System: raw::KernelInterruptLine> {
250244
fn interrupt_line_define<Properties: ~const Bag>(
251245
&mut self,
252246
descriptor: InterruptLineDescriptor<Self::System>,

src/r3_core/src/lib.md

Lines changed: 4 additions & 4 deletions
Original file line numberDiff line numberDiff line change
@@ -81,8 +81,8 @@ const COTTAGE: Objects = r3_kernel::build!(SystemTraits, configure_app => Object
8181
// This is the top-level configuration function
8282
const fn configure_app<C>(b: &mut Cfg<C>) -> Objects
8383
where
84-
C: ~const traits::CfgBase<System = System>
85-
+ ~const traits::CfgTask,
84+
// `C: ~const CfgBase` is implied by `CfgTask`'s supertrait
85+
C: ~const traits::CfgTask<System = System>,
8686
{
8787
b.num_task_priority_levels(4);
8888
let task = StaticTask::define()
@@ -110,7 +110,7 @@ Configuration functions are highly composable as they can make nested calls to o
110110
// Top-level configuration function
111111
const fn configure_app<C>(b: &mut Cfg<C>) -> Objects<C::System>
112112
where
113-
C: ~const traits::CfgBase + ~const traits::CfgTask,
113+
C: ~const traits::CfgTask,
114114
{
115115
b.num_task_priority_levels(4);
116116
let my_module = m::configure(b);
@@ -122,7 +122,7 @@ mod m {
122122
# pub struct MyModule<System: traits::KernelBase> { task: StaticTask<System> }
123123
pub const fn configure<C>(b: &mut Cfg<C>) -> MyModule<C::System>
124124
where
125-
C: ~const traits::CfgBase + ~const traits::CfgTask,
125+
C: ~const traits::CfgTask,
126126
{
127127
let task = StaticTask::define()
128128
.start(task_body).priority(3).active(true).finish(b);

src/r3_core/src/utils/binary_heap/mod.rs

Lines changed: 1 addition & 3 deletions
Original file line numberDiff line numberDiff line change
@@ -48,9 +48,7 @@ pub trait BinaryHeap: VecLike {
4848

4949
impl<T> const BinaryHeap for T
5050
where
51-
// `~const Deref` isn't implied because of
52-
// [ref:veclike_const_supertrait]
53-
T: ~const VecLike + ~const core::ops::Deref + ~const core::ops::DerefMut,
51+
T: ~const VecLike,
5452
T::Element: ~const Destruct,
5553
{
5654
fn heap_pop<Ctx>(&mut self, ctx: Ctx) -> Option<Self::Element>

src/r3_core/src/utils/binary_heap/veclike.rs

Lines changed: 3 additions & 3 deletions
Original file line numberDiff line numberDiff line change
@@ -1,9 +1,9 @@
11
use arrayvec::ArrayVec;
22
use core::ops;
33

4-
// [tag:veclike_const_supertrait] Can't specify `~const Deref` due to
5-
// [ref:const_supertraits]
6-
pub trait VecLike: ops::Deref<Target = [<Self as VecLike>::Element]> + ops::DerefMut {
4+
pub trait VecLike:
5+
~const ops::Deref<Target = [<Self as VecLike>::Element]> + ~const ops::DerefMut
6+
{
77
type Element;
88
fn is_empty(&self) -> bool;
99
fn len(&self) -> usize;

src/r3_port_arm_m/src/systick_tickful/cfg.rs

Lines changed: 1 addition & 2 deletions
Original file line numberDiff line numberDiff line change
@@ -100,8 +100,7 @@ macro_rules! use_systick_tickful {
100100
impl $Traits {
101101
pub const fn configure_systick<C>(b: &mut Cfg<C>)
102102
where
103-
C: ~const traits::CfgBase<System = System<Self>>
104-
+ ~const traits::CfgInterruptLine,
103+
C: ~const traits::CfgInterruptLine<System = System<Self>>,
105104
{
106105
imp::configure(b);
107106
}

src/r3_port_arm_m/src/systick_tickful/imp.rs

Lines changed: 1 addition & 1 deletion
Original file line numberDiff line numberDiff line change
@@ -39,7 +39,7 @@ pub unsafe trait SysTickTickfulInstance: KernelTraits + SysTickOptions {
3939
/// The configuration function.
4040
pub const fn configure<C, Traits: SysTickTickfulInstance>(b: &mut Cfg<C>)
4141
where
42-
C: ~const traits::CfgBase<System = System<Traits>> + ~const traits::CfgInterruptLine,
42+
C: ~const traits::CfgInterruptLine<System = System<Traits>>,
4343
{
4444
InterruptLine::define()
4545
.line(INTERRUPT_SYSTICK)

src/r3_port_arm_m_test_driver/src/board_rp2040.rs

Lines changed: 1 addition & 1 deletion
Original file line numberDiff line numberDiff line change
@@ -63,7 +63,7 @@ impl log::Log for Logger {
6363

6464
pub const fn configure<C>(b: &mut Cfg<C>)
6565
where
66-
C: ~const traits::CfgBase + ~const traits::CfgInterruptLine,
66+
C: ~const traits::CfgInterruptLine,
6767
C::System: traits::KernelInterruptLine + traits::KernelStatic,
6868
{
6969
let (rp2040_resets, rp2040_usbctrl_regs) = bind((), || {

src/r3_port_riscv_test_driver/src/driver_kernel_tests/execute_lr_sc.rs

Lines changed: 1 addition & 1 deletion
Original file line numberDiff line numberDiff line change
@@ -12,7 +12,7 @@ pub struct App<System> {
1212
impl<System: traits::KernelBase> App<System> {
1313
pub const fn new<C, D: Driver<Self>>(b: &mut Cfg<C>) -> Self
1414
where
15-
C: ~const traits::CfgBase<System = System> + ~const traits::CfgTask,
15+
C: ~const traits::CfgTask<System = System>,
1616
{
1717
StartupHook::define()
1818
.start(startup_hook_body::<System, D>)

src/r3_port_std/tests/kernel_tests/external_interrupt.rs

Lines changed: 1 addition & 3 deletions
Original file line numberDiff line numberDiff line change
@@ -24,9 +24,7 @@ pub struct App<System: traits::KernelBase + traits::KernelInterruptLine + traits
2424
impl<Traits: SupportedSystemTraits> App<System<Traits>> {
2525
pub const fn new<C, D: Driver<Self>>(b: &mut Cfg<C>) -> Self
2626
where
27-
C: ~const traits::CfgBase<System = System<Traits>>
28-
+ ~const traits::CfgInterruptLine
29-
+ ~const traits::CfgTask,
27+
C: ~const traits::CfgTask<System = System<Traits>> + ~const traits::CfgInterruptLine,
3028
{
3129
StaticTask::define()
3230
.start(task_body1::<Traits, D>)

src/r3_port_std/tests/kernel_tests/interrupt_table_sparsity.rs

Lines changed: 1 addition & 1 deletion
Original file line numberDiff line numberDiff line change
@@ -18,7 +18,7 @@ pub struct App<System> {
1818
impl<Traits: SupportedSystemTraits> App<System<Traits>> {
1919
pub const fn new<C, D: Driver<Self>>(b: &mut Cfg<C>) -> Self
2020
where
21-
C: ~const traits::CfgBase<System = System<Traits>> + ~const traits::CfgInterruptLine,
21+
C: ~const traits::CfgInterruptLine<System = System<Traits>>,
2222
{
2323
StartupHook::define()
2424
.start(hook_body::<Traits, D>)

src/r3_port_std/tests/kernel_tests/stack_align.rs

Lines changed: 1 addition & 1 deletion
Original file line numberDiff line numberDiff line change
@@ -17,7 +17,7 @@ pub struct App<System> {
1717
impl<Traits: SupportedSystemTraits> App<System<Traits>> {
1818
pub const fn new<C, D: Driver<Self>>(b: &mut Cfg<C>) -> Self
1919
where
20-
C: ~const traits::CfgBase<System = System<Traits>> + ~const traits::CfgTask,
20+
C: ~const traits::CfgTask<System = System<Traits>>,
2121
{
2222
StaticTask::define()
2323
.start(task_body::<Traits, D>)

src/r3_support_rp2040/src/usbstdio.rs

Lines changed: 1 addition & 2 deletions
Original file line numberDiff line numberDiff line change
@@ -79,8 +79,7 @@ pub const fn configure<'pool, C, TOptions: Options>(
7979
rp2040_resets: Bind<'pool, C::System, rp2040_pac::RESETS>,
8080
rp2040_usbctrl_regs: Bind<'pool, C::System, rp2040_pac::USBCTRL_REGS>,
8181
) where
82-
// `~const CfgBase` not implied due to [ref:const_supertraits]
83-
C: ~const traits::CfgBase + ~const traits::CfgStatic + ~const traits::CfgInterruptLine,
82+
C: ~const traits::CfgStatic + ~const traits::CfgInterruptLine,
8483
{
8584
bind(
8685
(rp2040_resets.borrow_mut(), rp2040_usbctrl_regs.take()),

0 commit comments

Comments
 (0)