<commit_before>use crate::data::{Id, Item, Rating};
use crate::helpers::{ElementDataRef, QuerySelector};
use html5ever::{expanded_name, local_name, namespace_url, ns};
use kuchiki::NodeRef;

fn get_item_id(elem: &ElementDataRef) -> Id {
    static ID_PREFIX: &'static str = "item_";
    let attrs = elem.attributes.borrow();
    let id = attrs.get(local_name!("id")).unwrap();
    let (prefix, id_str) = id.split_at(ID_PREFIX.len());
    assert!(prefix == ID_PREFIX);
    id_str.parse().unwrap()
}

fn get_item_title(elem: &ElementDataRef) -> String {
    let title_node = elem.query_selector("h3>*:last-child").unwrap();
    assert!(
        title_node.name.expanded() == expanded_name!(html "small")
            || title_node.name.expanded() == expanded_name!(html "a")
    );
    title_node.text_contents()
}

fn get_item_rating(elem: &ElementDataRef) -> Option<Rating> {
    static STARS_PREFIX: &'static str = "sstars";
    elem.query_selector(".starsinfo").map(|e| {
        let mut result = None;
        let attrs = e.attributes.borrow();
        let classes = attrs.get(local_name!("class")).unwrap();
        for class in classes.split_whitespace() {
            let (prefix, class_str) = class.split_at(STARS_PREFIX.len());
            if prefix == STARS_PREFIX {
                let rating = class_str.parse().unwrap();
                assert!(rating >= 1 && rating <= 10);
                result = Some(rating);
                break;
            }
        }
        result.unwrap()
    })
}

fn get_item_tags(elem: &ElementDataRef) -> Vec<String> {
    static TAGS_PREFIX: &'static str = "标签: ";
    if let Some(tags_elem) = elem.query_selector(".collectInfo>.tip") {
        let all_text = tags_elem.text_contents();
        let tag_text = all_text.trim();
        assert!(tag_text.starts_with(TAGS_PREFIX));
        tag_text[TAGS_PREFIX.len()..]
            .split(" ")
            .filter_map(|s| {
                if !s.is_empty() {
                    Some(s.to_string())
                } else {
                    None
                }
            })
            .collect()
    } else {
        vec![]
    }
}

fn generate_item_from_node(elem: &ElementDataRef) -> Item {
    Item {
        id: get_item_id(elem),
        title: get_item_title(elem),
        rating: get_item_rating(elem),
        tags: get_item_tags(elem),
    }
}

pub fn get_all_items(html: NodeRef) -> Vec<Item> {
    html.select("#browserItemList>li")
        .unwrap()
        .map(|elem| generate_item_from_node(&elem))
        .collect()
}
<commit_msg>简化部分代码<commit_after>use crate::data::{Id, Item, Rating};
use crate::helpers::{ElementDataRef, QuerySelector};
use html5ever::{expanded_name, local_name, namespace_url, ns};
use kuchiki::NodeRef;

fn get_item_id(elem: &ElementDataRef) -> Id {
    static ID_PREFIX: &'static str = "item_";
    let attrs = elem.attributes.borrow();
    let id = attrs.get(local_name!("id")).unwrap();
    let (prefix, id_str) = id.split_at(ID_PREFIX.len());
    assert!(prefix == ID_PREFIX);
    id_str.parse().unwrap()
}

fn get_item_title(elem: &ElementDataRef) -> String {
    let title_node = elem.query_selector("h3>*:last-child").unwrap();
    assert!(
        title_node.name.expanded() == expanded_name!(html "small")
            || title_node.name.expanded() == expanded_name!(html "a")
    );
    title_node.text_contents()
}

fn get_item_rating(elem: &ElementDataRef) -> Option<Rating> {
    static STARS_PREFIX: &'static str = "sstars";
    let elem = elem.query_selector(".starsinfo")?;
    let attrs = elem.attributes.borrow();
    let classes = attrs.get(local_name!("class")).unwrap();
    let result = classes
        .split_whitespace()
        .find_map(|class| {
            if !class.starts_with(STARS_PREFIX) {
                return None;
            }
            let rating = class[STARS_PREFIX.len()..].parse().unwrap();
            assert!(rating >= 1 && rating <= 10);
            Some(rating)
        })
        .unwrap();
    Some(result)
}

fn get_item_tags(elem: &ElementDataRef) -> Vec<String> {
    static TAGS_PREFIX: &'static str = "标签: ";
    if let Some(tags_elem) = elem.query_selector(".collectInfo>.tip") {
        let all_text = tags_elem.text_contents();
        let tag_text = all_text.trim();
        assert!(tag_text.starts_with(TAGS_PREFIX));
        tag_text[TAGS_PREFIX.len()..]
            .split(" ")
            .filter_map(|s| {
                if !s.is_empty() {
                    Some(s.to_string())
                } else {
                    None
                }
            })
            .collect()
    } else {
        vec![]
    }
}

fn generate_item_from_node(elem: &ElementDataRef) -> Item {
    Item {
        id: get_item_id(elem),
        title: get_item_title(elem),
        rating: get_item_rating(elem),
        tags: get_item_tags(elem),
    }
}

pub fn get_all_items(html: NodeRef) -> Vec<Item> {
    html.select("#browserItemList>li")
        .unwrap()
        .map(|elem| generate_item_from_node(&elem))
        .collect()
}
<|endoftext|>


<commit_before>use simag::model::*;
use simag::sampling::{DefContSampler, Sampler};

use super::contvar_construct;

#[test]
fn build() {
    let vars = contvar_construct();

    let workload = &vars["recent_workload"];
    let sleep = &vars["hours_sleep"];
    let fitness = &vars["preflight_fitness"];
    let nighttime = &vars["nighttime_flight"];
    let alertness = &vars["crew_alertness"];
    let opload = &vars["operational_load"];
    let duty_period = &vars["fly_duty_period"];
    let rest_time = &vars["rest_time_on_flight"];

    let sampler = DefContSampler::new(None, None);
    let mut model: DefContModel = ContModel::new(workload, sampler).unwrap();
    //let mut model: DefContModel = ContModel::default(workload).unwrap();
    //let mut model: DefContModel = ContModel::default();

    //model.add_var(workload).unwrap();
    model.add_var(sleep).unwrap();
    model.add_var(fitness).unwrap();
    model.add_var(nighttime).unwrap();
    model.add_var(alertness).unwrap();
    model.add_var(opload).unwrap();
    model.add_var(duty_period).unwrap();
    model.add_var(rest_time).unwrap();

    model.add_parent(fitness, sleep, -0.9).unwrap();
    model.add_parent(fitness, workload, 0.9).unwrap();
    model.add_parent(alertness, fitness, 0.85).unwrap();
    model.add_parent(alertness, nighttime, -0.4).unwrap();
    model.add_parent(alertness, opload, -0.8).unwrap();
    model.add_parent(opload, nighttime, -0.95).unwrap();
    model.add_parent(opload, duty_period, 0.5).unwrap();
    model.add_parent(rest_time, duty_period, 0.8).unwrap();
}

#[ignore]
#[test]
fn sample() {
    unimplemented!()
}
<commit_msg>add chained (undirected) graph model<commit_after>use simag::model::*;
use simag::sampling::{DefContSampler, Sampler};

use super::contvar_construct;

#[test]
fn build() {
    let vars = contvar_construct();

    let workload = &vars["recent_workload"];
    let sleep = &vars["hours_sleep"];
    let fitness = &vars["preflight_fitness"];
    let nighttime = &vars["nighttime_flight"];
    let alertness = &vars["crew_alertness"];
    let opload = &vars["operational_load"];
    let duty_period = &vars["fly_duty_period"];
    let rest_time = &vars["rest_time_on_flight"];

    let mut model = DefContModel::default();
    model.add_var(workload).unwrap();
    model.add_var(sleep).unwrap();
    model.add_var(fitness).unwrap();
    model.add_var(nighttime).unwrap();
    model.add_var(alertness).unwrap();
    model.add_var(opload).unwrap();
    model.add_var(duty_period).unwrap();
    model.add_var(rest_time).unwrap();

    model.add_parent(fitness, sleep, -0.9).unwrap();
    model.add_parent(fitness, workload, 0.9).unwrap();
    model.add_parent(alertness, fitness, 0.85).unwrap();
    model.add_parent(alertness, nighttime, -0.4).unwrap();
    model.add_parent(alertness, opload, -0.8).unwrap();
    model.add_parent(opload, nighttime, -0.95).unwrap();
    model.add_parent(opload, duty_period, 0.5).unwrap();
    model.add_parent(rest_time, duty_period, 0.8).unwrap();
}

#[ignore]
#[test]
fn sample() {
    unimplemented!()
}
<|endoftext|>


<commit_before>#[derive(Debug)]
struct ListNode {
    val: i32,
    next: Option<Box<ListNode>>,
}

impl ListNode {
    fn new(i: i32) -> Self {
        ListNode { val: i, next: None }
    }

    //:= DEBUG: self point changed
    //:= https://stackoverflow.com/questions/50957738/how-to-copy-a-raw-pointer-in-rust?noredirect=1#comment88913158_50957738
    fn add_l(&mut self, l: &Vec<i32>) {
        //let mut p: *mut ListNode = self as *mut ListNode;
        let org = self;
        let mut p: *mut ListNode = org as *mut ListNode;

        for i in l {
            unsafe {
                //println!("{:?}", *p);
                (*p).next = Some(Box::new(ListNode::new(*i)));
                let temp_b = Box::from_raw(p);
                p = Box::into_raw(temp_b.next.unwrap());
            };
        }
    }

    fn add(&mut self, i: &i32) {
        self.next = Some(Box::new(ListNode::new(*i)));
    }

    fn add_head(self, i: i32) -> Self {
        ListNode {
            val: i,
            next: Some(Box::new(self)),
        }
    }

    fn new_l(l: Vec<i32>) -> Self {
        let mut inner_l = l;
        let mut temp = Self::new(*&inner_l.pop().unwrap());
        for i in inner_l.into_iter().rev() {
            temp = temp.add_head(i)
        }
        temp
    }

    fn removeNthFromEnd(&self, n: i32) {}
}
fn main() {
    //let mut a = ListNode::new(1);
    //a.add_l(&vec![2, 3, 4, 5]);

    let a = ListNode::new_l(vec![1, 2, 3, 4, 5]);
    println!("{:?}", a);
}
<commit_msg>a bit<commit_after>use std::rc::Rc;

#[derive(Debug, Clone)]
struct ListNode {
    val: i32,
    next: Option<Box<ListNode>>,
}

impl ListNode {
    fn new(i: i32) -> Self {
        ListNode { val: i, next: None }
    }

    //:= https://stackoverflow.com/questions/50957738/how-to-copy-a-raw-pointer-in-rust?noredirect=1#comment88913158_50957738
    /*
    fn add_l(&mut self, l: &Vec<i32>) {
        //let mut p: *mut ListNode = self as *mut ListNode;
        let org = self;
        let mut p: *mut ListNode = org as *mut ListNode;

        for i in l {
            unsafe {
                //println!("{:?}", *p);
                (*p).next = Some(Box::new(ListNode::new(*i)));
                let temp_b = Box::from_raw(p);
                p = Box::into_raw(temp_b.next.unwrap());
            };
        }
    }*/

