<commit_before>use std::path::PathBuf;
use std::process::Command;
use std::env;
use std::io::stderr;
use std::io::Write;

pub use clap::App;

use clap::{Arg, ArgMatches};
use log;
use log::LogLevelFilter;

use configuration::Configuration;
use error::RuntimeError;
use error::RuntimeErrorKind;
use error::MapErrInto;
use logger::ImagLogger;

use libimagstore::store::Store;

#[derive(Debug)]
pub struct Runtime<'a> {
    rtp: PathBuf,
    configuration: Option<Configuration>,
    cli_matches: ArgMatches<'a>,
    store: Store,
}

impl<'a> Runtime<'a> {

    /**
     * Gets the CLI spec for the program and retreives the config file path (or uses the default on
     * in $HOME/.imag/config, $XDG_CONFIG_DIR/imag/config or from env("$IMAG_CONFIG")
     * and builds the Runtime object with it.
     *
     * The cli_spec object should be initially build with the ::get_default_cli_builder() function.
     *
     */
    pub fn new(cli_spec: App<'a, 'a>) -> Result<Runtime<'a>, RuntimeError> {
        use std::env;

        use libimagstore::hook::position::HookPosition as HP;
        use libimagstore::hook::Hook;
        use libimagstore::error::StoreErrorKind;
        use libimagstorestdhook::debug::DebugHook;
        use libimagerror::trace::trace_error;
        use libimagerror::trace::trace_error_dbg;
        use libimagerror::into::IntoError;

        use configuration::error::ConfigErrorKind;

        let matches = cli_spec.get_matches();

        let is_debugging = matches.is_present("debugging");
        let is_verbose   = matches.is_present("verbosity");

        Runtime::init_logger(is_debugging, is_verbose);

        let rtp : PathBuf = matches.value_of("runtimepath")
            .map_or_else(|| {
                env::var("HOME")
                    .map(PathBuf::from)
                    .map(|mut p| { p.push(".imag"); p})
                    .unwrap_or_else(|_| {
                        panic!("You seem to be $HOME-less. Please get a $HOME before using this software. We are sorry for you and hope you have some accommodation anyways.");
                    })
            }, PathBuf::from);
        let storepath = matches.value_of("storepath")
                                .map_or_else(|| {
                                    let mut spath = rtp.clone();
                                    spath.push("store");
                                    spath
                                }, PathBuf::from);

        let configpath = matches.value_of("config")
                                .map_or_else(|| rtp.clone(), PathBuf::from);

        let cfg = match Configuration::new(&configpath) {
            Err(e) => if e.err_type() != ConfigErrorKind::NoConfigFileFound {
                return Err(RuntimeErrorKind::Instantiate.into_error_with_cause(Box::new(e)));
            } else {
                warn!("No config file found.");
                warn!("Continuing without configuration file");
                None
            },

            Ok(mut cfg) => {
                if let Err(e) = cfg.override_config(get_override_specs(&matches)) {
                    error!("Could not apply config overrides");
                    trace_error(&e);

                    // TODO: continue question (interactive)
                }

                Some(cfg)
            }
        };

        let store_config = match cfg {
            Some(ref c) => c.store_config().cloned(),
            None        => None,
        };

        if is_debugging {
            write!(stderr(), "Config: {:?}\n", cfg).ok();
            write!(stderr(), "Store-config: {:?}\n", store_config).ok();
        }

        Store::new(storepath, store_config).map(|mut store| {
            // If we are debugging, generate hooks for all positions
            if is_debugging {
                let hooks : Vec<(Box<Hook>, &str, HP)> = vec![
                    (Box::new(DebugHook::new(HP::PreCreate))          , "debug", HP::PreCreate),
                    (Box::new(DebugHook::new(HP::PostCreate))         , "debug", HP::PostCreate),
                    (Box::new(DebugHook::new(HP::PreRetrieve))        , "debug", HP::PreRetrieve),
                    (Box::new(DebugHook::new(HP::PostRetrieve))       , "debug", HP::PostRetrieve),
                    (Box::new(DebugHook::new(HP::PreUpdate))          , "debug", HP::PreUpdate),
                    (Box::new(DebugHook::new(HP::PostUpdate))         , "debug", HP::PostUpdate),
                    (Box::new(DebugHook::new(HP::PreDelete))          , "debug", HP::PreDelete),
                    (Box::new(DebugHook::new(HP::PostDelete))         , "debug", HP::PostDelete),
                ];

                // If hook registration fails, trace the error and warn, but continue.
                for (hook, aspectname, position) in hooks {
                    if let Err(e) = store.register_hook(position, &String::from(aspectname), hook) {
                        if e.err_type() == StoreErrorKind::HookRegisterError {
                            trace_error_dbg(&e);
                            warn!("Registering debug hook with store failed");
                        } else {
                            trace_error(&e);
                        };
                    }
                }
            }

            Runtime {
                cli_matches: matches,
                configuration: cfg,
                rtp: rtp,
                store: store,
            }
        })
        .map_err_into(RuntimeErrorKind::Instantiate)
    }

    /**
     * Get a commandline-interface builder object from `clap`
     *
     * This commandline interface builder object already contains some predefined interface flags:
     *   * -v | --verbose for verbosity
     *   * --debug for debugging
     *   * -c <file> | --config <file> for alternative configuration file
     *   * -r <path> | --rtp <path> for alternative runtimepath
     *   * --store <path> for alternative store path
     * Each has the appropriate help text included.
     *
     * The `appname` shall be "imag-<command>".
     */
    pub fn get_default_cli_builder(appname: &'a str,
                                   version: &'a str,
                                   about: &'a str)
        -> App<'a, 'a>
    {
        App::new(appname)
            .version(version)
            .author("Matthias Beyer <mail@beyermatthias.de>")
            .about(about)
            .arg(Arg::with_name("verbosity")
                .short("v")
                .long("verbose")
                .help("Enables verbosity")
                .required(false)
                .takes_value(false))

            .arg(Arg::with_name("debugging")
                .long("debug")
                .help("Enables debugging output")
                .required(false)
                .takes_value(false))

            .arg(Arg::with_name("config")
                .long("config")
                .help("Path to alternative config file")
                .required(false)
                .takes_value(true))

            .arg(Arg::with_name("config-override")
                 .long("override-config")
                 .help("Override a configuration settings. Use 'key=value' pairs, where the key is a path in the TOML configuration. The value must be present in the configuration and be convertible to the type of the configuration setting. If the argument does not contain a '=', it gets ignored. Setting Arrays and Tables is not yet supported.")
                 .required(false)
                 .takes_value(true))

            .arg(Arg::with_name("runtimepath")
                .long("rtp")
                .help("Alternative runtimepath")
                .required(false)
                .takes_value(true))

            .arg(Arg::with_name("storepath")
                .long("store")
                .help("Alternative storepath. Must be specified as full path, can be outside of the RTP")
                .required(false)
                .takes_value(true))

            .arg(Arg::with_name("editor")
                .long("editor")
                .help("Set editor")
                .required(false)
                .takes_value(true))
    }

    /**
     * Initialize the internal logger
     */
    fn init_logger(is_debugging: bool, is_verbose: bool) {
        use std::env::var as env_var;
        use env_logger;

        if env_var("IMAG_LOG_ENV").is_ok() {
            env_logger::init().unwrap();
        } else {
            let lvl = if is_debugging {
                LogLevelFilter::Debug
            } else if is_verbose {
                LogLevelFilter::Info
            } else {
                LogLevelFilter::Warn
            };

            log::set_logger(|max_log_lvl| {
                max_log_lvl.set(lvl);
                debug!("Init logger with {}", lvl);
                Box::new(ImagLogger::new(lvl.to_log_level().unwrap()))
            })
            .map_err(|_| {
                panic!("Could not setup logger");
            })
            .ok();
        }
    }

    /**
     * Get the verbosity flag value
     */
    pub fn is_verbose(&self) -> bool {
        self.cli_matches.is_present("verbosity")
    }

    /**
     * Get the debugging flag value
     */
    pub fn is_debugging(&self) -> bool {
        self.cli_matches.is_present("debugging")
    }

    /**
     * Get the runtimepath
     */
    pub fn rtp(&self) -> &PathBuf {
        &self.rtp
    }

    /**
     * Get the commandline interface matches
     */
    pub fn cli(&self) -> &ArgMatches {
        &self.cli_matches
    }

    /**
     * Get the configuration object
     */
    pub fn config(&self) -> Option<&Configuration> {
        self.configuration.as_ref()
    }

    /**
     * Get the store object
     */
    pub fn store(&self) -> &Store {
        &self.store
    }

    pub fn editor(&self) -> Option<Command> {
        self.cli()
            .value_of("editor")
            .map(String::from)
            .or({
                match self.configuration {
                    Some(ref c) => c.editor().cloned(),
                    _ => None,
                }
            })
            .or(env::var("EDITOR").ok())
            .map(Command::new)
    }
}

fn get_override_specs(matches: &ArgMatches) -> Vec<String> {
    matches
        .values_of("config-override")
        .map(|values| {
             values
             .filter(|s| {
                 let b = s.contains("=");
                 if !b { warn!("override '{}' does not contain '=' - will be ignored!", s); }
                 b
             })
             .map(String::from)
             .collect()
        })
        .unwrap_or(vec![])
}

<commit_msg>Configure coloring in logger setup<commit_after>use std::path::PathBuf;
use std::process::Command;
use std::env;
use std::io::stderr;
use std::io::Write;

pub use clap::App;

use clap::{Arg, ArgMatches};
use log;
use log::LogLevelFilter;

use configuration::Configuration;
use error::RuntimeError;
use error::RuntimeErrorKind;
use error::MapErrInto;
use logger::ImagLogger;

use libimagstore::store::Store;

#[derive(Debug)]
pub struct Runtime<'a> {
    rtp: PathBuf,
    configuration: Option<Configuration>,
    cli_matches: ArgMatches<'a>,
    store: Store,
}

impl<'a> Runtime<'a> {

    /**
     * Gets the CLI spec for the program and retreives the config file path (or uses the default on
     * in $HOME/.imag/config, $XDG_CONFIG_DIR/imag/config or from env("$IMAG_CONFIG")
     * and builds the Runtime object with it.
     *
     * The cli_spec object should be initially build with the ::get_default_cli_builder() function.
     *
     */
    pub fn new(cli_spec: App<'a, 'a>) -> Result<Runtime<'a>, RuntimeError> {
        use std::env;

        use libimagstore::hook::position::HookPosition as HP;
        use libimagstore::hook::Hook;
        use libimagstore::error::StoreErrorKind;
        use libimagstorestdhook::debug::DebugHook;
        use libimagerror::trace::trace_error;
        use libimagerror::trace::trace_error_dbg;
        use libimagerror::into::IntoError;

        use configuration::error::ConfigErrorKind;

        let matches = cli_spec.get_matches();

        let is_debugging = matches.is_present("debugging");
        let is_verbose   = matches.is_present("verbosity");
        let colored      = !matches.is_present("no-color-output");

        Runtime::init_logger(is_debugging, is_verbose, colored);

        let rtp : PathBuf = matches.value_of("runtimepath")
            .map_or_else(|| {
                env::var("HOME")
                    .map(PathBuf::from)
                    .map(|mut p| { p.push(".imag"); p})
                    .unwrap_or_else(|_| {
                        panic!("You seem to be $HOME-less. Please get a $HOME before using this software. We are sorry for you and hope you have some accommodation anyways.");
                    })
            }, PathBuf::from);
        let storepath = matches.value_of("storepath")
                                .map_or_else(|| {
                                    let mut spath = rtp.clone();
                                    spath.push("store");
                                    spath
                                }, PathBuf::from);

        let configpath = matches.value_of("config")
                                .map_or_else(|| rtp.clone(), PathBuf::from);

