<commit_before><commit_msg>Added DAG example<commit_after>/*

A Directed Acyclic Graph (DAG) of `n` nodes is isomorphic to
the upper or lower strictly triangular binary square `n ⨯ n` matrix.

The discrete space of pairs can be thought of as:

    -       (a, b)  (a, c)  (a, d)
    -       -       (b, c)  (b, d)
    -       -       -       (c, d)
    -       -       -       -

In a strictly triangular binary square matrix,
these pairs are filled with `0` or `1`.

Therefore, by taking the powerset of pair,
one can construct a discrete space that is isomorphic to DAGs.

    n       count
    1       1
    2       2
    3       8
    4       64
    5       1024
    6       32768
    7       2097152
    8       268435456
    9       68719476736
    10      35184372088832
    11      36028797018963968

When enumerating this space, the position is a list of pairs.

For example: `[(0, 2), (1, 2)]`.

This means that `2` depends on `0` and `1`.
The DAG can be constructed directly from this data.

*/

extern crate discrete;

use discrete::{ Construct, Count, ToPos, PowerSet, Of, Pair };

fn main() {
    let n = 3;
    let dag: PowerSet<Of<Pair>> = Construct::new();
    let count = dag.count(&n);
    println!("{}", count);

    let mut pos = vec![];
    for i in 0..count {
        dag.to_pos(&n, i, &mut pos);
        println!("{:?}", pos);
    }
}

<|endoftext|>


<commit_before>use core::mem;

use common::elf::*;

use programs::common::*;

pub struct Executor {
    executable: ELF,
    mapped: AtomicUsize,
    entry: usize,
    exit: usize,
    draw: usize,
    on_key: usize,
    on_mouse: usize
}

impl Executor {
    pub fn new() -> Executor {
        Executor {
            executable: ELF::new(),
            mapped: AtomicUsize::new(0),
            entry: 0,
            exit: 0,
            draw: 0,
            on_mouse: 0,
            on_key: 0
        }
    }

    unsafe fn entry(&mut self){
        if self.executable.can_call(self.entry){
            //Rediculous call mechanism
            self.unsafe_map();
            let fn_ptr: *const usize = &self.entry;
            (*(fn_ptr as *const fn()))();
            self.unsafe_unmap();
        }
    }

    unsafe fn unsafe_map(&mut self){
        let mapped = self.mapped.fetch_add(1, Ordering::SeqCst);
        if self.executable.data > 0 && mapped == 0{
            self.executable.map();
        }
    }

    unsafe fn unsafe_unmap(&mut self){
        let mapped = self.mapped.fetch_sub(1, Ordering::SeqCst);
        if self.executable.data > 0 && mapped == 1{
            self.executable.unmap();
        }
    }
}

impl Drop for Executor {
    fn drop(&mut self){
        unsafe{
            if self.executable.can_call(self.exit){
                //Rediculous call mechanism
                self.unsafe_map();
                let fn_ptr: *const usize = &self.exit;
                (*(fn_ptr as *const fn()))();
                self.unsafe_unmap();
            }
        }
    }
}

impl SessionItem for Executor {
    fn main(&mut self, url: URL){
        let mut resource = url.open();

        let mut vec: Vec<u8> = Vec::new();
        match resource.read_to_end(&mut vec){
            Option::Some(_) => {
                unsafe{
                    let reenable = start_no_ints();

                    self.executable = ELF::from_data(vec.as_ptr() as usize);
                    //self.executable.d();

                    self.entry = self.executable.entry();
                    self.exit = self.executable.symbol("exit".to_string());
                    self.draw = self.executable.symbol("draw".to_string());
                    self.on_key = self.executable.symbol("on_key".to_string());
                    self.on_mouse = self.executable.symbol("on_mouse".to_string());

                    self.entry();

                    RedrawEvent {
                        redraw: REDRAW_ALL
                    }.to_event().trigger();

                    end_no_ints(reenable);
                }
            },
            Option::None => ()
        }
    }

    fn draw(&self, display: &Display) -> bool{
        unsafe {
            if self.executable.can_call(self.draw){
                //Rediculous call mechanism
                let self_mut: *mut Executor = mem::transmute(self);

                (*self_mut).unsafe_map();
                let fn_ptr: *const usize = &self.draw;
                let ret = (*(fn_ptr as *const fn(&Display) -> bool))(display);
                (*self_mut).unsafe_unmap();

                return ret;
            }
        }
        return true;
    }

    fn on_key(&mut self, key_event: KeyEvent){
        unsafe {
            if self.executable.can_call(self.on_key){
                //Rediculous call mechanism
                self.unsafe_map();
                let fn_ptr: *const usize = &self.on_key;
                (*(fn_ptr as *const fn(KeyEvent)))(key_event);
                self.unsafe_unmap();
            }
        }
    }

    fn on_mouse(&mut self, mouse_event: MouseEvent, allow_catch: bool) -> bool{
        unsafe {
            if self.executable.can_call(self.on_mouse){
                //Rediculous call mechanism
                self.unsafe_map();
                let fn_ptr: *const usize = &self.on_mouse;
                let ret = (*(fn_ptr as *const fn(MouseEvent, bool) -> bool))(mouse_event, allow_catch);
                self.unsafe_unmap();
                return ret;
            }
        }
        return false;
    }
}
<commit_msg>Atomic map/unmap<commit_after>use core::mem;

use common::elf::*;

use programs::common::*;

pub struct Executor {
    executable: ELF,
    mapped: AtomicUsize,
    entry: usize,
    exit: usize,
    draw: usize,
    on_key: usize,
    on_mouse: usize
}

impl Executor {
    pub fn new() -> Executor {
        Executor {
            executable: ELF::new(),
            mapped: AtomicUsize::new(0),
            entry: 0,
            exit: 0,
            draw: 0,
            on_mouse: 0,
            on_key: 0
        }
    }

    unsafe fn entry(&mut self){
        if self.executable.can_call(self.entry){
            //Rediculous call mechanism
            self.unsafe_map();
            let fn_ptr: *const usize = &self.entry;
            (*(fn_ptr as *const fn()))();
            self.unsafe_unmap();
        }
    }

    unsafe fn unsafe_map(&mut self){
        let reenable = start_no_ints();
        let mapped = self.mapped.fetch_add(1, Ordering::SeqCst);
        if self.executable.data > 0 && mapped == 0{
            self.executable.map();
        }
        end_no_ints(reenable);
    }

    unsafe fn unsafe_unmap(&mut self){
        let reenable = start_no_ints();
        let mapped = self.mapped.fetch_sub(1, Ordering::SeqCst);
        if self.executable.data > 0 && mapped == 1{
            self.executable.unmap();
        }
        end_no_ints(reenable);
    }
}

impl Drop for Executor {
    fn drop(&mut self){
        unsafe{
            if self.executable.can_call(self.exit){
                //Rediculous call mechanism
                self.unsafe_map();
                let fn_ptr: *const usize = &self.exit;
                (*(fn_ptr as *const fn()))();
                self.unsafe_unmap();
            }
        }
    }
}

impl SessionItem for Executor {
    fn main(&mut self, url: URL){
        let mut resource = url.open();

        let mut vec: Vec<u8> = Vec::new();
        match resource.read_to_end(&mut vec){
            Option::Some(_) => {
                unsafe{
                    let reenable = start_no_ints();

                    self.executable = ELF::from_data(vec.as_ptr() as usize);
                    //self.executable.d();

                    self.entry = self.executable.entry();
                    self.exit = self.executable.symbol("exit".to_string());
                    self.draw = self.executable.symbol("draw".to_string());
                    self.on_key = self.executable.symbol("on_key".to_string());
                    self.on_mouse = self.executable.symbol("on_mouse".to_string());

                    self.entry();

                    RedrawEvent {
                        redraw: REDRAW_ALL
                    }.to_event().trigger();

                    end_no_ints(reenable);
                }
            },
            Option::None => ()
        }
    }

    fn draw(&self, display: &Display) -> bool{
        unsafe {
            if self.executable.can_call(self.draw){
                //Rediculous call mechanism
                let self_mut: *mut Executor = mem::transmute(self);

                (*self_mut).unsafe_map();
                let fn_ptr: *const usize = &self.draw;
                let ret = (*(fn_ptr as *const fn(&Display) -> bool))(display);
                (*self_mut).unsafe_unmap();

                return ret;
            }
        }
        return true;
    }

    fn on_key(&mut self, key_event: KeyEvent){
        unsafe {
            if self.executable.can_call(self.on_key){
                //Rediculous call mechanism
                self.unsafe_map();
                let fn_ptr: *const usize = &self.on_key;
                (*(fn_ptr as *const fn(KeyEvent)))(key_event);
                self.unsafe_unmap();
            }
        }
    }

    fn on_mouse(&mut self, mouse_event: MouseEvent, allow_catch: bool) -> bool{
        unsafe {
            if self.executable.can_call(self.on_mouse){
                //Rediculous call mechanism
                self.unsafe_map();
                let fn_ptr: *const usize = &self.on_mouse;
                let ret = (*(fn_ptr as *const fn(MouseEvent, bool) -> bool))(mouse_event, allow_catch);
                self.unsafe_unmap();
                return ret;
            }
        }
        return false;
    }
}
<|endoftext|>


<commit_before><commit_msg>Suppress warnings.<commit_after><|endoftext|>


<commit_before><commit_msg>Impl expression lvl 0.<commit_after><|endoftext|>


<commit_before>// Copyright 2015 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.

#![allow(non_snake_case)]