    //https://stackoverflow.com/questions/50957738/how-to-copy-a-raw-pointer-when-implementing-a-linked-list-in-rust/50970414#50970414
    fn add_l(&mut self, l: &[i32]) {
        let mut head = self;

        for &val in l {
            head.next = Some(Box::new(ListNode::new(val)));
            head = { head }.next.as_mut().unwrap();
        }
    }

    fn add(&mut self, i: &i32) {
        self.next = Some(Box::new(ListNode::new(*i)));
    }

    fn add_head(self, i: i32) -> Self {
        ListNode {
            val: i,
            next: Some(Box::new(self)),
        }
    }

    fn new_l(l: Vec<i32>) -> Self {
        let mut inner_l = l;
        let mut temp = Self::new(*&inner_l.pop().unwrap());
        for i in inner_l.into_iter().rev() {
            temp = temp.add_head(i)
        }
        temp
    }

    fn removeNthFromEnd(&mut self, n: i32) {
        //let mut head_1 = { self }.next.as_ref().unwrap();
        let mut head_2 = { self }.next.as_ref().unwrap();
        let mut n_h = 0;

        for _ in 1..n {
            head_2 = { head_2 }.next.as_ref().unwrap();
        }

        while let Some(_) = { &head_2 }.next {
            //head_1 = { head_1 }.next.as_mut().unwrap();
            n_h += 1;
            head_2 = { head_2 }.next.as_ref().unwrap();
        }
    }
}
fn main() {
    //let mut a = ListNode::new(1);
    //a.add_l(&vec![2, 3, 4, 5]);

    let mut a = ListNode::new_l(vec![1, 2, 3, 4, 5]);
    a.removeNthFromEnd(1);
    println!("{:?}", a);
}
<|endoftext|>


<commit_before>//! Run the learning code with a single scale.

extern crate midilearn;

use midilearn::{MidiLearn, Problem, User};

fn main() {
    let prob = Problem::new("shell: Dm7-G7-CΔ (1/3-7)",
                            r#"{
                                "type": "voicing",
                                "chords": [[50,60,65],[43,59,65],[48,59,64]]
                            }"#);
    println!("guide: Dm7-G7-CΔ (1/3-7)");

    MidiLearn::with_new(|learn: &mut MidiLearn| {
        println!("Status: {:?}", learn.single(&prob));
    }).unwrap();
}
<commit_msg>Fix the description of the example chord<commit_after>//! Run the learning code with a single scale.

extern crate midilearn;

use midilearn::{MidiLearn, Problem, User};

fn main() {
    let prob = Problem::new("shell: Dm7-G7-CΔ (1/7-3)",
                            r#"{
                                "type": "voicing",
                                "chords": [[50,60,65],[43,59,65],[48,59,64]]
                            }"#);
    println!("guide: Dm7-G7-CΔ (1/3-7)");

    MidiLearn::with_new(|learn: &mut MidiLearn| {
        println!("Status: {:?}", learn.single(&prob));
    }).unwrap();
}
<|endoftext|>


<commit_before>#[macro_use]
extern crate log;
extern crate env_logger;
extern crate httparse;
extern crate mio;
extern crate slab;
extern crate time;

use std::ascii::AsciiExt;
use std::env;
use std::io::{self, Read, Write};
use std::mem;
use std::net::SocketAddr;
use std::slice;
use std::str;

use mio::tcp::{TcpStream, TcpListener};
use mio::{Poll, Token, EventSet, PollOpt, Events};
use slab::Slab;

const LISTENER: mio::Token = mio::Token(0);

struct Server<'a> {
    count: u64,
    listener: &'a TcpListener,
    connections: Slab<Connection, mio::Token>,
}

impl<'a> Server<'a> {
    fn new(listener: &'a TcpListener) -> Server<'a> {
        Server {
            count: 0,
            listener: listener,
            connections: Slab::new_starting_at(mio::Token(1), 1024),
        }
    }

    fn ready(&mut self,
             poll: &Poll,
             token: Token,
             events: EventSet) {
        debug!("{:?} {:?}", token, events);
        if token == LISTENER {
            while let Ok(Some(socket)) = self.listener.accept() {
                debug!("accepted");
                self.count += 1;
                // socket.0.set_nodelay(true).unwrap();
                let token = self.connections
                    .insert_with(move |_| Connection::new(socket.0))
                    .unwrap();
                self.try_connection(poll, token, EventSet::all());
            }
        } else {
            self.try_connection(poll, token, events);
        }
    }

    fn try_connection(&mut self, poll: &Poll, token: Token, events: EventSet) {
        let res = self.connections[token].ready(events);

        if res.is_err() || self.connections[token].closed {
            if let Err(ref e) = res {
                info!("error: {:?}\non: {:?} {:?}", e,
                       token, self.connections[token].socket);
            }
            debug!("removing");
            self.connections.remove(token);
        } else {
            self.connections[token].reregister(poll, token).unwrap();
        }
    }
}

struct Connection {
    socket: TcpStream,
    input: Vec<u8>,
    output: Output,
    keepalive: bool,
    closed: bool,
    first: bool,
    read_closed: bool,
}

struct Output {
    buf: Vec<u8>,
}

impl Connection {
    fn new(socket: TcpStream) -> Connection {
        Connection {
            socket: socket,
            input: Vec::with_capacity(2048),
            output: Output {
                buf: Vec::with_capacity(2048),
            },
            keepalive: false,
            read_closed: false,
            closed: false,
            first: true,
        }
    }

    fn ready(&mut self, events: EventSet) -> io::Result<()> {
        while events.is_readable() && !self.read_closed {
            let before = self.input.len();
            let eof = try!(read(&mut self.socket, &mut self.input));
            if eof {
                debug!("eof");
            }
            self.read_closed = eof;
            if self.input.len() == before {
                break
            }

            while self.input.len() > 0 {
                let (req, amt) = match try!(parse(&self.input)) {
                    Some(pair) => pair,
                    None => {
                        debug!("need more data for a request");
                        return Ok(())
                    }
                };
                let request = Request {
                    inner: req,
                    amt: amt,
                    data: mem::replace(&mut self.input, Vec::new()),
                };
                debug!("got a request");
                self.keepalive = request.version() >= 1;
                self.keepalive = self.keepalive || request.headers().any(|s| {
                    s.0.eq_ignore_ascii_case("connection") &&
                        s.1.eq_ignore_ascii_case(b"keep-alive")
                });

                let response = process(&request);
                response.to_bytes(&mut self.output.buf);

                self.input = request.into_input_buf();
                if !self.keepalive {
                    debug!("disabling keepalive");
                    self.read_closed = true;
                }
            }
        }

        if events.is_writable() && self.output.buf.len() > 0 {
            let done = try!(write(&mut self.socket, &mut self.output));
            if done {
                debug!("wrote response");
                if !self.keepalive || self.read_closed {
                    self.closed = true;
                }
            }
        }

        if self.read_closed && self.output.buf.len() == 0 {
            self.closed = true;
        }
        Ok(())
    }

    fn reregister(&mut self, poll: &Poll, token: Token) -> io::Result<()> {
        assert!(!self.closed);
        debug!("register");
        let events = if self.output.buf.len() > 0 {
            if self.read_closed {
                EventSet::writable()
            } else {
                EventSet::writable() | EventSet::readable()
            }
        } else {
            assert!(!self.read_closed);
            EventSet::readable()
        };
        let opts = PollOpt::edge() | PollOpt::oneshot();
        if self.first {
            self.first = false;
            poll.register(&self.socket, token, events, opts)
        } else {
            poll.reregister(&self.socket, token, events, opts)
        }
    }
}

fn process(r: &Request) -> Response {
    assert!(r.path() == "/plaintext");
    let mut r = Response::new();
    r.header("Content-Type", "text/plain; charset=UTF-8")
     .body("Hello, World!\r\n");
    return r
}

type Slice = (usize, usize);

#[allow(dead_code)]
pub struct Request {
    inner: RawRequest,
    data: Vec<u8>,
    amt: usize,
}

struct RawRequest {
    method: Slice,
    path: Slice,
    version: u8,
    headers: Vec<(Slice, Slice)>,
}

pub struct Headers<'a> {
    iter: slice::Iter<'a, (Slice, Slice)>,
    req: &'a Request,
}

impl Request {
    pub fn method(&self) -> &str {
        str::from_utf8(self.get(&self.inner.method)).unwrap()
    }

    pub fn path(&self) -> &str {
        str::from_utf8(self.get(&self.inner.path)).unwrap()
    }

    pub fn version(&self) -> u8 {
        self.inner.version
    }

    pub fn headers(&self) -> Headers {
        Headers {
            iter: self.inner.headers.iter(),
            req: self,
        }
    }

    pub fn into_input_buf(mut self) -> Vec<u8> {
        self.data.drain(..self.amt);
        self.data
    }

    fn get(&self, s: &Slice) -> &[u8] {
        &self.data[s.0..s.1]
    }
}

impl<'a> Iterator for Headers<'a> {
    type Item = (&'a str, &'a [u8]);

    fn next(&mut self) -> Option<(&'a str, &'a [u8])> {
        self.iter.next().map(|&(ref k, ref v)| {
            (str::from_utf8(self.req.get(k)).unwrap(), self.req.get(v))
        })
    }
}

fn parse(buf: &[u8]) -> io::Result<Option<(RawRequest, usize)>> {
    let mut headers = [httparse::EMPTY_HEADER; 16];
    let mut r = httparse::Request::new(&mut headers);
    let status = try!(r.parse(&buf).map_err(|_| {
        io::Error::new(io::ErrorKind::Other, "failed to parse")
    }));
    return match status {
        httparse::Status::Complete(amt) => {
            debug!("ok {:?}", String::from_utf8_lossy(&buf[..amt]));
            Ok(Some((RawRequest {
                method: slice(buf, r.method.unwrap().as_bytes()),
                path: slice(buf, r.path.unwrap().as_bytes()),
                version: r.version.unwrap(),
                headers: r.headers.iter().map(|h| {
                    (slice(buf, h.name.as_bytes()), slice(buf, &h.value))
                }).collect(),
            }, amt)))
        }
        httparse::Status::Partial => Ok(None),
    };

    fn slice(buf: &[u8], inner: &[u8]) -> Slice {
        let start = inner.as_ptr() as usize - buf.as_ptr() as usize;
        assert!(start < buf.len());
        (start, start + inner.len())
    }
}

pub struct Response {
    headers: Vec<(String, String)>,
    response: String,
}

impl Response {
    pub fn new() -> Response {
        Response {
            headers: Vec::new(),
            response: String::new(),
        }
    }

    pub fn header(&mut self, name: &str, val: &str) -> &mut Response {
        self.headers.push((name.to_string(), val.to_string()));
        self
    }

    pub fn body(&mut self, s: &str) -> &mut Response {
        self.response = s.to_string();
        self
    }

