Skip to content

Commit d06f4c4

Browse files
committed
feat(core): add CfgStatic
> The new blanket-implemented `CfgStatic` trait can be used to simplify > some trait bounds of configuration functions.
1 parent d564393 commit d06f4c4

File tree

6 files changed

+62
-10
lines changed

6 files changed

+62
-10
lines changed

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: 4 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -7,6 +7,10 @@ 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+
1014
### Fixed
1115

1216
- 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.

src/r3_core/src/bind.rs

Lines changed: 2 additions & 2 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();

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_support_rp2040/src/usbstdio.rs

Lines changed: 2 additions & 2 deletions
Original file line numberDiff line numberDiff line change
@@ -79,8 +79,8 @@ 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-
C: ~const traits::CfgBase + ~const traits::CfgInterruptLine,
83-
C::System: traits::KernelStatic,
82+
// `~const CfgBase` not implied due to [ref:const_supertraits]
83+
C: ~const traits::CfgBase + ~const traits::CfgStatic + ~const traits::CfgInterruptLine,
8484
{
8585
bind(
8686
(rp2040_resets.borrow_mut(), rp2040_usbctrl_regs.take()),

0 commit comments

Comments
 (0)