Skip to content

Commit

Permalink
Introduce a generic Carrier type, lifting event handler states into a…
Browse files Browse the repository at this point in the history
…n event context.

The code right now is super nasty, since it is not currently possible to implement
  TelnetChannel for all T where Carrier<T> implements ChannelHandler. This is an
  explicit feature of the `where` clause, and a Rust issue is open for this.
  (rust-lang/rust#20414)
  • Loading branch information
Twisol committed Jan 3, 2015
1 parent dee8a09 commit 81a9b6c
Show file tree
Hide file tree
Showing 3 changed files with 42 additions and 10 deletions.
4 changes: 4 additions & 0 deletions src/carrier.rs
Original file line number Diff line number Diff line change
@@ -0,0 +1,4 @@
pub struct Carrier<'parent, 'state, Parent: 'parent, State: 'state> {
pub parent: &'parent mut Parent,
pub state: &'state mut State,
}
2 changes: 2 additions & 0 deletions src/lib.rs
Original file line number Diff line number Diff line change
@@ -1,5 +1,7 @@
#![feature(slicing_syntax, globs, unboxed_closures)]

pub mod carrier;

pub mod parser;
pub mod dispatch;
pub mod demux;
Expand Down
46 changes: 36 additions & 10 deletions src/registry.rs
Original file line number Diff line number Diff line change
@@ -1,8 +1,10 @@
use std::collections::{HashMap};
use std::vec::{Vec};
use carrier::{Carrier};
pub use demux::{ChannelHandler};
use qstate::{QAttitude};


pub trait TelnetChannel<Parent> {
fn on_data<'a>(&mut self, _parent: &mut Parent, _channel: Option<u8>, _data: &'a [u8]) {}
fn on_command(&mut self, _parent: &mut Parent, _channel: Option<u8>, _command: u8) {}
Expand All @@ -17,32 +19,56 @@ pub trait TelnetChannel<Parent> {

impl<Parent> TelnetChannel<Parent> for ()
where Parent: ChannelHandler {
fn on_data<'a>(&mut self, parent: &mut Parent, channel: Option<u8>, data: &'a [u8]) {
parent.on_data(channel, data)
fn on_data<'a>(&mut self, parent: &mut Parent, channel: Option<u8>, text: &'a [u8]) {
Carrier{parent: parent, state: self}.on_data(channel, text)
}
fn on_command(&mut self, parent: &mut Parent, channel: Option<u8>, command: u8) {
parent.on_command(channel, command)
Carrier{parent: parent, state: self}.on_command(channel, command)
}

fn on_enable(&mut self, parent: &mut Parent, channel: Option<u8>) {
parent.on_enable(channel)
Carrier{parent: parent, state: self}.on_enable(channel)
}
fn on_disable(&mut self, parent: &mut Parent, channel: Option<u8>) {
parent.on_disable(channel)
Carrier{parent: parent, state: self}.on_disable(channel)
}
fn on_focus(&mut self, parent: &mut Parent, channel: Option<u8>) {
parent.on_focus(channel)
Carrier{parent: parent, state: self}.on_focus(channel)
}
fn on_blur(&mut self, parent: &mut Parent, channel: Option<u8>) {
parent.on_blur(channel)
Carrier{parent: parent, state: self}.on_focus(channel)
}

fn should_enable(&mut self, parent: &mut Parent, channel: Option<u8>, attitude: QAttitude) -> bool {
parent.should_enable(channel, attitude)
Carrier{parent: parent, state: self}.should_enable(channel, attitude)
}
}
impl<'parent, 'state, Parent> ChannelHandler for Carrier<'parent, 'state, Parent, ()>
where Parent: ChannelHandler {
fn on_data<'a>(&mut self, channel: Option<u8>, data: &'a [u8]) {
self.parent.on_data(channel, data)
}
fn on_command(&mut self, channel: Option<u8>, command: u8) {
self.parent.on_command(channel, command)
}

fn on_enable(&mut self, channel: Option<u8>) {
self.parent.on_enable(channel)
}
fn on_disable(&mut self, channel: Option<u8>) {
self.parent.on_disable(channel)
}
fn on_focus(&mut self, channel: Option<u8>) {
self.parent.on_focus(channel)
}
fn on_blur(&mut self, channel: Option<u8>) {
self.parent.on_blur(channel)
}

fn should_enable(&mut self, channel: Option<u8>, attitude: QAttitude) -> bool {
self.parent.should_enable(channel, attitude)
}
}

// Routes command and channel events to the appropriate handlers.
pub struct EndpointRegistry<'parent, Parent: 'parent> {
pub parent: &'parent mut Parent,

Expand Down

0 comments on commit 81a9b6c

Please sign in to comment.