        let cfg = match Configuration::new(&configpath) {
            Err(e) => if e.err_type() != ConfigErrorKind::NoConfigFileFound {
                return Err(RuntimeErrorKind::Instantiate.into_error_with_cause(Box::new(e)));
            } else {
                warn!("No config file found.");
                warn!("Continuing without configuration file");
                None
            },

            Ok(mut cfg) => {
                if let Err(e) = cfg.override_config(get_override_specs(&matches)) {
                    error!("Could not apply config overrides");
                    trace_error(&e);

                    // TODO: continue question (interactive)
                }

                Some(cfg)
            }
        };

        let store_config = match cfg {
            Some(ref c) => c.store_config().cloned(),
            None        => None,
        };

        if is_debugging {
            write!(stderr(), "Config: {:?}\n", cfg).ok();
            write!(stderr(), "Store-config: {:?}\n", store_config).ok();
        }

        Store::new(storepath, store_config).map(|mut store| {
            // If we are debugging, generate hooks for all positions
            if is_debugging {
                let hooks : Vec<(Box<Hook>, &str, HP)> = vec![
                    (Box::new(DebugHook::new(HP::PreCreate))          , "debug", HP::PreCreate),
                    (Box::new(DebugHook::new(HP::PostCreate))         , "debug", HP::PostCreate),
                    (Box::new(DebugHook::new(HP::PreRetrieve))        , "debug", HP::PreRetrieve),
                    (Box::new(DebugHook::new(HP::PostRetrieve))       , "debug", HP::PostRetrieve),
                    (Box::new(DebugHook::new(HP::PreUpdate))          , "debug", HP::PreUpdate),
                    (Box::new(DebugHook::new(HP::PostUpdate))         , "debug", HP::PostUpdate),
                    (Box::new(DebugHook::new(HP::PreDelete))          , "debug", HP::PreDelete),
                    (Box::new(DebugHook::new(HP::PostDelete))         , "debug", HP::PostDelete),
                ];

                // If hook registration fails, trace the error and warn, but continue.
                for (hook, aspectname, position) in hooks {
                    if let Err(e) = store.register_hook(position, &String::from(aspectname), hook) {
                        if e.err_type() == StoreErrorKind::HookRegisterError {
                            trace_error_dbg(&e);
                            warn!("Registering debug hook with store failed");
                        } else {
                            trace_error(&e);
                        };
                    }
                }
            }

            Runtime {
                cli_matches: matches,
                configuration: cfg,
                rtp: rtp,
                store: store,
            }
        })
        .map_err_into(RuntimeErrorKind::Instantiate)
    }

    /**
     * Get a commandline-interface builder object from `clap`
     *
     * This commandline interface builder object already contains some predefined interface flags:
     *   * -v | --verbose for verbosity
     *   * --debug for debugging
     *   * -c <file> | --config <file> for alternative configuration file
     *   * -r <path> | --rtp <path> for alternative runtimepath
     *   * --store <path> for alternative store path
     * Each has the appropriate help text included.
     *
     * The `appname` shall be "imag-<command>".
     */
    pub fn get_default_cli_builder(appname: &'a str,
                                   version: &'a str,
                                   about: &'a str)
        -> App<'a, 'a>
    {
        App::new(appname)
            .version(version)
            .author("Matthias Beyer <mail@beyermatthias.de>")
            .about(about)
            .arg(Arg::with_name("verbosity")
                .short("v")
                .long("verbose")
                .help("Enables verbosity")
                .required(false)
                .takes_value(false))

            .arg(Arg::with_name("debugging")
                .long("debug")
                .help("Enables debugging output")
                .required(false)
                .takes_value(false))

            .arg(Arg::with_name("no-color-output")
                .long("no-color")
                .help("Disable color output")
                .required(false)
                .takes_value(false))

            .arg(Arg::with_name("config")
                .long("config")
                .help("Path to alternative config file")
                .required(false)
                .takes_value(true))

            .arg(Arg::with_name("config-override")
                 .long("override-config")
                 .help("Override a configuration settings. Use 'key=value' pairs, where the key is a path in the TOML configuration. The value must be present in the configuration and be convertible to the type of the configuration setting. If the argument does not contain a '=', it gets ignored. Setting Arrays and Tables is not yet supported.")
                 .required(false)
                 .takes_value(true))

            .arg(Arg::with_name("runtimepath")
                .long("rtp")
                .help("Alternative runtimepath")
                .required(false)
                .takes_value(true))

            .arg(Arg::with_name("storepath")
                .long("store")
                .help("Alternative storepath. Must be specified as full path, can be outside of the RTP")
                .required(false)
                .takes_value(true))

            .arg(Arg::with_name("editor")
                .long("editor")
                .help("Set editor")
                .required(false)
                .takes_value(true))
    }

    /**
     * Initialize the internal logger
     */
    fn init_logger(is_debugging: bool, is_verbose: bool, colored: bool) {
        use std::env::var as env_var;
        use env_logger;

        if env_var("IMAG_LOG_ENV").is_ok() {
            env_logger::init().unwrap();
        } else {
            let lvl = if is_debugging {
                LogLevelFilter::Debug
            } else if is_verbose {
                LogLevelFilter::Info
            } else {
                LogLevelFilter::Warn
            };

            log::set_logger(|max_log_lvl| {
                max_log_lvl.set(lvl);
                debug!("Init logger with {}", lvl);
                Box::new(ImagLogger::new(lvl.to_log_level().unwrap()).with_color(colored))
            })
            .map_err(|_| {
                panic!("Could not setup logger");
            })
            .ok();
        }
    }

    /**
     * Get the verbosity flag value
     */
    pub fn is_verbose(&self) -> bool {
        self.cli_matches.is_present("verbosity")
    }

    /**
     * Get the debugging flag value
     */
    pub fn is_debugging(&self) -> bool {
        self.cli_matches.is_present("debugging")
    }

    /**
     * Get the runtimepath
     */
    pub fn rtp(&self) -> &PathBuf {
        &self.rtp
    }

    /**
     * Get the commandline interface matches
     */
    pub fn cli(&self) -> &ArgMatches {
        &self.cli_matches
    }

    /**
     * Get the configuration object
     */
    pub fn config(&self) -> Option<&Configuration> {
        self.configuration.as_ref()
    }

    /**
     * Get the store object
     */
    pub fn store(&self) -> &Store {
        &self.store
    }

    pub fn editor(&self) -> Option<Command> {
        self.cli()
            .value_of("editor")
            .map(String::from)
            .or({
                match self.configuration {
                    Some(ref c) => c.editor().cloned(),
                    _ => None,
                }
            })
            .or(env::var("EDITOR").ok())
            .map(Command::new)
    }
}

fn get_override_specs(matches: &ArgMatches) -> Vec<String> {
    matches
        .values_of("config-override")
        .map(|values| {
             values
             .filter(|s| {
                 let b = s.contains("=");
                 if !b { warn!("override '{}' does not contain '=' - will be ignored!", s); }
                 b
             })
             .map(String::from)
             .collect()
        })
        .unwrap_or(vec![])
}

<|endoftext|>


<commit_before><commit_msg>Another broken example<commit_after>#[macro_use]
extern crate nom;
extern crate pcapng;

use std::env;
use std::fs;
use std::io::{Read,Write};
use nom::{FileProducer,MemProducer,Producer};
use nom::{ConsumerState,Consumer};
use nom::{IResult};
use pcapng::block::{RawBlock,parse_blocks,parse_block};

fn main() {
    let args: Vec<_> = env::args().collect();
    if args.len() != 2 {
        println!("Usage: {} <foo.pcapng>", args[0]);
        return;
    }

    // I can't currently get the Producer machinery to work for me, so instead we read into a
    // gigantor byte array and dump that in

    // This explodes, claiming to be incomplete. I *think* there's potentially an underflow in my
    // pcap

    let mut fh = fs::File::open(&args[1]).unwrap();
    let mut buf: Vec<u8> = Vec::new();
    let read = fh.read_to_end(&mut buf);

    println!("Seeded the buffer with {}", buf.len());

    match pcapng::block::parse_block(&buf[..]) {
        IResult::Done(_, block) => {
            println!("block: {:?}", block)
            // for i in blocks {
            //     println!("block: {:?}", i);
            // }
        }
        IResult::Error(e)      => panic!("Error: {:?}", e),
        IResult::Incomplete(i) => panic!("Incomplete: {:?}", i),

    }
}
<|endoftext|>


<commit_before><commit_msg>Provide index.html<commit_after><|endoftext|>


<commit_before><commit_msg>Add `process_immediately` flag to Event<commit_after><|endoftext|>


<commit_before><commit_msg>Add docs for merge().<commit_after><|endoftext|>


<commit_before><commit_msg>Added benchmarks for all integration targets<commit_after>use super::{scheme,svm};

use test::Bencher;

macro_rules! impl_bench {
    ($name:ident, $it:expr) => {
        #[bench]
        fn $name(b: &mut Bencher) {
            b.iter(|| {
                svm::eval_program(scheme::compile($it)
                    .unwrap(), true)
                    .unwrap()
            })
        }

    }
}

impl_bench!(
    bench_list_creation,
    "(cons 10 (cons 20 nil))"
);

impl_bench!(
    bench_list_car,
    "(car (cons 20 (cons 10 nil)))"
);

impl_bench!(
    bench_list_cdr,
    "(cdr (cons 20 (cons 10 nil)))"
);

impl_bench!(
    bench_simple_add,
    "(+ 10 10)"
);

impl_bench!(
    bench_nested_arith,
    "(- 20 (+ 5 5))"
);

impl_bench!(
    bench_basic_branching_1,
    "(if (= 0 (- 1 1)) #t #f)"
);

impl_bench!(
    bench_basic_branching_2,
    "(+ 10 (if (nil? nil) 10 20))"
);

impl_bench!(
    bench_lambda_ap,
    "((lambda (x y) (+ x y)) 2 3)"
);

impl_bench!(
    bench_nested_lambda,
    "((lambda (z) ((lambda (x y) (+ (- x y) z)) 3 5)) 6)"
);
<|endoftext|>


<commit_before><commit_msg>Add ui test with explicit Unpin impl<commit_after>use std::marker::Unpin;

#[cxx::bridge]
mod ffi {
    extern "C++" {
        type Opaque;
    }
}

impl Unpin for ffi::Opaque {}

fn main() {}
<|endoftext|>


<commit_before><commit_msg>rustup<commit_after><|endoftext|>


<commit_before><commit_msg>vec: update for the new slice representation<commit_after><|endoftext|>


<commit_before><commit_msg>Change global variables from extern to non static.  Make script local global vars static.<commit_after><|endoftext|>


<commit_before>/**
   A parallel word-frequency counting program.

   This is meant primarily to demonstrate Rust's MapReduce framework.

   It takes a list of files on the command line and outputs a list of
   words along with how many times each word is used.

*/

use std;

import option = option;
import option::some;
import option::none;
import str;
import std::treemap;
import vec;
import io;
import io::{reader_util, writer_util};

import std::time;
import u64;

import task;
import comm;
import comm::chan;
import comm::port;
import comm::recv;
import comm::send;
import comm::methods;

trait word_reader {
    fn read_word() -> option<str>;
}

// These used to be in task, but they disappeard.
type joinable_task = port<()>;
fn spawn_joinable(+f: fn~()) -> joinable_task {
    let p = port();
    let c = chan(p);
    do task::spawn() |move f| {
        f();
        c.send(());
    }
    p
}

fn join(t: joinable_task) {
    t.recv()
}

impl of word_reader for io::reader {
    fn read_word() -> option<str> { read_word(self) }
}