    fn to_bytes(&self, into: &mut Vec<u8>) {
        write!(into, "\
            HTTP/1.1 200 OK\r\n\
            Server: Example\r\n\
            Date: {}\r\n\
            Content-Length: {}\r\n\
        ", time::now().rfc822(), self.response.len()).unwrap();
        for &(ref k, ref v) in &self.headers {
            extend(into, k.as_bytes());
            extend(into, b": ");
            extend(into, v.as_bytes());
            extend(into, b"\r\n");
        }
        extend(into, b"\r\n");
        extend(into, self.response.as_bytes());
    }
}

// TODO: why does extend_from_slice not optimize?
fn extend(dst: &mut Vec<u8>, data: &[u8]) {
    use std::ptr;
    dst.reserve(data.len());
    let prev = dst.len();
    unsafe {
        ptr::copy_nonoverlapping(data.as_ptr(),
                                 dst.as_mut_ptr().offset(prev as isize),
                                 data.len());
        dst.set_len(prev + data.len());
    }
}

fn read(socket: &mut TcpStream, input: &mut Vec<u8>) -> io::Result<bool> {
    loop {
        match socket.read(unsafe { slice_to_end(input) }) {
            Ok(0) => return Ok(true),
            Ok(n) => {
                let len = input.len();
                unsafe { input.set_len(len + n); }
                return Ok(false)
            }
            Err(ref e) if e.kind() == io::ErrorKind::WouldBlock => {
                return Ok(false)
            }
            Err(e) => return Err(e),
        }
    }

    unsafe fn slice_to_end(v: &mut Vec<u8>) -> &mut [u8] {
        use std::slice;
        if v.capacity() == 0 {
            v.reserve(16);
        }
        if v.capacity() == v.len() {
            v.reserve(1);
        }
        slice::from_raw_parts_mut(v.as_mut_ptr().offset(v.len() as isize),
                                  v.capacity() - v.len())
    }
}

fn write(socket: &mut TcpStream, output: &mut Output) -> io::Result<bool> {
    assert!(output.buf.len() > 0);
    loop {
        match socket.write(&output.buf) {
            Ok(0) => {
                return Err(io::Error::new(io::ErrorKind::Other, "early eof2"))
            }
            Ok(n) => {
                output.buf.drain(..n);
                if output.buf.len() == 0 {
                    return Ok(true)
                }
            }
            Err(ref e) if e.kind() == io::ErrorKind::WouldBlock => {
                return Ok(false)
            }
            Err(e) => return Err(e),
        }
    }
}

fn main() {
    env_logger::init().unwrap();

    let poll = mio::Poll::new().unwrap();

    let addr = env::args().nth(1).unwrap_or("127.0.0.1:8080".to_string());
    let addr = addr.parse::<SocketAddr>().unwrap();
    let listener = TcpListener::bind(&addr).unwrap();
    poll.register(&listener,
                  LISTENER,
                  EventSet::readable(),
                  PollOpt::level()).unwrap();

    let mut events = Events::new();
    let mut server = Server::new(&listener);
    loop {
        poll.poll(&mut events, None).unwrap();

        for i in 0..events.len() {
            let event = events.get(i).unwrap();
            server.ready(&poll, event.token(), event.kind());
        }
    }
}
<commit_msg>940k<commit_after>#[macro_use]
extern crate log;
extern crate env_logger;
extern crate httparse;
extern crate mio;
extern crate slab;
extern crate time;

use std::ascii::AsciiExt;
use std::env;
use std::io::{self, Read, Write};
use std::mem;
use std::net::SocketAddr;
use std::slice;
use std::str;

use mio::tcp::{TcpStream, TcpListener};
use mio::{Poll, Token, EventSet, PollOpt, Events};
use slab::Slab;

const LISTENER: mio::Token = mio::Token(0);

struct Server<'a> {
    count: u64,
    listener: &'a TcpListener,
    connections: Slab<Connection, mio::Token>,
}

impl<'a> Server<'a> {
    fn new(listener: &'a TcpListener) -> Server<'a> {
        Server {
            count: 0,
            listener: listener,
            connections: Slab::new_starting_at(mio::Token(1), 1024),
        }
    }

    fn ready(&mut self,
             poll: &Poll,
             token: Token,
             events: EventSet) {
        debug!("{:?} {:?}", token, events);
        if token == LISTENER {
            while let Ok(Some(socket)) = self.listener.accept() {
                debug!("accepted");
                self.count += 1;
                // socket.0.set_nodelay(true).unwrap();
                let token = self.connections
                    .insert_with(move |_| Connection::new(socket.0))
                    .unwrap();
                poll.register(&self.connections[token].socket,
                              token,
                              EventSet::readable() | EventSet::writable(),
                              PollOpt::edge()).unwrap();
                self.try_connection(poll, token, EventSet::all());
            }
        } else {
            self.try_connection(poll, token, events);
        }
    }

    fn try_connection(&mut self, poll: &Poll, token: Token, events: EventSet) {
        let res = self.connections[token].ready(events);

        if res.is_err() || self.connections[token].closed {
            if let Err(ref e) = res {
                info!("error: {:?}\non: {:?} {:?}", e,
                       token, self.connections[token].socket);
            }
            debug!("removing");
            self.connections.remove(token);
        }
    }
}

struct Connection {
    socket: TcpStream,
    input: Vec<u8>,
    output: Output,
    keepalive: bool,
    closed: bool,
    read_closed: bool,
    events: EventSet,
}

struct Output {
    buf: Vec<u8>,
}

impl Connection {
    fn new(socket: TcpStream) -> Connection {
        Connection {
            socket: socket,
            input: Vec::with_capacity(2048),
            output: Output {
                buf: Vec::with_capacity(2048),
            },
            keepalive: false,
            read_closed: false,
            closed: false,
            events: EventSet::none(),
        }
    }

    fn ready(&mut self, events: EventSet) -> io::Result<()> {
        self.events = self.events | events;
        while self.events.is_readable() && !self.read_closed {
            let before = self.input.len();
            let eof = try!(read(&mut self.socket,
                                &mut self.input,
                                &mut self.events));
            if eof {
                debug!("eof");
            }
            self.read_closed = eof;
            if self.input.len() == before {
                break
            }

            while self.input.len() > 0 {
                let (req, amt) = match try!(parse(&self.input)) {
                    Some(pair) => pair,
                    None => {
                        debug!("need more data for a request");
                        return Ok(())
                    }
                };
                let request = Request {
                    inner: req,
                    amt: amt,
                    data: mem::replace(&mut self.input, Vec::new()),
                };
                debug!("got a request");
                self.keepalive = request.version() >= 1;
                self.keepalive = self.keepalive || request.headers().any(|s| {
                    s.0.eq_ignore_ascii_case("connection") &&
                        s.1.eq_ignore_ascii_case(b"keep-alive")
                });

                let response = process(&request);
                response.to_bytes(&mut self.output.buf);

                self.input = request.into_input_buf();
                if !self.keepalive {
                    debug!("disabling keepalive");
                    self.read_closed = true;
                }
            }
        }

        if self.events.is_writable() && self.output.buf.len() > 0 {
            let done = try!(write(&mut self.socket,
                                  &mut self.output,
                                  &mut self.events));
            if done {
                debug!("wrote response");
                if !self.keepalive || self.read_closed {
                    self.closed = true;
                }
            }
        }

        if self.read_closed && self.output.buf.len() == 0 {
            self.closed = true;
        }
        Ok(())
    }
}

fn process(r: &Request) -> Response {
    assert!(r.path() == "/plaintext");
    let mut r = Response::new();
    r.header("Content-Type", "text/plain; charset=UTF-8")
     .body("Hello, World!");
    return r
}

type Slice = (usize, usize);

#[allow(dead_code)]
pub struct Request {
    inner: RawRequest,
    data: Vec<u8>,
    amt: usize,
}

struct RawRequest {
    method: Slice,
    path: Slice,
    version: u8,
    headers: Vec<(Slice, Slice)>,
}

pub struct Headers<'a> {
    iter: slice::Iter<'a, (Slice, Slice)>,
    req: &'a Request,
}

impl Request {
    pub fn method(&self) -> &str {
        str::from_utf8(self.get(&self.inner.method)).unwrap()
    }

    pub fn path(&self) -> &str {
        str::from_utf8(self.get(&self.inner.path)).unwrap()
    }

    pub fn version(&self) -> u8 {
        self.inner.version
    }

    pub fn headers(&self) -> Headers {
        Headers {
            iter: self.inner.headers.iter(),
            req: self,
        }
    }

    pub fn into_input_buf(mut self) -> Vec<u8> {
        self.data.drain(..self.amt);
        self.data
    }

    fn get(&self, s: &Slice) -> &[u8] {
        &self.data[s.0..s.1]
    }
}

impl<'a> Iterator for Headers<'a> {
    type Item = (&'a str, &'a [u8]);

    fn next(&mut self) -> Option<(&'a str, &'a [u8])> {
        self.iter.next().map(|&(ref k, ref v)| {
            (str::from_utf8(self.req.get(k)).unwrap(), self.req.get(v))
        })
    }
}

fn parse(buf: &[u8]) -> io::Result<Option<(RawRequest, usize)>> {
    let mut headers = [httparse::EMPTY_HEADER; 16];
    let mut r = httparse::Request::new(&mut headers);
    let status = try!(r.parse(&buf).map_err(|_| {
        io::Error::new(io::ErrorKind::Other, "failed to parse")
    }));
    return match status {
        httparse::Status::Complete(amt) => {
            debug!("ok {:?}", String::from_utf8_lossy(&buf[..amt]));
            Ok(Some((RawRequest {
                method: slice(buf, r.method.unwrap().as_bytes()),
                path: slice(buf, r.path.unwrap().as_bytes()),
                version: r.version.unwrap(),
                headers: r.headers.iter().map(|h| {
                    (slice(buf, h.name.as_bytes()), slice(buf, &h.value))
                }).collect(),
            }, amt)))
        }
        httparse::Status::Partial => Ok(None),
    };

    fn slice(buf: &[u8], inner: &[u8]) -> Slice {
        let start = inner.as_ptr() as usize - buf.as_ptr() as usize;
        assert!(start < buf.len());
        (start, start + inner.len())
    }
}

pub struct Response {
    headers: Vec<(String, String)>,
    response: String,
}

impl Response {
    pub fn new() -> Response {
        Response {
            headers: Vec::new(),
            response: String::new(),
        }
    }

    pub fn header(&mut self, name: &str, val: &str) -> &mut Response {
        self.headers.push((name.to_string(), val.to_string()));
        self
    }

    pub fn body(&mut self, s: &str) -> &mut Response {
        self.response = s.to_string();
        self
    }

    fn to_bytes(&self, into: &mut Vec<u8>) {
        write!(into, "\
            HTTP/1.1 200 OK\r\n\
            Server: Example\r\n\
            Date: {}\r\n\
            Content-Length: {}\r\n\
        ", time::now().rfc822(), self.response.len()).unwrap();
        for &(ref k, ref v) in &self.headers {
            extend(into, k.as_bytes());
            extend(into, b": ");
            extend(into, v.as_bytes());
            extend(into, b"\r\n");
        }
        extend(into, b"\r\n");
        extend(into, self.response.as_bytes());
    }
}