register_long_diagnostics! {
E0454: r##"
A link name was given with an empty name. Erroneous code example:

```compile_fail,E0454
#[link(name = "")] extern {} // error: #[link(name = "")] given with empty name
```

The rust compiler cannot link to an external library if you don't give it its
name. Example:

```
#[link(name = "some_lib")] extern {} // ok!
```
"##,

E0455: r##"
Linking with `kind=framework` is only supported when targeting OS X,
as frameworks are specific to that operating system.

Erroneous code example:

```compile_fail,E0455
#[link(name = "FooCoreServices",  kind = "framework")] extern {}
// OS used to compile is Linux for example
```

To solve this error you can use conditional compilation:

```
#[cfg_attr(target="macos", link(name = "FooCoreServices", kind = "framework"))]
extern {}
```

See more: https://doc.rust-lang.org/book/conditional-compilation.html
"##,

E0458: r##"
An unknown "kind" was specified for a link attribute. Erroneous code example:

```compile_fail,E0458
#[link(kind = "wonderful_unicorn")] extern {}
// error: unknown kind: `wonderful_unicorn`
```

Please specify a valid "kind" value, from one of the following:
 * static
 * dylib
 * framework
"##,

E0459: r##"
A link was used without a name parameter. Erroneous code example:

```compile_fail,E0459
#[link(kind = "dylib")] extern {}
// error: #[link(...)] specified without `name = "foo"`
```

Please add the name parameter to allow the rust compiler to find the library
you want. Example:

```
#[link(kind = "dylib", name = "some_lib")] extern {} // ok!
```
"##,

E0463: r##"
A plugin/crate was declared but cannot be found. Erroneous code example:

```compile_fail,E0463
#![feature(plugin)]
#![plugin(cookie_monster)] // error: can't find crate for `cookie_monster`
extern crate cake_is_a_lie; // error: can't find crate for `cake_is_a_lie`
```

You need to link your code to the relevant crate in order to be able to use it
(through Cargo or the `-L` option of rustc example). Plugins are crates as
well, and you link to them the same way.
"##,

E0466: r##"
Macro import declarations were malformed.

Erroneous code examples:

```compile_fail,E0466
#[macro_use(a_macro(another_macro))] // error: invalid import declaration
extern crate some_crate;

#[macro_use(i_want = "some_macros")] // error: invalid import declaration
extern crate another_crate;
```

This is a syntax error at the level of attribute declarations. The proper
syntax for macro imports is the following:

```ignore
// In some_crate:
#[macro_export]
macro_rules! get_tacos {
    ...
}

#[macro_export]
macro_rules! get_pimientos {
    ...
}

// In your crate:
#[macro_use(get_tacos, get_pimientos)] // It imports `get_tacos` and
extern crate some_crate;               // `get_pimientos` macros from some_crate.
```

If you would like to import all exported macros, write `macro_use` with no
arguments.
"##,

E0467: r##"
Macro reexport declarations were empty or malformed.

Erroneous code examples:

```compile_fail,E0467
#[macro_reexport]                    // error: no macros listed for export
extern crate macros_for_good;

#[macro_reexport(fun_macro = "foo")] // error: not a macro identifier
extern crate other_macros_for_good;
```

This is a syntax error at the level of attribute declarations.

Currently, `macro_reexport` requires at least one macro name to be listed.
Unlike `macro_use`, listing no names does not reexport all macros from the
given crate.

Decide which macros you would like to export and list them properly.

These are proper reexport declarations:

```ignore
#[macro_reexport(some_macro, another_macro)]
extern crate macros_for_good;
```
"##,

E0468: r##"
A non-root module attempts to import macros from another crate.

Example of erroneous code:

```compile_fail,E0468
mod foo {
    #[macro_use(helpful_macro)] // error: must be at crate root to import
    extern crate some_crate;    //        macros from another crate
    helpful_macro!(...)
}
```

Only `extern crate` imports at the crate root level are allowed to import
macros.

Either move the macro import to crate root or do without the foreign macros.
This will work:

```ignore
#[macro_use(helpful_macro)]
extern crate some_crate;

mod foo {
    helpful_macro!(...)
}
```
"##,

E0469: r##"
A macro listed for import was not found.

Erroneous code example:

```compile_fail,E0469
#[macro_use(drink, be_merry)] // error: imported macro not found
extern crate collections;

fn main() {
    // ...
}
```

Either the listed macro is not contained in the imported crate, or it is not
exported from the given crate.

This could be caused by a typo. Did you misspell the macro's name?

Double-check the names of the macros listed for import, and that the crate
in question exports them.

A working version would be:

```ignore
// In some_crate:
#[macro_export]
macro_rules! eat {
    ...
}

#[macro_export]
macro_rules! drink {
    ...
}

// In your crate:
#[macro_use(eat, drink)]
extern crate some_crate; //ok!
```
"##,

}

register_diagnostics! {
    E0456, // plugin `..` is not available for triple `..`
    E0457, // plugin `..` only found in rlib format, but must be available...
    E0514, // metadata version mismatch
    E0460, // found possibly newer version of crate `..`
    E0461, // couldn't find crate `..` with expected target triple ..
    E0462, // found staticlib `..` instead of rlib or dylib
    E0464, // multiple matching crates for `..`
    E0465, // multiple .. candidates for `..` found
    E0470, // reexported macro not found
    E0519, // local crate and dependency have same (crate-name, disambiguator)
    E0523, // two dependencies have same (crate-name, disambiguator) but different SVH
}
<commit_msg>Add E0470 error explanation<commit_after>// Copyright 2015 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.

#![allow(non_snake_case)]

register_long_diagnostics! {
E0454: r##"
A link name was given with an empty name. Erroneous code example:

```compile_fail,E0454
#[link(name = "")] extern {} // error: #[link(name = "")] given with empty name
```

The rust compiler cannot link to an external library if you don't give it its
name. Example:

```
#[link(name = "some_lib")] extern {} // ok!
```
"##,

E0455: r##"
Linking with `kind=framework` is only supported when targeting OS X,
as frameworks are specific to that operating system.

Erroneous code example:

```compile_fail,E0455
#[link(name = "FooCoreServices",  kind = "framework")] extern {}
// OS used to compile is Linux for example
```

To solve this error you can use conditional compilation:

```
#[cfg_attr(target="macos", link(name = "FooCoreServices", kind = "framework"))]
extern {}
```

See more: https://doc.rust-lang.org/book/conditional-compilation.html
"##,

E0458: r##"
An unknown "kind" was specified for a link attribute. Erroneous code example:

```compile_fail,E0458
#[link(kind = "wonderful_unicorn")] extern {}
// error: unknown kind: `wonderful_unicorn`
```

Please specify a valid "kind" value, from one of the following:
 * static
 * dylib
 * framework
"##,

E0459: r##"
A link was used without a name parameter. Erroneous code example:

```compile_fail,E0459
#[link(kind = "dylib")] extern {}
// error: #[link(...)] specified without `name = "foo"`
```

Please add the name parameter to allow the rust compiler to find the library
you want. Example:

```
#[link(kind = "dylib", name = "some_lib")] extern {} // ok!
```
"##,

E0463: r##"
A plugin/crate was declared but cannot be found. Erroneous code example:

```compile_fail,E0463
#![feature(plugin)]
#![plugin(cookie_monster)] // error: can't find crate for `cookie_monster`
extern crate cake_is_a_lie; // error: can't find crate for `cake_is_a_lie`
```

You need to link your code to the relevant crate in order to be able to use it
(through Cargo or the `-L` option of rustc example). Plugins are crates as
well, and you link to them the same way.
"##,

E0466: r##"
Macro import declarations were malformed.

Erroneous code examples:

```compile_fail,E0466
#[macro_use(a_macro(another_macro))] // error: invalid import declaration
extern crate some_crate;

#[macro_use(i_want = "some_macros")] // error: invalid import declaration
extern crate another_crate;
```

This is a syntax error at the level of attribute declarations. The proper
syntax for macro imports is the following:

```ignore
// In some_crate:
#[macro_export]
macro_rules! get_tacos {
    ...
}

#[macro_export]
macro_rules! get_pimientos {
    ...
}

// In your crate:
#[macro_use(get_tacos, get_pimientos)] // It imports `get_tacos` and
extern crate some_crate;               // `get_pimientos` macros from some_crate.
```

If you would like to import all exported macros, write `macro_use` with no
arguments.
"##,

E0467: r##"
Macro reexport declarations were empty or malformed.

Erroneous code examples:

```compile_fail,E0467
#[macro_reexport]                    // error: no macros listed for export
extern crate macros_for_good;

#[macro_reexport(fun_macro = "foo")] // error: not a macro identifier
extern crate other_macros_for_good;
```

This is a syntax error at the level of attribute declarations.

Currently, `macro_reexport` requires at least one macro name to be listed.
Unlike `macro_use`, listing no names does not reexport all macros from the
given crate.

Decide which macros you would like to export and list them properly.

These are proper reexport declarations:

```ignore
#[macro_reexport(some_macro, another_macro)]
extern crate macros_for_good;
```
"##,

E0468: r##"
A non-root module attempts to import macros from another crate.

Example of erroneous code:

```compile_fail,E0468
mod foo {
    #[macro_use(helpful_macro)] // error: must be at crate root to import
    extern crate some_crate;    //        macros from another crate
    helpful_macro!(...)
}
```

Only `extern crate` imports at the crate root level are allowed to import
macros.

Either move the macro import to crate root or do without the foreign macros.
This will work:

```ignore
#[macro_use(helpful_macro)]
extern crate some_crate;

mod foo {
    helpful_macro!(...)
}
```
"##,

E0469: r##"
A macro listed for import was not found.

Erroneous code example:

```compile_fail,E0469
#[macro_use(drink, be_merry)] // error: imported macro not found
extern crate collections;

fn main() {
    // ...
}
```

Either the listed macro is not contained in the imported crate, or it is not
exported from the given crate.

This could be caused by a typo. Did you misspell the macro's name?

Double-check the names of the macros listed for import, and that the crate
in question exports them.

A working version would be:

```ignore
// In some_crate crate:
#[macro_export]
macro_rules! eat {
    ...
}

#[macro_export]
macro_rules! drink {
    ...
}

// In your crate:
#[macro_use(eat, drink)]
extern crate some_crate; //ok!
```
"##,

E0470: r##"
A macro listed for reexport was not found.

Erroneous code example:

```compile_fail,E0470
#[macro_reexport(drink, be_merry)]
extern crate collections;

fn main() {
    // ...
}
```

Either the listed macro is not contained in the imported crate, or it is not
exported from the given crate.

This could be caused by a typo. Did you misspell the macro's name?

Double-check the names of the macros listed for reexport, and that the crate
in question exports them.

A working version:

```ignore
// In some_crate crate:
#[macro_export]
macro_rules! eat {
    ...
}

#[macro_export]
macro_rules! drink {
    ...
}

// In your_crate:
#[macro_reexport(eat, drink)]
extern crate some_crate;
```
"##,

}

register_diagnostics! {
    E0456, // plugin `..` is not available for triple `..`
    E0457, // plugin `..` only found in rlib format, but must be available...
    E0514, // metadata version mismatch
    E0460, // found possibly newer version of crate `..`
    E0461, // couldn't find crate `..` with expected target triple ..
    E0462, // found staticlib `..` instead of rlib or dylib
    E0464, // multiple matching crates for `..`
    E0465, // multiple .. candidates for `..` found
    E0519, // local crate and dependency have same (crate-name, disambiguator)
    E0523, // two dependencies have same (crate-name, disambiguator) but different SVH
}
<|endoftext|>


<commit_before><commit_msg>Rollup merge of #72547 - alex:patch-1, r=oli-obk<commit_after>// no-system-llvm
// compile-flags: -O -C overflow-checks=on

#![crate_type = "lib"]

pub struct S1<'a> {
    data: &'a [u8],
    position: usize,
}

// CHECK-LABEL: @slice_no_index_order
#[no_mangle]
pub fn slice_no_index_order<'a>(s: &'a mut S1, n: usize) -> &'a [u8] {
    // CHECK-NOT: slice_index_order_fail
    let d = &s.data[s.position..s.position+n];
    s.position += n;
    return d;
}

// CHECK-LABEL: @test_check
#[no_mangle]
pub fn test_check<'a>(s: &'a mut S1, x: usize, y: usize) -> &'a [u8] {
    // CHECK: slice_index_order_fail
    &s.data[x..y]
}
<|endoftext|>


<commit_before><commit_msg>Rollup merge of #26451 - nham:fix_18058, r=arielb1<commit_after>// Copyright 2015 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.

impl Undefined {}
//~^ ERROR use of undeclared type name `Undefined`

fn main() {}
<|endoftext|>


<commit_before>// Copyright 2017 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.

extern crate toml;
extern crate rustc_serialize;

use std::collections::{BTreeMap, HashMap};
use std::env;
use std::fs::File;
use std::io::{self, Read, Write};
use std::path::{PathBuf, Path};
use std::process::{Command, Stdio};

static HOSTS: &'static [&'static str] = &[
    "aarch64-unknown-linux-gnu",
    "arm-unknown-linux-gnueabi",
    "arm-unknown-linux-gnueabihf",
    "armv7-unknown-linux-gnueabihf",
    "i686-apple-darwin",
    "i686-pc-windows-gnu",
    "i686-pc-windows-msvc",
    "i686-unknown-linux-gnu",
    "mips-unknown-linux-gnu",
    "mips64-unknown-linux-gnuabi64",
    "mips64el-unknown-linux-gnuabi64",
    "mipsel-unknown-linux-gnu",
    "powerpc-unknown-linux-gnu",
    "powerpc64-unknown-linux-gnu",
    "powerpc64le-unknown-linux-gnu",
    "s390x-unknown-linux-gnu",
    "x86_64-apple-darwin",
    "x86_64-pc-windows-gnu",
    "x86_64-pc-windows-msvc",
    "x86_64-unknown-freebsd",
    "x86_64-unknown-linux-gnu",
    "x86_64-unknown-netbsd",
];