fn file_word_reader(filename: str) -> word_reader {
    alt io::file_reader(filename) {
      result::ok(f) { f as word_reader }
      result::err(e) { fail #fmt("%?", e) }
    }
}

fn map(f: fn~() -> word_reader, emit: map_reduce::putter<str, int>) {
    let f = f();
    loop {
        alt f.read_word() {
          some(w) { emit(w, 1); }
          none { break; }
        }
    }
}

fn reduce(&&word: str, get: map_reduce::getter<int>) {
    let mut count = 0;

    loop { alt get() { some(_) { count += 1; } none { break; } } }

    io::println(#fmt("%s\t%?", word, count));
}

mod map_reduce {
    export putter;
    export getter;
    export mapper;
    export reducer;
    export map_reduce;

    type putter<K: send, V: send> = fn(K, V);

    type mapper<K1: send, K2: send, V: send> = fn~(K1, putter<K2, V>);

    type getter<V: send> = fn() -> option<V>;

    type reducer<K: copy send, V: copy send> = fn~(K, getter<V>);

    enum ctrl_proto<K: copy send, V: copy send> {
        find_reducer(K, chan<chan<reduce_proto<V>>>),
        mapper_done
    }

    enum reduce_proto<V: copy send> { emit_val(V), done, ref, release }

    fn start_mappers<K1: copy send, K2: copy send, V: copy send>(
        map: mapper<K1, K2, V>,
        ctrl: chan<ctrl_proto<K2, V>>, inputs: ~[K1])
        -> ~[joinable_task]
    {
        let mut tasks = ~[];
        for inputs.each |i| {
            tasks += ~[spawn_joinable(|| map_task(map, ctrl, i) )];
        }
        ret tasks;
    }

    fn map_task<K1: copy send, K2: copy send, V: copy send>(
        map: mapper<K1, K2, V>,
        ctrl: chan<ctrl_proto<K2, V>>,
        input: K1)
    {
        // log(error, "map_task " + input);
        let intermediates = treemap::treemap();

        fn emit<K2: copy send, V: copy send>(
            im: treemap::treemap<K2, chan<reduce_proto<V>>>,
            ctrl: chan<ctrl_proto<K2, V>>, key: K2, val: V)
        {
            let c;
            alt treemap::find(im, key) {
              some(_c) { c = _c; }
              none {
                let p = port();
                send(ctrl, find_reducer(key, chan(p)));
                c = recv(p);
                treemap::insert(im, key, c);
                send(c, ref);
              }
            }
            send(c, emit_val(val));
        }

        map(input, {|a,b|emit(intermediates, ctrl, a, b)});

        fn finish<K: copy send, V: copy send>(_k: K, v: chan<reduce_proto<V>>)
        {
            send(v, release);
        }
        treemap::traverse(intermediates, finish);
        send(ctrl, mapper_done);
    }

    fn reduce_task<K: copy send, V: copy send>(
        reduce: reducer<K, V>,
        key: K,
        out: chan<chan<reduce_proto<V>>>)
    {
        let p = port();

        send(out, chan(p));

        let mut ref_count = 0;
        let mut is_done = false;

        fn get<V: copy send>(p: port<reduce_proto<V>>,
                             &ref_count: int, &is_done: bool)
           -> option<V> {
            while !is_done || ref_count > 0 {
                alt recv(p) {
                  emit_val(v) {
                    // #error("received %d", v);
                    ret some(v);
                  }
                  done {
                    // #error("all done");
                    is_done = true;
                  }
                  ref { ref_count += 1; }
                  release { ref_count -= 1; }
                }
            }
            ret none;
        }

        reduce(key, || get(p, ref_count, is_done) );
    }

    fn map_reduce<K1: copy send, K2: copy send, V: copy send>(
        map: mapper<K1, K2, V>,
        reduce: reducer<K2, V>,
        inputs: ~[K1])
    {
        let ctrl = port();

        // This task becomes the master control task. It task::_spawns
        // to do the rest.

        let reducers = treemap::treemap();
        let mut tasks = start_mappers(map, chan(ctrl), inputs);
        let mut num_mappers = vec::len(inputs) as int;

        while num_mappers > 0 {
            alt recv(ctrl) {
              mapper_done {
                // #error("received mapper terminated.");
                num_mappers -= 1;
              }
              find_reducer(k, cc) {
                let c;
                // log(error, "finding reducer for " + k);
                alt treemap::find(reducers, k) {
                  some(_c) {
                    // log(error,
                    // "reusing existing reducer for " + k);
                    c = _c;
                  }
                  none {
                    // log(error, "creating new reducer for " + k);
                    let p = port();
                    let ch = chan(p);
                    let r = reduce, kk = k;
                    tasks += ~[
                        spawn_joinable(|| reduce_task(r, kk, ch) )
                    ];
                    c = recv(p);
                    treemap::insert(reducers, k, c);
                  }
                }
                send(cc, c);
              }
            }
        }

        fn finish<K: copy send, V: copy send>(_k: K, v: chan<reduce_proto<V>>)
        {
            send(v, done);
        }
        treemap::traverse(reducers, finish);

        for tasks.each |t| { join(t); }
    }
}

fn main(argv: ~[str]) {
    if vec::len(argv) < 2u && !os::getenv("RUST_BENCH").is_some() {
        let out = io::stdout();

        out.write_line(#fmt["Usage: %s <filename> ...", argv[0]]);

        ret;
    }

    let readers: ~[fn~() -> word_reader]  = if argv.len() >= 2 {
        vec::view(argv, 1u, argv.len()).map(
            |f| fn~() -> word_reader { file_word_reader(f) } )
    }
    else {
        let num_readers = 50;
        let words_per_reader = 400;
        vec::from_fn(
            num_readers,
            |_i| fn~() -> word_reader {
                random_word_reader(words_per_reader) as word_reader
            })
    };

    let start = time::precise_time_ns();

    map_reduce::map_reduce(map, reduce, readers);
    let stop = time::precise_time_ns();

    let elapsed = (stop - start) / 1000000u64;

    log(error, "MapReduce completed in "
             + u64::str(elapsed) + "ms");
}

fn read_word(r: io::reader) -> option<str> {
    let mut w = "";

    while !r.eof() {
        let c = r.read_char();

        if is_word_char(c) {
            w += str::from_char(c);
        } else { if w != "" { ret some(w); } }
    }
    ret none;
}

fn is_word_char(c: char) -> bool {
    char::is_alphabetic(c) || char::is_digit(c) || c == '_'
}

class random_word_reader: word_reader {
    let mut remaining: uint;
    let rng: rand::rng;
    new(count: uint) {
        self.remaining = count;
        self.rng = rand::rng();
    }

    fn read_word() -> option<str> {
        if self.remaining > 0 {
            self.remaining -= 1;
            some(self.rng.gen_str(5))
        }
        else { none }
    }
}
<commit_msg>Remove slow vec+=, and make word-count difficulty harder.<commit_after>/**
   A parallel word-frequency counting program.

   This is meant primarily to demonstrate Rust's MapReduce framework.

   It takes a list of files on the command line and outputs a list of
   words along with how many times each word is used.

*/

use std;

import option = option;
import option::some;
import option::none;
import str;
import std::treemap;
import vec;
import io;
import io::{reader_util, writer_util};

import std::time;
import u64;

import task;
import comm;
import comm::chan;
import comm::port;
import comm::recv;
import comm::send;
import comm::methods;

trait word_reader {
    fn read_word() -> option<str>;
}

// These used to be in task, but they disappeard.
type joinable_task = port<()>;
fn spawn_joinable(+f: fn~()) -> joinable_task {
    let p = port();
    let c = chan(p);
    do task::spawn() |move f| {
        f();
        c.send(());
    }
    p
}

fn join(t: joinable_task) {
    t.recv()
}

impl of word_reader for io::reader {
    fn read_word() -> option<str> { read_word(self) }
}

fn file_word_reader(filename: str) -> word_reader {
    alt io::file_reader(filename) {
      result::ok(f) { f as word_reader }
      result::err(e) { fail #fmt("%?", e) }
    }
}

fn map(f: fn~() -> word_reader, emit: map_reduce::putter<str, int>) {
    let f = f();
    loop {
        alt f.read_word() {
          some(w) { emit(w, 1); }
          none { break; }
        }
    }
}

fn reduce(&&word: str, get: map_reduce::getter<int>) {
    let mut count = 0;

    loop { alt get() { some(_) { count += 1; } none { break; } } }

    io::println(#fmt("%s\t%?", word, count));
}

mod map_reduce {
    export putter;
    export getter;
    export mapper;
    export reducer;
    export map_reduce;

    type putter<K: send, V: send> = fn(K, V);

    type mapper<K1: send, K2: send, V: send> = fn~(K1, putter<K2, V>);

    type getter<V: send> = fn() -> option<V>;

    type reducer<K: copy send, V: copy send> = fn~(K, getter<V>);

    enum ctrl_proto<K: copy send, V: copy send> {
        find_reducer(K, chan<chan<reduce_proto<V>>>),
        mapper_done
    }

    enum reduce_proto<V: copy send> { emit_val(V), done, ref, release }

    fn start_mappers<K1: copy send, K2: copy send, V: copy send>(
        map: mapper<K1, K2, V>,
        ctrl: chan<ctrl_proto<K2, V>>, inputs: ~[K1])
        -> ~[joinable_task]
    {
        let mut tasks = ~[];
        for inputs.each |i| {
            vec::push(tasks, spawn_joinable(|| map_task(map, ctrl, i) ));
        }
        ret tasks;
    }

    fn map_task<K1: copy send, K2: copy send, V: copy send>(
        map: mapper<K1, K2, V>,
        ctrl: chan<ctrl_proto<K2, V>>,
        input: K1)
    {
        // log(error, "map_task " + input);
        let intermediates = treemap::treemap();

        fn emit<K2: copy send, V: copy send>(
            im: treemap::treemap<K2, chan<reduce_proto<V>>>,
            ctrl: chan<ctrl_proto<K2, V>>, key: K2, val: V)
        {
            let c;
            alt treemap::find(im, key) {
              some(_c) { c = _c; }
              none {
                let p = port();
                send(ctrl, find_reducer(key, chan(p)));
                c = recv(p);
                treemap::insert(im, key, c);
                send(c, ref);
              }
            }
            send(c, emit_val(val));
        }

        map(input, {|a,b|emit(intermediates, ctrl, a, b)});

        fn finish<K: copy send, V: copy send>(_k: K, v: chan<reduce_proto<V>>)
        {
            send(v, release);
        }
        treemap::traverse(intermediates, finish);
        send(ctrl, mapper_done);
    }

    fn reduce_task<K: copy send, V: copy send>(
        reduce: reducer<K, V>,
        key: K,
        out: chan<chan<reduce_proto<V>>>)
    {
        let p = port();

        send(out, chan(p));

        let mut ref_count = 0;
        let mut is_done = false;

        fn get<V: copy send>(p: port<reduce_proto<V>>,
                             &ref_count: int, &is_done: bool)
           -> option<V> {
            while !is_done || ref_count > 0 {
                alt recv(p) {
                  emit_val(v) {
                    // #error("received %d", v);
                    ret some(v);
                  }
                  done {
                    // #error("all done");
                    is_done = true;
                  }
                  ref { ref_count += 1; }
                  release { ref_count -= 1; }
                }
            }
            ret none;
        }

        reduce(key, || get(p, ref_count, is_done) );
    }

    fn map_reduce<K1: copy send, K2: copy send, V: copy send>(
        map: mapper<K1, K2, V>,
        reduce: reducer<K2, V>,
        inputs: ~[K1])
    {
        let ctrl = port();

        // This task becomes the master control task. It task::_spawns
        // to do the rest.

        let reducers = treemap::treemap();
        let mut tasks = start_mappers(map, chan(ctrl), inputs);
        let mut num_mappers = vec::len(inputs) as int;

        while num_mappers > 0 {
            alt recv(ctrl) {
              mapper_done {
                // #error("received mapper terminated.");
                num_mappers -= 1;
              }
              find_reducer(k, cc) {
                let c;
                // log(error, "finding reducer for " + k);
                alt treemap::find(reducers, k) {
                  some(_c) {
                    // log(error,
                    // "reusing existing reducer for " + k);
                    c = _c;
                  }
                  none {
                    // log(error, "creating new reducer for " + k);
                    let p = port();
                    let ch = chan(p);
                    let r = reduce, kk = k;
                    vec::push(tasks,
                              spawn_joinable(|| reduce_task(r, kk, ch) ));
                    c = recv(p);
                    treemap::insert(reducers, k, c);
                  }
                }
                send(cc, c);
              }
            }
        }

        fn finish<K: copy send, V: copy send>(_k: K, v: chan<reduce_proto<V>>)
        {
            send(v, done);
        }
        treemap::traverse(reducers, finish);

        for tasks.each |t| { join(t); }
    }
}

fn main(argv: ~[str]) {
    if vec::len(argv) < 2u && !os::getenv("RUST_BENCH").is_some() {
        let out = io::stdout();

        out.write_line(#fmt["Usage: %s <filename> ...", argv[0]]);

        ret;
    }

    let readers: ~[fn~() -> word_reader]  = if argv.len() >= 2 {
        vec::view(argv, 1u, argv.len()).map(
            |f| fn~() -> word_reader { file_word_reader(f) } )
    }
    else {
        let num_readers = 50;
        let words_per_reader = 1000;
        vec::from_fn(
            num_readers,
            |_i| fn~() -> word_reader {
                random_word_reader(words_per_reader) as word_reader
            })
    };

    let start = time::precise_time_ns();

    map_reduce::map_reduce(map, reduce, readers);
    let stop = time::precise_time_ns();

    let elapsed = (stop - start) / 1000000u64;

    log(error, "MapReduce completed in "
             + u64::str(elapsed) + "ms");
}

fn read_word(r: io::reader) -> option<str> {
    let mut w = "";

    while !r.eof() {
        let c = r.read_char();

        if is_word_char(c) {
            w += str::from_char(c);
        } else { if w != "" { ret some(w); } }
    }
    ret none;
}

fn is_word_char(c: char) -> bool {
    char::is_alphabetic(c) || char::is_digit(c) || c == '_'
}

class random_word_reader: word_reader {
    let mut remaining: uint;
    let rng: rand::rng;
    new(count: uint) {
        self.remaining = count;
        self.rng = rand::rng();
    }

    fn read_word() -> option<str> {
        if self.remaining > 0 {
            self.remaining -= 1;
            some(self.rng.gen_str(5))
        }
        else { none }
    }
}
<|endoftext|>


<commit_before><commit_msg>Add dumpmodules as an example<commit_after>// Copyright 2016 Ted Mielczarek. See the COPYRIGHT
// file at the top-level directory of this distribution.

use std::env;
use std::ffi::OsStr;
use std::path::Path;
use std::io::Write;

extern crate breakpad_symbols;
extern crate minidump;

use breakpad_symbols::relative_symbol_path;
use minidump::*;

const USAGE : &'static str = "Usage: dumpmodules [-v|-d] <minidump>
Print full paths of modules from a minidump that were loaded in the crashed
process.

Options:
  -v  Also print debug IDs
  -d  Print relative paths to symbol files that would be loaded instead of
      module paths.";

#[derive(PartialEq)]
enum Verbose {
    Yes,
    No,
}

enum PrintMode {
    Modules,
    SymbolPaths,
}

fn print_minidump_modules<T: AsRef<Path>>(path: T,
                                          mode: PrintMode,
                                          verbose: Verbose) {
    match Minidump::read_path(path.as_ref()) {
        Ok(mut dump) => {
            if let Ok(module_list) = dump.get_stream::<MinidumpModuleList>() {
                for module in module_list.by_addr() {
                    match mode {
                        PrintMode::Modules => {
                            print!("{}", module.code_file());
                            if verbose == Verbose::Yes {
                                print!("\t");
                                if let Some(debug_id) = module.debug_identifier() {
                                    print!("{}", debug_id);
                                }
                            }
                            println!("");
                        }
                        PrintMode::SymbolPaths => {
                            if let Some(path) = relative_symbol_path(module, "sym") {
                                println!("{}", path);
                            }
                        }
                    }
                }
            }
        },
        Err(err) => {
            let mut stderr = std::io::stderr();
            writeln!(&mut stderr, "Error reading dump: {:?}", err).unwrap();
        }
    }
}

#[cfg_attr(test, allow(dead_code))]
fn main() {
    let mut verbose = Verbose::No;
    let mut mode = PrintMode::Modules;
    let mut stderr = std::io::stderr();
    for arg in env::args_os().skip(1) {
        if arg == OsStr::new("-v") {
            verbose = Verbose::Yes;
        } else if arg == OsStr::new("-d") {
            mode = PrintMode::SymbolPaths;
        } else if arg.to_str().map(|s| s.starts_with("-")).unwrap_or(false) {
            writeln!(&mut stderr, "Unknown argument {:?}", arg).unwrap();
            break;
        } else {
            return print_minidump_modules(arg, mode, verbose);
        }
    }
    writeln!(&mut stderr, "{}", USAGE).unwrap();
}
<|endoftext|>


<commit_before><commit_msg>remove unused benchmarks<commit_after><|endoftext|>


<commit_before><commit_msg>Rewrite and finish item parsers. Remove tokenizer and mix parsers and lexer. Fix some error in rules of literals. Compact the rules.<commit_after><|endoftext|>


<commit_before><commit_msg>Add bitflags library<commit_after>// Copyright 2014 The Rust Project Developers. See the COPYRIGHT
// file at the top-level directory of this distribution and at
// http://rust-lang.org/COPYRIGHT.
//
// Licensed under the Apache License, Version 2.0 <LICENSE-APACHE or
// http://www.apache.org/licenses/LICENSE-2.0> or the MIT license
// <LICENSE-MIT or http://opensource.org/licenses/MIT>, at your
// option. This file may not be copied, modified, or distributed
// except according to those terms.

//! A typesafe bitmask flag generator.

#![cfg_attr(test, feature(hash))]

/// The `bitflags!` macro generates a `struct` that holds a set of C-style
/// bitmask flags. It is useful for creating typesafe wrappers for C APIs.
///
/// The flags should only be defined for integer types, otherwise unexpected
/// type errors may occur at compile time.
///
/// # Example
///
/// ```{.rust}
/// #[macro_use]
/// extern crate bitflags;
///
/// bitflags! {
///     flags Flags: u32 {
///         const FLAG_A       = 0b00000001,
///         const FLAG_B       = 0b00000010,
///         const FLAG_C       = 0b00000100,
///         const FLAG_ABC     = FLAG_A.bits
///                            | FLAG_B.bits
///                            | FLAG_C.bits,
///     }
/// }
///
/// fn main() {
///     let e1 = FLAG_A | FLAG_C;
///     let e2 = FLAG_B | FLAG_C;
///     assert!((e1 | e2) == FLAG_ABC);   // union
///     assert!((e1 & e2) == FLAG_C);     // intersection
///     assert!((e1 - e2) == FLAG_A);     // set difference
///     assert!(!e2 == FLAG_A);           // set complement
/// }
/// ```
///
/// The generated `struct`s can also be extended with type and trait implementations:
///
/// ```{.rust}
/// #[macro_use]
/// extern crate bitflags;
///
/// use std::fmt;
///
/// bitflags! {
///     flags Flags: u32 {
///         const FLAG_A   = 0b00000001,
///         const FLAG_B   = 0b00000010,
///     }
/// }
///
/// impl Flags {
///     pub fn clear(&mut self) {
///         self.bits = 0;  // The `bits` field can be accessed from within the
///                         // same module where the `bitflags!` macro was invoked.
///     }
/// }
///
/// impl fmt::Debug for Flags {
///     fn fmt(&self, f: &mut fmt::Formatter) -> fmt::Result {
///         write!(f, "hi!")
///     }
/// }
///
/// fn main() {
///     let mut flags = FLAG_A | FLAG_B;
///     flags.clear();
///     assert!(flags.is_empty());
///     assert_eq!(format!("{:?}", flags), "hi!");
/// }
/// ```
///
/// # Attributes
///
/// Attributes can be attached to the generated `struct` by placing them
/// before the `flags` keyword.
///
/// # Derived traits
///
/// The `PartialEq` and `Clone` traits are automatically derived for the `struct` using
/// the `deriving` attribute. Additional traits can be derived by providing an
/// explicit `deriving` attribute on `flags`.
///
/// # Operators
///
/// The following operator traits are implemented for the generated `struct`:
///
/// - `BitOr`: union
/// - `BitAnd`: intersection
/// - `BitXor`: toggle
/// - `Sub`: set difference
/// - `Not`: set complement
///
/// # Methods
///
/// The following methods are defined for the generated `struct`:
///
/// - `empty`: an empty set of flags
/// - `all`: the set of all flags
/// - `bits`: the raw value of the flags currently stored
/// - `from_bits`: convert from underlying bit representation, unless that
///                representation contains bits that do not correspond to a flag
/// - `from_bits_truncate`: convert from underlying bit representation, dropping
///                         any bits that do not correspond to flags
/// - `is_empty`: `true` if no flags are currently stored
/// - `is_all`: `true` if all flags are currently set
/// - `intersects`: `true` if there are flags common to both `self` and `other`
/// - `contains`: `true` all of the flags in `other` are contained within `self`
/// - `insert`: inserts the specified flags in-place
/// - `remove`: removes the specified flags in-place
/// - `toggle`: the specified flags will be inserted if not present, and removed
///             if they are.
#[macro_export]
macro_rules! bitflags {
    ($(#[$attr:meta])* flags $BitFlags:ident: $T:ty {
        $($(#[$Flag_attr:meta])* const $Flag:ident = $value:expr),+
    }) => {
        #[derive(Copy, PartialEq, Eq, Clone, PartialOrd, Ord, Hash)]
        $(#[$attr])*
        pub struct $BitFlags {
            bits: $T,
        }

        $($(#[$Flag_attr])* pub const $Flag: $BitFlags = $BitFlags { bits: $value };)+

        impl $BitFlags {
            /// Returns an empty set of flags.
            #[inline]
            pub fn empty() -> $BitFlags {
                $BitFlags { bits: 0 }
            }

            /// Returns the set containing all flags.
            #[inline]
            pub fn all() -> $BitFlags {
                $BitFlags { bits: $($value)|+ }
            }

            /// Returns the raw value of the flags currently stored.
            #[inline]
            pub fn bits(&self) -> $T {
                self.bits
            }

            /// Convert from underlying bit representation, unless that
            /// representation contains bits that do not correspond to a flag.
            #[inline]
            pub fn from_bits(bits: $T) -> ::std::option::Option<$BitFlags> {
                if (bits & !$BitFlags::all().bits()) != 0 {
                    ::std::option::Option::None
                } else {
                    ::std::option::Option::Some($BitFlags { bits: bits })
                }
            }

            /// Convert from underlying bit representation, dropping any bits
            /// that do not correspond to flags.
            #[inline]
            pub fn from_bits_truncate(bits: $T) -> $BitFlags {
                $BitFlags { bits: bits } & $BitFlags::all()
            }

            /// Returns `true` if no flags are currently stored.
            #[inline]
            pub fn is_empty(&self) -> bool {
                *self == $BitFlags::empty()
            }

            /// Returns `true` if all flags are currently set.
            #[inline]
            pub fn is_all(&self) -> bool {
                *self == $BitFlags::all()
            }

            /// Returns `true` if there are flags common to both `self` and `other`.
            #[inline]
            pub fn intersects(&self, other: $BitFlags) -> bool {
                !(*self & other).is_empty()
            }

            /// Returns `true` all of the flags in `other` are contained within `self`.
            #[inline]
            pub fn contains(&self, other: $BitFlags) -> bool {
                (*self & other) == other
            }

            /// Inserts the specified flags in-place.
            #[inline]
            pub fn insert(&mut self, other: $BitFlags) {
                self.bits |= other.bits;
            }

            /// Removes the specified flags in-place.
            #[inline]
            pub fn remove(&mut self, other: $BitFlags) {
                self.bits &= !other.bits;
            }

            /// Toggles the specified flags in-place.
            #[inline]
            pub fn toggle(&mut self, other: $BitFlags) {
                self.bits ^= other.bits;
            }
        }

        impl ::std::ops::BitOr for $BitFlags {
            type Output = $BitFlags;

            /// Returns the union of the two sets of flags.
            #[inline]
            fn bitor(self, other: $BitFlags) -> $BitFlags {
                $BitFlags { bits: self.bits | other.bits }
            }
        }

        impl ::std::ops::BitXor for $BitFlags {
            type Output = $BitFlags;

            /// Returns the left flags, but with all the right flags toggled.
            #[inline]
            fn bitxor(self, other: $BitFlags) -> $BitFlags {
                $BitFlags { bits: self.bits ^ other.bits }
            }
        }

        impl ::std::ops::BitAnd for $BitFlags {
            type Output = $BitFlags;

            /// Returns the intersection between the two sets of flags.
            #[inline]
            fn bitand(self, other: $BitFlags) -> $BitFlags {
                $BitFlags { bits: self.bits & other.bits }
            }
        }

        impl ::std::ops::Sub for $BitFlags {
            type Output = $BitFlags;

            /// Returns the set difference of the two sets of flags.
            #[inline]
            fn sub(self, other: $BitFlags) -> $BitFlags {
                $BitFlags { bits: self.bits & !other.bits }
            }
        }

        impl ::std::ops::Not for $BitFlags {
            type Output = $BitFlags;

            /// Returns the complement of this set of flags.
            #[inline]
            fn not(self) -> $BitFlags {
                $BitFlags { bits: !self.bits } & $BitFlags::all()
            }
        }
    };
    ($(#[$attr:meta])* flags $BitFlags:ident: $T:ty {
        $($(#[$Flag_attr:meta])* const $Flag:ident = $value:expr),+,
    }) => {
        bitflags! {
            $(#[$attr])*
            flags $BitFlags: $T {
                $($(#[$Flag_attr])* const $Flag = $value),+
            }
        }
    };
}

#[cfg(test)]
#[allow(non_upper_case_globals)]
mod tests {
    use std::prelude::v1::*;
    use std::hash::{self, SipHasher};

    bitflags! {
        #[doc = "> The first principle is that you must not fool yourself — and"]
        #[doc = "> you are the easiest person to fool."]
        #[doc = "> "]
        #[doc = "> - Richard Feynman"]
        flags Flags: u32 {
            const FlagA       = 0b00000001,
            #[doc = "<pcwalton> macros are way better at generating code than trans is"]
            const FlagB       = 0b00000010,
            const FlagC       = 0b00000100,
            #[doc = "* cmr bed"]
            #[doc = "* strcat table"]
            #[doc = "<strcat> wait what?"]
            const FlagABC     = FlagA.bits
                               | FlagB.bits
                               | FlagC.bits,
        }
    }

    bitflags! {
        flags AnotherSetOfFlags: i8 {
            const AnotherFlag = -1_i8,
        }
    }

    #[test]
    fn test_bits(){
        assert_eq!(Flags::empty().bits(), 0b00000000);
        assert_eq!(FlagA.bits(), 0b00000001);
        assert_eq!(FlagABC.bits(), 0b00000111);

        assert_eq!(AnotherSetOfFlags::empty().bits(), 0b00);
        assert_eq!(AnotherFlag.bits(), !0_i8);
    }

    #[test]
    fn test_from_bits() {
        assert!(Flags::from_bits(0) == Some(Flags::empty()));
        assert!(Flags::from_bits(0b1) == Some(FlagA));
        assert!(Flags::from_bits(0b10) == Some(FlagB));
        assert!(Flags::from_bits(0b11) == Some(FlagA | FlagB));
        assert!(Flags::from_bits(0b1000) == None);

        assert!(AnotherSetOfFlags::from_bits(!0_i8) == Some(AnotherFlag));
    }

    #[test]
    fn test_from_bits_truncate() {
        assert!(Flags::from_bits_truncate(0) == Flags::empty());
        assert!(Flags::from_bits_truncate(0b1) == FlagA);
        assert!(Flags::from_bits_truncate(0b10) == FlagB);
        assert!(Flags::from_bits_truncate(0b11) == (FlagA | FlagB));
        assert!(Flags::from_bits_truncate(0b1000) == Flags::empty());
        assert!(Flags::from_bits_truncate(0b1001) == FlagA);

        assert!(AnotherSetOfFlags::from_bits_truncate(0_i8) == AnotherSetOfFlags::empty());
    }

    #[test]
    fn test_is_empty(){
        assert!(Flags::empty().is_empty());
        assert!(!FlagA.is_empty());
        assert!(!FlagABC.is_empty());

        assert!(!AnotherFlag.is_empty());
    }

    #[test]
    fn test_is_all() {
        assert!(Flags::all().is_all());
        assert!(!FlagA.is_all());
        assert!(FlagABC.is_all());

        assert!(AnotherFlag.is_all());
    }

    #[test]
    fn test_two_empties_do_not_intersect() {
        let e1 = Flags::empty();
        let e2 = Flags::empty();
        assert!(!e1.intersects(e2));

        assert!(AnotherFlag.intersects(AnotherFlag));
    }

    #[test]
    fn test_empty_does_not_intersect_with_full() {
        let e1 = Flags::empty();
        let e2 = FlagABC;
        assert!(!e1.intersects(e2));
    }

    #[test]
    fn test_disjoint_intersects() {
        let e1 = FlagA;
        let e2 = FlagB;
        assert!(!e1.intersects(e2));
    }

    #[test]
    fn test_overlapping_intersects() {
        let e1 = FlagA;
        let e2 = FlagA | FlagB;
        assert!(e1.intersects(e2));
    }

    #[test]
    fn test_contains() {
        let e1 = FlagA;
        let e2 = FlagA | FlagB;
        assert!(!e1.contains(e2));
        assert!(e2.contains(e1));
        assert!(FlagABC.contains(e2));

        assert!(AnotherFlag.contains(AnotherFlag));
    }

    #[test]
    fn test_insert(){
        let mut e1 = FlagA;
        let e2 = FlagA | FlagB;
        e1.insert(e2);
        assert!(e1 == e2);

        let mut e3 = AnotherSetOfFlags::empty();
        e3.insert(AnotherFlag);
        assert!(e3 == AnotherFlag);
    }

    #[test]
    fn test_remove(){
        let mut e1 = FlagA | FlagB;
        let e2 = FlagA | FlagC;
        e1.remove(e2);
        assert!(e1 == FlagB);

        let mut e3 = AnotherFlag;
        e3.remove(AnotherFlag);
        assert!(e3 == AnotherSetOfFlags::empty());
    }

    #[test]
    fn test_operators() {
        let e1 = FlagA | FlagC;
        let e2 = FlagB | FlagC;
        assert!((e1 | e2) == FlagABC);     // union
        assert!((e1 & e2) == FlagC);       // intersection
        assert!((e1 - e2) == FlagA);       // set difference
        assert!(!e2 == FlagA);             // set complement
        assert!(e1 ^ e2 == FlagA | FlagB); // toggle
        let mut e3 = e1;
        e3.toggle(e2);
        assert!(e3 == FlagA | FlagB);

        let mut m4 = AnotherSetOfFlags::empty();
        m4.toggle(AnotherSetOfFlags::empty());
        assert!(m4 == AnotherSetOfFlags::empty());
    }

    #[test]
    fn test_lt() {
        let mut a = Flags::empty();
        let mut b = Flags::empty();

        assert!(!(a < b) && !(b < a));
        b = FlagB;
        assert!(a < b);
        a = FlagC;
        assert!(!(a < b) && b < a);
        b = FlagC | FlagB;
        assert!(a < b);
    }

    #[test]
    fn test_ord() {
        let mut a = Flags::empty();
        let mut b = Flags::empty();

        assert!(a <= b && a >= b);
        a = FlagA;
        assert!(a > b && a >= b);
        assert!(b < a && b <= a);
        b = FlagB;
        assert!(b > a && b >= a);
        assert!(a < b && a <= b);
    }

    #[test]
    fn test_hash() {
      let mut x = Flags::empty();
      let mut y = Flags::empty();
      assert!(hash::hash::<Flags, SipHasher>(&x) == hash::hash::<Flags, SipHasher>(&y));
      x = Flags::all();
      y = FlagABC;
      assert!(hash::hash::<Flags, SipHasher>(&x) == hash::hash::<Flags, SipHasher>(&y));
    }
}
<|endoftext|>


<commit_before><commit_msg>title filter fixed<commit_after><|endoftext|>


<commit_before><commit_msg>Boosted generations per second by 100 just by using a smaller unit for each cell (i8)<commit_after><|endoftext|>


<commit_before><commit_msg>Added 404 error<commit_after><|endoftext|>


<commit_before><commit_msg>Update main.rs<commit_after><|endoftext|>


<commit_before><commit_msg>Fix macro imports<commit_after><|endoftext|>


<commit_before><commit_msg>Косметические изменения<commit_after><|endoftext|>


<commit_before><commit_msg>Complete `ast` and modify some names and structs.<commit_after><|endoftext|>


<commit_before><commit_msg>Add test for #5062<commit_after><|endoftext|>


<commit_before><commit_msg>add a failing test where an immovable generator gets moved<commit_after>#![feature(generators, generator_trait)]

use std::{
    ops::{Generator, GeneratorState},
    pin::Pin,
};

fn firstn() -> impl Generator<Yield = u64, Return = ()> {
    static move || {
        let mut num = 0;
        let num = &mut num;

        yield *num;
        *num += 1; //~ ERROR dangling pointer was dereferenced
    }
}

struct GeneratorIteratorAdapter<G>(G);

impl<G> Iterator for GeneratorIteratorAdapter<G>
where
    G: Generator<Return = ()>,
{
    type Item = G::Yield;

    fn next(&mut self) -> Option<Self::Item> {
        let me = unsafe { Pin::new_unchecked(&mut self.0) };
        match me.resume() {
            GeneratorState::Yielded(x) => Some(x),
            GeneratorState::Complete(_) => None,
        }
    }
}

fn main() {
    let mut generator_iterator_2 = {
        let mut generator_iterator = GeneratorIteratorAdapter(firstn());
        generator_iterator.next(); // pin it

        generator_iterator // move it
    }; // *deallocate* generator_iterator

    generator_iterator_2.next(); // and use moved value
}
<|endoftext|>


<commit_before>
fn foo(x:  fn(&u8, &u8), y: Vec<&u8>, z: &u8) {
// Debruijn   1    1            1        1
// Anon-Index 0    1            0        1
//            ------
//            debruijn indices are shifted by 1 in here
  y.push(z); // index will be zero or one
}
<commit_msg>Delete fn.rs<commit_after><|endoftext|>


<commit_before><commit_msg>Added several parsers for numeric-literal statement<commit_after>use nom::{digit};

named!(digits<&[u8]>,
    recognize!(digit)
);

named!(start_from_dot<&[u8]>,
    recognize!(
        complete!(
            pair!(
                tag!("."),
                digits
            )
        )
    )
);

named!(maybe_float_literal<&[u8]>,
    recognize!(
        complete!(
            pair!(
                digits,
                opt!(start_from_dot)
            )
        )
    )
);

named!(first_part_of_int_literal<&[u8]>,
    alt!(
        maybe_float_literal |
        start_from_dot
    )
);

#[cfg(test)]
mod tests {
    use super::*;
    use nom::{ErrorKind, Needed};

    #[test]
    fn test_digits_success_0() {
        let res = digits("0".as_bytes()).unwrap();
        assert_eq!(res, (&b""[..], &b"0"[..]));
    }

    #[test]
    fn test_digits_success_1() {
        let res = digits("012".as_bytes()).unwrap();
        assert_eq!(res, (&b""[..], &b"012"[..]));
    }

    #[test]
    fn test_digits_success_2() {
        let res = digits("012ab".as_bytes()).unwrap();
        assert_eq!(res, (&b"ab"[..], &b"012"[..]));
    }

    #[test]
    fn test_digits_success_3() {
        let res = digits("42ab".as_bytes()).unwrap();
        assert_eq!(res, (&b"ab"[..], &b"42"[..]));
    }

    #[test]
    fn test_digits_incomplete() {
        let res = digits("".as_bytes()).unwrap_inc();
        assert_eq!(res, Needed::Unknown);
    }

    #[test]
    fn test_digits_error() {
        let res = digits("a".as_bytes()).unwrap_err();
        assert_eq!(res, ErrorKind::Digit);
    }

    #[test]
    fn test_start_from_dot_success_0() {
        let res = start_from_dot(".0".as_bytes()).unwrap();
        assert_eq!(res, (&b""[..], &b".0"[..]));
    }

    #[test]
    fn test_start_from_dot_success_1() {
        let res = start_from_dot(".012".as_bytes()).unwrap();
        assert_eq!(res, (&b""[..], &b".012"[..]));
    }

    #[test]
    fn test_start_from_dot_success_2() {
        let res = start_from_dot(".012ab".as_bytes()).unwrap();
        assert_eq!(res, (&b"ab"[..], &b".012"[..]));
    }

    #[test]
    fn test_start_from_dot_success_3() {
        let res = start_from_dot(".42ab".as_bytes()).unwrap();
        assert_eq!(res, (&b"ab"[..], &b".42"[..]));
    }

    #[test]
    fn test_start_from_dot_error_0() {
        let res = start_from_dot("a".as_bytes()).unwrap_err();
        assert_eq!(res, ErrorKind::Tag);
    }

    #[test]
    fn test_start_from_dot_error_1() {
        let res = start_from_dot(".a".as_bytes()).unwrap_err();
        assert_eq!(res, ErrorKind::Digit);
    }

    #[test]
    fn test_start_from_dot_error_2() {
        let res = start_from_dot("".as_bytes()).unwrap_err();
        assert_eq!(res, ErrorKind::Complete);
    }

    #[test]
    fn test_start_from_dot_error_3() {
        let res = start_from_dot(".".as_bytes()).unwrap_err();
        assert_eq!(res, ErrorKind::Complete);
    }
}<|endoftext|>


<commit_before><commit_msg>some notes about Copy trait and std::mem::replace<commit_after>fn dont_drop_my_label() {

    struct Label { number: u32 }

    fn print(l: Label) { println!("STAMP: {}", l.number); }
    let l = Label { number : 3 };
    print(l); // this thing will take ownership and destroy my label
    println!("My label number is: {}", l.number); // so this line can do nothing - l is destoyed :(

    // solution is to implement Copy for struct Label.
    // compiler will help you with this.
    // #[derive(Copy, Clone)]
}

fn main() {
    struct Person {
        name: Option<String>,
        birth: i32,
    }

    let mut composers = Vec::new();

    composers.push(Person {
        name: Some("Palestrina".to_string()),
        birth: 1525,
    });

    // This two lines are equivalent
    let first_name = composers[0].name.take();
    // replace doc - https://doc.rust-lang.org/std/mem/fn.replace.html
    // let first_name = std::mem::replace(&mut composers[0].name, None);

    assert_eq!(first_name, Some("Palestrina".to_string()));
    assert_eq!(composers[0].name, None);
}
<|endoftext|>


<commit_before><commit_msg>define default words<commit_after><|endoftext|>


<commit_before>#![feature(core)]

extern crate core;

use std::Box;
use std::{io, rand};
use core::ptr;

macro_rules! readln {
    () => {
        {
            let mut line = String::new();
            match io::stdin().read_line(&mut line) {
                Ok(_) => Some(line.trim().to_string()),
                Err(_) => None
            }
        }
    };
}

fn console_title(title: &str){

}

#[no_mangle]
pub fn main() {
    console_title("Test");

    println!("Type help for a command list");
    while let Some(line) = readln!() {
        let args: Vec<String> = line.split(' ').map(|arg| arg.to_string()).collect();

        if let Some(a_command) = args.get(0) {
            println!("# {}", line);
            let console_commands = ["panic",
                                    "ls",
                                    "ptr_write",
                                    "box_write",];

            match &a_command[..] {
                command if command == console_commands[0] =>
                    panic!("Test panic"),
                command if command == console_commands[1] => {
                    // TODO: import std::fs functions into libredox
                    //fs::read_dir("/").unwrap().map(|dir| println!("{}", dir));
                }
                command if command == console_commands[2] => {
                    let a_ptr = rand() as *mut u8;
                    unsafe { ptr::write(a_ptr, rand() as u8); }
                }
                command if command == console_commands[3] => {
                    let mut a_box = Box::new(rand() as u8);
                    unsafe { ptr::write(Box::into_raw(a_box), rand() as u8); }
                }
                _ => println!("Commands: {}", console_commands.join(" ")),
            }
        }
    }
}
<commit_msg>Add more tests to test.rs<commit_after>#![feature(asm)]

use std::Box;
use std::{io, rand};
use std::ptr;

macro_rules! readln {
    () => {
        {
            let mut line = String::new();
            match io::stdin().read_line(&mut line) {
                Ok(_) => Some(line.trim().to_string()),
                Err(_) => None
            }
        }
    };
}

fn console_title(title: &str){

}

#[no_mangle]
pub fn main() {
    console_title("Test");

    println!("Type help for a command list");
    while let Some(line) = readln!() {
        let args: Vec<String> = line.split(' ').map(|arg| arg.to_string()).collect();

        if let Some(a_command) = args.get(0) {
            println!("# {}", line);
            let console_commands = ["panic",
                                    "ls",
                                    "ptr_write",
                                    "box_write",
                                    "reboot",
                                    "shutdown"];

            match &a_command[..] {
                command if command == console_commands[0] =>
                    panic!("Test panic"),
                command if command == console_commands[1] => {
                    // TODO: import std::fs functions into libredox
                    //fs::read_dir("/").unwrap().map(|dir| println!("{}", dir));
                }
                command if command == console_commands[2] => {
                    let a_ptr = rand() as *mut u8;
                    unsafe { ptr::write(a_ptr, rand() as u8); }
                }
                command if command == console_commands[3] => {
                    let mut a_box = Box::new(rand() as u8);
                    unsafe { ptr::write(Box::into_raw(a_box), rand() as u8); }
                }
                command if command == console_commands[4] => {
                    unsafe {
                        let mut good: u8 = 2;
                        while good & 2 == 2 {
                            asm!("in al, dx" : "={al}"(good) : "{dx}"(0x64) : : "intel", "volatile");
                        }
                        asm!("out dx, al" : : "{dx}"(0x64), "{al}"(0xFE) : : "intel", "volatile");
                        loop {
                            asm!("cli" : : : : "intel", "volatile");
                            asm!("hlt" : : : : "intel", "volatile");
                        }
                    }
                }
                command if command == console_commands[5] => {
                    unsafe {
                        loop {
                            asm!("cli" : : : : "intel", "volatile");
                            asm!("hlt" : : : : "intel", "volatile");
                        }
                    }
                }
                _ => println!("Commands: {}", console_commands.join(" ")),
            }
        }
    }
}
<|endoftext|>


<commit_before><commit_msg>easage-list: Add an 'order' parameter to control EntryOrderCriteria<commit_after><|endoftext|>


<commit_before><commit_msg>Added files via upload<commit_after>//#[macro_use] extern crate nickel;

fn  main() {
   println!("Hello World");
}
<|endoftext|>


<commit_before><commit_msg>Truncate flag<commit_after><|endoftext|>


<commit_before>/**
   A parallel word-frequency counting program.

   This is meant primarily to demonstrate Rust's MapReduce framework.

   It takes a list of files on the command line and outputs a list of
   words along with how many times each word is used.

*/

use std;

import option = option;
import option::some;
import option::none;
import str;
import std::treemap;
import vec;
import io;
import io::{reader_util, writer_util};

import std::time;
import u64;

import task;
import comm;
import comm::chan;
import comm::port;
import comm::recv;
import comm::send;
import comm::methods;

// These used to be in task, but they disappeard.
type joinable_task = port<()>;
fn spawn_joinable(f: fn~()) -> joinable_task {
    let p = port();
    let c = chan(p);
    task::spawn() {||
        f();
        c.send(());
    }
    p
}

fn join(t: joinable_task) {
    t.recv()
}

fn map(&&filename: [u8], emit: map_reduce::putter<[u8], int>) {
    let f = alt io::file_reader(str::from_bytes(filename)) {
      result::ok(f) { f }
      result::err(e) { fail #fmt("%?", e) }
    };

    loop {
        alt read_word(f) {
          some(w) { emit(str::bytes(w), 1); }
          none { break; }
        }
    }
}

fn reduce(&&word: [u8], get: map_reduce::getter<int>) {
    let mut count = 0;

    loop { alt get() { some(_) { count += 1; } none { break; } } }

    io::println(#fmt("%?\t%?", word, count));
}

mod map_reduce {
    export putter;
    export getter;
    export mapper;
    export reducer;
    export map_reduce;

    type putter<K: send, V: send> = fn(K, V);

    // FIXME: the first K1 parameter should probably be a -, but that
    // doesn't parse at the moment.
    type mapper<K1: send, K2: send, V: send> = fn~(K1, putter<K2, V>);

    type getter<V: send> = fn() -> option<V>;

    type reducer<K: copy send, V: copy send> = fn~(K, getter<V>);

    enum ctrl_proto<K: copy send, V: copy send> {
        find_reducer(K, chan<chan<reduce_proto<V>>>),
        mapper_done
    }

    enum reduce_proto<V: copy send> { emit_val(V), done, ref, release }

    fn start_mappers<K1: copy send, K2: copy send, V: copy send>(
        map: mapper<K1, K2, V>,
        ctrl: chan<ctrl_proto<K2, V>>, inputs: [K1])
        -> [joinable_task]
    {
        let mut tasks = [];
        for inputs.each {|i|
            tasks += [spawn_joinable {|| map_task(map, ctrl, i)}];
        }
        ret tasks;
    }

    fn map_task<K1: copy send, K2: copy send, V: copy send>(
        map: mapper<K1, K2, V>,
        ctrl: chan<ctrl_proto<K2, V>>,
        input: K1)
    {
        // log(error, "map_task " + input);
        let intermediates = treemap::treemap();

        fn emit<K2: copy send, V: copy send>(
            im: treemap::treemap<K2, chan<reduce_proto<V>>>,
            ctrl: chan<ctrl_proto<K2, V>>, key: K2, val: V)
        {
            let c;
            alt treemap::find(im, key) {
              some(_c) { c = _c; }
              none {
                let p = port();
                send(ctrl, find_reducer(key, chan(p)));
                c = recv(p);
                treemap::insert(im, key, c);
                send(c, ref);
              }
            }
            send(c, emit_val(val));
        }

        map(input, bind emit(intermediates, ctrl, _, _));

        fn finish<K: copy send, V: copy send>(_k: K, v: chan<reduce_proto<V>>)
        {
            send(v, release);
        }
        treemap::traverse(intermediates, finish);
        send(ctrl, mapper_done);
    }

    fn reduce_task<K: copy send, V: copy send>(
        reduce: reducer<K, V>,
        key: K,
        out: chan<chan<reduce_proto<V>>>)
    {
        let p = port();

        send(out, chan(p));

        let ref_count = 0;
        let is_done = false;

        fn get<V: copy send>(p: port<reduce_proto<V>>,
                             &ref_count: int, &is_done: bool)
           -> option<V> {
            while !is_done || ref_count > 0 {
                alt recv(p) {
                  emit_val(v) {
                    // #error("received %d", v);
                    ret some(v);
                  }
                  done {
                    // #error("all done");
                    is_done = true;
                  }
                  ref { ref_count += 1; }
                  release { ref_count -= 1; }
                }
            }
            ret none;
        }

        reduce(key, bind get(p, ref_count, is_done));
    }

    fn map_reduce<K1: copy send, K2: copy send, V: copy send>(
        map: mapper<K1, K2, V>,
        reduce: reducer<K2, V>,
        inputs: [K1])
    {
        let ctrl = port();

        // This task becomes the master control task. It task::_spawns
        // to do the rest.

        let reducers = treemap::treemap();
        let mut tasks = start_mappers(map, chan(ctrl), inputs);
        let mut num_mappers = vec::len(inputs) as int;

        while num_mappers > 0 {
            alt recv(ctrl) {
              mapper_done {
                // #error("received mapper terminated.");
                num_mappers -= 1;
              }
              find_reducer(k, cc) {
                let c;
                // log(error, "finding reducer for " + k);
                alt treemap::find(reducers, k) {
                  some(_c) {
                    // log(error,
                    // "reusing existing reducer for " + k);
                    c = _c;
                  }
                  none {
                    // log(error, "creating new reducer for " + k);
                    let p = port();
                    let ch = chan(p);
                    let r = reduce, kk = k;
                    tasks += [
                        spawn_joinable {|| reduce_task(r, kk, ch) }
                    ];
                    c = recv(p);
                    treemap::insert(reducers, k, c);
                  }
                }
                send(cc, c);
              }
            }
        }

        fn finish<K: copy send, V: copy send>(_k: K, v: chan<reduce_proto<V>>)
        {
            send(v, done);
        }
        treemap::traverse(reducers, finish);

        for tasks.each {|t| join(t); }
    }
}

fn main(argv: [str]) {
    if vec::len(argv) < 2u {
        let out = io::stdout();

        out.write_line(#fmt["Usage: %s <filename> ...", argv[0]]);

        // TODO: run something just to make sure the code hasn't
        // broken yet. This is the unit test mode of this program.

        ret;
    }

    let mut iargs = [];
    vec::iter_between(argv, 1u, vec::len(argv)) {|a|
        iargs += [str::bytes(a)];
    }

    let start = time::precise_time_ns();

    map_reduce::map_reduce(map, reduce, iargs);
    let stop = time::precise_time_ns();

    let elapsed = (stop - start) / 1000000u64;

    log(error, "MapReduce completed in "
             + u64::str(elapsed) + "ms");
}

fn read_word(r: io::reader) -> option<str> {
    let mut w = "";

    while !r.eof() {
        let c = r.read_char();

        if is_word_char(c) {
            w += str::from_char(c);
        } else { if w != "" { ret some(w); } }
    }
    ret none;
}

fn is_digit(c: char) -> bool {
    alt c {
      '0' { true }
      '1' { true }
      '2' { true }
      '3' { true }
      '4' { true }
      '5' { true }
      '6' { true }
      '7' { true }
      '8' { true }
      '9' { true }
      _ { false }
    }
}

fn is_alpha_lower(c: char) -> bool {
    alt c {
      'a' { true }
      'b' { true }
      'c' { true }
      'd' { true }
      'e' { true }
      'f' { true }
      'g' { true }
      'h' { true }
      'i' { true }
      'j' { true }
      'k' { true }
      'l' { true }
      'm' { true }
      'n' { true }
      'o' { true }
      'p' { true }
      'q' { true }
      'r' { true }
      's' { true }
      't' { true }
      'u' { true }
      'v' { true }
      'w' { true }
      'x' { true }
      'y' { true }
      'z' { true }
      _ { false }
    }
}

fn is_alpha_upper(c: char) -> bool {
    alt c {
      'A' { true }
      'B' { true }
      'C' { true }
      'D' { true }
      'E' { true }
      'F' { true }
      'G' { true }
      'H' { true }
      'I' { true }
      'J' { true }
      'K' { true }
      'L' { true }
      'M' { true }
      'N' { true }
      'O' { true }
      'P' { true }
      'Q' { true }
      'R' { true }
      'S' { true }
      'T' { true }
      'U' { true }
      'V' { true }
      'W' { true }
      'X' { true }
      'Y' { true }
      'Z' { true }
      _ { false }
    }
}

fn is_alpha(c: char) -> bool { is_alpha_upper(c) || is_alpha_lower(c) }

fn is_word_char(c: char) -> bool { is_alpha(c) || is_digit(c) || c == '_' }
<commit_msg>In generic word count, use str instead of [u8], and use built in is_alphabetic, etc. functions.<commit_after>/**
   A parallel word-frequency counting program.

   This is meant primarily to demonstrate Rust's MapReduce framework.

   It takes a list of files on the command line and outputs a list of
   words along with how many times each word is used.

*/

use std;

import option = option;
import option::some;
import option::none;
import str;
import std::treemap;
import vec;
import io;
import io::{reader_util, writer_util};

import std::time;
import u64;

import task;
import comm;
import comm::chan;
import comm::port;
import comm::recv;
import comm::send;
import comm::methods;

// These used to be in task, but they disappeard.
type joinable_task = port<()>;
fn spawn_joinable(f: fn~()) -> joinable_task {
    let p = port();
    let c = chan(p);
    task::spawn() {||
        f();
        c.send(());
    }
    p
}

fn join(t: joinable_task) {
    t.recv()
}

fn map(&&filename: str, emit: map_reduce::putter<str, int>) {
    let f = alt io::file_reader(filename) {
      result::ok(f) { f }
      result::err(e) { fail #fmt("%?", e) }
    };

    loop {
        alt read_word(f) {
          some(w) { emit(w, 1); }
          none { break; }
        }
    }
}

fn reduce(&&word: str, get: map_reduce::getter<int>) {
    let mut count = 0;

    loop { alt get() { some(_) { count += 1; } none { break; } } }

    io::println(#fmt("%s\t%?", word, count));
}

mod map_reduce {
    export putter;
    export getter;
    export mapper;
    export reducer;
    export map_reduce;

    type putter<K: send, V: send> = fn(K, V);

    // FIXME: the first K1 parameter should probably be a -, but that
    // doesn't parse at the moment.
    type mapper<K1: send, K2: send, V: send> = fn~(K1, putter<K2, V>);

    type getter<V: send> = fn() -> option<V>;

    type reducer<K: copy send, V: copy send> = fn~(K, getter<V>);

    enum ctrl_proto<K: copy send, V: copy send> {
        find_reducer(K, chan<chan<reduce_proto<V>>>),
        mapper_done
    }

    enum reduce_proto<V: copy send> { emit_val(V), done, ref, release }

    fn start_mappers<K1: copy send, K2: copy send, V: copy send>(
        map: mapper<K1, K2, V>,
        ctrl: chan<ctrl_proto<K2, V>>, inputs: [K1])
        -> [joinable_task]
    {
        let mut tasks = [];
        for inputs.each {|i|
            tasks += [spawn_joinable {|| map_task(map, ctrl, i)}];
        }
        ret tasks;
    }

    fn map_task<K1: copy send, K2: copy send, V: copy send>(
        map: mapper<K1, K2, V>,
        ctrl: chan<ctrl_proto<K2, V>>,
        input: K1)
    {
        // log(error, "map_task " + input);
        let intermediates = treemap::treemap();

        fn emit<K2: copy send, V: copy send>(
            im: treemap::treemap<K2, chan<reduce_proto<V>>>,
            ctrl: chan<ctrl_proto<K2, V>>, key: K2, val: V)
        {
            let c;
            alt treemap::find(im, key) {
              some(_c) { c = _c; }
              none {
                let p = port();
                send(ctrl, find_reducer(key, chan(p)));
                c = recv(p);
                treemap::insert(im, key, c);
                send(c, ref);
              }
            }
            send(c, emit_val(val));
        }

        map(input, bind emit(intermediates, ctrl, _, _));

        fn finish<K: copy send, V: copy send>(_k: K, v: chan<reduce_proto<V>>)
        {
            send(v, release);
        }
        treemap::traverse(intermediates, finish);
        send(ctrl, mapper_done);
    }

    fn reduce_task<K: copy send, V: copy send>(
        reduce: reducer<K, V>,
        key: K,
        out: chan<chan<reduce_proto<V>>>)
    {
        let p = port();

        send(out, chan(p));

        let ref_count = 0;
        let is_done = false;

        fn get<V: copy send>(p: port<reduce_proto<V>>,
                             &ref_count: int, &is_done: bool)
           -> option<V> {
            while !is_done || ref_count > 0 {
                alt recv(p) {
                  emit_val(v) {
                    // #error("received %d", v);
                    ret some(v);
                  }
                  done {
                    // #error("all done");
                    is_done = true;
                  }
                  ref { ref_count += 1; }
                  release { ref_count -= 1; }
                }
            }
            ret none;
        }

        reduce(key, bind get(p, ref_count, is_done));
    }

    fn map_reduce<K1: copy send, K2: copy send, V: copy send>(
        map: mapper<K1, K2, V>,
        reduce: reducer<K2, V>,
        inputs: [K1])
    {
        let ctrl = port();

        // This task becomes the master control task. It task::_spawns
        // to do the rest.

        let reducers = treemap::treemap();
        let mut tasks = start_mappers(map, chan(ctrl), inputs);
        let mut num_mappers = vec::len(inputs) as int;

        while num_mappers > 0 {
            alt recv(ctrl) {
              mapper_done {
                // #error("received mapper terminated.");
                num_mappers -= 1;
              }
              find_reducer(k, cc) {
                let c;
                // log(error, "finding reducer for " + k);
                alt treemap::find(reducers, k) {
                  some(_c) {
                    // log(error,
                    // "reusing existing reducer for " + k);
                    c = _c;
                  }
                  none {
                    // log(error, "creating new reducer for " + k);
                    let p = port();
                    let ch = chan(p);
                    let r = reduce, kk = k;
                    tasks += [
                        spawn_joinable {|| reduce_task(r, kk, ch) }
                    ];
                    c = recv(p);
                    treemap::insert(reducers, k, c);
                  }
                }
                send(cc, c);
              }
            }
        }

        fn finish<K: copy send, V: copy send>(_k: K, v: chan<reduce_proto<V>>)
        {
            send(v, done);
        }
        treemap::traverse(reducers, finish);

        for tasks.each {|t| join(t); }
    }
}

fn main(argv: [str]) {
    if vec::len(argv) < 2u {
        let out = io::stdout();

        out.write_line(#fmt["Usage: %s <filename> ...", argv[0]]);

        // TODO: run something just to make sure the code hasn't
        // broken yet. This is the unit test mode of this program.

        ret;
    }

    let start = time::precise_time_ns();

    map_reduce::map_reduce(map, reduce, vec::slice(argv, 1u, argv.len()));
    let stop = time::precise_time_ns();

    let elapsed = (stop - start) / 1000000u64;

    log(error, "MapReduce completed in "
             + u64::str(elapsed) + "ms");
}

fn read_word(r: io::reader) -> option<str> {
    let mut w = "";

    while !r.eof() {
        let c = r.read_char();

        if is_word_char(c) {
            w += str::from_char(c);
        } else { if w != "" { ret some(w); } }
    }
    ret none;
}
fn is_word_char(c: char) -> bool {
    char::is_alphabetic(c) || char::is_digit(c) || c == '_' }
<|endoftext|>


<commit_before><commit_msg>Clear the terminal before printing loading results<commit_after><|endoftext|>


<commit_before>use core::usize;

use io::{Read, Write, Seek, SeekFrom};
use str;
use string::{String, ToString};
use vec::Vec;

use syscall::{sys_open, sys_dup, sys_close, sys_execve, sys_fpath, sys_ftruncate, sys_read, sys_write, sys_lseek, sys_fsync, sys_chdir};
use syscall::common::{O_RDWR, O_CREAT, O_TRUNC, SEEK_SET, SEEK_CUR, SEEK_END};

/// A Unix-style file
pub struct File {
    /// The id for the file
    fd: usize,
}

impl File {
    pub fn exec(path: &str, args: &[&str]) -> bool {
        let path_c = path.to_string() + "\0";

        let mut args_vec: Vec<String> = Vec::new();
        for arg in args.iter() {
            args_vec.push(arg.to_string() + "\0");
        }

        let mut args_c: Vec<*const u8> = Vec::new();
        for arg_vec in args_vec.iter() {
            args_c.push(arg_vec.as_ptr());
        }
        args_c.push(0 as *const u8);

        unsafe {
            sys_execve(path_c.as_ptr(), args_c.as_ptr()) == 0
        }
    }

    /// Open a new file using a path
    pub fn open(path: &str) -> Option<File> {
        unsafe {
            let fd = sys_open((path.to_string() + "\0").as_ptr(), O_RDWR, 0);
            if fd == usize::MAX {
                None
            } else {
                Some(File {
                    fd: fd
                })
            }
        }
    }

    /// Create a new file using a path
    pub fn create(path: &str) -> Option<File> {
        unsafe {
            let fd = sys_open((path.to_string() + "\0").as_ptr(), O_CREAT | O_RDWR | O_TRUNC, 0);
            if fd == usize::MAX {
                None
            } else {
                Some(File {
                    fd: fd
                })
            }
        }
    }

    /// Duplicate the file
    pub fn dup(&self) -> Option<File> {
        unsafe{
            let new_fd = sys_dup(self.fd);
            if new_fd == usize::MAX {
                None
            } else {
                Some(File {
                    fd: new_fd
                })
            }
        }
    }

    /// Get the canonical path of the file
    pub fn path(&self) -> Option<String> {
        unsafe {
            let mut buf: [u8; 4096] = [0; 4096];
            let count = sys_fpath(self.fd, buf.as_mut_ptr(), buf.len());
            if count == usize::MAX {
                None
            } else {
                Some(String::from_utf8_unchecked(Vec::from(&buf[0..count])))
            }
        }
    }

    /// Flush the io
    pub fn sync(&mut self) -> bool {
        unsafe { sys_fsync(self.fd) == 0 }
    }

    pub fn set_len(&mut self, size: usize) -> bool {
        unsafe { sys_ftruncate(self.fd, size) == 0 }
    }
}

impl Read for File {
    fn read(&mut self, buf: &mut [u8]) -> Option<usize> {
        unsafe {
            let count = sys_read(self.fd, buf.as_mut_ptr(), buf.len());
            if count == usize::MAX {
                None
            } else {
                Some(count)
            }
        }
    }
}

impl Write for File {
    fn write(&mut self, buf: &[u8]) -> Option<usize> {
        unsafe {
            let count = sys_write(self.fd, buf.as_ptr(), buf.len());
            if count == usize::MAX {
                None
            } else {
                Some(count)
            }
        }
    }
}

impl Seek for File {
    /// Seek a given position
    fn seek(&mut self, pos: SeekFrom) -> Option<usize> {
        let (whence, offset) = match pos {
            SeekFrom::Start(offset) => (SEEK_SET, offset as isize),
            SeekFrom::Current(offset) => (SEEK_CUR, offset),
            SeekFrom::End(offset) => (SEEK_END, offset),
        };

        let position = unsafe { sys_lseek(self.fd, offset, whence) };
        if position == usize::MAX {
            None
        } else {
            Some(position)
        }
    }
}

impl Drop for File {
    fn drop(&mut self) {
        unsafe {
            sys_close(self.fd);
        }
    }
}

pub struct DirEntry {
    path: String
}

impl DirEntry {
    pub fn path(&self) -> &str {
        &self.path
    }
}

pub struct ReadDir {
    file: File
}

impl Iterator for ReadDir {
    type Item = DirEntry;
    fn next(&mut self) -> Option<DirEntry> {
        let mut path = String::new();
        let mut buf: [u8; 1] = [0; 1];
        loop {
            match self.file.read(&mut buf) {
                Some(0) => break,
                Some(count) => {
                    if buf[0] == 10 {
                        break;
                    } else {
                        path.push_str(unsafe { str::from_utf8_unchecked(&buf[.. count]) });
                    }
                },
                None => break
            }
        }
        if path.is_empty() {
            None
        }else {
            Some(DirEntry {
                path: path
            })
        }
    }
}

pub fn read_dir(path: &str) -> Option<ReadDir> {
    let file_option = if path.is_empty() || path.ends_with('/') {
        File::open(path)
    } else {
        File::open(&(path.to_string() + "/"))
    };

    if let Some(file) = file_option {
        Some(ReadDir{
            file: file
        })
    } else {
        None
    }
}

pub fn change_cwd(path: &str) -> bool {
    let file_option = if path.is_empty() || path.ends_with('/') {
        File::open(path)
    } else {
        File::open(&(path.to_string() + "/"))
    };

    if let Some(file) = file_option {
        if let Some(file_path) = file.path() {
            if unsafe { sys_chdir((file_path + "\0").as_ptr()) } == 0 {
                return true;
            }
        }
    }

    false
}
<commit_msg>Import syscall sys_mkdir<commit_after>use core::usize;

use io::{Read, Write, Seek, SeekFrom};
use str;
use string::{String, ToString};
use vec::Vec;

use syscall::{sys_open, sys_dup, sys_close, sys_execve, sys_fpath, sys_ftruncate, sys_read, sys_write, sys_lseek, sys_fsync, sys_chdir, sys_mkdir};
use syscall::common::{O_RDWR, O_CREAT, O_TRUNC, SEEK_SET, SEEK_CUR, SEEK_END};

/// A Unix-style file
pub struct File {
    /// The id for the file
    fd: usize,
}

impl File {
    pub fn exec(path: &str, args: &[&str]) -> bool {
        let path_c = path.to_string() + "\0";

        let mut args_vec: Vec<String> = Vec::new();
        for arg in args.iter() {
            args_vec.push(arg.to_string() + "\0");
        }

        let mut args_c: Vec<*const u8> = Vec::new();
        for arg_vec in args_vec.iter() {
            args_c.push(arg_vec.as_ptr());
        }
        args_c.push(0 as *const u8);

        unsafe {
            sys_execve(path_c.as_ptr(), args_c.as_ptr()) == 0
        }
    }

    /// Open a new file using a path
    pub fn open(path: &str) -> Option<File> {
        unsafe {
            let fd = sys_open((path.to_string() + "\0").as_ptr(), O_RDWR, 0);
            if fd == usize::MAX {
                None
            } else {
                Some(File {
                    fd: fd
                })
            }
        }
    }

    /// Create a new file using a path
    pub fn create(path: &str) -> Option<File> {
        unsafe {
            let fd = sys_open((path.to_string() + "\0").as_ptr(), O_CREAT | O_RDWR | O_TRUNC, 0);
            if fd == usize::MAX {
                None
            } else {
                Some(File {
                    fd: fd
                })
            }
        }
    }

    /// Duplicate the file
    pub fn dup(&self) -> Option<File> {
        unsafe{
            let new_fd = sys_dup(self.fd);
            if new_fd == usize::MAX {
                None
            } else {
                Some(File {
                    fd: new_fd
                })
            }
        }
    }

    /// Get the canonical path of the file
    pub fn path(&self) -> Option<String> {
        unsafe {
            let mut buf: [u8; 4096] = [0; 4096];
            let count = sys_fpath(self.fd, buf.as_mut_ptr(), buf.len());
            if count == usize::MAX {
                None
            } else {
                Some(String::from_utf8_unchecked(Vec::from(&buf[0..count])))
            }
        }
    }

    /// Flush the io
    pub fn sync(&mut self) -> bool {
        unsafe { sys_fsync(self.fd) == 0 }
    }

    pub fn set_len(&mut self, size: usize) -> bool {
        unsafe { sys_ftruncate(self.fd, size) == 0 }
    }
}

impl Read for File {
    fn read(&mut self, buf: &mut [u8]) -> Option<usize> {
        unsafe {
            let count = sys_read(self.fd, buf.as_mut_ptr(), buf.len());
            if count == usize::MAX {
                None
            } else {
                Some(count)
            }
        }
    }
}

impl Write for File {
    fn write(&mut self, buf: &[u8]) -> Option<usize> {
        unsafe {
            let count = sys_write(self.fd, buf.as_ptr(), buf.len());
            if count == usize::MAX {
                None
            } else {
                Some(count)
            }
        }
    }
}

impl Seek for File {
    /// Seek a given position
    fn seek(&mut self, pos: SeekFrom) -> Option<usize> {
        let (whence, offset) = match pos {
            SeekFrom::Start(offset) => (SEEK_SET, offset as isize),
            SeekFrom::Current(offset) => (SEEK_CUR, offset),
            SeekFrom::End(offset) => (SEEK_END, offset),
        };

        let position = unsafe { sys_lseek(self.fd, offset, whence) };
        if position == usize::MAX {
            None
        } else {
            Some(position)
        }
    }
}

impl Drop for File {
    fn drop(&mut self) {
        unsafe {
            sys_close(self.fd);
        }
    }
}

pub struct DirEntry {
    path: String
}

impl DirEntry {
    pub fn path(&self) -> &str {
        &self.path
    }
}

pub struct ReadDir {
    file: File
}

impl Iterator for ReadDir {
    type Item = DirEntry;
    fn next(&mut self) -> Option<DirEntry> {
        let mut path = String::new();
        let mut buf: [u8; 1] = [0; 1];
        loop {
            match self.file.read(&mut buf) {
                Some(0) => break,
                Some(count) => {
                    if buf[0] == 10 {
                        break;
                    } else {
                        path.push_str(unsafe { str::from_utf8_unchecked(&buf[.. count]) });
                    }
                },
                None => break
            }
        }
        if path.is_empty() {
            None
        }else {
            Some(DirEntry {
                path: path
            })
        }
    }
}

pub fn read_dir(path: &str) -> Option<ReadDir> {
    let file_option = if path.is_empty() || path.ends_with('/') {
        File::open(path)
    } else {
        File::open(&(path.to_string() + "/"))
    };

    if let Some(file) = file_option {
        Some(ReadDir{
            file: file
        })
    } else {
        None
    }
}

pub fn change_cwd(path: &str) -> bool {
    let file_option = if path.is_empty() || path.ends_with('/') {
        File::open(path)
    } else {
        File::open(&(path.to_string() + "/"))
    };

    if let Some(file) = file_option {
        if let Some(file_path) = file.path() {
            if unsafe { sys_chdir((file_path + "\0").as_ptr()) } == 0 {
                return true;
            }
        }
    }

    false
}
<|endoftext|>


<commit_before><commit_msg>practice<commit_after>fn main() {
    println!("practice")
}
<|endoftext|>


<commit_before><commit_msg>Added trait implementation for integer types<commit_after><|endoftext|>


<commit_before><commit_msg>mem: add `swap` function<commit_after><|endoftext|>


<commit_before><commit_msg>add `replace` function<commit_after><|endoftext|>


<commit_before><commit_msg>add privacy view<commit_after><|endoftext|>


<commit_before><commit_msg>Fix API test<commit_after><|endoftext|>


<commit_before><commit_msg>Minor import tidy.<commit_after><|endoftext|>