I'd like to propose to extend the language with some concepts from nesC,
which is a nice but quite unknown C superset outside of TinyOS world.

The concept would allow better static polymorphism and better code
optimisations and is loosely based on nesC modules/components. It might be
possible to implement the same in rust without extending the syntax and
compiler, please tell me if that's the case.

unbound struct S {
  reg: u32,
  comp: ~T,
}

defines a struct that has a u32 value and some trait. Those values could be
used in S impls as usual.

It is impossible to create an instance of S, the only way to get it is to
bind it in compile time:

let Binding: binding {
  let SImpl: &S;
  let TImpl: &T;

  bind SImpl.reg = 0x12345678;
  bind SImpl.comp = TImpl;
}

The result of this is that all references to reg and comp are replaced by
actual values (TImpl must obviously be an unbound struct as well), so that
the whole SImpl could be optimised away and don't use any storage bytes
other than required by constants.

Why is it useful? Embedded systems are commonly represented as a tree of
components hooked up to one another. Also, resources on embedded are used
to be expensive. This code allows to define a "configuration" of a system
which is completely resolved in compile time, allowing better optimisation
of the code while maintaining readable code.

Some close-to-real-life example:

unbound struct Led;
impl Led {
  pub fn set_enabled(&self, enabled: bool) {
    // set the led state
  }
}

trait Callable {
  fn callback(&self);
}
unbound struct Timer {
  actor: ~Callable,
}
impl Timer {
  pub fn start(&self) {
    // init the timer, hardware will call fired() via ISR
  }
  fn fired(&self) {
    actor.callback();
  }
}

unbound struct Blinker {
  mut state: bool,
  led: &Led,
  timer: &Timer,
}
impl Callable for Blinker {
  fn callback(&self) {
    self.state != self.state;
    led.set_enabled(self.state);
  }
}
impl Blinker {
  pub fn main(&self) {
    self.timer.start();
  }
}

let Binding: binding {
  let timer = &Timer;
  let led = &Led;
  let blinker = &Blinker { state: false }; // we can also set the mut
property here. This one is static mut by nature, but available only in
Blinker impl.

  bind timer.actor = blinker;
  bind blinker.timer = timer;
  bind blinker.led = led;
}

pub fn main() {
  Binding.blinker.start();
}

-- 
Sincerely,
Vladimir "Farcaller" Pouzanov
http://farcaller.net/
_______________________________________________
Rust-dev mailing list
Rust-dev@mozilla.org
https://mail.mozilla.org/listinfo/rust-dev

Reply via email to