static TARGETS: &'static [&'static str] = &[
    "aarch64-apple-ios",
    "aarch64-linux-android",
    "aarch64-unknown-linux-gnu",
    "arm-linux-androideabi",
    "arm-unknown-linux-gnueabi",
    "arm-unknown-linux-gnueabihf",
    "arm-unknown-linux-musleabi",
    "arm-unknown-linux-musleabihf",
    "armv7-apple-ios",
    "armv7-linux-androideabi",
    "armv7-unknown-linux-gnueabihf",
    "armv7-unknown-linux-musleabihf",
    "armv7s-apple-ios",
    "asmjs-unknown-emscripten",
    "i386-apple-ios",
    "i586-pc-windows-msvc",
    "i586-unknown-linux-gnu",
    "i686-apple-darwin",
    "i686-linux-android",
    "i686-pc-windows-gnu",
    "i686-pc-windows-msvc",
    "i686-unknown-freebsd",
    "i686-unknown-linux-gnu",
    "i686-unknown-linux-musl",
    "mips-unknown-linux-gnu",
    "mips-unknown-linux-musl",
    "mips64-unknown-linux-gnuabi64",
    "mips64el-unknown-linux-gnuabi64",
    "mipsel-unknown-linux-gnu",
    "mipsel-unknown-linux-musl",
    "powerpc-unknown-linux-gnu",
    "powerpc64-unknown-linux-gnu",
    "powerpc64le-unknown-linux-gnu",
    "s390x-unknown-linux-gnu",
    "wasm32-unknown-emscripten",
    "x86_64-apple-darwin",
    "x86_64-apple-ios",
    "x86_64-pc-windows-gnu",
    "x86_64-pc-windows-msvc",
    "x86_64-rumprun-netbsd",
    "x86_64-unknown-freebsd",
    "x86_64-unknown-linux-gnu",
    "x86_64-unknown-linux-musl",
    "x86_64-unknown-netbsd",
];

static MINGW: &'static [&'static str] = &[
    "i686-pc-windows-gnu",
    "x86_64-pc-windows-gnu",
];

struct Manifest {
    manifest_version: String,
    date: String,
    pkg: HashMap<String, Package>,
}

#[derive(RustcEncodable)]
struct Package {
    version: String,
    target: HashMap<String, Target>,
}

#[derive(RustcEncodable)]
struct Target {
    available: bool,
    url: Option<String>,
    hash: Option<String>,
    components: Option<Vec<Component>>,
    extensions: Option<Vec<Component>>,
}

#[derive(RustcEncodable)]
struct Component {
    pkg: String,
    target: String,
}

macro_rules! t {
    ($e:expr) => (match $e {
        Ok(e) => e,
        Err(e) => panic!("{} failed with {}", stringify!($e), e),
    })
}

struct Builder {
    channel: String,
    input: PathBuf,
    output: PathBuf,
    gpg_passphrase: String,
    digests: HashMap<String, String>,
    s3_address: String,
    date: String,
    rust_version: String,
    cargo_version: String,
}

fn main() {
    let mut args = env::args().skip(1);
    let input = PathBuf::from(args.next().unwrap());
    let output = PathBuf::from(args.next().unwrap());
    let date = args.next().unwrap();
    let channel = args.next().unwrap();
    let s3_address = args.next().unwrap();
    let mut passphrase = String::new();
    t!(io::stdin().read_to_string(&mut passphrase));

    Builder {
        channel: channel,
        input: input,
        output: output,
        gpg_passphrase: passphrase,
        digests: HashMap::new(),
        s3_address: s3_address,
        date: date,
        rust_version: String::new(),
        cargo_version: String::new(),
    }.build();
}

impl Builder {
    fn build(&mut self) {
        self.rust_version = self.version("rust", "x86_64-unknown-linux-gnu");
        self.cargo_version = self.version("cargo", "x86_64-unknown-linux-gnu");

        self.digest_and_sign();
        let Manifest { manifest_version, date, pkg } = self.build_manifest();

        // Unfortunately we can't use derive(RustcEncodable) here because the
        // version field is called `manifest-version`, not `manifest_version`.
        // In lieu of that just create the table directly here with a `BTreeMap`
        // and wrap it up in a `Value::Table`.
        let mut manifest = BTreeMap::new();
        manifest.insert("manifest-version".to_string(),
                        toml::encode(&manifest_version));
        manifest.insert("date".to_string(), toml::encode(&date));
        manifest.insert("pkg".to_string(), toml::encode(&pkg));
        let manifest = toml::Value::Table(manifest).to_string();

        let filename = format!("channel-rust-{}.toml", self.channel);
        self.write_manifest(&manifest, &filename);

        if self.channel != "beta" && self.channel != "nightly" {
            self.write_manifest(&manifest, "channel-rust-stable.toml");
        }
    }

    fn digest_and_sign(&mut self) {
        for file in t!(self.input.read_dir()).map(|e| t!(e).path()) {
            let filename = file.file_name().unwrap().to_str().unwrap();
            let digest = self.hash(&file);
            self.sign(&file);
            assert!(self.digests.insert(filename.to_string(), digest).is_none());
        }
    }

    fn build_manifest(&mut self) -> Manifest {
        let mut manifest = Manifest {
            manifest_version: "2".to_string(),
            date: self.date.to_string(),
            pkg: HashMap::new(),
        };

        self.package("rustc", &mut manifest.pkg, HOSTS);
        self.package("cargo", &mut manifest.pkg, HOSTS);
        self.package("rust-mingw", &mut manifest.pkg, MINGW);
        self.package("rust-std", &mut manifest.pkg, TARGETS);
        self.package("rust-docs", &mut manifest.pkg, TARGETS);
        self.package("rust-src", &mut manifest.pkg, &["*"]);

        let mut pkg = Package {
            version: self.cached_version("rust").to_string(),
            target: HashMap::new(),
        };
        for host in HOSTS {
            let filename = self.filename("rust", host);
            let digest = match self.digests.remove(&filename) {
                Some(digest) => digest,
                None => {
                    pkg.target.insert(host.to_string(), Target {
                        available: false,
                        url: None,
                        hash: None,
                        components: None,
                        extensions: None,
                    });
                    continue
                }
            };
            let mut components = Vec::new();
            let mut extensions = Vec::new();

            // rustc/rust-std/cargo are all required, and so is rust-mingw if it's
            // available for the target.
            components.extend(vec![
                Component { pkg: "rustc".to_string(), target: host.to_string() },
                Component { pkg: "rust-std".to_string(), target: host.to_string() },
                Component { pkg: "cargo".to_string(), target: host.to_string() },
            ]);
            if host.contains("pc-windows-gnu") {
                components.push(Component {
                    pkg: "rust-mingw".to_string(),
                    target: host.to_string(),
                });
            }

            // Docs, other standard libraries, and the source package are all
            // optional.
            extensions.push(Component {
                pkg: "rust-docs".to_string(),
                target: host.to_string(),
            });
            for target in TARGETS {
                if target != host {
                    extensions.push(Component {
                        pkg: "rust-std".to_string(),
                        target: target.to_string(),
                    });
                }
            }
            extensions.push(Component {
                pkg: "rust-src".to_string(),
                target: "*".to_string(),
            });

            pkg.target.insert(host.to_string(), Target {
                available: true,
                url: Some(self.url("rust", host)),
                hash: Some(to_hex(digest.as_ref())),
                components: Some(components),
                extensions: Some(extensions),
            });
        }
        manifest.pkg.insert("rust".to_string(), pkg);

        return manifest
    }

    fn package(&mut self,
               pkgname: &str,
               dst: &mut HashMap<String, Package>,
               targets: &[&str]) {
        let targets = targets.iter().map(|name| {
            let filename = self.filename(pkgname, name);
            let digest = match self.digests.remove(&filename) {
                Some(digest) => digest,
                None => {
                    return (name.to_string(), Target {
                        available: false,
                        url: None,
                        hash: None,
                        components: None,
                        extensions: None,
                    })
                }
            };

            (name.to_string(), Target {
                available: true,
                url: Some(self.url(pkgname, name)),
                hash: Some(digest),
                components: None,
                extensions: None,
            })
        }).collect();

        dst.insert(pkgname.to_string(), Package {
            version: self.cached_version(pkgname).to_string(),
            target: targets,
        });
    }

    fn url(&self, component: &str, target: &str) -> String {
        format!("{}/{}/{}",
                self.s3_address,
                self.date,
                self.filename(component, target))
    }

    fn filename(&self, component: &str, target: &str) -> String {
        if component == "rust-src" {
            format!("rust-src-{}.tar.gz", self.channel)
        } else {
            format!("{}-{}-{}.tar.gz", component, self.channel, target)
        }
    }

    fn cached_version(&self, component: &str) -> &str {
        if component == "cargo" {
            &self.cargo_version
        } else {
            &self.rust_version
        }
    }

    fn version(&self, component: &str, target: &str) -> String {
        let mut cmd = Command::new("tar");
        let filename = self.filename(component, target);
        cmd.arg("xf")
           .arg(self.input.join(&filename))
           .arg(format!("{}/version", filename.replace(".tar.gz", "")))
           .arg("-O");
        let version = t!(cmd.output());
        if !version.status.success() {
            panic!("failed to learn version:\n\n{:?}\n\n{}\n\n{}",
                   cmd,
                   String::from_utf8_lossy(&version.stdout),
                   String::from_utf8_lossy(&version.stderr));
        }
        String::from_utf8_lossy(&version.stdout).trim().to_string()
    }

    fn hash(&self, path: &Path) -> String {
        let sha = t!(Command::new("shasum")
                        .arg("-a").arg("256")
                        .arg(path)
                        .output());
        assert!(sha.status.success());

        let filename = path.file_name().unwrap().to_str().unwrap();
        let sha256 = self.output.join(format!("{}.sha256", filename));
        t!(t!(File::create(&sha256)).write_all(&sha.stdout));

        let stdout = String::from_utf8_lossy(&sha.stdout);
        stdout.split_whitespace().next().unwrap().to_string()
    }

    fn sign(&self, path: &Path) {
        let filename = path.file_name().unwrap().to_str().unwrap();
        let asc = self.output.join(format!("{}.asc", filename));
        println!("signing: {:?}", path);
        let mut cmd = Command::new("gpg");
        cmd.arg("--no-tty")
            .arg("--yes")
            .arg("--passphrase-fd").arg("0")
            .arg("--armor")
            .arg("--output").arg(&asc)
            .arg("--detach-sign").arg(path)
            .stdin(Stdio::piped());
        let mut child = t!(cmd.spawn());
        t!(child.stdin.take().unwrap().write_all(self.gpg_passphrase.as_bytes()));
        assert!(t!(child.wait()).success());
    }

    fn write_manifest(&self, manifest: &str, name: &str) {
        let dst = self.output.join(name);
        t!(t!(File::create(&dst)).write_all(manifest.as_bytes()));
        self.hash(&dst);
        self.sign(&dst);
    }
}

fn to_hex(digest: &[u8]) -> String {
    let mut ret = String::new();
    for byte in digest {
        ret.push(hex((byte & 0xf0) >> 4));
        ret.push(hex(byte & 0xf));
    }
    return ret;

    fn hex(b: u8) -> char {
        match b {
            0...9 => (b'0' + b) as char,
            _ => (b'a' + b - 10) as char,
        }
    }
}
<commit_msg>Rollup merge of #39599 - alexcrichton:cargo-tarball-name, r=brson<commit_after>// Copyright 2017 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.