// TODO: why does extend_from_slice not optimize?
fn extend(dst: &mut Vec<u8>, data: &[u8]) {
    use std::ptr;
    dst.reserve(data.len());
    let prev = dst.len();
    unsafe {
        ptr::copy_nonoverlapping(data.as_ptr(),
                                 dst.as_mut_ptr().offset(prev as isize),
                                 data.len());
        dst.set_len(prev + data.len());
    }
}

fn read(socket: &mut TcpStream,
        input: &mut Vec<u8>,
        events: &mut EventSet) -> io::Result<bool> {
    match socket.read(unsafe { slice_to_end(input) }) {
        Ok(0) => return Ok(true),
        Ok(n) => {
            let len = input.len();
            unsafe { input.set_len(len + n); }
            return Ok(false)
        }
        Err(ref e) if e.kind() == io::ErrorKind::WouldBlock => {
            *events = *events & !EventSet::readable();
            return Ok(false)
        }
        Err(e) => return Err(e),
    }

    unsafe fn slice_to_end(v: &mut Vec<u8>) -> &mut [u8] {
        use std::slice;
        if v.capacity() == 0 {
            v.reserve(16);
        }
        if v.capacity() == v.len() {
            v.reserve(1);
        }
        slice::from_raw_parts_mut(v.as_mut_ptr().offset(v.len() as isize),
                                  v.capacity() - v.len())
    }
}

fn write(socket: &mut TcpStream,
         output: &mut Output,
         events: &mut EventSet) -> io::Result<bool> {
    assert!(output.buf.len() > 0);
    loop {
        match socket.write(&output.buf) {
            Ok(0) => {
                return Err(io::Error::new(io::ErrorKind::Other, "early eof2"))
            }
            Ok(n) => {
                output.buf.drain(..n);
                if output.buf.len() == 0 {
                    return Ok(true)
                }
            }
            Err(ref e) if e.kind() == io::ErrorKind::WouldBlock => {
                *events = *events & !EventSet::writable();
                return Ok(false)
            }
            Err(e) => return Err(e),
        }
    }
}

fn main() {
    env_logger::init().unwrap();

    let poll = mio::Poll::new().unwrap();

    let addr = env::args().nth(1).unwrap_or("127.0.0.1:8080".to_string());
    let addr = addr.parse::<SocketAddr>().unwrap();
    let listener = TcpListener::bind(&addr).unwrap();
    poll.register(&listener,
                  LISTENER,
                  EventSet::readable(),
                  PollOpt::level()).unwrap();

    let mut events = Events::new();
    let mut server = Server::new(&listener);
    loop {
        poll.poll(&mut events, None).unwrap();

        for i in 0..events.len() {
            let event = events.get(i).unwrap();
            server.ready(&poll, event.token(), event.kind());
        }
    }
}
<|endoftext|>


<commit_before>//! An operator which captures the streams of records and notifications to play back later.
//!
//! Not yet tested; please be careful using!

use std::rc::Rc;
use std::cell::RefCell;

use ::Data;
use dataflow::{Scope, Stream};
use dataflow::channels::pact::{ParallelizationContract, Pipeline};
use dataflow::channels::pullers::Counter as PullCounter;
use dataflow::channels::pushers::Counter as PushCounter;
use dataflow::channels::pushers::buffer::Buffer as PushBuffer;
use dataflow::channels::pushers::Tee;
use dataflow::channels::message::Content;

use progress::count_map::CountMap;
use progress::nested::subgraph::Source::ChildOutput;
use progress::nested::subgraph::Target::ChildInput;
use progress::{Timestamp, Operate, Antichain};

pub trait Capture<T: Timestamp, D: Data> {
    fn capture(&self) -> Handle<T, D>;
}

impl<S: Scope, D: Data> Capture<S::Timestamp, D> for Stream<S, D> {
    fn capture(&self) -> Handle<S::Timestamp, D> {

        let events = Rc::new(RefCell::new(EventLink { event: Event::Start, next: None }));
        let handle = Handle { events: events.clone() };

        let mut scope = self.scope();   // clones the scope
        let channel_id = scope.new_identifier();

        let (sender, receiver) = Pipeline.connect(&mut scope, channel_id);
        let operator = CaptureOperator {
            input: PullCounter::new(receiver),
            events: events,
        };

        let index = scope.add_operator(operator);
        self.connect_to(ChildInput(index, 0), sender, channel_id);
        handle
    }
}

/// Possible events that the captured stream may provide.
pub enum Event<T, D> {
    Start,
    /// Progress received via `push_external_progress`.
    Progress(Vec<(T, i64)>),
    /// Messages received via the data stream.
    Messages(T, Content<D>),
}

pub struct EventLink<T, D> {
    event: Event<T, D>,
    next: Option<Rc<RefCell<EventLink<T, D>>>>,
}

// TODO : No notion of streaming garbage collection, which is probably a bug.
// TODO : The whole list will be collected when everyone is done, but perhaps a linked list would
// TODO : be better / have better properties?

/// A handle to the captured data, shared with the capture operator itself.
pub struct Handle<T: Timestamp, D: Data> {
    events: Rc<RefCell<EventLink<T, D>>>,
}

impl<T: Timestamp, D: Data> Handle<T, D> {
    pub fn replay<S: Scope<Timestamp=T>>(&self, scope: &mut S) -> Stream<S, D> {

        let (targets, registrar) = Tee::<S::Timestamp, D>::new();
        let operator = ReplayOperator {
            output: PushBuffer::new(PushCounter::new(targets, Rc::new(RefCell::new(CountMap::new())))),
            events: self.events.clone(),
        };

        let index = scope.add_operator(operator);
        Stream::new(ChildOutput(index, 0), registrar, scope.clone())
    }
}

struct CaptureOperator<T: Timestamp, D: Data> {
    input: PullCounter<T, D>,
    events: Rc<RefCell<EventLink<T, D>>>,
}

impl<T:Timestamp, D: Data> Operate<T> for CaptureOperator<T, D> {
    fn name(&self) -> &str { "Capture" }
    fn inputs(&self) -> usize { 1 }
    fn outputs(&self) -> usize { 0 }

    // we need to set the initial value of the frontier
    fn set_external_summary(&mut self, _: Vec<Vec<Antichain<T::Summary>>>, counts: &mut [CountMap<T>]) {
        self.events.borrow_mut().next = Some(Rc::new(RefCell::new(EventLink { event: Event::Progress(counts[0].updates.clone()), next: None })));
        let next = self.events.borrow().next.as_ref().unwrap().clone();
        self.events = next;
        counts[0].updates.clear();
    }

    // each change to the frontier should be shared
    fn push_external_progress(&mut self, counts: &mut [CountMap<T>]) {
        self.events.borrow_mut().next = Some(Rc::new(RefCell::new(EventLink { event: Event::Progress(counts[0].updates.clone()), next: None })));
        let next = self.events.borrow().next.as_ref().unwrap().clone();
        self.events = next;
        counts[0].updates.clear();
    }

    fn pull_internal_progress(&mut self, _: &mut [CountMap<T>], consumed: &mut [CountMap<T>], _: &mut [CountMap<T>]) -> bool {
        let events = &mut self.events;
        self.input.for_each(|&time, data| {
            events.borrow_mut().next = Some(Rc::new(RefCell::new(EventLink { event: Event::Messages(time, data.clone()), next: None })));
            let next = events.borrow().next.as_ref().unwrap().clone();
            *events = next;
        });
        self.input.pull_progress(&mut consumed[0]);
        false
    }
}

struct ReplayOperator<T:Timestamp, D: Data> {
    events: Rc<RefCell<EventLink<T, D>>>,
    output: PushBuffer<T, D, PushCounter<T, D, Tee<T, D>>>,
}

impl<T:Timestamp, D: Data> Operate<T> for ReplayOperator<T, D> {
    fn name(&self) -> &str { "Replay" }
    fn inputs(&self) -> usize { 0 }
    fn outputs(&self) -> usize { 1 }

    fn get_internal_summary(&mut self) -> (Vec<Vec<Antichain<T::Summary>>>, Vec<CountMap<T>>) {

        // panics if the event link has not been initialized; should only happen if no one has
        // called set_external_summary on the `CaptureOperator`. So, please don't use this in the
        // same graph as the `CaptureOperator`.

        // TODO : use Default::default() as the initial time, and in the first Event we dequeue,
        // TODO : announce that we've moved beyond Default::default().
        let mut result = CountMap::new();
        let next = self.events.borrow().next.as_ref().unwrap().clone();
        self.events = next;
        if let Event::Progress(ref vec) = self.events.borrow().event {
            for &(ref time, delta) in vec {
                result.update(time, delta);
            }
        }
        (vec![], vec![result])
    }

    fn pull_internal_progress(&mut self, internal: &mut [CountMap<T>], _: &mut [CountMap<T>], produced: &mut [CountMap<T>]) -> bool {

        while self.events.borrow().next.is_some() {
            let next = self.events.borrow().next.as_ref().unwrap().clone();
            self.events = next;
            match self.events.borrow().event {
                Event::Start => { },
                Event::Progress(ref vec) => {
                    for &(ref time, delta) in vec {
                        internal[0].update(time, delta);
                    }
                },
                Event::Messages(ref time, ref data) => {
                    let mut data = data.clone();
                    self.output.session(time).give_content(&mut data);
                }
            }
        }

        self.output.cease();
        self.output.inner().pull_progress(&mut produced[0]);

        false
    }
}

#[cfg(test)]
mod tests {

    use ::Configuration;
    use dataflow::*;
    use dataflow::operators::{Capture, ToStream, Inspect};

    #[test]
    fn probe() {

        // initializes and runs a timely dataflow computation
        ::execute(Configuration::Thread, |computation| {
            let handle = computation.scoped::<u64,_,_>(|builder|
                (0..10).to_stream(builder)
                       .capture()
            );

            computation.step();
            computation.step();
            computation.step();

            computation.scoped(|builder| {
                handle.replay(builder)
                      .inspect(|x| println!("replayed: {:?}", x));
            })
        });
    }

}
<commit_msg>documentation/test update for capture<commit_after>//! An operator which captures the streams of records and notifications to play back later.
//!
//! Not yet tested; please be careful using!

use std::rc::Rc;
use std::cell::RefCell;

use ::Data;
use dataflow::{Scope, Stream};
use dataflow::channels::pact::{ParallelizationContract, Pipeline};
use dataflow::channels::pullers::Counter as PullCounter;
use dataflow::channels::pushers::Counter as PushCounter;
use dataflow::channels::pushers::buffer::Buffer as PushBuffer;
use dataflow::channels::pushers::Tee;
use dataflow::channels::message::Content;

use progress::count_map::CountMap;
use progress::nested::subgraph::Source::ChildOutput;
use progress::nested::subgraph::Target::ChildInput;
use progress::{Timestamp, Operate, Antichain};

pub trait Capture<T: Timestamp, D: Data> {
    ///
    ///
    /// #Examples
    /// ```
    /// use timely::dataflow::Scope;
    /// use timely::dataflow::operators::{Capture, ToStream, Inspect};

