Skip to content

Instantly share code, notes, and snippets.

Created January 9, 2013 12:44
Show Gist options
  • Star 0 You must be signed in to star a gist
  • Fork 1 You must be signed in to fork a gist
  • Save anonymous/4492878 to your computer and use it in GitHub Desktop.
Save anonymous/4492878 to your computer and use it in GitHub Desktop.
/*
* This is a first attempt at dynamic Rust channels
*
* Paul Harvey
* 8 jan 2013
*/
//Macros?
rules_macro! chan_eq(
)
//This should be replaced by generics
enum Msg_Type{
integer,
string,
}
/*
* Structure of a name
*
*/
struct Name{
mut location:~str,
mut actor:~str,
mut chan_name:~str,
}
/*
* Structure of a channel
*
* NAME => location:actor:channel_name
* TYPE => [short, int, long, unsigned ....] || custom structure
* CONNECTIONS => should this be a table in the system, rather than per actor.
* This would make it smaller, but makes migration a little more difficult.
*/
struct Channel{
mut name:Name,
mut msg_type:Msg_Type,
mut connections: @[Channel],
}
/*-----------------------------------------------------------------------------*/
fn create_channel(n:~str, mt:Msg_Type) -> Channel{
let mut nme = Name{location:~"location", actor:~"actor", chan_name:n};
Channel{name:nme, msg_type:mt, connections:@[]}
}
/*-----------------------------------------------------------------------------*/
fn channel_eq(a:Channel, b:Channel) -> bool{
a.name.location == b.name.location && a.name.actor == b.name.actor && a.name.chan_name == b.name.chan_name
}
/*-----------------------------------------------------------------------------*/
fn channel_connect(a:Channel, b:Channel){
//if not already connected
let mut count = 0;
while count < a.connections.len(){
if channel_eq(a.connections[count], b) {
io::print(~"connection_error: channels already connected`n");
}
count += 1;
}
//a.connections += b;
//b.connections += a;
}
/*-----------------------------------------------------------------------------*/
/******************************************************************************/
//DEBUG
/******************************************************************************/
fn get_type(t:Msg_Type) -> ~str {
match t {
//the possible types
integer => ~"integer",
string => ~"string",
}
}
/*-----------------------------------------------------------------------------*/
fn debug_channel( c:Channel){
io::print(fmt!("Channel: %s\n\t", debug_name(c.name)));
io::println(get_type(c.msg_type));
let mut count = 0;
while(count < c.connections.len()){
debug_channel(copy c.connections[count]);
count = count + 1;
}
}
/*-----------------------------------------------------------------------------*/
fn debug_name(n:Name) -> ~str{
//io::print(fmt!("%s:%s:%s\n", n.location, n.actor, n.chan_name));
n.location + ~":" + n.actor + ~":" + n.chan_name
}
/*-----------------------------------------------------------------------------*/
fn main(){
// let c1:Channel, c2:Channel;
// let mut a = Channel{name:~"a", msg_type:integer, connections:@[]};
// let mut b = Channel{name:~"b", msg_type:integer}
io::println("Started\n");
let mut a = create_channel(~"a", integer);
let mut b = create_channel(~"b", integer);
debug_channel(a);
debug_channel(b);
let mut test:@[Channel];
}
Sign up for free to join this conversation on GitHub. Already have an account? Sign in to comment