extern crate toml;
extern crate rustc_serialize;

use std::collections::{BTreeMap, HashMap};
use std::env;
use std::fs::File;
use std::io::{self, Read, Write};
use std::path::{PathBuf, Path};
use std::process::{Command, Stdio};

static HOSTS: &'static [&'static str] = &[
    "aarch64-unknown-linux-gnu",
    "arm-unknown-linux-gnueabi",
    "arm-unknown-linux-gnueabihf",
    "armv7-unknown-linux-gnueabihf",
    "i686-apple-darwin",
    "i686-pc-windows-gnu",
    "i686-pc-windows-msvc",
    "i686-unknown-linux-gnu",
    "mips-unknown-linux-gnu",
    "mips64-unknown-linux-gnuabi64",
    "mips64el-unknown-linux-gnuabi64",
    "mipsel-unknown-linux-gnu",
    "powerpc-unknown-linux-gnu",
    "powerpc64-unknown-linux-gnu",
    "powerpc64le-unknown-linux-gnu",
    "s390x-unknown-linux-gnu",
    "x86_64-apple-darwin",
    "x86_64-pc-windows-gnu",
    "x86_64-pc-windows-msvc",
    "x86_64-unknown-freebsd",
    "x86_64-unknown-linux-gnu",
    "x86_64-unknown-netbsd",
];

static TARGETS: &'static [&'static str] = &[
    "aarch64-apple-ios",
    "aarch64-linux-android",
    "aarch64-unknown-linux-gnu",
    "arm-linux-androideabi",
    "arm-unknown-linux-gnueabi",
    "arm-unknown-linux-gnueabihf",
    "arm-unknown-linux-musleabi",
    "arm-unknown-linux-musleabihf",
    "armv7-apple-ios",
    "armv7-linux-androideabi",
    "armv7-unknown-linux-gnueabihf",
    "armv7-unknown-linux-musleabihf",
    "armv7s-apple-ios",
    "asmjs-unknown-emscripten",
    "i386-apple-ios",
    "i586-pc-windows-msvc",
    "i586-unknown-linux-gnu",
    "i686-apple-darwin",
    "i686-linux-android",
    "i686-pc-windows-gnu",
    "i686-pc-windows-msvc",
    "i686-unknown-freebsd",
    "i686-unknown-linux-gnu",
    "i686-unknown-linux-musl",
    "mips-unknown-linux-gnu",
    "mips-unknown-linux-musl",
    "mips64-unknown-linux-gnuabi64",
    "mips64el-unknown-linux-gnuabi64",
    "mipsel-unknown-linux-gnu",
    "mipsel-unknown-linux-musl",
    "powerpc-unknown-linux-gnu",
    "powerpc64-unknown-linux-gnu",
    "powerpc64le-unknown-linux-gnu",
    "s390x-unknown-linux-gnu",
    "wasm32-unknown-emscripten",
    "x86_64-apple-darwin",
    "x86_64-apple-ios",
    "x86_64-pc-windows-gnu",
    "x86_64-pc-windows-msvc",
    "x86_64-rumprun-netbsd",
    "x86_64-unknown-freebsd",
    "x86_64-unknown-linux-gnu",
    "x86_64-unknown-linux-musl",
    "x86_64-unknown-netbsd",
];

static MINGW: &'static [&'static str] = &[
    "i686-pc-windows-gnu",
    "x86_64-pc-windows-gnu",
];

struct Manifest {
    manifest_version: String,
    date: String,
    pkg: HashMap<String, Package>,
}

#[derive(RustcEncodable)]
struct Package {
    version: String,
    target: HashMap<String, Target>,
}

#[derive(RustcEncodable)]
struct Target {
    available: bool,
    url: Option<String>,
    hash: Option<String>,
    components: Option<Vec<Component>>,
    extensions: Option<Vec<Component>>,
}

#[derive(RustcEncodable)]
struct Component {
    pkg: String,
    target: String,
}

macro_rules! t {
    ($e:expr) => (match $e {
        Ok(e) => e,
        Err(e) => panic!("{} failed with {}", stringify!($e), e),
    })
}

struct Builder {
    channel: String,
    input: PathBuf,
    output: PathBuf,
    gpg_passphrase: String,
    digests: HashMap<String, String>,
    s3_address: String,
    date: String,
    rust_version: String,
    cargo_version: String,
}

fn main() {
    let mut args = env::args().skip(1);
    let input = PathBuf::from(args.next().unwrap());
    let output = PathBuf::from(args.next().unwrap());
    let date = args.next().unwrap();
    let channel = args.next().unwrap();
    let s3_address = args.next().unwrap();
    let mut passphrase = String::new();
    t!(io::stdin().read_to_string(&mut passphrase));

    Builder {
        channel: channel,
        input: input,
        output: output,
        gpg_passphrase: passphrase,
        digests: HashMap::new(),
        s3_address: s3_address,
        date: date,
        rust_version: String::new(),
        cargo_version: String::new(),
    }.build();
}

impl Builder {
    fn build(&mut self) {
        self.rust_version = self.version("rust", "x86_64-unknown-linux-gnu");
        self.cargo_version = self.version("cargo", "x86_64-unknown-linux-gnu");

        self.digest_and_sign();
        let Manifest { manifest_version, date, pkg } = self.build_manifest();

        // Unfortunately we can't use derive(RustcEncodable) here because the
        // version field is called `manifest-version`, not `manifest_version`.
        // In lieu of that just create the table directly here with a `BTreeMap`
        // and wrap it up in a `Value::Table`.
        let mut manifest = BTreeMap::new();
        manifest.insert("manifest-version".to_string(),
                        toml::encode(&manifest_version));
        manifest.insert("date".to_string(), toml::encode(&date));
        manifest.insert("pkg".to_string(), toml::encode(&pkg));
        let manifest = toml::Value::Table(manifest).to_string();

        let filename = format!("channel-rust-{}.toml", self.channel);
        self.write_manifest(&manifest, &filename);

        if self.channel != "beta" && self.channel != "nightly" {
            self.write_manifest(&manifest, "channel-rust-stable.toml");
        }
    }

    fn digest_and_sign(&mut self) {
        for file in t!(self.input.read_dir()).map(|e| t!(e).path()) {
            let filename = file.file_name().unwrap().to_str().unwrap();
            let digest = self.hash(&file);
            self.sign(&file);
            assert!(self.digests.insert(filename.to_string(), digest).is_none());
        }
    }

    fn build_manifest(&mut self) -> Manifest {
        let mut manifest = Manifest {
            manifest_version: "2".to_string(),
            date: self.date.to_string(),
            pkg: HashMap::new(),
        };

        self.package("rustc", &mut manifest.pkg, HOSTS);
        self.package("cargo", &mut manifest.pkg, HOSTS);
        self.package("rust-mingw", &mut manifest.pkg, MINGW);
        self.package("rust-std", &mut manifest.pkg, TARGETS);
        self.package("rust-docs", &mut manifest.pkg, TARGETS);
        self.package("rust-src", &mut manifest.pkg, &["*"]);

        let mut pkg = Package {
            version: self.cached_version("rust").to_string(),
            target: HashMap::new(),
        };
        for host in HOSTS {
            let filename = self.filename("rust", host);
            let digest = match self.digests.remove(&filename) {
                Some(digest) => digest,
                None => {
                    pkg.target.insert(host.to_string(), Target {
                        available: false,
                        url: None,
                        hash: None,
                        components: None,
                        extensions: None,
                    });
                    continue
                }
            };
            let mut components = Vec::new();
            let mut extensions = Vec::new();

            // rustc/rust-std/cargo are all required, and so is rust-mingw if it's
            // available for the target.
            components.extend(vec![
                Component { pkg: "rustc".to_string(), target: host.to_string() },
                Component { pkg: "rust-std".to_string(), target: host.to_string() },
                Component { pkg: "cargo".to_string(), target: host.to_string() },
            ]);
            if host.contains("pc-windows-gnu") {
                components.push(Component {
                    pkg: "rust-mingw".to_string(),
                    target: host.to_string(),
                });
            }

            // Docs, other standard libraries, and the source package are all
            // optional.
            extensions.push(Component {
                pkg: "rust-docs".to_string(),
                target: host.to_string(),
            });
            for target in TARGETS {
                if target != host {
                    extensions.push(Component {
                        pkg: "rust-std".to_string(),
                        target: target.to_string(),
                    });
                }
            }
            extensions.push(Component {
                pkg: "rust-src".to_string(),
                target: "*".to_string(),
            });

            pkg.target.insert(host.to_string(), Target {
                available: true,
                url: Some(self.url("rust", host)),
                hash: Some(to_hex(digest.as_ref())),
                components: Some(components),
                extensions: Some(extensions),
            });
        }
        manifest.pkg.insert("rust".to_string(), pkg);

        return manifest
    }

    fn package(&mut self,
               pkgname: &str,
               dst: &mut HashMap<String, Package>,
               targets: &[&str]) {
        let targets = targets.iter().map(|name| {
            let filename = self.filename(pkgname, name);
            let digest = match self.digests.remove(&filename) {
                Some(digest) => digest,
                None => {
                    return (name.to_string(), Target {
                        available: false,
                        url: None,
                        hash: None,
                        components: None,
                        extensions: None,
                    })
                }
            };

            (name.to_string(), Target {
                available: true,
                url: Some(self.url(pkgname, name)),
                hash: Some(digest),
                components: None,
                extensions: None,
            })
        }).collect();

        dst.insert(pkgname.to_string(), Package {
            version: self.cached_version(pkgname).to_string(),
            target: targets,
        });
    }

    fn url(&self, component: &str, target: &str) -> String {
        format!("{}/{}/{}",
                self.s3_address,
                self.date,
                self.filename(component, target))
    }

    fn filename(&self, component: &str, target: &str) -> String {
        if component == "rust-src" {
            format!("rust-src-{}.tar.gz", self.channel)
        } else if component == "cargo" {
            format!("cargo-nightly-{}.tar.gz", target)
        } else {
            format!("{}-{}-{}.tar.gz", component, self.channel, target)
        }
    }

    fn cached_version(&self, component: &str) -> &str {
        if component == "cargo" {
            &self.cargo_version
        } else {
            &self.rust_version
        }
    }

    fn version(&self, component: &str, target: &str) -> String {
        let mut cmd = Command::new("tar");
        let filename = self.filename(component, target);
        cmd.arg("xf")
           .arg(self.input.join(&filename))
           .arg(format!("{}/version", filename.replace(".tar.gz", "")))
           .arg("-O");
        let version = t!(cmd.output());
        if !version.status.success() {
            panic!("failed to learn version:\n\n{:?}\n\n{}\n\n{}",
                   cmd,
                   String::from_utf8_lossy(&version.stdout),
                   String::from_utf8_lossy(&version.stderr));
        }
        String::from_utf8_lossy(&version.stdout).trim().to_string()
    }

    fn hash(&self, path: &Path) -> String {
        let sha = t!(Command::new("shasum")
                        .arg("-a").arg("256")
                        .arg(path)
                        .output());
        assert!(sha.status.success());

        let filename = path.file_name().unwrap().to_str().unwrap();
        let sha256 = self.output.join(format!("{}.sha256", filename));
        t!(t!(File::create(&sha256)).write_all(&sha.stdout));

        let stdout = String::from_utf8_lossy(&sha.stdout);
        stdout.split_whitespace().next().unwrap().to_string()
    }