    /// timely::execute(timely::Configuration::Thread, |computation| {
    ///     let handle = computation.scoped::<u64,_,_>(|builder|
    ///         (0..10).to_stream(builder)
    ///                .capture()
    ///     );
    ///
    ///     computation.scoped(|builder| {
    ///         handle.replay(builder)
    ///               .inspect(|x| println!("replayed: {:?}", x));
    ///     })
    /// });
    /// ```
    fn capture(&self) -> Handle<T, D>;
}

impl<S: Scope, D: Data> Capture<S::Timestamp, D> for Stream<S, D> {
    fn capture(&self) -> Handle<S::Timestamp, D> {

        let events = Rc::new(RefCell::new(EventLink { event: Event::Start, next: None }));
        let handle = Handle { events: events.clone() };

        let mut scope = self.scope();   // clones the scope
        let channel_id = scope.new_identifier();

        let (sender, receiver) = Pipeline.connect(&mut scope, channel_id);
        let operator = CaptureOperator {
            input: PullCounter::new(receiver),
            events: events,
        };

        let index = scope.add_operator(operator);
        self.connect_to(ChildInput(index, 0), sender, channel_id);
        handle
    }
}

/// Possible events that the captured stream may provide.
pub enum Event<T, D> {
    Start,
    /// Progress received via `push_external_progress`.
    Progress(Vec<(T, i64)>),
    /// Messages received via the data stream.
    Messages(T, Content<D>),
}

pub struct EventLink<T, D> {
    event: Event<T, D>,
    next: Option<Rc<RefCell<EventLink<T, D>>>>,
}

// TODO : No notion of streaming garbage collection, which is probably a bug.
// TODO : The whole list will be collected when everyone is done, but perhaps a linked list would
// TODO : be better / have better properties?

/// A handle to the captured data, shared with the capture operator itself.
pub struct Handle<T: Timestamp, D: Data> {
    events: Rc<RefCell<EventLink<T, D>>>,
}

impl<T: Timestamp, D: Data> Handle<T, D> {
    pub fn replay<S: Scope<Timestamp=T>>(&self, scope: &mut S) -> Stream<S, D> {

        let (targets, registrar) = Tee::<S::Timestamp, D>::new();
        let operator = ReplayOperator {
            output: PushBuffer::new(PushCounter::new(targets, Rc::new(RefCell::new(CountMap::new())))),
            events: self.events.clone(),
        };

        let index = scope.add_operator(operator);
        Stream::new(ChildOutput(index, 0), registrar, scope.clone())
    }
}

struct CaptureOperator<T: Timestamp, D: Data> {
    input: PullCounter<T, D>,
    events: Rc<RefCell<EventLink<T, D>>>,
}

impl<T:Timestamp, D: Data> Operate<T> for CaptureOperator<T, D> {
    fn name(&self) -> &str { "Capture" }
    fn inputs(&self) -> usize { 1 }
    fn outputs(&self) -> usize { 0 }

    // we need to set the initial value of the frontier
    fn set_external_summary(&mut self, _: Vec<Vec<Antichain<T::Summary>>>, counts: &mut [CountMap<T>]) {
        self.events.borrow_mut().next = Some(Rc::new(RefCell::new(EventLink { event: Event::Progress(counts[0].updates.clone()), next: None })));
        let next = self.events.borrow().next.as_ref().unwrap().clone();
        self.events = next;
        counts[0].updates.clear();
    }

    // each change to the frontier should be shared
    fn push_external_progress(&mut self, counts: &mut [CountMap<T>]) {
        self.events.borrow_mut().next = Some(Rc::new(RefCell::new(EventLink { event: Event::Progress(counts[0].updates.clone()), next: None })));
        let next = self.events.borrow().next.as_ref().unwrap().clone();
        self.events = next;
        counts[0].updates.clear();
    }

    fn pull_internal_progress(&mut self, _: &mut [CountMap<T>], consumed: &mut [CountMap<T>], _: &mut [CountMap<T>]) -> bool {
        let events = &mut self.events;
        self.input.for_each(|&time, data| {
            events.borrow_mut().next = Some(Rc::new(RefCell::new(EventLink { event: Event::Messages(time, data.clone()), next: None })));
            let next = events.borrow().next.as_ref().unwrap().clone();
            *events = next;
        });
        self.input.pull_progress(&mut consumed[0]);
        false
    }
}

struct ReplayOperator<T:Timestamp, D: Data> {
    events: Rc<RefCell<EventLink<T, D>>>,
    output: PushBuffer<T, D, PushCounter<T, D, Tee<T, D>>>,
}

impl<T:Timestamp, D: Data> Operate<T> for ReplayOperator<T, D> {
    fn name(&self) -> &str { "Replay" }
    fn inputs(&self) -> usize { 0 }
    fn outputs(&self) -> usize { 1 }

    fn get_internal_summary(&mut self) -> (Vec<Vec<Antichain<T::Summary>>>, Vec<CountMap<T>>) {

        // panics if the event link has not been initialized; should only happen if no one has
        // called set_external_summary on the `CaptureOperator`. So, please don't use this in the
        // same graph as the `CaptureOperator`.

        // TODO : use Default::default() as the initial time, and in the first Event we dequeue,
        // TODO : announce that we've moved beyond Default::default().
        let mut result = CountMap::new();
        let next = self.events.borrow().next.as_ref().unwrap().clone();
        self.events = next;
        if let Event::Progress(ref vec) = self.events.borrow().event {
            for &(ref time, delta) in vec {
                result.update(time, delta);
            }
        }
        (vec![], vec![result])
    }

    fn pull_internal_progress(&mut self, internal: &mut [CountMap<T>], _: &mut [CountMap<T>], produced: &mut [CountMap<T>]) -> bool {

        while self.events.borrow().next.is_some() {
            let next = self.events.borrow().next.as_ref().unwrap().clone();
            self.events = next;
            match self.events.borrow().event {
                Event::Start => { },
                Event::Progress(ref vec) => {
                    for &(ref time, delta) in vec {
                        internal[0].update(time, delta);
                    }
                },
                Event::Messages(ref time, ref data) => {
                    let mut data = data.clone();
                    self.output.session(time).give_content(&mut data);
                }
            }
        }

        self.output.cease();
        self.output.inner().pull_progress(&mut produced[0]);

        false
    }
}

#[cfg(test)]
mod tests {

    use ::Configuration;
    use dataflow::*;
    use dataflow::operators::{Capture, ToStream, Inspect};

    #[test]
    fn probe() {

        // initializes and runs a timely dataflow computation
        ::execute(Configuration::Thread, |computation| {
            let handle = computation.scoped::<u64,_,_>(|builder|
                (0..10).to_stream(builder)
                       .capture()
            );

            // computation.step();
            // computation.step();
            // computation.step();

            computation.scoped(|builder| {
                handle.replay(builder)
                      .inspect(|x| println!("replayed: {:?}", x));
            })
        });
    }

}
<|endoftext|>


<commit_before>use std::sync::Arc;
use super::{Args, Configuration};
use client::Context;
use model::{Message, Permissions};
use std::collections::HashMap;
use std::fmt;

pub type Check = Fn(&mut Context, &Message, &mut Args, &Arc<Command>) -> bool
                     + Send
                     + Sync
                     + 'static;
pub type Exec = fn(&mut Context, &Message, Args) -> Result<(), Error>;
pub type Help = fn(&mut Context, &Message, HashMap<String, Arc<CommandGroup>>, Args)
                   -> Result<(), Error>;
pub type BeforeHook = Fn(&mut Context, &Message, &str) -> bool + Send + Sync + 'static;
pub type AfterHook = Fn(&mut Context, &Message, &str, Result<(), Error>) + Send + Sync + 'static;
pub(crate) type InternalCommand = Arc<Command>;
pub type PrefixCheck = Fn(&mut Context, &Message) -> Option<String> + Send + Sync + 'static;

pub enum CommandOrAlias {
    Alias(String),
    Command(InternalCommand),
}

/// An error from a command.
#[derive(Clone, Debug)]
pub struct Error(String);

impl<D: fmt::Display> From<D> for Error {
    fn from(d: D) -> Self {
        Error(format!("{}", d))
    }
}

/// Command function type. Allows to access internal framework things inside
/// your commands.
pub enum CommandType {
    StringResponse(String),
    Basic(Exec),
    WithCommands(Help),
}

pub struct CommandGroup {
    pub prefix: Option<String>,
    pub commands: HashMap<String, CommandOrAlias>,
    /// Some fields taken from Command
    pub bucket: Option<String>,
    pub required_permissions: Permissions,
    pub allowed_roles: Vec<String>,
    pub help_available: bool,
    pub dm_only: bool,
    pub guild_only: bool,
    pub owners_only: bool,
}

/// Command struct used to store commands internally.
pub struct Command {
    /// A set of checks to be called prior to executing the command. The checks
    /// will short-circuit on the first check that returns `false`.
    pub checks: Vec<Box<Check>>,
    /// Function called when the command is called.
    pub exec: CommandType,
    /// Ratelimit bucket.
    pub bucket: Option<String>,
    /// Command description, used by other commands.
    pub desc: Option<String>,
    /// Example arguments, used by other commands.
    pub example: Option<String>,
    /// Command usage schema, used by other commands.
    pub usage: Option<String>,
    /// Minumum amount of arguments that should be passed.
    pub min_args: Option<i32>,
    /// Maximum amount of arguments that can be passed.
    pub max_args: Option<i32>,
    /// Permissions required to use this command.
    pub required_permissions: Permissions,
    /// Roles allowed to use this command.
    pub allowed_roles: Vec<String>,
    /// Whether command should be displayed in help list or not, used by other commands.
    pub help_available: bool,
    /// Whether command can be used only privately or not.
    pub dm_only: bool,
    /// Whether command can be used only in guilds or not.
    pub guild_only: bool,
    /// Whether command can only be used by owners or not.
    pub owners_only: bool,
    pub(crate) aliases: Vec<String>,
}

impl Command {
    pub fn new(f: fn(&mut Context, &Message, Args) -> Result<(), Error>) -> Self {
        Command {
            exec: CommandType::Basic(f),
            ..Command::default()
        }
    }
}

impl Default for Command {
    fn default() -> Command {
        Command {
            aliases: Vec::new(),
            checks: Vec::default(),
            exec: CommandType::Basic(|_, _, _| Ok(())),
            desc: None,
            usage: None,
            example: None,
            min_args: None,
            bucket: None,
            max_args: None,
            required_permissions: Permissions::empty(),
            dm_only: false,
            guild_only: false,
            help_available: true,
            owners_only: false,
            allowed_roles: Vec::new(),
        }
    }
}

