Skip to content

Commit 184888d

Browse files
committed
Merge branch '♻️-associated-type-bounds' into 🦆
2 parents 3ebd23c + d06f4c4 commit 184888d

File tree

11 files changed

+73
-63
lines changed

11 files changed

+73
-63
lines changed

doc/toolchain_limitations.md

Lines changed: 0 additions & 23 deletions
Original file line numberDiff line numberDiff line change
@@ -38,29 +38,6 @@ impl<T> StGeneric<for<'a> fn(&'a T)> {
3838
```
3939

4040

41-
### `[tag:trait_constraints_on_associated_types_do_not_propagate]` Trait constraints on associated types do not propagate to the trait's use sites
42-
43-
*Upstream issue:* [rust-lang/rust#32722](https://github.com/rust-lang/rust/issues/32722)
44-
45-
According to [this comment](https://github.com/rust-lang/rust/issues/32722#issuecomment-618044689), this is a symptom of [rust-lang/rust#20671](https://github.com/rust-lang/rust/issues/20671).
46-
47-
```rust,compile_fail,E0277
48-
trait KernelMutex {}
49-
50-
trait CfgBase {
51-
type System;
52-
}
53-
54-
trait CfgMutex: CfgBase
55-
where
56-
Self::System: KernelMutex,
57-
{}
58-
59-
// error[E0277]: the trait bound `<C as CfgBase>::System: KernelMutex` is not satisfied
60-
fn foo<C: CfgMutex>() {}
61-
```
62-
63-
6441
### `[tag:impl_trait_false_type_alias_bounds]` `type_alias_bounds` misfires when `impl Trait` is used in a portion of a type alias
6542

6643
*Upstream issue:* [rust-lang/rust#94395](https://github.com/rust-lang/rust/issues/94395)

src/r3/src/bind/mod.rs

Lines changed: 5 additions & 5 deletions
Original file line numberDiff line numberDiff line change
@@ -119,7 +119,7 @@
119119
/// ```
120120
)]
121121
#![doc = include_str!("../common.md")]
122-
use r3_core::kernel::{cfg, raw, raw_cfg};
122+
use r3_core::kernel::{cfg, raw_cfg};
123123

124124
pub use r3_core::bind::{
125125
fn_bind_map, Bind, BindBorrow, BindBorrowMut, BindDefiner, BindRef, BindTable, BindTake,
@@ -196,8 +196,8 @@ pub const fn bind_uninit<'pool, T, C>(
196196
) -> Bind<'pool, C::System, core::mem::MaybeUninit<T>>
197197
where
198198
T: 'static,
199-
C: ~const raw_cfg::CfgBase,
200-
C::System: raw::KernelBase + cfg::KernelStatic,
199+
// `~const CfgBase` not implied due to [ref:const_supertraits]
200+
C: ~const raw_cfg::CfgBase + ~const cfg::CfgStatic,
201201
{
202202
// Safety: `MaybeUninit` is safe to leave uninitialized
203203
unsafe { Bind::define().uninit_unchecked().finish(cfg) }
@@ -234,8 +234,8 @@ where
234234
pub const fn bind_default<'pool, T, C>(cfg: &mut cfg::Cfg<'pool, C>) -> Bind<'pool, C::System, T>
235235
where
236236
T: Default + 'static,
237-
C: ~const raw_cfg::CfgBase,
238-
C::System: raw::KernelBase + cfg::KernelStatic,
237+
// `~const CfgBase` not implied due to [ref:const_supertraits]
238+
C: ~const raw_cfg::CfgBase + ~const cfg::CfgStatic,
239239
{
240240
Bind::define().init(Default::default).finish(cfg)
241241
}

src/r3_core/CHANGELOG.md