    fn sign(&self, path: &Path) {
        let filename = path.file_name().unwrap().to_str().unwrap();
        let asc = self.output.join(format!("{}.asc", filename));
        println!("signing: {:?}", path);
        let mut cmd = Command::new("gpg");
        cmd.arg("--no-tty")
            .arg("--yes")
            .arg("--passphrase-fd").arg("0")
            .arg("--armor")
            .arg("--output").arg(&asc)
            .arg("--detach-sign").arg(path)
            .stdin(Stdio::piped());
        let mut child = t!(cmd.spawn());
        t!(child.stdin.take().unwrap().write_all(self.gpg_passphrase.as_bytes()));
        assert!(t!(child.wait()).success());
    }

    fn write_manifest(&self, manifest: &str, name: &str) {
        let dst = self.output.join(name);
        t!(t!(File::create(&dst)).write_all(manifest.as_bytes()));
        self.hash(&dst);
        self.sign(&dst);
    }
}

fn to_hex(digest: &[u8]) -> String {
    let mut ret = String::new();
    for byte in digest {
        ret.push(hex((byte & 0xf0) >> 4));
        ret.push(hex(byte & 0xf));
    }
    return ret;

    fn hex(b: u8) -> char {
        match b {
            0...9 => (b'0' + b) as char,
            _ => (b'a' + b - 10) as char,
        }
    }
}
<|endoftext|>


<commit_before><commit_msg>Missing file in #f79fb8ef<commit_after>// Copyright 2016 The Grin Developers
//
// Licensed under the Apache License, Version 2.0 (the "License");
// you may not use this file except in compliance with the License.
// You may obtain a copy of the License at
//
//     http://www.apache.org/licenses/LICENSE-2.0
//
// Unless required by applicable law or agreed to in writing, software
// distributed under the License is distributed on an "AS IS" BASIS,
// WITHOUT WARRANTIES OR CONDITIONS OF ANY KIND, either express or implied.
// See the License for the specific language governing permissions and
// limitations under the License.

//! Utilities to check the status of all the outputs we have stored in
//! the wallet storage and update them.

use api;
use core::core::{Output, DEFAULT_OUTPUT, COINBASE_OUTPUT};
use core::core::hash::Hashed;
use secp::{self, pedersen};
use util;

use extkey::ExtendedKey;
use types::{WalletConfig, OutputStatus, WalletData};

pub fn refresh_outputs(config: &WalletConfig, ext_key: &ExtendedKey) {
	println!("REFRESH");
	let secp = secp::Secp256k1::with_caps(secp::ContextFlag::Commit);
	let mut wallet_data = WalletData::read_or_create().expect("Could not open wallet data.");

	let mut changed = 0;
	for out in &mut wallet_data.outputs {
		println!("check out {}", out.n_child);
		if out.status != OutputStatus::Spent {
			let key = ext_key.derive(&secp, out.n_child).unwrap();
			let commitment = secp.commit(out.value, key.key).unwrap();

			// TODO check the pool for unconfirmed
			let out_res = get_output_by_commitment(config, commitment);
			if out_res.is_ok() {
				println!("ok");
				out.status = OutputStatus::Unspent;
				changed += 1;
			}
		}
	}
	if changed > 0 {
		wallet_data.write().unwrap();
	}
}

fn get_output_by_commitment(config: &WalletConfig,
                            commit: pedersen::Commitment)
                            -> Result<Output, api::Error> {
	let url = format!("{}/v1/chain/output/{}",
	                  config.api_http_addr,
	                  util::to_hex(commit.as_ref().to_vec()));
	api::client::get::<Output>(url.as_str())
}
<|endoftext|>


<commit_before><commit_msg>Simplify relative submodule handling<commit_after><|endoftext|>


<commit_before><commit_msg>Daemonize hyper via janky hack.<commit_after><|endoftext|>


<commit_before><commit_msg>rustfmt.<commit_after><|endoftext|>


<commit_before><commit_msg>Actually propagate the error from the pipeline.<commit_after><|endoftext|>


<commit_before><commit_msg>Add rust code for custom-set case<commit_after>use std::collections::HashSet;
use std::hash::Hash;

#[derive(Debug, PartialEq)]
pub struct CustomSet<T: Hash + Eq> {
    store: HashSet<T>,
}

impl<T> CustomSet<T>
where
    T: Hash + Eq,
{
    pub fn new(list: Vec<T>) -> CustomSet<T>
    where
        T: Clone,
    {
        CustomSet {
            store: list.iter().cloned().collect(),
        }
    }

    pub fn is_empty(&self) -> bool {
        let h = &self.store;
        h.is_empty()
    }

    pub fn contains(&self, v: &T) -> bool {
        let h = &self.store;
        h.contains(v)
    }

    pub fn add(&mut self, v: T) -> bool {
        let h = &mut self.store;
        h.insert(v)
    }

    pub fn is_subset(&self, set: &CustomSet<T>) -> bool {
        let h = &self.store;
        h.is_subset(&set.store)
    }

    pub fn is_disjoint(&self, set: &CustomSet<T>) -> bool {
        let h = &self.store;
        h.is_disjoint(&set.store)
    }

    pub fn intersection(&self, set: &CustomSet<T>) -> CustomSet<T>
    where
        T: Clone,
    {
        let h = &self.store;
        CustomSet {
            store: h.intersection(&set.store).cloned().collect(),
        }
    }

    pub fn difference(&self, set: &CustomSet<T>) -> CustomSet<T>
    where
        T: Clone,
    {
        let h = &self.store;
        CustomSet {
            store: h.difference(&set.store).cloned().collect(),
        }
    }

    pub fn union(&self, set: &CustomSet<T>) -> CustomSet<T>
    where
        T: Clone,
    {
        let h = &self.store;
        CustomSet {
            store: h.union(&set.store).cloned().collect(),
        }
    }
}
<|endoftext|>


<commit_before><commit_msg>Add a test for `--sort-modules-by-appearance`<commit_after>// Copyright 2017 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.

// Tests the rustdoc --sort-modules-by-appearance option, that allows module declarations to appear
// in the order they are declared in the source code, rather than only alphabetically.

// compile-flags: -Z unstable-options --sort-modules-by-appearance

pub mod module_b {}

pub mod module_c {}

pub mod module_a {}

// @matches 'sort_modules_by_appearance/index.html' '(?s)module_b.*module_c.*module_a'
// @matches 'sort_modules_by_appearance/sidebar-items.js' '"module_b".*"module_c".*"module_a"'
<|endoftext|>


<commit_before><commit_msg>re-enable ignoring commands in history<commit_after><|endoftext|>


<commit_before><commit_msg>Test that bounds checks are elided for indexing after .[r]position()<commit_after>// min-llvm-version: 11.0.0
// compile-flags: -O
// ignore-debug: the debug assertions get in the way
#![crate_type = "lib"]

// Make sure no bounds checks are emitted when slicing or indexing
// with an index from `position()` or `rposition()`.

// CHECK-LABEL: @position_slice_to_no_bounds_check
#[no_mangle]
pub fn position_slice_to_no_bounds_check(s: &[u8]) -> &[u8] {
    // CHECK-NOT: panic
    // CHECK-NOT: slice_index_len_fail
    if let Some(idx) = s.iter().position(|b| *b == b'\\') {
        &s[..idx]
    } else {
        s
    }
}

// CHECK-LABEL: @position_slice_from_no_bounds_check
#[no_mangle]
pub fn position_slice_from_no_bounds_check(s: &[u8]) -> &[u8] {
    // CHECK-NOT: panic
    // CHECK-NOT: slice_index_len_fail
    if let Some(idx) = s.iter().position(|b| *b == b'\\') {
        &s[idx..]
    } else {
        s
    }
}

// CHECK-LABEL: @position_index_no_bounds_check
#[no_mangle]
pub fn position_index_no_bounds_check(s: &[u8]) -> u8 {
    // CHECK-NOT: panic
    // CHECK-NOT: slice_index_len_fail
    if let Some(idx) = s.iter().position(|b| *b == b'\\') {
        s[idx]
    } else {
        42
    }
}
// CHECK-LABEL: @rposition_slice_to_no_bounds_check
#[no_mangle]
pub fn rposition_slice_to_no_bounds_check(s: &[u8]) -> &[u8] {
    // CHECK-NOT: panic
    // CHECK-NOT: slice_index_len_fail
    if let Some(idx) = s.iter().rposition(|b| *b == b'\\') {
        &s[..idx]
    } else {
        s
    }
}

// CHECK-LABEL: @rposition_slice_from_no_bounds_check
#[no_mangle]
pub fn rposition_slice_from_no_bounds_check(s: &[u8]) -> &[u8] {
    // CHECK-NOT: panic
    // CHECK-NOT: slice_index_len_fail
    if let Some(idx) = s.iter().rposition(|b| *b == b'\\') {
        &s[idx..]
    } else {
        s
    }
}

// CHECK-LABEL: @rposition_index_no_bounds_check
#[no_mangle]
pub fn rposition_index_no_bounds_check(s: &[u8]) -> u8 {
    // CHECK-NOT: panic
    // CHECK-NOT: slice_index_len_fail
    if let Some(idx) = s.iter().rposition(|b| *b == b'\\') {
        s[idx]
    } else {
        42
    }
}
<|endoftext|>


<commit_before><commit_msg>Add porblem #84<commit_after>use std::sort::{ quick_sort };

use common::problem::{ Problem };

pub static problem: Problem<'static> = Problem {
    id: 84,
    answer: "101524",
    solver: solve
};

pub enum Square {
    GO, A1, CC1, A2, T1, R1, B1, CH1, B2, B3, JAIL,
    C1, U1, C2, C3, R2, D1, CC2, D2, D3, FP,
    E1, CH2, E2, E3, R3, F1, F2, U2, F3, G2J,
    G1, G2, CC3, G3, R4, CH3, H1, T2, H2,
    NumSquare
}

impl Square {
    #[inline(always)]
    fn to_uint(&self) -> uint { *self as uint }

    #[inline(always)]
    fn from_uint(n: uint) -> Square {
        match n {
            0 => GO, 1 => A1, 2 => CC1, 3 => A2, 4 => T1,
            5 => R1, 6 => B1, 7 => CH1, 8 => B2, 9 => B3,
            10 => JAIL, 11 => C1, 12 => U1, 13 => C2, 14 => C3,
            15 => R2, 16 => D1, 17 => CC2, 18 => D2, 19 => D3,
            20 => FP, 21 => E1, 22 => CH2, 23 => E2, 24 => E3,
            25 => R3, 26 => F1, 27 => F2, 28 => U2, 29 => F3,
            30 => G2J, 31 => G1, 32 => G2, 33 => CC3, 34 => G3,
            35 => R4, 36 => CH3, 37 => H1, 38 => T2, 39 => H2,
            _ => fail!()
        }
    }
}

static num_square: uint = NumSquare as uint;

#[inline(always)]
fn create_mat<T>(h: uint, w: uint, f: &fn(uint, uint) -> T) -> ~[~[T]] {
    do vec::from_fn(h) |i| { do vec::from_fn(w) |j| { f(i, j) } }
}

#[inline(always)]
pub fn mul_mat(m1: &[~[float]], m2: &[~[float]]) -> ~[~[float]] {
    do create_mat(m1.len(), m2[0].len()) |i, j| {
        let mut sum = 0f;
        for uint::range(0, m1[0].len()) |k| {
            sum += m1[i][k] * m2[k][j];
        }
        sum
    }
}

#[inline(always)]
pub fn add_mat(m1: &[~[float]], m2: &[~[float]]) -> ~[~[float]] {
    do create_mat(m1.len(), m1[0].len()) |i, j| { m1[i][j] + m2[i][j] }
}