pub fn positions(ctx: &mut Context, msg: &Message, conf: &Configuration) -> Option<Vec<usize>> {
    if !conf.prefixes.is_empty() || conf.dynamic_prefix.is_some() {
        // Find out if they were mentioned. If not, determine if the prefix
        // was used. If not, return None.
        let mut positions: Vec<usize> = vec![];

        if let Some(mention_end) = find_mention_end(&msg.content, conf) {
            positions.push(mention_end);
        } else if let Some(ref func) = conf.dynamic_prefix {
            if let Some(x) = func(ctx, msg) {
                if msg.content.starts_with(&x) {
                    positions.push(x.len());
                }
            } else {
                for n in &conf.prefixes {
                    if msg.content.starts_with(n) {
                        positions.push(n.len());
                    }
                }
            }
        } else {
            for n in &conf.prefixes {
                if msg.content.starts_with(n) {
                    positions.push(n.len());
                }
            }
        };

        if positions.is_empty() {
            return None;
        }

        if conf.allow_whitespace {
            let pos = *unsafe { positions.get_unchecked(0) };

            positions.insert(0, pos + 1);
        }

        Some(positions)
    } else if conf.on_mention.is_some() {
        find_mention_end(&msg.content, conf).map(|mention_end| {
            let mut positions = vec![mention_end];

            if conf.allow_whitespace {
                positions.insert(0, mention_end + 1);
            }

            positions
        })
    } else {
        None
    }
}

fn find_mention_end(content: &str, conf: &Configuration) -> Option<usize> {
    conf.on_mention.as_ref().and_then(|mentions| {
        mentions
            .iter()
            .find(|mention| content.starts_with(&mention[..]))
            .map(|m| m.len())
    })
}
<commit_msg>Make the internal string public<commit_after>use std::sync::Arc;
use super::{Args, Configuration};
use client::Context;
use model::{Message, Permissions};
use std::collections::HashMap;
use std::fmt;

pub type Check = Fn(&mut Context, &Message, &mut Args, &Arc<Command>) -> bool
                     + Send
                     + Sync
                     + 'static;
pub type Exec = fn(&mut Context, &Message, Args) -> Result<(), Error>;
pub type Help = fn(&mut Context, &Message, HashMap<String, Arc<CommandGroup>>, Args)
                   -> Result<(), Error>;
pub type BeforeHook = Fn(&mut Context, &Message, &str) -> bool + Send + Sync + 'static;
pub type AfterHook = Fn(&mut Context, &Message, &str, Result<(), Error>) + Send + Sync + 'static;
pub(crate) type InternalCommand = Arc<Command>;
pub type PrefixCheck = Fn(&mut Context, &Message) -> Option<String> + Send + Sync + 'static;

pub enum CommandOrAlias {
    Alias(String),
    Command(InternalCommand),
}

/// An error from a command.
#[derive(Clone, Debug)]
pub struct Error(pub String);

impl<D: fmt::Display> From<D> for Error {
    fn from(d: D) -> Self {
        Error(format!("{}", d))
    }
}

/// Command function type. Allows to access internal framework things inside
/// your commands.
pub enum CommandType {
    StringResponse(String),
    Basic(Exec),
    WithCommands(Help),
}

pub struct CommandGroup {
    pub prefix: Option<String>,
    pub commands: HashMap<String, CommandOrAlias>,
    /// Some fields taken from Command
    pub bucket: Option<String>,
    pub required_permissions: Permissions,
    pub allowed_roles: Vec<String>,
    pub help_available: bool,
    pub dm_only: bool,
    pub guild_only: bool,
    pub owners_only: bool,
}

/// Command struct used to store commands internally.
pub struct Command {
    /// A set of checks to be called prior to executing the command. The checks
    /// will short-circuit on the first check that returns `false`.
    pub checks: Vec<Box<Check>>,
    /// Function called when the command is called.
    pub exec: CommandType,
    /// Ratelimit bucket.
    pub bucket: Option<String>,
    /// Command description, used by other commands.
    pub desc: Option<String>,
    /// Example arguments, used by other commands.
    pub example: Option<String>,
    /// Command usage schema, used by other commands.
    pub usage: Option<String>,
    /// Minumum amount of arguments that should be passed.
    pub min_args: Option<i32>,
    /// Maximum amount of arguments that can be passed.
    pub max_args: Option<i32>,
    /// Permissions required to use this command.
    pub required_permissions: Permissions,
    /// Roles allowed to use this command.
    pub allowed_roles: Vec<String>,
    /// Whether command should be displayed in help list or not, used by other commands.
    pub help_available: bool,
    /// Whether command can be used only privately or not.
    pub dm_only: bool,
    /// Whether command can be used only in guilds or not.
    pub guild_only: bool,
    /// Whether command can only be used by owners or not.
    pub owners_only: bool,
    pub(crate) aliases: Vec<String>,
}

impl Command {
    pub fn new(f: fn(&mut Context, &Message, Args) -> Result<(), Error>) -> Self {
        Command {
            exec: CommandType::Basic(f),
            ..Command::default()
        }
    }
}

impl Default for Command {
    fn default() -> Command {
        Command {
            aliases: Vec::new(),
            checks: Vec::default(),
            exec: CommandType::Basic(|_, _, _| Ok(())),
            desc: None,
            usage: None,
            example: None,
            min_args: None,
            bucket: None,
            max_args: None,
            required_permissions: Permissions::empty(),
            dm_only: false,
            guild_only: false,
            help_available: true,
            owners_only: false,
            allowed_roles: Vec::new(),
        }
    }
}

pub fn positions(ctx: &mut Context, msg: &Message, conf: &Configuration) -> Option<Vec<usize>> {
    if !conf.prefixes.is_empty() || conf.dynamic_prefix.is_some() {
        // Find out if they were mentioned. If not, determine if the prefix
        // was used. If not, return None.
        let mut positions: Vec<usize> = vec![];

        if let Some(mention_end) = find_mention_end(&msg.content, conf) {
            positions.push(mention_end);
        } else if let Some(ref func) = conf.dynamic_prefix {
            if let Some(x) = func(ctx, msg) {
                if msg.content.starts_with(&x) {
                    positions.push(x.len());
                }
            } else {
                for n in &conf.prefixes {
                    if msg.content.starts_with(n) {
                        positions.push(n.len());
                    }
                }
            }
        } else {
            for n in &conf.prefixes {
                if msg.content.starts_with(n) {
                    positions.push(n.len());
                }
            }
        };

        if positions.is_empty() {
            return None;
        }

        if conf.allow_whitespace {
            let pos = *unsafe { positions.get_unchecked(0) };

            positions.insert(0, pos + 1);
        }

        Some(positions)
    } else if conf.on_mention.is_some() {
        find_mention_end(&msg.content, conf).map(|mention_end| {
            let mut positions = vec![mention_end];

            if conf.allow_whitespace {
                positions.insert(0, mention_end + 1);
            }

            positions
        })
    } else {
        None
    }
}

fn find_mention_end(content: &str, conf: &Configuration) -> Option<usize> {
    conf.on_mention.as_ref().and_then(|mentions| {
        mentions
            .iter()
            .find(|mention| content.starts_with(&mention[..]))
            .map(|m| m.len())
    })
}
<|endoftext|>


<commit_before>//! Simple loader error.

use std::error;
use std::fmt;
use std::io;
use parser::binary::Error as ParseError;

/// Load result.
pub type Result<T> = ::std::result::Result<T, Error>;

/// Load error.
#[derive(Debug, Clone)]
pub enum Error {
    /// Attribute is invalid.
    ///
    /// This includes the cases below:
    /// - The node has too few or too many node attributes
    /// - The node has wrong types of node attribute values.
    /// - The node has wrong (or unsupported) values of node attributes values.
    InvalidAttribute(String),
    /// Required node is missing.
    MissingNode {
        /// Parent node.
        parent: String,
        /// The missing node, which is child node of the `parent` node.
        ///
        /// This may be `None` if the missing node is unknown or cannot be identified.
        child: Option<String>,
    },
    /// Parse error (including I/O error).
    Parse(ParseError),
    /// Got an unexpected node.
    UnexpectedNode(String),
}

impl Error {
    /// Creates a new `Error::MissingNode`.
    pub fn missing_node<'a, S: Into<String>, T: Into<Option<&'a str>>>(
        parent: S,
        child: T
    ) -> Self {
        Error::MissingNode {
            parent: parent.into(),
            child: child.into().map(|s| s.to_owned()),
        }
    }
}

impl fmt::Display for Error {
    fn fmt(&self, f: &mut fmt::Formatter) -> fmt::Result {
        match *self {
            Error::InvalidAttribute(ref name) => write!(f, "Invalid attribute for node: {}", name),
            Error::MissingNode { ref parent, ref child } => {
                if let Some(child) = child.as_ref() {
                    write!(f, "Missing node: {} (parent={})", child, parent)
                } else {
                    write!(f, "Missing node: parent={}", parent)
                }
            },
            Error::UnexpectedNode(ref name) => write!(f, "Unexpected node: {}", name),
            _ => write!(f, "{}", (self as &error::Error).description()),
        }
    }
}

impl error::Error for Error {
    fn description(&self) -> &str {
        match *self {
            Error::InvalidAttribute(_) => "Invalid node attribute",
            Error::MissingNode { .. } => "Missing node",
            Error::UnexpectedNode(_) => "Unexpected node",
            Error::Parse(ref err) => err.description(),
        }
    }

    fn cause(&self) -> Option<&error::Error> {
        match *self {
            Error::Parse(ref err) => Some(err),
            _ => None,
        }
    }
}

impl From<ParseError> for Error {
    fn from(e: ParseError) -> Self {
        Error::Parse(e)
    }
}

impl From<io::Error> for Error {
    fn from(e: io::Error) -> Self {
        Error::Parse(e.into())
    }
}
<commit_msg>Add `Error::LoadObject` load error variant<commit_after>//! Simple loader error.

use std::error;
use std::fmt;
use std::io;
use parser::binary::Error as ParseError;

/// Load result.
pub type Result<T> = ::std::result::Result<T, Error>;

/// Load error.
#[derive(Debug)]
pub enum Error {
    /// Attribute is invalid.
    ///
    /// This includes the cases below:
    /// - The node has too few or too many node attributes
    /// - The node has wrong types of node attribute values.
    /// - The node has wrong (or unsupported) values of node attributes values.
    InvalidAttribute(String),
    /// Object load error.
    LoadObject(Box<error::Error + Send + Sync>),
    /// Required node is missing.
    MissingNode {
        /// Parent node.
        parent: String,
        /// The missing node, which is child node of the `parent` node.
        ///
        /// This may be `None` if the missing node is unknown or cannot be identified.
        child: Option<String>,
    },
    /// Parse error (including I/O error).
    Parse(ParseError),
    /// Got an unexpected node.
    UnexpectedNode(String),
}

impl Error {
    /// Creates a new `Error::MissingNode`.
    pub fn missing_node<'a, S: Into<String>, T: Into<Option<&'a str>>>(
        parent: S,
        child: T
    ) -> Self {
        Error::MissingNode {
            parent: parent.into(),
            child: child.into().map(|s| s.to_owned()),
        }
    }
}

impl fmt::Display for Error {
    fn fmt(&self, f: &mut fmt::Formatter) -> fmt::Result {
        match *self {
            Error::InvalidAttribute(ref name) => write!(f, "Invalid attribute for node: {}", name),
            Error::LoadObject(ref err) => write!(f, "Object load error: {}", err),
            Error::MissingNode { ref parent, ref child } => {
                if let Some(child) = child.as_ref() {
                    write!(f, "Missing node: {} (parent={})", child, parent)
                } else {
                    write!(f, "Missing node: parent={}", parent)
                }
            },
            Error::UnexpectedNode(ref name) => write!(f, "Unexpected node: {}", name),
            _ => write!(f, "{}", (self as &error::Error).description()),
        }
    }
}

