Skip to content

Commit

Permalink
Make actor queue size configurable as associated type (#7)
Browse files Browse the repository at this point in the history
Making it an associated type avoids needing to pass queue size as a
parameter to ActorState and Address, which makes code very
template-fatigued.

Downside is that min const generics doesn't support assoc types, so we
wont be able to use const generic heapless.
  • Loading branch information
lulf committed May 6, 2021
1 parent 300a47d commit 623e5b2
Show file tree
Hide file tree
Showing 3 changed files with 13 additions and 6 deletions.
2 changes: 2 additions & 0 deletions actors/src/button.rs
Original file line number Diff line number Diff line change
Expand Up @@ -2,6 +2,7 @@ use core::future::Future;
use core::pin::Pin;
use drogue_device_kernel::{
actor::{Actor, Address},
channel::consts,
util::ImmediateFuture,
};
use embassy_traits::gpio::WaitForAnyEdge;
Expand Down Expand Up @@ -43,6 +44,7 @@ impl<'a, P: WaitForAnyEdge + InputPin + 'a, A: Actor + FromButtonEvent<A::Messag
impl<'a, P: WaitForAnyEdge + InputPin + 'a, A: Actor + FromButtonEvent<A::Message<'a>> + 'a> Actor
for Button<'a, P, A>
{
type QueueLength<'m> where 'a: 'm = consts::U0;
type Configuration = Address<'a, A>;
#[rustfmt::skip]
type Message<'m> where 'a: 'm = ();
Expand Down
16 changes: 10 additions & 6 deletions kernel/src/actor.rs
Original file line number Diff line number Diff line change
@@ -1,4 +1,4 @@
use crate::channel::{consts, Channel, ChannelSend};
use crate::channel::{consts, ArrayLength, Channel, ChannelSend};
use crate::signal::{SignalFuture, SignalSlot};
use core::cell::UnsafeCell;
use core::future::Future;
Expand All @@ -7,7 +7,11 @@ use core::task::{Context, Poll};
use embassy::util::DropBomb;

/// Trait that each actor must implement.
pub trait Actor {
pub trait Actor: Sized {
/// Queue size;
#[rustfmt::skip]
type QueueLength<'a>: ArrayLength<ActorMessage<'a, Self>> + ArrayLength<SignalSlot> + 'a where Self: 'a = consts::U1;

/// The configuration that this actor will expect when mounted.
type Configuration;

Expand Down Expand Up @@ -100,13 +104,13 @@ impl<'a, A: Actor> Clone for Address<'a, A> {

pub struct ActorState<'a, A: Actor> {
pub actor: UnsafeCell<A>,
pub channel: Channel<'a, ActorMessage<'a, A>, consts::U4>,
pub channel: Channel<'a, ActorMessage<'a, A>, A::QueueLength<'a>>,
signals: UnsafeCell<[SignalSlot; 4]>,
}

impl<'a, A: Actor> ActorState<'a, A> {
pub fn new(actor: A) -> Self {
let channel: Channel<'a, ActorMessage<A>, consts::U4> = Channel::new();
let channel: Channel<'a, ActorMessage<A>, A::QueueLength<'a>> = Channel::new();
Self {
actor: UnsafeCell::new(actor),
channel,
Expand Down Expand Up @@ -176,15 +180,15 @@ enum SendState {
}

pub struct SendFuture<'a, 'm, A: Actor + 'a> {
channel: ChannelSend<'a, ActorMessage<'a, A>, consts::U4>,
channel: ChannelSend<'a, ActorMessage<'a, A>, A::QueueLength<'a>>,
signal: SignalFuture<'a, 'm>,
state: SendState,
bomb: Option<DropBomb>,
}

impl<'a, 'm, A: Actor> SendFuture<'a, 'm, A> {
pub fn new(
channel: ChannelSend<'a, ActorMessage<'a, A>, consts::U4>,
channel: ChannelSend<'a, ActorMessage<'a, A>, A::QueueLength<'a>>,
signal: SignalFuture<'a, 'm>,
) -> Self {
Self {
Expand Down
1 change: 1 addition & 0 deletions kernel/src/lib.rs
Original file line number Diff line number Diff line change
Expand Up @@ -4,6 +4,7 @@
#![feature(min_type_alias_impl_trait)]
#![feature(impl_trait_in_bindings)]
#![feature(generic_associated_types)]
#![feature(associated_type_defaults)]
#![feature(type_alias_impl_trait)]

pub(crate) mod fmt;
Expand Down

0 comments on commit 623e5b2

Please sign in to comment.