#[inline(always)]
pub fn sub_mat(m1: &[~[float]], m2: &[~[float]]) -> ~[~[float]] {
    do create_mat(m1.len(), m1[0].len()) |i, j| { m1[i][j] - m2[i][j] }
}

#[inline(always)]
pub fn trans_mat(m: &[~[float]]) -> ~[~[float]] {
    do create_mat(m[0].len(), m.len()) |i, j| { m[j][i] }
}

fn print_mat(mat: &[~[float]]) {
    print("     ");
    for uint::range(0, mat[0].len()) |j| {
        print(fmt!(" %-4? ", Square::from_uint(j % num_square)));
    }
    println("");
    for uint::range(0, mat.len()) |i| {
        let mut sum = 0f;
        print(fmt!("%-4? ", Square::from_uint(i % num_square)));
        for uint::range(0, mat[i].len()) |j| {
            if mat[i][j] == 0f {
                print("0     ");
            } else{
                print(fmt!("%.3f ", mat[i][j]));
            }
            sum += mat[i][j];
        }
        println(fmt!("| %.3f", sum));
    }

    print("     ");
    for mat[0].len().times {
        print("----- ")
    }
    println("");
    print("     ");
    for uint::range(0, mat[0].len()) |j| {
        let mut sum = 0f;
        for uint::range(0, mat.len()) |i| {
            sum += mat[i][j];
        }
        print(fmt!("%.3f ", sum));
    }
    println("");
}

fn create_roll_map(dice_side: uint) -> ~[(float, float)] {
    let mut map = vec::from_elem(dice_side * 2 + 1, (0, 0));
    for uint::range(1, dice_side + 1) |i| {
        for uint::range(i, dice_side + 1) |j| {
            let sum = i + j;
            let (p, q) = map[sum];
            map[sum] = if i == j { (p, q + 1) } else { (p + 2, q) };
        }
    }

    let cases = dice_side * dice_side;
    return do map.map |&(p, q)| {
        ((p as float) / (cases as float), (q as float) / (cases as float))
    };
}

fn get_trans(roll_map: &[(float, float)]) -> ~[~[float]] {
    let trans_singles = do create_mat(num_square, num_square) |dst, src| {
        let diff = (dst + num_square - src) % num_square;
        if diff < roll_map.len() { roll_map[diff].first() } else { 0f }
    };
    let trans_doubles = do create_mat(num_square, num_square) |dst, src| {
        let diff = (dst + num_square - src) % num_square;
        if diff < roll_map.len() { roll_map[diff].second() } else { 0f }
    };

    let trans_cc = do create_mat(num_square, num_square) |dst, src| {
        match Square::from_uint(src) {
            CC1 | CC2 | CC3 => {
                match Square::from_uint(dst) {
                    GO   => 1f / 16f,
                    JAIL => 1f / 16f,
                    _ => if src == dst { 14f / 16f } else { 0f }
                }
            },
            _ => { if src == dst { 1f } else { 0f } }
        }
    };

    let trans_ch = do create_mat(num_square, num_square) |dst, src| {
        match Square::from_uint(src) {
            ch @ CH1 | ch @ CH2 | ch @ CH3 => {
                match Square::from_uint(dst) {
                    GO => 1f / 16f,
                    JAIL => 1f / 16f,
                    // Go back 3 square
                    CC3 => match ch { CH3 => 1f / 16f, _ => 0f},
                    // Go back 3 square
                    D2 => match ch { CH2 => 1f / 16f, _ => 0f },
                    // Go back 3 square
                    T1 => match ch { CH1 => 1f / 16f, _ => 0f },
                    C1 => 1f / 16f,
                    E3 => 1f / 16f,
                    H2 => 1f / 16f,
                    // Go to next R + Go to R1
                    R1 => match ch { CH3 => 3f / 16f, _ => 1f / 16f },
                    // Go to next R only
                    R2 => match ch { CH1 => 2f / 16f, _ => 0f },
                    // Go to next R only
                    R3 => match ch { CH2 => 2f / 16f, _ => 0f },
                    // Go to next U
                    U1 => match ch { CH1 | CH3 => 1f / 16f, _ => 0f },
                    // Go to next U
                    U2 => match ch { CH2 => 1f / 16f, _ => 0f },
                    _  if dst == src => 6f / 16f,
                    _  => 0f
                }
            },
            _ => { if dst == src { 1f } else { 0f } }
        }
    };

    let trans_g2j = do create_mat(num_square, num_square) |dst, src| {
        match Square::from_uint(src) {
            G2J => match Square::from_uint(dst) { JAIL => 1f, _ => 0f },
            _ => { if src == dst { 1f } else { 0f } }
        }
    };

    let trans_all_g2j = do create_mat(num_square, num_square) |dst, _src| {
        match Square::from_uint(dst) { JAIL => 1f, _ => 0f }
    };

    let trans_square = mul_mat(mul_mat(trans_cc, trans_ch), trans_g2j);
    let trans_sq_singles  = mul_mat(trans_square, trans_singles);
    let trans_sq_doubles  = mul_mat(trans_square, trans_doubles);
    let trans_g2j_doubles = mul_mat(trans_all_g2j, trans_doubles);

    return do create_mat(num_square * 3, num_square * 3) |i, j| {
        let dst_block = i / num_square;
        let dst = i % num_square;
        let src_block = j / num_square;
        let src = j % num_square;

        match (dst_block, src_block) {
            (0, 0) => trans_sq_singles[dst][src],
            (1, 0) => trans_sq_doubles[dst][src],
            (0, 1) => trans_sq_singles[dst][src],
            (2, 1) => trans_sq_doubles[dst][src],
            (0, 2) => trans_sq_singles[dst][src] + trans_g2j_doubles[dst][src],
            _      => 0f
        }
    }

    // return add_mat(
    //     mul_mat(trans_square, trans_singles),
    //     mul_mat(
    //         add_mat(
    //             mul_mat(trans_square, trans_singles),
    //             mul_mat(
    //                 add_mat(
    //                     mul_mat(trans_square, trans_singles),
    //                     mul_mat(trans_all_g2j, trans_doubles)),
    //                 mul_mat(trans_square, trans_doubles))),
    //         mul_mat(trans_square, trans_doubles)));
}

fn solve() -> ~str {
    let trans = get_trans(create_roll_map(4));
    let mut vec = do create_mat(trans.len(), 1) |i, _j| {
        if i == 0 { 1f } else { 0f }
    };

    loop {
        let vec2 = mul_mat(trans, vec);
        let sub = sub_mat(vec2, vec);
        let err = mul_mat(trans_mat(sub), sub);
        if err[0][0] < 1e-10 { break; }
        vec = vec2;
    }

    let mut pairs = do vec::from_fn(num_square) |i| {
        (0f, Square::from_uint(i))
    };
    for vec.eachi |i, vs| {
        let dst = i % num_square;
        let (p, sq) = pairs[dst];
        pairs[dst] = (p + vs[0], sq);
    }
    quick_sort(pairs, |&(p1, _), &(p2, _)| p1 >= p2);
    return str::concat(
        do pairs.slice(0, 3).map |&(_, sq)| { fmt!("%02u", sq.to_uint()) }
    );
}

#[cfg(test)]
mod test {
    use core::num::{ IntConvertible };
    use super::*;

    #[test]
    fn test_int_convertible_square() {
        for uint::range(0, 40) |n| {
            let sq = Square::from_uint(n);
            assert_eq!(sq.to_uint(), n);
        }
    }

    #[test]
    fn test_mul_mat() {
        assert_eq!(mul_mat(~[~[1f, 2f, 3f]], ~[~[1f], ~[2f], ~[3f]]),
                   ~[~[14f]]);
        assert_eq!(mul_mat(~[~[1f, 2f, 3f], ~[4f, 5f, 6f], ~[7f, 8f, 9f]],
                           ~[~[1f], ~[0f], ~[0f]]),
                   ~[~[1f], ~[4f], ~[7f]]);
        assert_eq!(mul_mat(~[~[1f, 2f, 3f], ~[4f, 5f, 6f], ~[7f, 8f, 9f]],
                           ~[~[1f, 0f], ~[0f, 0f], ~[0f, 1f]]),
                   ~[~[1f, 3f], ~[4f, 6f], ~[7f, 9f]]);
    }
}
<|endoftext|>


<commit_before><commit_msg>Send errors back to the main thread.<commit_after><|endoftext|>


<commit_before><commit_msg>Run consumer in an async task so we can supervise it later.<commit_after><|endoftext|>


<commit_before><commit_msg>add usernotice handling to message_handler.rs<commit_after><|endoftext|>


<commit_before><commit_msg>test vector lerp<commit_after><|endoftext|>


<commit_before><commit_msg>fix(ClearTextPassword): clear the whole source slice in `from_slice`<commit_after><|endoftext|>


<commit_before><commit_msg>Combine drain with string to allocate and dealocate parts of str<commit_after>fn main() {
    let mut choco = "chocolate".to_string();

    println!("{}", choco.len());
    // this crap takes an ownership, how does it reallocate the buffer?
    // TODO: Know how? Perhaps it just repositions the symbols in current buffer?
    choco.drain(3..6).collect::<String>();
    println!("{}", choco.len());
}
<|endoftext|>


<commit_before><commit_msg>Updated library::handle_submissons()<commit_after><|endoftext|>


<commit_before><commit_msg>add test for adding a field<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.

// Test where we change a type definition by adding a field.  Fns with
// this type in their signature are recompiled, as are their callers.
// Fns with that type used only in their body are also recompiled, but
// their callers are not.

// revisions:rpass1 rpass2
// compile-flags: -Z query-dep-graph

#![feature(rustc_attrs)]
#![feature(stmt_expr_attributes)]
#![feature(static_in_const)]
#![allow(dead_code)]

// These are expected to require translation.
#![rustc_partition_translated(module="struct_point-point", cfg="rpass2")]
#![rustc_partition_translated(module="struct_point-fn_with_type_in_sig", cfg="rpass2")]
#![rustc_partition_translated(module="struct_point-call_fn_with_type_in_sig", cfg="rpass2")]
#![rustc_partition_translated(module="struct_point-fn_with_type_in_body", cfg="rpass2")]
#![rustc_partition_translated(module="struct_point-fn_make_struct", cfg="rpass2")]
#![rustc_partition_translated(module="struct_point-fn_read_field", cfg="rpass2")]
#![rustc_partition_translated(module="struct_point-fn_write_field", cfg="rpass2")]

#![rustc_partition_reused(module="struct_point-call_fn_with_type_in_body", cfg="rpass2")]

mod point {
    #[cfg(rpass1)]
    pub struct Point {
        pub x: f32,
        pub y: f32,
    }

    #[cfg(rpass2)]
    pub struct Point {
        pub x: f32,
        pub y: f32,
        pub z: f32,
    }

    impl Point {
        pub fn origin() -> Point {
            #[cfg(rpass1)]
            return Point { x: 0.0, y: 0.0 };

            #[cfg(rpass2)]
            return Point { x: 0.0, y: 0.0, z: 0.0 };
        }

        pub fn total(&self) -> f32 {
            #[cfg(rpass1)]
            return self.x + self.y;

            #[cfg(rpass2)]
            return self.x + self.y + self.z;
        }

        pub fn x(&self) -> f32 {
            self.x
        }
    }
}

/// A fn item that calls (public) methods on `Point` from the same impl which changed
mod fn_with_type_in_sig {
    use point::Point;

    #[rustc_dirty(label="TypeckItemBody", cfg="rpass2")]
    pub fn boop(p: Option<&Point>) -> f32 {
        p.map(|p| p.total()).unwrap_or(0.0)
    }
}