impl error::Error for Error {
    fn description(&self) -> &str {
        match *self {
            Error::InvalidAttribute(_) => "Invalid node attribute",
            Error::LoadObject(ref err) => err.description(),
            Error::MissingNode { .. } => "Missing node",
            Error::UnexpectedNode(_) => "Unexpected node",
            Error::Parse(ref err) => err.description(),
        }
    }

    fn cause(&self) -> Option<&error::Error> {
        match *self {
            Error::LoadObject(ref err) => Some(err.as_ref()),
            Error::Parse(ref err) => Some(err),
            _ => None,
        }
    }
}

impl From<ParseError> for Error {
    fn from(e: ParseError) -> Self {
        Error::Parse(e)
    }
}

impl From<io::Error> for Error {
    fn from(e: io::Error) -> Self {
        Error::Parse(e.into())
    }
}
<|endoftext|>


<commit_before>use std::slice;

use ruby_sys::string;

use types::Value;
use util;

pub fn new(string: &str) -> Value {
    let str = util::str_to_cstring(string);

    unsafe { string::rb_str_new_cstr(str.as_ptr()) }
}

pub fn from_value(value: Value) -> String {
    let str = unsafe { string::rb_string_value_cstr(&value) };

    util::cstr_as_string(str)
}

pub fn from_value_unchecked(value: Value) -> String {
    unsafe {
        let str = string::rb_string_value_ptr(&value) as *const u8;
        let len = string::rb_str_len(value) as usize;

        let vec = slice::from_raw_parts(str, len).to_vec();

        String::from_utf8_unchecked(vec)
    }
}

pub fn bytesize(value: Value) -> i64 {
    unsafe { string::rb_str_len(value) }
}
<commit_msg>Convert rb_str_len() result to i64<commit_after>use std::slice;

use ruby_sys::string;

use types::Value;
use util;

pub fn new(string: &str) -> Value {
    let str = util::str_to_cstring(string);

    unsafe { string::rb_str_new_cstr(str.as_ptr()) }
}

pub fn from_value(value: Value) -> String {
    let str = unsafe { string::rb_string_value_cstr(&value) };

    util::cstr_as_string(str)
}

pub fn from_value_unchecked(value: Value) -> String {
    unsafe {
        let str = string::rb_string_value_ptr(&value) as *const u8;
        let len = string::rb_str_len(value) as usize;

        let vec = slice::from_raw_parts(str, len).to_vec();

        String::from_utf8_unchecked(vec)
    }
}

pub fn bytesize(value: Value) -> i64 {
    unsafe { string::rb_str_len(value) as i64 }
}
<|endoftext|>


<commit_before>use super::invoke::{Invoke, Identity};
use super::code::{self, SignaturesBuilder, FunctionBuilder};
use super::import;
use elements;

/// Module builder
pub struct ModuleBuilder<F=Identity> {
    callback: F,
    module: ModuleScaffold,
}

/// Location of the internal module function
pub struct CodeLocation {
    /// Location (index in 'functions' section) of the signature
    pub signature: u32,
    /// Location (index in the 'code' section) of the body
    pub body: u32,
}

#[derive(Default)]
struct ModuleScaffold {
    pub functions: elements::FunctionsSection,
    pub types: elements::TypeSection,
    pub import: elements::ImportSection,
    pub code: elements::CodeSection,
    pub other: Vec<elements::Section>,
}

impl From<elements::Module> for ModuleScaffold {
    fn from(module: elements::Module) -> Self {
        let mut funcs: Option<elements::FunctionsSection> = None;
        let mut types: Option<elements::TypeSection> = None;
        let mut import: Option<elements::ImportSection> = None;
        let mut code: Option<elements::CodeSection> = None;

        let mut sections = module.into_sections();
        while let Some(section) = sections.pop() {
            match section {
                elements::Section::Type(sect) => { types = Some(sect); }
                elements::Section::Function(sect) => { funcs = Some(sect); }
                elements::Section::Import(sect) => { import = Some(sect); }
                elements::Section::Code(sect) => { code = Some(sect); }
                _ => {}
            }
        }

        ModuleScaffold {
            functions: funcs.unwrap_or_default(),
            types: types.unwrap_or_default(),
            import: import.unwrap_or_default(),
            code: code.unwrap_or_default(),
            other: sections,
        }
    }
}

impl From<ModuleScaffold> for elements::Module {
    fn from(module: ModuleScaffold) -> Self {
        let mut sections = Vec::new();

        let types = module.types;
        if types.types().len() > 0 {
            sections.push(elements::Section::Type(types));
        }
        let import = module.import;
        if import.entries().len() > 0 {
            sections.push(elements::Section::Import(import));
        }
        let functions = module.functions;
        if functions.entries().len() > 0 {
            sections.push(elements::Section::Function(functions));
        }
        let code = module.code;
        if code.bodies().len() > 0 {
            sections.push(elements::Section::Code(code));
        }
        sections.extend(module.other);
        elements::Module::new(sections)
    }
}

impl ModuleBuilder {
    /// New empty module builder
    pub fn new() -> Self {
        ModuleBuilder::with_callback(Identity)
    }
}

impl<F> ModuleBuilder<F> where F: Invoke<elements::Module> {
    /// New module builder with bound callback
    pub fn with_callback(callback: F) -> Self {
        ModuleBuilder {
            callback: callback,
            module: Default::default(),
        }
    }

    /// Builder from raw module
    pub fn with_module(mut self, module: elements::Module) -> Self {
        self.module = module.into();
        self
    }

    /// Fill module with sections from iterator
    pub fn with_sections<I>(mut self, sections: I) -> Self
        where I: IntoIterator<Item=elements::Section>
    {
        self.module.other.extend(sections);
        self
    }

    /// Add additional section
    pub fn with_section(mut self, section: elements::Section) -> Self {
        self.module.other.push(section);
        self
    }

    /// Binds to the type section, creates additional types when required
    pub fn with_signatures(mut self, bindings: code::SignatureBindings) -> Self {
        self.push_signatures(bindings);
        self
    }

    /// Push stand-alone function definition, creating sections, signature and code blocks
    /// in corresponding sections.
    /// `FunctionDefinition` can be build using `builder::function` builder
    pub fn push_function(&mut self, func: code::FunctionDefinition) -> CodeLocation {
        let signature = func.signature;
        let body = func.code;

        let type_ref = self.resolve_type_ref(signature);

        self.module.functions.entries_mut().push(elements::Func::new(type_ref));
        let signature_index = self.module.functions.entries_mut().len() as u32 - 1;
        self.module.code.bodies_mut().push(body);
        let body_index = self.module.code.bodies_mut().len() as u32 - 1;

        CodeLocation {
            signature: signature_index,
            body: body_index,
        }
    }

    fn resolve_type_ref(&mut self, signature: code::Signature) -> u32 {
        match signature {
            code::Signature::Inline(func_type) => {
                // todo: maybe search for existing type
                self.module.types.types_mut().push(elements::Type::Function(func_type));
                self.module.types.types().len() as u32 - 1
            }
            code::Signature::TypeReference(type_ref) => {
                type_ref
            }
        }
    }

    /// Push one function signature, returning it's calling index.
    /// Can create corresponding type in type section.
    pub fn push_signature(&mut self, signature: code::Signature) -> u32 {
        self.resolve_type_ref(signature)
    }

    /// Push signatures in the module, returning corresponding indices of pushed signatures
    pub fn push_signatures(&mut self, signatures: code::SignatureBindings) -> Vec<u32> {
        signatures.into_iter().map(|binding|
            self.resolve_type_ref(binding)
        ).collect()
    }

    pub fn function(self) -> FunctionBuilder<Self> {
        FunctionBuilder::with_callback(self)
    }

    /// Define functions section
    pub fn functions(self) -> SignaturesBuilder<Self> {
        SignaturesBuilder::with_callback(self)
    }

    /// With inserted import entry
    pub fn with_import(mut self, entry: elements::ImportEntry) -> Self {
        self.module.import.entries_mut().push(entry);
        self
    }

    /// Import entry builder
    pub fn import(self) -> import::ImportBuilder<Self> {
        import::ImportBuilder::with_callback(self)
    }

    /// Build module (final step)
    pub fn build(self) -> F::Result {
        self.callback.invoke(self.module.into())
    }
}

impl<F> Invoke<elements::FunctionsSection> for ModuleBuilder<F>
    where F: Invoke<elements::Module>
{
	type Result = Self;

	fn invoke(self, section: elements::FunctionsSection) -> Self {
		self.with_section(elements::Section::Function(section))
    }
}

impl<F> Invoke<code::SignatureBindings> for ModuleBuilder<F>
    where F: Invoke<elements::Module>
{
    type Result = Self;

    fn invoke(self, bindings: code::SignatureBindings) -> Self {
        self.with_signatures(bindings)
    }
}

impl<F> Invoke<code::FunctionDefinition> for ModuleBuilder<F>
    where F: Invoke<elements::Module>
{
    type Result = Self;

    fn invoke(self, def: code::FunctionDefinition) -> Self {
        let mut b = self;
        b.push_function(def);
        b
    }
}

impl<F> Invoke<elements::ImportEntry> for ModuleBuilder<F>
    where F: Invoke<elements::Module>
{
    type Result = Self;

    fn invoke(self, entry: elements::ImportEntry) -> Self::Result {
        self.with_import(entry)
    }
}

/// Start new module builder
pub fn module() -> ModuleBuilder {
    ModuleBuilder::new()
}

/// Start builder to extend existing module
pub fn from_module(module: elements::Module) -> ModuleBuilder {
    ModuleBuilder::new().with_module(module)
}

#[cfg(test)]
mod tests {

    use super::module;

    #[test]
    fn smoky() {
        let module = module().build();
        assert_eq!(module.sections().len(), 0);
    }

    #[test]
    fn functions() {
        let module = module()
            .function()
                .signature().param().i32().build()
                .body().build()
                .build()
            .build();

        assert_eq!(module.type_section().expect("type section to exist").types().len(), 1);
        assert_eq!(module.functions_section().expect("function section to exist").entries().len(), 1);
        assert_eq!(module.code_section().expect("code section to exist").bodies().len(), 1);
    }

}
<commit_msg>doc comment<commit_after>use super::invoke::{Invoke, Identity};
use super::code::{self, SignaturesBuilder, FunctionBuilder};
use super::import;
use elements;

/// Module builder
pub struct ModuleBuilder<F=Identity> {
    callback: F,
    module: ModuleScaffold,
}

/// Location of the internal module function
pub struct CodeLocation {
    /// Location (index in 'functions' section) of the signature
    pub signature: u32,
    /// Location (index in the 'code' section) of the body
    pub body: u32,
}