Lines changed: 8 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -7,6 +7,14 @@ and this project adheres to [Semantic Versioning](http://semver.org/spec/v2.0.0.
77

88
## [Unreleased]
99

10+
### Added
11+
12+
- The new blanket-implemented `CfgStatic` trait can be used to simplify some trait bounds of configuration functions.
13+
14+
### Fixed
15+
16+
- The `Cfg*` traits now imply the corresponding `Kernel*` traits (e.g., `C: CfgTimer` implies `C::System: KernelTimer`), making some trait bounds in configuration functions unnecessary.
17+
1018
## [0.1.2] - 2022-03-30
1119

1220
### Changed

src/r3_core/src/bind.rs

Lines changed: 3 additions & 3 deletions
Original file line numberDiff line numberDiff line change
@@ -686,8 +686,8 @@ impl<'pool, System, T> DivideBind<'pool, System, T> {
686686
///
687687
/// const fn configure_app<C>(cfg: &mut Cfg<C>)
688688
/// where
689-
/// C: ~const traits::CfgBase,
690-
/// C::System: traits::KernelBase + traits::KernelStatic,
689+
// `~const CfgBase` not implied due to [ref:const_supertraits]
690+
/// C: ~const traits::CfgBase + ~const traits::CfgStatic,
691691
/// {
692692
/// let values = Bind::define().init(|| (12, 34)).finish(cfg);
693693
/// let (value0, value1) = values.unzip();
@@ -820,7 +820,7 @@ impl<'pool, const LEN: usize, System, T> const UnzipBind for Bind<'pool, System,
820820
/// where
821821
/// C: ~const traits::CfgBase +
822822
/// ~const traits::CfgTask,
823-
/// C::System: traits::KernelBase + traits::KernelStatic,
823+
/// C::System: traits::KernelStatic,
824824
/// {
825825
/// let foo = Bind::define().init(|| {
826826
/// // `BindTable::get()` will fail because some bindings might not

src/r3_core/src/kernel/cfg.rs

Lines changed: 48 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -725,3 +725,51 @@ macro array_item_from_fn($(
725725
unsafe { mem::transmute(values) }
726726
};
727727
)*}
728+
729+
/// A subtrait of [`raw_cfg::CfgBase`][] that implies
730+
/// `Self::System: `[`KernelStatic`][].
731+
///
732+
/// This trait by itself has no function, but it may help you keep your
733+
/// configuration functions clean by getting rid of trait bounds on
734+
/// `C::System`.
735+
///
736+
/// # Example
737+
///
738+
/// ```rust
739+
/// # #![feature(const_trait_impl)]
740+
/// # #![feature(const_mut_refs)]
741+
/// # use r3_core::kernel::traits;
742+
/// #
743+
/// const fn configure<C>(cfg: &mut C)
744+
/// where
745+
// `~const CfgBase` not implied due to [ref:const_supertraits]
746+
/// C: ~const traits::CfgBase + ~const traits::CfgStatic,
747+
/// {
748+
/// todo!()
749+
/// }
750+
/// ```
751+
///
752+
/// The above is equivalent to:
753+
///
754+
/// ```rust
755+
/// # #![feature(const_trait_impl)]
756+
/// # #![feature(const_mut_refs)]
757+
/// # use r3_core::kernel::traits;
758+
/// #
759+
/// const fn configure<C>(cfg: &mut C)
760+
/// where
761+
/// C: ~const traits::CfgBase,
762+
/// C::System: traits::KernelStatic,
763+
/// {
764+
/// todo!()
765+
/// }
766+
/// ```
767+
// The supertrait can't be `~const` due to [ref:const_supertraits]
768+
pub trait CfgStatic: raw_cfg::CfgBase<System: KernelStatic> {}
769+
770+
impl<C> const CfgStatic for C
771+
where
772+
C: ~const raw_cfg::CfgBase,
773+
C::System: KernelStatic,
774+
{
775+
}

src/r3_core/src/kernel/mod.rs

Lines changed: 1 addition & 1 deletion
Original file line numberDiff line numberDiff line change
@@ -72,7 +72,7 @@ pub mod prelude {
7272
pub mod traits {
7373
#[doc(no_inline)]
7474
pub use super::{
75-
cfg::KernelStatic,
75+
cfg::{CfgStatic, KernelStatic},
7676
event_group::{EventGroupHandle, EventGroupMethods},
7777
mutex::{MutexHandle, MutexMethods},
7878
raw::{

src/r3_core/src/kernel/mutex.rs

Lines changed: 0 additions & 3 deletions
Original file line numberDiff line numberDiff line change
@@ -54,9 +54,6 @@ define_object! {
5454
/// const fn configure<C>(cfg: &mut Cfg<C>) -> Objects<C::System>
5555
/// where
5656
/// C: ~const traits::CfgMutex,
57-
// The following bound is necessary becauase of a bug in the compiler
58-
// [ref:trait_constraints_on_associated_types_do_not_propagate]
59-
/// C::System: traits::KernelMutex,
6057
/// {
6158
/// let mutex = StaticMutex::define()
6259
/// .protocol(MutexProtocol::Ceiling(1))

src/r3_core/src/kernel/raw_cfg.rs

Lines changed: 5 additions & 20 deletions
Original file line numberDiff line numberDiff line change
@@ -116,10 +116,7 @@ pub struct TaskDescriptor<System> {
116116
/// [3]: self#stability
117117
/// [4]: self#safety
118118
// The supertrait can't be `~const` due to [ref:const_supertraits]
119-
pub unsafe trait CfgEventGroup: CfgBase
120-
where
121-
Self::System: raw::KernelEventGroup,
122-
{
119+
pub unsafe trait CfgEventGroup: CfgBase<System: raw::KernelEventGroup> {
123120
fn event_group_define<Properties: ~const Bag>(
124121
&mut self,
125122
descriptor: EventGroupDescriptor<Self::System>,
@@ -151,10 +148,7 @@ pub struct EventGroupDescriptor<System> {
151148
/// [3]: self#stability
152149
/// [4]: self#safety
153150
// The supertrait can't be `~const` due to [ref:const_supertraits]
154-
pub unsafe trait CfgMutex: CfgBase
155-
where
156-
Self::System: raw::KernelMutex,
157-
{
151+
pub unsafe trait CfgMutex: CfgBase<System: raw::KernelMutex> {
158152
fn mutex_define<Properties: ~const Bag>(
159153
&mut self,
160154
descriptor: MutexDescriptor<Self::System>,
@@ -185,10 +179,7 @@ pub struct MutexDescriptor<System> {
185179
/// [3]: self#stability
186180
/// [4]: self#safety
187181
// The supertrait can't be `~const` due to [ref:const_supertraits]
188-
pub unsafe trait CfgSemaphore: CfgBase
189-
where
190-
Self::System: raw::KernelSemaphore,
191-
{
182+
pub unsafe trait CfgSemaphore: CfgBase<System: raw::KernelSemaphore> {
192183
fn semaphore_define<Properties: ~const Bag>(
193184
&mut self,
194185
descriptor: SemaphoreDescriptor<Self::System>,
@@ -221,10 +212,7 @@ pub struct SemaphoreDescriptor<System> {
221212
/// [3]: self#stability
222213
/// [4]: self#safety
223214
// The supertrait can't be `~const` due to [ref:const_supertraits]
224-
pub unsafe trait CfgTimer: CfgBase
225-
where
226-
Self::System: raw::KernelTimer,
227-
{
215+
pub unsafe trait CfgTimer: CfgBase<System: raw::KernelTimer> {
228216
fn timer_define<Properties: ~const Bag>(
229217
&mut self,
230218
descriptor: TimerDescriptor<Self::System>,
@@ -258,10 +246,7 @@ pub struct TimerDescriptor<System> {
258246
/// [3]: self#stability
259247
/// [4]: self#safety
260248
// The supertrait can't be `~const` due to [ref:const_supertraits]
261-
pub unsafe trait CfgInterruptLine: CfgBase
262-
where
263-
Self::System: raw::KernelInterruptLine,
264-
{
249+
pub unsafe trait CfgInterruptLine: CfgBase<System: raw::KernelInterruptLine> {
265250
fn interrupt_line_define<Properties: ~const Bag>(
266251
&mut self,
267252
descriptor: InterruptLineDescriptor<Self::System>,

src/r3_core/src/kernel/timer.rs

Lines changed: 0 additions & 6 deletions
Original file line numberDiff line numberDiff line change
@@ -279,9 +279,6 @@ define_object! {
279279
/// const fn configure<C>(b: &mut Cfg<C>) -> StaticTimer<C::System>
280280
/// where
281281
/// C: ~const traits::CfgTimer,
282-
// The following bound is necessary becauase of a bug in the compiler
283-
// [ref:trait_constraints_on_associated_types_do_not_propagate]
284-
/// C::System: traits::KernelTimer,
285282
/// {
286283
/// StaticTimer::define()
287284
/// .delay(Duration::from_millis(70))
@@ -319,9 +316,6 @@ define_object! {
319316
/// const fn configure<C>(b: &mut Cfg<C>) -> StaticTimer<C::System>
320317
/// where
321318
/// C: ~const traits::CfgTimer,
322-
// The following bound is necessary becauase of a bug in the compiler
323-
// [ref:trait_constraints_on_associated_types_do_not_propagate]
324-
/// C::System: traits::KernelTimer,
325319
/// {
326320
/// StaticTimer::define()
327321
/// .active(true)

src/r3_core/src/lib.rs

Lines changed: 1 addition & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -13,6 +13,7 @@
1313
#![feature(const_precise_live_drops)]
1414
#![feature(const_raw_ptr_comparison)]
1515
#![feature(generic_associated_types)]
16+
#![feature(associated_type_bounds)]
1617
#![feature(const_slice_first_last)]
1718
#![feature(cfg_target_has_atomic)] // `#[cfg(target_has_atomic_load_store)]`
1819
#![feature(const_cell_into_inner)]

0 commit comments

Comments
 (0)