mod call_fn_with_type_in_sig {
    use fn_with_type_in_sig;

    #[rustc_dirty(label="TypeckItemBody", cfg="rpass2")]
    pub fn bip() -> f32 {
        fn_with_type_in_sig::boop(None)
    }
}

/// A fn item that calls (public) methods on `Point` from the same impl which changed
mod fn_with_type_in_body {
    use point::Point;

    #[rustc_dirty(label="TypeckItemBody", cfg="rpass2")]
    pub fn boop() -> f32 {
        Point::origin().total()
    }
}

/// A fn item that calls (public) methods on `Point` from the same impl which changed
mod call_fn_with_type_in_body {
    use fn_with_type_in_body;

    #[rustc_clean(label="TypeckItemBody", cfg="rpass2")]
    pub fn bip() -> f32 {
        fn_with_type_in_body::boop()
    }
}

/// A fn item that makes an instance of `Point` but does not invoke methods
mod fn_make_struct {
    use point::Point;

    #[rustc_dirty(label="TypeckItemBody", cfg="rpass2")]
    pub fn make_origin(p: Point) -> Point {
        Point { ..p }
    }
}

/// A fn item that reads fields from `Point` but does not invoke methods
mod fn_read_field {
    use point::Point;

    #[rustc_dirty(label="TypeckItemBody", cfg="rpass2")]
    pub fn get_x(p: Point) -> f32 {
        p.x
    }
}

/// A fn item that writes to a field of `Point` but does not invoke methods
mod fn_write_field {
    use point::Point;

    #[rustc_dirty(label="TypeckItemBody", cfg="rpass2")]
    pub fn inc_x(p: &mut Point) {
        p.x += 1.0;
    }
}

fn main() {
}
<|endoftext|>


<commit_before><commit_msg>Fix: macro call build_subcommand<commit_after><|endoftext|>


<commit_before><commit_msg>Adds solution to problem 1<commit_after>use std::iter::AdditiveIterator;

fn is_devisable_to_3_or_5(num: i32) -> bool {
    num % 3 == 0 || num % 5 == 0
}

fn main() {
    let result = range(0i32, 1000).filter(|&x| is_devisable_to_3_or_5(x)).sum();
    println!("Sum is: {}", result);
}
<|endoftext|>


<commit_before><commit_msg>Fix: Forward flags as well<commit_after><|endoftext|>


<commit_before><commit_msg>rename<commit_after><|endoftext|>


<commit_before><commit_msg>Add a C ABI compatible struct for use when performing<commit_after>static IFNAMSIZ: uint = 16;
static PADDINGSIZE: uint = 22;
static IFF_TAP: u16 = 2;
static IFF_NO_PI: u16 = 0x1000;

// An enum representing the possible results of trying to create a tap_ifreq struct
pub enum GetTapDescriptorResult {
    InterfaceNameTooLong,
    InterfaceNameTooShort
}

// A struct to pass to ioctl to create a new tap device. The only elements of the struct required are the name and
// flags
#[repr(C)]
pub struct tap_ifreq {
    ifr_name: [u8, ..IFNAMSIZ],
    ifr_flags: u16,
    ifr_padding: [u8, ..PADDINGSIZE]
}

impl tap_ifreq {
    pub fn new(interface: &str) -> Result<tap_ifreq, GetTapDescriptorResult> {
        match interface.len() {
            0 => Err(InterfaceNameTooShort),

            // We can only accept IFNAMSIZ - 1 bytes, as we need to null-terminate the name
            n @ _ if n >= IFNAMSIZ => Err(InterfaceNameTooLong),

            _ => {
                let mut ifreq = tap_ifreq {
                    ifr_name: [0, ..IFNAMSIZ],
                    ifr_flags: IFF_TAP | IFF_NO_PI,
                    ifr_padding: [0, ..PADDINGSIZE]
                };

                for (i, byte) in interface.bytes().enumerate() {
                    ifreq.ifr_name[i] = byte;
                }

                Ok(ifreq)
            }
        }
    }
}

#[cfg(test)]
mod test {
    use super::{tap_ifreq, InterfaceNameTooLong, InterfaceNameTooShort};

    #[test]
    fn new_bails_if_interface_len_zero() {
        assert!(match tap_ifreq::new("") {
            Err(InterfaceNameTooShort) => true,
            _ => false
        });
    }

    #[test]
    fn new_bails_if_interface_len_too_long() {
        assert!(match tap_ifreq::new("123456789ABCDEF0") {
            Err(InterfaceNameTooLong) => true,
            _ => false
        });
    }
}
<|endoftext|>


<commit_before><commit_msg>smooth watch<commit_after><|endoftext|>


<commit_before>use crossbeam;

use std::fs::{self, File};
use std::io::{Read, Write};
use std::path::{Path, PathBuf};
use std::collections::HashMap;
use std::ffi::OsStr;
use liquid::Value;
use walkdir::WalkDir;
use document::Document;
use error::{Error, Result};
use config::Config;
use yaml_rust::YamlLoader;
use chrono::{DateTime, UTC, FixedOffset};
use chrono::offset::TimeZone;
use rss::{Channel, Rss};

macro_rules! walker {
    ($dir:expr) => {
        WalkDir::new($dir)
            .into_iter()
            .filter_map(|e| e.ok())
            .filter(|f| {
                // skip directories
                f.file_type().is_file()
                    &&
                    // don't copy hidden files
                    !f.path()
                    .file_name()
                    .and_then(|name| name.to_str())
                    .unwrap_or(".")
                    .starts_with(".")
            })
    }
}

/// The primary build function that tranforms a directory into a site
pub fn build(config: &Config) -> Result<()> {
    trace!("Build configuration: {:?}", config);

    // join("") makes sure path has a trailing slash
    let source = PathBuf::from(&config.source).join("");
    let source = source.as_path();
    let dest = PathBuf::from(&config.dest).join("");
    let dest = dest.as_path();

    let template_extensions: Vec<&OsStr> = config.template_extensions
                                                 .iter()
                                                 .map(OsStr::new)
                                                 .collect();

    let layouts_path = source.join(&config.layouts);
    let posts_path = source.join(&config.posts);

    debug!("Layouts directory: {:?}", layouts_path);
    debug!("Posts directory: {:?}", posts_path);

    let layouts = try!(get_layouts(&layouts_path));

    let mut documents = vec![];

    for entry in walker!(&source) {
        if template_extensions.contains(&entry.path()
                                              .extension()
                                              .unwrap_or(OsStr::new(""))) &&
           entry.path().parent() != Some(layouts_path.as_path()) {
            let mut doc = try!(parse_document(&entry.path(), &source));

            // if the document is in the posts folder it's considered a post
            if entry.path().parent() == Some(posts_path.as_path()) {
                doc.is_post = true;
            }

            documents.push(doc);
        }
    }

    // January 1, 1970 0:00:00 UTC, the beginning of time
    let default_date = UTC.timestamp(0, 0).with_timezone(&FixedOffset::east(0));

    // sort documents by date, if there's no date (none was provided or it couldn't be read) then
    // fall back to the default date
    documents.sort_by(|a, b| {
        b.date.unwrap_or(default_date.clone()).cmp(&a.date.unwrap_or(default_date.clone()))
    });

    // check if we should create an RSS file and create it!
    if let &Some(ref path) = &config.rss {
        try!(create_rss(path, dest, &config, &documents));
    }

    // these are the attributes of all documents that are posts, so that they can be
    // passed to the renderer
    let post_data: Vec<Value> = documents.iter()
                                         .filter(|x| x.is_post)
                                         .map(|x| Value::Object(x.get_attributes()))
                                         .collect();

    // thread handles to join later
    let mut handles = vec![];

    // generate documents (in parallel)
    // TODO I'm probably underutilizing crossbeam
    crossbeam::scope(|scope| {
        for doc in &documents {
            trace!("Generating {}", doc.path);
            let post_data = post_data.clone();
            let layouts = layouts.clone();
            let handle = scope.spawn(move || doc.create_file(&dest, &layouts, &post_data));
            handles.push(handle);
        }
    });

    for handle in handles {
        try!(handle.join());
    }

    // copy all remaining files in the source to the destination
    if source != dest {
        info!("Copying remaining assets");
        let source_str = try!(source.to_str()
                                    .ok_or(format!("Cannot convert pathname {:?} to UTF-8",
                                                   source)));

        for entry in walker!(&source).filter(|f| {
            !template_extensions.contains(&f.path()
                                            .extension()
                                            .unwrap_or(OsStr::new(""))) &&
            f.path() != dest && f.path() != layouts_path.as_path()
        }) {
            let entry_path = try!(entry.path()
                                       .to_str()
                                       .ok_or(format!("Cannot convert pathname {:?} to UTF-8",
                                                      entry.path())));

            let relative = try!(entry_path.split(source_str)
                                          .last()
                                          .ok_or(format!("Empty path")));

            if try!(entry.metadata()).is_dir() {
                try!(fs::create_dir_all(&dest.join(relative)));
                debug!("Created new directory {:?}", dest.join(relative));
            } else {
                let parent_folder_path = dest.clone()
                                             .join(Path::new(relative).parent().unwrap());

                try!(fs::create_dir_all(&parent_folder_path));

                try!(fs::copy(entry.path(), &dest.join(relative))
                         .map_err(|_| format!("Could not copy {:?}", entry.path())));
                debug!("Copied {:?} to {:?}", entry.path(), dest.join(relative));
            }
        }
    }

    Ok(())
}

/// Gets all layout files from the specified path (usually _layouts/)
/// This walks the specified directory recursively
///
/// Returns a map filename -> content
fn get_layouts(layouts_path: &Path) -> Result<HashMap<String, String>> {
    let mut layouts = HashMap::new();

    // go through the layout directory and add
    // filename -> text content to the layout map
    for entry in walker!(layouts_path) {
        let mut text = String::new();
        let mut file = try!(File::open(entry.path()));
        try!(file.read_to_string(&mut text));

        let path = try!(entry.path()
                             .file_name()
                             .and_then(|name| name.to_str())
                             .ok_or(format!("Cannot convert pathname {:?} to UTF-8",
                                            entry.path().file_name())));

        layouts.insert(path.to_owned(), text);
    }

    Ok(layouts)
}

// creates a new RSS file with the contents of the site blog
fn create_rss(path: &str, dest: &Path, config: &Config, documents: &[Document]) -> Result<()> {
    match (&config.name, &config.description, &config.link) {
        // these three fields are mandatory in the RSS standard
        (&Some(ref name), &Some(ref description), &Some(ref link)) => {
            trace!("Generating RSS data");

            let items = documents.iter()
                                 .filter(|x| x.is_post)
                                 .map(|doc| doc.to_rss(link))
                                 .collect();

            let channel = Channel {
                title: name.to_owned(),
                link: link.to_owned(),
                description: description.to_owned(),
                items: items,
                ..Default::default()
            };

            let rss = Rss(channel);
            let rss_string = rss.to_string();
            trace!("RSS data: {}", rss_string);

            let rss_path = dest.join(path);

            let mut rss_file = try!(File::create(&rss_path));
            try!(rss_file.write_all(&rss_string.into_bytes()));

            info!("Created RSS file at {}", rss_path.display());
            Ok(())
        }
        _ => {
            Err(Error::from("name, description and link need to be defined in the config file to \
                             generate RSS"))
        }
    }
}