#[derive(Default)]
struct ModuleScaffold {
    pub functions: elements::FunctionsSection,
    pub types: elements::TypeSection,
    pub import: elements::ImportSection,
    pub code: elements::CodeSection,
    pub other: Vec<elements::Section>,
}

impl From<elements::Module> for ModuleScaffold {
    fn from(module: elements::Module) -> Self {
        let mut funcs: Option<elements::FunctionsSection> = None;
        let mut types: Option<elements::TypeSection> = None;
        let mut import: Option<elements::ImportSection> = None;
        let mut code: Option<elements::CodeSection> = None;

        let mut sections = module.into_sections();
        while let Some(section) = sections.pop() {
            match section {
                elements::Section::Type(sect) => { types = Some(sect); }
                elements::Section::Function(sect) => { funcs = Some(sect); }
                elements::Section::Import(sect) => { import = Some(sect); }
                elements::Section::Code(sect) => { code = Some(sect); }
                _ => {}
            }
        }

        ModuleScaffold {
            functions: funcs.unwrap_or_default(),
            types: types.unwrap_or_default(),
            import: import.unwrap_or_default(),
            code: code.unwrap_or_default(),
            other: sections,
        }
    }
}

impl From<ModuleScaffold> for elements::Module {
    fn from(module: ModuleScaffold) -> Self {
        let mut sections = Vec::new();

        let types = module.types;
        if types.types().len() > 0 {
            sections.push(elements::Section::Type(types));
        }
        let import = module.import;
        if import.entries().len() > 0 {
            sections.push(elements::Section::Import(import));
        }
        let functions = module.functions;
        if functions.entries().len() > 0 {
            sections.push(elements::Section::Function(functions));
        }
        let code = module.code;
        if code.bodies().len() > 0 {
            sections.push(elements::Section::Code(code));
        }
        sections.extend(module.other);
        elements::Module::new(sections)
    }
}

impl ModuleBuilder {
    /// New empty module builder
    pub fn new() -> Self {
        ModuleBuilder::with_callback(Identity)
    }
}

impl<F> ModuleBuilder<F> where F: Invoke<elements::Module> {
    /// New module builder with bound callback
    pub fn with_callback(callback: F) -> Self {
        ModuleBuilder {
            callback: callback,
            module: Default::default(),
        }
    }

    /// Builder from raw module
    pub fn with_module(mut self, module: elements::Module) -> Self {
        self.module = module.into();
        self
    }

    /// Fill module with sections from iterator
    pub fn with_sections<I>(mut self, sections: I) -> Self
        where I: IntoIterator<Item=elements::Section>
    {
        self.module.other.extend(sections);
        self
    }

    /// Add additional section
    pub fn with_section(mut self, section: elements::Section) -> Self {
        self.module.other.push(section);
        self
    }

    /// Binds to the type section, creates additional types when required
    pub fn with_signatures(mut self, bindings: code::SignatureBindings) -> Self {
        self.push_signatures(bindings);
        self
    }

    /// Push stand-alone function definition, creating sections, signature and code blocks
    /// in corresponding sections.
    /// `FunctionDefinition` can be build using `builder::function` builder
    pub fn push_function(&mut self, func: code::FunctionDefinition) -> CodeLocation {
        let signature = func.signature;
        let body = func.code;

        let type_ref = self.resolve_type_ref(signature);

        self.module.functions.entries_mut().push(elements::Func::new(type_ref));
        let signature_index = self.module.functions.entries_mut().len() as u32 - 1;
        self.module.code.bodies_mut().push(body);
        let body_index = self.module.code.bodies_mut().len() as u32 - 1;

        CodeLocation {
            signature: signature_index,
            body: body_index,
        }
    }

    fn resolve_type_ref(&mut self, signature: code::Signature) -> u32 {
        match signature {
            code::Signature::Inline(func_type) => {
                // todo: maybe search for existing type
                self.module.types.types_mut().push(elements::Type::Function(func_type));
                self.module.types.types().len() as u32 - 1
            }
            code::Signature::TypeReference(type_ref) => {
                type_ref
            }
        }
    }

    /// Push one function signature, returning it's calling index.
    /// Can create corresponding type in type section.
    pub fn push_signature(&mut self, signature: code::Signature) -> u32 {
        self.resolve_type_ref(signature)
    }

    /// Push signatures in the module, returning corresponding indices of pushed signatures
    pub fn push_signatures(&mut self, signatures: code::SignatureBindings) -> Vec<u32> {
        signatures.into_iter().map(|binding|
            self.resolve_type_ref(binding)
        ).collect()
    }

    /// Add new function using dedicated builder
    pub fn function(self) -> FunctionBuilder<Self> {
        FunctionBuilder::with_callback(self)
    }

    /// Define functions section
    pub fn functions(self) -> SignaturesBuilder<Self> {
        SignaturesBuilder::with_callback(self)
    }

    /// With inserted import entry
    pub fn with_import(mut self, entry: elements::ImportEntry) -> Self {
        self.module.import.entries_mut().push(entry);
        self
    }

    /// Import entry builder
    pub fn import(self) -> import::ImportBuilder<Self> {
        import::ImportBuilder::with_callback(self)
    }

    /// Build module (final step)
    pub fn build(self) -> F::Result {
        self.callback.invoke(self.module.into())
    }
}

impl<F> Invoke<elements::FunctionsSection> for ModuleBuilder<F>
    where F: Invoke<elements::Module>
{
	type Result = Self;

	fn invoke(self, section: elements::FunctionsSection) -> Self {
		self.with_section(elements::Section::Function(section))
    }
}

impl<F> Invoke<code::SignatureBindings> for ModuleBuilder<F>
    where F: Invoke<elements::Module>
{
    type Result = Self;

    fn invoke(self, bindings: code::SignatureBindings) -> Self {
        self.with_signatures(bindings)
    }
}

impl<F> Invoke<code::FunctionDefinition> for ModuleBuilder<F>
    where F: Invoke<elements::Module>
{
    type Result = Self;

    fn invoke(self, def: code::FunctionDefinition) -> Self {
        let mut b = self;
        b.push_function(def);
        b
    }
}

impl<F> Invoke<elements::ImportEntry> for ModuleBuilder<F>
    where F: Invoke<elements::Module>
{
    type Result = Self;

    fn invoke(self, entry: elements::ImportEntry) -> Self::Result {
        self.with_import(entry)
    }
}

/// Start new module builder
pub fn module() -> ModuleBuilder {
    ModuleBuilder::new()
}

/// Start builder to extend existing module
pub fn from_module(module: elements::Module) -> ModuleBuilder {
    ModuleBuilder::new().with_module(module)
}

#[cfg(test)]
mod tests {

    use super::module;

    #[test]
    fn smoky() {
        let module = module().build();
        assert_eq!(module.sections().len(), 0);
    }

    #[test]
    fn functions() {
        let module = module()
            .function()
                .signature().param().i32().build()
                .body().build()
                .build()
            .build();

        assert_eq!(module.type_section().expect("type section to exist").types().len(), 1);
        assert_eq!(module.functions_section().expect("function section to exist").entries().len(), 1);
        assert_eq!(module.code_section().expect("code section to exist").bodies().len(), 1);
    }

}
<|endoftext|>


<commit_before>use std::io;

macro_rules! print_err {
    ($($arg:tt)*) => (
        {
            use std::io::Write;
            writeln!(&mut ::std::io::stderr(), $($arg)*).ok();
        }
    )
}

macro_rules! parse_input {
    ($x:expr, $t:ident) => ($x.trim().parse::<$t>().unwrap())
}

/**
 * Auto-generated code below aims at helping you parse
 * the standard input according to the problem statement.
 **/
fn main() {
    print_err!("Enter the numer of temperatures that will be input: ");
    let mut input_line = String::new();
    io::stdin().read_line(&mut input_line).unwrap();
    let n = parse_input!(input_line, i32); // the number of temperatures to analyse
    print_err!("The number of temperatures to input : {}", n);
    let mut input_line = String::new();
    print_err!("Enter the list of temperatures : ");
    io::stdin().read_line(&mut input_line).unwrap();
    let temps = input_line.trim_right().to_string(); // the n temperatures expressed as integers ranging from -273 to 5526
    print_err!("The line of temperatures input : {}", temps);

    //let vec: Vec<&str> = temps.split_whitespace().collect();

    //for s in vec {
    //    print_err!("Read temperature : {}", s);
    //}

    //let numbers: Vec<i32> = reader.read_line().unwrap().as_slice().split_whitespace().map(|s| s.parse().unwrap()).collect()
    //let my_int: i32 = my_string.parse().unwrap();
    let temperatures: Vec<i32> = temps.split_whitespace().map(|s| s.parse().unwrap()).collect();
    for t in temperatures {
        print_err!("Read temperature : {}", t);
    }
    // Write an action using println!("message...");
    // To debug: print_err!("Debug message...");

    println!("result");
}
<commit_msg>[WIP] Solving the CG easy puzzle "temperatures" in Rust - version that passes the test but needs some cleaning<commit_after>use std::io;

macro_rules! print_err {
    ($($arg:tt)*) => (
        {
            use std::io::Write;
            writeln!(&mut ::std::io::stderr(), $($arg)*).ok();
        }
    )
}

macro_rules! parse_input {
    ($x:expr, $t:ident) => ($x.trim().parse::<$t>().unwrap())
}

fn to_temperature_with_absolute_value(t: i32) -> (i32, i32) {
    (t, t.abs())
}

/**
 * Auto-generated code below aims at helping you parse
 * the standard input according to the problem statement.
 **/
fn main() {
    print_err!("Enter the numer of temperatures that will be input: ");
    let mut input_line = String::new();
    io::stdin().read_line(&mut input_line).unwrap();
    let n = parse_input!(input_line, i32); // the number of temperatures to analyse
    print_err!("The number of temperatures to input : {}", n);
    let mut input_line = String::new();
    print_err!("Enter the list of temperatures : ");
    io::stdin().read_line(&mut input_line).unwrap();
    let temps = input_line.trim_right().to_string(); // the n temperatures expressed as integers ranging from -273 to 5526
    print_err!("The line of temperatures input : {}", temps);

    let result:i32 = if n == 0 {
            0
        }
        else {
            //let vec: Vec<&str> = temps.split_whitespace().collect();

            //for s in vec {
            //    print_err!("Read temperature : {}", s);
            //}

            //let numbers: Vec<i32> = reader.read_line().unwrap().as_slice().split_whitespace().map(|s| s.parse().unwrap()).collect()
            //let my_int: i32 = my_string.parse().unwrap();
            let temperatures: Vec<(i32,i32)> = temps.split_whitespace().map(|s| to_temperature_with_absolute_value(s.parse().unwrap())).collect();
            print_err!("(Temperature,Temperature in absolute value)");
            let mut min_abs:(i32,i32) = temperatures[0];
            for t in temperatures {
                print_err!("({},{})", t.0,t.1);
                if t.1 < min_abs.1 || (t.1 == min_abs.1 && t.0 >= 0) {
                    min_abs = t;
                }
            }

            min_abs.0

            // Write an action using println!("message...");
            // To debug: print_err!("Debug message...");
        };
    println!("{}",result);
}
<|endoftext|>