fn parse_document(path: &Path, source: &Path) -> Result<Document> {
    let mut attributes = HashMap::new();
    let mut content = try!(parse_file(path));

    // if there is front matter, split the file and parse it
    if content.contains("---") {
        let content2 = content.clone();
        let mut content_splits = content2.split("---");

        // above the split are the attributes
        let attribute_string = content_splits.next().unwrap_or("");

        // everything below the split becomes the new content
        content = content_splits.next().unwrap_or("").to_owned();

        let yaml_result = try!(YamlLoader::load_from_str(attribute_string));

        let yaml_attributes = try!(yaml_result[0]
                                       .as_hash()
                                       .ok_or(format!("Incorrect front matter format in {:?}",
                                                      path)));

        for (key, value) in yaml_attributes {
            // TODO is unwrap_or the best way to handle this?
            attributes.insert(key.as_str().unwrap_or("").to_owned(),
                              value.as_str().unwrap_or("").to_owned());
        }
    }

    let date = attributes.get("date")
                         .and_then(|d| DateTime::parse_from_str(d, "%d %B %Y %H:%M:%S %z").ok());

    let path_str = try!(path.to_str()
                            .ok_or(format!("Cannot convert pathname {:?} to UTF-8", path)));

    let source_str = try!(source.to_str()
                                .ok_or(format!("Cannot convert pathname {:?} to UTF-8", source)));

    let new_path = try!(path_str.split(source_str)
                                .last()
                                .ok_or(format!("Empty path")));

    // construct path
    let mut path_buf = PathBuf::from(new_path);
    path_buf.set_extension("html");

    let path_str = try!(path_buf.to_str()
                                .ok_or(format!("Cannot convert pathname {:?} to UTF-8", path_str)));

    let markdown = path.extension().unwrap_or(OsStr::new("")) == OsStr::new("md");

    let name = try!(path.file_stem()
                        .and_then(|stem| stem.to_str())
                        .ok_or(format!("Invalid UTF-8 in file stem for {:?}", path)));

    Ok(Document::new(name.to_owned(),
                     path_str.to_owned(),
                     attributes,
                     content,
                     false,
                     date,
                     markdown))
}

fn parse_file(path: &Path) -> Result<String> {
    let mut file = try!(File::open(path));
    let mut text = String::new();
    try!(file.read_to_string(&mut text));
    Ok(text)
}
<commit_msg>Improve dir creation during copy<commit_after>use crossbeam;

use std::fs::{self, File};
use std::io::{Read, Write};
use std::path::{Path, PathBuf};
use std::collections::HashMap;
use std::ffi::OsStr;
use liquid::Value;
use walkdir::WalkDir;
use document::Document;
use error::{Error, Result};
use config::Config;
use yaml_rust::YamlLoader;
use chrono::{DateTime, UTC, FixedOffset};
use chrono::offset::TimeZone;
use rss::{Channel, Rss};

macro_rules! walker {
    ($dir:expr) => {
        WalkDir::new($dir)
            .into_iter()
            .filter_map(|e| e.ok())
            .filter(|f| {
                // skip directories
                f.file_type().is_file()
                    &&
                    // don't copy hidden files
                    !f.path()
                    .file_name()
                    .and_then(|name| name.to_str())
                    .unwrap_or(".")
                    .starts_with(".")
            })
    }
}

/// The primary build function that tranforms a directory into a site
pub fn build(config: &Config) -> Result<()> {
    trace!("Build configuration: {:?}", config);

    // join("") makes sure path has a trailing slash
    let source = PathBuf::from(&config.source).join("");
    let source = source.as_path();
    let dest = PathBuf::from(&config.dest).join("");
    let dest = dest.as_path();

    let template_extensions: Vec<&OsStr> = config.template_extensions
                                                 .iter()
                                                 .map(OsStr::new)
                                                 .collect();

    let layouts_path = source.join(&config.layouts);
    let posts_path = source.join(&config.posts);

    debug!("Layouts directory: {:?}", layouts_path);
    debug!("Posts directory: {:?}", posts_path);

    let layouts = try!(get_layouts(&layouts_path));

    let mut documents = vec![];

    for entry in walker!(&source) {
        if template_extensions.contains(&entry.path()
                                              .extension()
                                              .unwrap_or(OsStr::new(""))) &&
           entry.path().parent() != Some(layouts_path.as_path()) {
            let mut doc = try!(parse_document(&entry.path(), &source));

            // if the document is in the posts folder it's considered a post
            if entry.path().parent() == Some(posts_path.as_path()) {
                doc.is_post = true;
            }

            documents.push(doc);
        }
    }

    // January 1, 1970 0:00:00 UTC, the beginning of time
    let default_date = UTC.timestamp(0, 0).with_timezone(&FixedOffset::east(0));

    // sort documents by date, if there's no date (none was provided or it couldn't be read) then
    // fall back to the default date
    documents.sort_by(|a, b| {
        b.date.unwrap_or(default_date.clone()).cmp(&a.date.unwrap_or(default_date.clone()))
    });

    // check if we should create an RSS file and create it!
    if let &Some(ref path) = &config.rss {
        try!(create_rss(path, dest, &config, &documents));
    }

    // these are the attributes of all documents that are posts, so that they can be
    // passed to the renderer
    let post_data: Vec<Value> = documents.iter()
                                         .filter(|x| x.is_post)
                                         .map(|x| Value::Object(x.get_attributes()))
                                         .collect();

    // thread handles to join later
    let mut handles = vec![];

    // generate documents (in parallel)
    // TODO I'm probably underutilizing crossbeam
    crossbeam::scope(|scope| {
        for doc in &documents {
            trace!("Generating {}", doc.path);
            let post_data = post_data.clone();
            let layouts = layouts.clone();
            let handle = scope.spawn(move || doc.create_file(&dest, &layouts, &post_data));
            handles.push(handle);
        }
    });

    for handle in handles {
        try!(handle.join());
    }

    // copy all remaining files in the source to the destination
    if source != dest {
        info!("Copying remaining assets");
        let source_str = try!(source.to_str()
                                    .ok_or(format!("Cannot convert pathname {:?} to UTF-8",
                                                   source)));

        for entry in walker!(&source).filter(|f| {
            !template_extensions.contains(&f.path()
                                            .extension()
                                            .unwrap_or(OsStr::new(""))) &&
            f.path() != dest && f.path() != layouts_path.as_path()
        }) {
            let entry_path = try!(entry.path()
                                       .to_str()
                                       .ok_or(format!("Cannot convert pathname {:?} to UTF-8",
                                                      entry.path())));

            let relative = try!(entry_path.split(source_str)
                                          .last()
                                          .ok_or(format!("Empty path")));

            if try!(entry.metadata()).is_dir() {
                try!(fs::create_dir_all(&dest.join(relative)));
                debug!("Created new directory {:?}", dest.join(relative));
            } else {
                if let Some(ref parent) = Path::new(relative).parent() {
                    try!(fs::create_dir_all(&dest.join(parent)));
                }

                try!(fs::copy(entry.path(), &dest.join(relative))
                         .map_err(|_| format!("Could not copy {:?}", entry.path())));
                debug!("Copied {:?} to {:?}", entry.path(), dest.join(relative));
            }
        }
    }

    Ok(())
}

/// Gets all layout files from the specified path (usually _layouts/)
/// This walks the specified directory recursively
///
/// Returns a map filename -> content
fn get_layouts(layouts_path: &Path) -> Result<HashMap<String, String>> {
    let mut layouts = HashMap::new();

    // go through the layout directory and add
    // filename -> text content to the layout map
    for entry in walker!(layouts_path) {
        let mut text = String::new();
        let mut file = try!(File::open(entry.path()));
        try!(file.read_to_string(&mut text));

        let path = try!(entry.path()
                             .file_name()
                             .and_then(|name| name.to_str())
                             .ok_or(format!("Cannot convert pathname {:?} to UTF-8",
                                            entry.path().file_name())));

        layouts.insert(path.to_owned(), text);
    }

    Ok(layouts)
}

// creates a new RSS file with the contents of the site blog
fn create_rss(path: &str, dest: &Path, config: &Config, documents: &[Document]) -> Result<()> {
    match (&config.name, &config.description, &config.link) {
        // these three fields are mandatory in the RSS standard
        (&Some(ref name), &Some(ref description), &Some(ref link)) => {
            trace!("Generating RSS data");

            let items = documents.iter()
                                 .filter(|x| x.is_post)
                                 .map(|doc| doc.to_rss(link))
                                 .collect();

            let channel = Channel {
                title: name.to_owned(),
                link: link.to_owned(),
                description: description.to_owned(),
                items: items,
                ..Default::default()
            };

            let rss = Rss(channel);
            let rss_string = rss.to_string();
            trace!("RSS data: {}", rss_string);

            let rss_path = dest.join(path);

            let mut rss_file = try!(File::create(&rss_path));
            try!(rss_file.write_all(&rss_string.into_bytes()));

            info!("Created RSS file at {}", rss_path.display());
            Ok(())
        }
        _ => {
            Err(Error::from("name, description and link need to be defined in the config file to \
                             generate RSS"))
        }
    }
}

fn parse_document(path: &Path, source: &Path) -> Result<Document> {
    let mut attributes = HashMap::new();
    let mut content = try!(parse_file(path));

    // if there is front matter, split the file and parse it
    if content.contains("---") {
        let content2 = content.clone();
        let mut content_splits = content2.split("---");

        // above the split are the attributes
        let attribute_string = content_splits.next().unwrap_or("");

        // everything below the split becomes the new content
        content = content_splits.next().unwrap_or("").to_owned();

        let yaml_result = try!(YamlLoader::load_from_str(attribute_string));

        let yaml_attributes = try!(yaml_result[0]
                                       .as_hash()
                                       .ok_or(format!("Incorrect front matter format in {:?}",
                                                      path)));

        for (key, value) in yaml_attributes {
            // TODO is unwrap_or the best way to handle this?
            attributes.insert(key.as_str().unwrap_or("").to_owned(),
                              value.as_str().unwrap_or("").to_owned());
        }
    }

    let date = attributes.get("date")
                         .and_then(|d| DateTime::parse_from_str(d, "%d %B %Y %H:%M:%S %z").ok());

    let path_str = try!(path.to_str()
                            .ok_or(format!("Cannot convert pathname {:?} to UTF-8", path)));

    let source_str = try!(source.to_str()
                                .ok_or(format!("Cannot convert pathname {:?} to UTF-8", source)));

    let new_path = try!(path_str.split(source_str)
                                .last()
                                .ok_or(format!("Empty path")));

    // construct path
    let mut path_buf = PathBuf::from(new_path);
    path_buf.set_extension("html");

    let path_str = try!(path_buf.to_str()
                                .ok_or(format!("Cannot convert pathname {:?} to UTF-8", path_str)));

    let markdown = path.extension().unwrap_or(OsStr::new("")) == OsStr::new("md");

    let name = try!(path.file_stem()
                        .and_then(|stem| stem.to_str())
                        .ok_or(format!("Invalid UTF-8 in file stem for {:?}", path)));

    Ok(Document::new(name.to_owned(),
                     path_str.to_owned(),
                     attributes,
                     content,
                     false,
                     date,
                     markdown))
}

fn parse_file(path: &Path) -> Result<String> {
    let mut file = try!(File::open(path));
    let mut text = String::new();
    try!(file.read_to_string(&mut text));
    Ok(text)
}
<|endoftext|>


<commit_before><commit_msg>Unconditionally set up rAF loop on initialization<commit_after><|endoftext|>


<commit_before><commit_msg>Temporary hack to avoid problems with old state in collision engine<commit_after><|endoftext|>


<commit_before><commit_msg>maybe<commit_after><|endoftext|>


<commit_before><commit_msg>Converted to 8x8 chunks<commit_after><|endoftext|>