<commit_before>use super::utils::types::{ Sha256Digest };
use super::blake2::{ Blake2s, Digest };
use super::config::Config;

// Interprets a 3-byte slice from a [u8] as an integer.
fn get_shift_from_source(source: &[u8], offset: usize) -> u32 {
    (source[offset + 2] as u32) |
        ((source[offset + 1] as u32) << 8) |
        ((source[offset    ] as u32) << 16)
}

// Given entropy in the form of `seed`, return a shuffled list of validators
// of size `validator_count` or `sample`.
pub fn get_shuffling(
    seed: Sha256Digest,
    validator_count: u32,
    sample: Option<u32>,
    config: Config)
    -> Vec<u32>
{
    let max_validators = config.max_validators;
    assert!(validator_count <= max_validators);

    // TODO: figure out why the Python implementation uses
    // this `rand_max` var.
    // let rand_max = max_validators - (max_validators % validator_count);
    let validator_range = match sample {
        Some(x) => x,
        None => validator_count
    };
    let mut output: Vec<u32> = (0..validator_range).collect();
    let mut source = Blake2s::new();
    source.input(&seed);

    let mut v = 0;
    while v < validator_range {
        let current_source = source.result();
        let mut source_offset = 0;
        while source_offset < 30 {
            let m = get_shift_from_source(&current_source, source_offset);
            let shuffled_position = (m % (validator_count - v)) + v;
            output.swap(v as usize, shuffled_position as usize);
            v += 1;
            if v >= validator_count { break; }
            source_offset += 3;
        }
        // Re-hash the source
        source = Blake2s::new();
        source.input(&current_source);
    }
    output
}

#[cfg(test)]
mod tests {
    use super::*;

    #[test]
    fn test_shuffling_shift_fn() {
        let mut x = get_shift_from_source(
            &vec![0_u8, 0, 1],
            0);
        assert_eq!((x as u32), 1);

        x = get_shift_from_source(
            &vec![0_u8, 1, 1],
            0);
        assert_eq!(x, 257);

        x = get_shift_from_source(
            &vec![1_u8, 1, 1],
            0);
        assert_eq!(x, 65793);

        x = get_shift_from_source(
            &vec![255_u8, 1, 1],
            0);
        assert_eq!(x, 16711937);
    }

    #[test]
    fn test_shuffling() {
        let s = get_shuffling(
            Sha256Digest::zero(),
            10,
            None,
            Config::standard());
        assert_eq!(s,
                   vec!(0, 9, 7, 6, 4, 1, 8, 5, 2, 3),
                   "10 validator shuffle was not as expected");
    }
}
<commit_msg>Add extra shuffling test<commit_after>use super::utils::types::{ Sha256Digest };
use super::blake2::{ Blake2s, Digest };
use super::config::Config;

// Interprets a 3-byte slice from a [u8] as an integer.
fn get_shift_from_source(source: &[u8], offset: usize) -> u32 {
    (source[offset + 2] as u32) |
        ((source[offset + 1] as u32) << 8) |
        ((source[offset    ] as u32) << 16)
}

// Given entropy in the form of `seed`, return a shuffled list of validators
// of size `validator_count` or `sample`.
pub fn get_shuffling(
    seed: Sha256Digest,
    validator_count: u32,
    sample: Option<u32>,
    config: Config)
    -> Vec<u32>
{
    let max_validators = config.max_validators;
    assert!(validator_count <= max_validators);

    // TODO: figure out why the Python implementation uses
    // this `rand_max` var.
    // let rand_max = max_validators - (max_validators % validator_count);
    let validator_range = match sample {
        Some(x) => x,
        None => validator_count
    };
    let mut output: Vec<u32> = (0..validator_range).collect();
    let mut source = Blake2s::new();
    source.input(&seed);

    let mut v = 0;
    while v < validator_range {
        let current_source = source.result();
        let mut source_offset = 0;
        while source_offset < 30 {
            let m = get_shift_from_source(&current_source, source_offset);
            let shuffled_position = (m % (validator_count - v)) + v;
            output.swap(v as usize, shuffled_position as usize);
            v += 1;
            if v >= validator_count { break; }
            source_offset += 3;
        }
        // Re-hash the source
        source = Blake2s::new();
        source.input(&current_source);
    }
    output
}

#[cfg(test)]
mod tests {
    use super::*;

    #[test]
    fn test_shuffling_shift_fn() {
        let mut x = get_shift_from_source(
            &vec![0_u8, 0, 1],
            0);
        assert_eq!((x as u32), 1);

        x = get_shift_from_source(
            &vec![0_u8, 1, 1],
            0);
        assert_eq!(x, 257);

        x = get_shift_from_source(
            &vec![1_u8, 1, 1],
            0);
        assert_eq!(x, 65793);

        x = get_shift_from_source(
            &vec![255_u8, 1, 1],
            0);
        assert_eq!(x, 16711937);
    }

    #[test]
    fn test_shuffling() {
        let s = get_shuffling(
            Sha256Digest::zero(),
            10,
            None,
            Config::standard());
        assert_eq!(s,
                   vec!(0, 9, 7, 6, 4, 1, 8, 5, 2, 3),
                   "10 validator shuffle was not as expected");
    }

    #[test]
    fn test_shuffling_with_gt_half_max_validators() {
        let mut config = Config::standard();
        config.max_validators = 19;
        let s = get_shuffling(
            Sha256Digest::zero(),
            10,
            None,
            Config::standard());
        assert_eq!(s,
                   vec!(0, 9, 7, 6, 4, 1, 8, 5, 2, 3),
                   "10 validator shuffle was not as expected");
    }
}
<|endoftext|>


<commit_before><commit_msg>blank message<commit_after><|endoftext|>


<commit_before>use context::ContextMap;
use dispatcher::response::ResponseSender;
use dispatcher::Response;
use dispatcher::request::{Request, RequestHandle};
use condition::Condition;
use reactor::EventHandler;

pub struct ExitEventHandler {
    condition: Condition,
    response_handler: Box<ResponseSender>,
    stops: u32,
}

impl ExitEventHandler {
    pub fn new(condition: Condition, response_handler: Box<ResponseSender>) -> ExitEventHandler {
        ExitEventHandler {
            condition: condition,
            response_handler: response_handler,
            stops: 0,
        }
    }
}

impl EventHandler<Request, ContextMap> for ExitEventHandler {
    fn handle_event(&mut self, event: Request, _: &mut ContextMap) {
        if let Request::Exit = event {
            self.stops += 1;
            self.response_handler.send_response(Response::Exit);

            if self.stops >= 2 {
                self.condition.activate();
            }
        } else {
            unreachable!("An ExitEventHandler should only receive Exit events");
        }
    }
    fn handle(&self) -> RequestHandle {
        RequestHandle::Exit
    }
}
<commit_msg>ExitHandler: use higher-ranked lifetime<commit_after>use dispatcher::response::ResponseSender;
use dispatcher::Response;
use dispatcher::request::{Request, RequestHandle};
use condition::Condition;
use reactor::{EventHandler, SharedData};

pub struct ExitEventHandler {
    condition: Condition,
    response_handler: Box<ResponseSender>,
    stops: u32,
}

impl ExitEventHandler {
    pub fn new(condition: Condition, response_handler: Box<ResponseSender>) -> ExitEventHandler {
        ExitEventHandler {
            condition: condition,
            response_handler: response_handler,
            stops: 0,
        }
    }
}

impl<'a> EventHandler<Request, SharedData<'a>> for ExitEventHandler {
    fn handle_event(&mut self, event: Request, _: &mut SharedData) {
        if let Request::Exit = event {
            self.stops += 1;
            self.response_handler.send_response(Response::Exit);

            if self.stops >= 2 {
                self.condition.activate();
            }
        } else {
            unreachable!("An ExitEventHandler should only receive Exit events");
        }
    }
    fn handle(&self) -> RequestHandle {
        RequestHandle::Exit
    }
}
<|endoftext|>


<commit_before>use rustc_serialize::{Encodable, Encoder};

use super::UpdateRequestId;

#[derive(RustcDecodable, RustcEncodable, Clone, Debug, PartialEq, Eq)]
pub struct UpdateReport {
    pub update_id:         UpdateRequestId,
    pub operation_results: Vec<OperationResult>
}

impl UpdateReport {
    pub fn new(update_id: String, results: Vec<OperationResult>) -> UpdateReport {
        UpdateReport { update_id: update_id, operation_results: results }
    }

    pub fn single(update_id: UpdateRequestId, result_code: UpdateResultCode, result_text: String) -> UpdateReport {
        let result = OperationResult { id: update_id.clone(), result_code: code, result_text: output };
        UpdateReport { update_id: update_id, operation_results: vec![result] }
    }
}

#[derive(RustcEncodable, Clone, Debug)]
pub struct DeviceReport<'d, 'r> {
    pub device: &'d str,
    pub report: &'r UpdateReport
}

impl<'d, 'r> DeviceReport<'d, 'r> {
    pub fn new(device: &'d str, report: &'r UpdateReport) -> DeviceReport<'d, 'r> {
        DeviceReport { device: &device, report: &report }
    }
}

#[allow(non_camel_case_types)]
#[derive(RustcDecodable, Clone, Debug, PartialEq, Eq)]
pub enum UpdateResultCode {
    OK = 0,                  // Operation executed successfully
    ALREADY_PROCESSED,       // Operation has already been processed
    DEPENDENCY_FAILURE,      // Dependency failure during package install, upgrade, or removal
    VALIDATION_FAILED,       // Update image integrity has been compromised
    INSTALL_FAILED,          // Package installation failed
    UPGRADE_FAILED,          // Package upgrade failed
    REMOVAL_FAILED,          // Package removal failed
    FLASH_FAILED,            // The module loader could not flash its managed module
    CREATE_PARTITION_FAILED, // Partition creation failed
    DELETE_PARTITION_FAILED, // Partition deletion failed
    RESIZE_PARTITION_FAILED, // Partition resize failed
    WRITE_PARTITION_FAILED,  // Partition write failed
    PATCH_PARTITION_FAILED,  // Partition patching failed
    USER_DECLINED,           // User declined the update
    SOFTWARE_BLACKLISTED,    // Software was blacklisted
    DISK_FULL,               // Ran out of disk space
    NOT_FOUND,               // Software package not found
    OLD_VERSION,             // Tried to downgrade to older version
    INTERNAL_ERROR,          // SWM Internal integrity error
    GENERAL_ERROR,           // Other error
}

impl Encodable for UpdateResultCode {
    fn encode<S: Encoder>(&self, s: &mut S) -> Result<(), S::Error> {
        s.emit_u64(self.clone() as u64)
    }
}

#[derive(RustcDecodable, RustcEncodable, Clone, Debug, PartialEq, Eq)]
pub struct OperationResult {
    pub id:            String,
    pub result_code:   UpdateResultCode,
    pub result_output: String,
}

#[derive(RustcDecodable, RustcEncodable, Clone, Debug, PartialEq, Eq)]
pub struct InstalledFirmware {
    pub module:        String,
    pub firmware_id:   String,
    pub last_modified: u64
}

#[derive(RustcDecodable, RustcEncodable, Clone, Debug, PartialEq, Eq)]
pub struct InstalledPackage {
    pub package_id:    String,
    pub name:          String,
    pub description:   String,
    pub last_modified: u64
}

#[derive(RustcDecodable, RustcEncodable, Clone, Debug, PartialEq, Eq)]
pub struct InstalledSoftware {
    pub packages:  Vec<InstalledPackage>,
    pub firmwares: Vec<InstalledFirmware>
}

impl InstalledSoftware {
    pub fn new(packages: Vec<InstalledPackage>, firmwares: Vec<InstalledFirmware>) -> InstalledSoftware {
        InstalledSoftware { packages: packages, firmwares: firmwares }
    }
}
<commit_msg>Fix mismatched field names<commit_after>use rustc_serialize::{Encodable, Encoder};

use super::UpdateRequestId;

#[derive(RustcDecodable, RustcEncodable, Clone, Debug, PartialEq, Eq)]
pub struct UpdateReport {
    pub update_id:         UpdateRequestId,
    pub operation_results: Vec<OperationResult>
}

impl UpdateReport {
    pub fn new(update_id: String, results: Vec<OperationResult>) -> UpdateReport {
        UpdateReport { update_id: update_id, operation_results: results }
    }

    pub fn single(update_id: UpdateRequestId, result_code: UpdateResultCode, result_text: String) -> UpdateReport {
        let result = OperationResult {
            id: update_id.clone(),
            result_code: result_code,
            result_text: result_text
        };
        UpdateReport { update_id: update_id, operation_results: vec![result] }
    }
}

#[derive(RustcEncodable, Clone, Debug)]
pub struct DeviceReport<'d, 'r> {
    pub device: &'d str,
    pub report: &'r UpdateReport
}

impl<'d, 'r> DeviceReport<'d, 'r> {
    pub fn new(device: &'d str, report: &'r UpdateReport) -> DeviceReport<'d, 'r> {
        DeviceReport { device: &device, report: &report }
    }
}

#[allow(non_camel_case_types)]
#[derive(RustcDecodable, Clone, Debug, PartialEq, Eq)]
pub enum UpdateResultCode {
    OK = 0,                  // Operation executed successfully
    ALREADY_PROCESSED,       // Operation has already been processed
    DEPENDENCY_FAILURE,      // Dependency failure during package install, upgrade, or removal
    VALIDATION_FAILED,       // Update image integrity has been compromised
    INSTALL_FAILED,          // Package installation failed
    UPGRADE_FAILED,          // Package upgrade failed
    REMOVAL_FAILED,          // Package removal failed
    FLASH_FAILED,            // The module loader could not flash its managed module
    CREATE_PARTITION_FAILED, // Partition creation failed
    DELETE_PARTITION_FAILED, // Partition deletion failed
    RESIZE_PARTITION_FAILED, // Partition resize failed
    WRITE_PARTITION_FAILED,  // Partition write failed
    PATCH_PARTITION_FAILED,  // Partition patching failed
    USER_DECLINED,           // User declined the update
    SOFTWARE_BLACKLISTED,    // Software was blacklisted
    DISK_FULL,               // Ran out of disk space
    NOT_FOUND,               // Software package not found
    OLD_VERSION,             // Tried to downgrade to older version
    INTERNAL_ERROR,          // SWM Internal integrity error
    GENERAL_ERROR,           // Other error
}

impl Encodable for UpdateResultCode {
    fn encode<S: Encoder>(&self, s: &mut S) -> Result<(), S::Error> {
        s.emit_u64(self.clone() as u64)
    }
}

#[derive(RustcDecodable, RustcEncodable, Clone, Debug, PartialEq, Eq)]
pub struct OperationResult {
    pub id:          String,
    pub result_code: UpdateResultCode,
    pub result_text: String,
}

#[derive(RustcDecodable, RustcEncodable, Clone, Debug, PartialEq, Eq)]
pub struct InstalledFirmware {
    pub module:        String,
    pub firmware_id:   String,
    pub last_modified: u64
}

#[derive(RustcDecodable, RustcEncodable, Clone, Debug, PartialEq, Eq)]
pub struct InstalledPackage {
    pub package_id:    String,
    pub name:          String,
    pub description:   String,
    pub last_modified: u64
}

#[derive(RustcDecodable, RustcEncodable, Clone, Debug, PartialEq, Eq)]
pub struct InstalledSoftware {
    pub packages:  Vec<InstalledPackage>,
    pub firmwares: Vec<InstalledFirmware>
}

impl InstalledSoftware {
    pub fn new(packages: Vec<InstalledPackage>, firmwares: Vec<InstalledFirmware>) -> InstalledSoftware {
        InstalledSoftware { packages: packages, firmwares: firmwares }
    }
}
<|endoftext|>


<commit_before>extern crate itertools;
use self::itertools::Itertools;
extern crate crossbeam;
use std;

#[derive(Debug, Copy, Clone)]
pub struct Endian {
  big: bool,
}

impl Endian {
  pub fn ru32(&self, buf: &[u8], pos: usize) -> u32 {
    if self.big {
      BEu32(buf,pos)
    } else {
      LEu32(buf,pos)
    }
  }

  pub fn ru16(&self, buf: &[u8], pos: usize) -> u16 {
    if self.big {
      BEu16(buf,pos)
    } else {
      LEu16(buf,pos)
    }
  }
}

pub static BIG_ENDIAN: Endian = Endian{big: true};
pub static LITTLE_ENDIAN: Endian = Endian{big: false};

#[allow(non_snake_case)] pub fn BEu32(buf: &[u8], pos: usize) -> u32 {
  (buf[pos] as u32) << 24 |
  (buf[pos+1] as u32) << 16 |
  (buf[pos+2] as u32) << 8 |
  (buf[pos+3] as u32)
}

#[allow(non_snake_case)] pub fn LEu32(buf: &[u8], pos: usize) -> u32 {
  (buf[pos] as u32) |
  (buf[pos+1] as u32) << 8 |
  (buf[pos+2] as u32) << 16 |
  (buf[pos+3] as u32) << 24
}

#[allow(non_snake_case)] pub fn BEu16(buf: &[u8], pos: usize) -> u16 {
  (buf[pos] as u16) << 8 | (buf[pos+1] as u16)
}

#[allow(non_snake_case)] pub fn LEu16(buf: &[u8], pos: usize) -> u16 {
  (buf[pos] as u16) | (buf[pos+1] as u16) << 8
}

pub fn decode_12be(buf: &[u8], width: usize, height: usize) -> Vec<u16> {
  decode_threaded(width, height, &(|out: &mut [u16], start, width, height| {
    let inb = &buf[((start*width*12/8) as usize)..buf.len()];
    let mut pos: usize = 0;

    for row in 0..height {
      for col in (0..width).step(2) {
        let g1: u16 = inb[pos] as u16;
        let g2: u16 = inb[pos+1] as u16;
        let g3: u16 = inb[pos+2] as u16;
        pos += 3;

        out[width*row+col]   = (g1 << 4) | (g2 >> 4);
        out[width*row+col+1] = ((g2 & 0x0f) << 8) | g3;
      }
    }
  }))
}

pub fn decode_12le(buf: &[u8], width: usize, height: usize) -> Vec<u16> {
  decode_threaded(width, height, &(|out: &mut [u16], start, width, height| {
    let inb = &buf[((start*width*12/8) as usize)..buf.len()];
    let mut pos: usize = 0;

    for row in 0..height {
      for col in (0..width).step(2) {
        let g1: u16 = inb[pos] as u16;
        let g2: u16 = inb[pos+1] as u16;
        let g3: u16 = inb[pos+2] as u16;
        pos += 3;

        out[width*row+col]   = ((g2 & 0x0f) << 8) | g1;
        out[width*row+col+1] = (g3 << 4) | (g2 >> 4);
      }
    }
  }))
}

pub fn decode_12be_unpacked(buf: &[u8], width: usize, height: usize) -> Vec<u16> {
  decode_threaded(width, height, &(|out: &mut [u16], start, width, height| {
    let inb = &buf[((start*width*2) as usize)..buf.len()];
    let mut pos: usize = 0;

    for row in 0..height {
      for col in 0..width {
        let g1: u16 = inb[pos] as u16;
        let g2: u16 = inb[pos+1] as u16;
        pos += 2;

        out[width*row+col] = ((g1 & 0x0f) << 8) | g2;
      }
    }
  }))
}

pub fn decode_16le(buf: &[u8], width: usize, height: usize) -> Vec<u16> {
  decode_threaded(width, height, &(|out: &mut [u16], start, width, height| {
    let inb = &buf[((start*width*2) as usize)..buf.len()];
    let mut pos: usize = 0;

    for row in 0..height {
      for col in 0..width {
        let g1: u16 = inb[pos] as u16;
        let g2: u16 = inb[pos+1] as u16;
        pos += 2;

        out[width*row+col] = (g2 << 8) | g1;
      }
    }
  }))
}

pub fn decode_threaded<F>(width: usize, height: usize, closure: &F) -> Vec<u16>
  where F : Fn(&mut [u16], usize, usize, usize)+std::marker::Sync {
  let mut out: Vec<u16> = vec![0; (width*height) as usize];

  // Default to 4 threads as that should be close to the ideal speedup on most
  // machines. In the future we need a way to have this be a parameter or even
  // to set it dynamically based on how loaded the machine is.
  let threads = 3;

  // If we're only using one thread do it all sequentially and be done with it
  if threads < 2 || height < threads {
    closure(&mut out, 0, width, height);
    return out
  }

  let mut split = height/threads;
  if split*threads < height { // Make sure the last split is always the smallest
    split += 1;
  }

  crossbeam::scope(|scope| {
    let mut handles = Vec::new();
    for (i,out_part) in (&mut out[..]).chunks_mut(split*width).enumerate() {
      let start = split*(i);
      let tall = out_part.len()/width;
      let handle = scope.spawn(move || {
        closure(out_part, start, width, tall);
      });
      handles.push(handle);
    }

    for h in handles { h.join() };
  });

  out
}

#[derive(Debug, Clone)]
pub struct LookupTable {
  table: Vec<(u16, u16, u16)>,
}

impl LookupTable {
  pub fn new(table: &[u16]) -> LookupTable {
    let mut tbl = vec![(0,0,0); table.len()];
    for i in 0..table.len() {
      let center = table[i];
      let lower = if i > 0 {table[i-1]} else {center};
      let upper = if i < (table.len()-1) {table[i+1]} else {center};
      let base = center - ((upper - lower + 2) / 4);
      let delta = upper - lower;
      tbl[i] = (center, base, delta);
    }
    LookupTable {
      table: tbl,
    }
  }

//  pub fn lookup(&self, value: u16) -> u16 {
//    let (val, _, _) = self.table[value as usize];
//    val
//  }

  pub fn dither(&self, value: u16, rand: &mut u32) -> u16 {
    let (_, sbase, sdelta) = self.table[value as usize];
    let base = sbase as u32;
    let delta = sdelta as u32;
    let pixel = base + ((delta * (*rand & 2047) + 1024) >> 12);
    *rand = 15700 * (*rand & 65535) + (*rand >> 16);
    pixel as u16
  }
}

pub struct BitPump<'a> {
  buffer: &'a [u8],
  offset: usize,
}

impl<'a> BitPump<'a> {
  pub fn new(src: &'a [u8]) -> BitPump {
    BitPump {
      buffer: src,
      offset: 0,
    }
  }

  pub fn peek_bits(&self, nbits: u32) -> u32 {
    (LEu32(self.buffer, self.offset>>3) >> (self.offset&7)) & ((1<<nbits) - 1)
  }

  pub fn get_bits(&mut self, nbits: u32) -> u32 {
    let val = self.peek_bits(nbits);
    self.offset += nbits as usize;
    val
  }
}
<commit_msg>Go back to using 4 threads<commit_after>extern crate itertools;
use self::itertools::Itertools;
extern crate crossbeam;
use std;

#[derive(Debug, Copy, Clone)]
pub struct Endian {
  big: bool,
}

impl Endian {
  pub fn ru32(&self, buf: &[u8], pos: usize) -> u32 {
    if self.big {
      BEu32(buf,pos)
    } else {
      LEu32(buf,pos)
    }
  }

  pub fn ru16(&self, buf: &[u8], pos: usize) -> u16 {
    if self.big {
      BEu16(buf,pos)
    } else {
      LEu16(buf,pos)
    }
  }
}

pub static BIG_ENDIAN: Endian = Endian{big: true};
pub static LITTLE_ENDIAN: Endian = Endian{big: false};

#[allow(non_snake_case)] pub fn BEu32(buf: &[u8], pos: usize) -> u32 {
  (buf[pos] as u32) << 24 |
  (buf[pos+1] as u32) << 16 |
  (buf[pos+2] as u32) << 8 |
  (buf[pos+3] as u32)
}

#[allow(non_snake_case)] pub fn LEu32(buf: &[u8], pos: usize) -> u32 {
  (buf[pos] as u32) |
  (buf[pos+1] as u32) << 8 |
  (buf[pos+2] as u32) << 16 |
  (buf[pos+3] as u32) << 24
}

#[allow(non_snake_case)] pub fn BEu16(buf: &[u8], pos: usize) -> u16 {
  (buf[pos] as u16) << 8 | (buf[pos+1] as u16)
}

#[allow(non_snake_case)] pub fn LEu16(buf: &[u8], pos: usize) -> u16 {
  (buf[pos] as u16) | (buf[pos+1] as u16) << 8
}

pub fn decode_12be(buf: &[u8], width: usize, height: usize) -> Vec<u16> {
  decode_threaded(width, height, &(|out: &mut [u16], start, width, height| {
    let inb = &buf[((start*width*12/8) as usize)..buf.len()];
    let mut pos: usize = 0;

    for row in 0..height {
      for col in (0..width).step(2) {
        let g1: u16 = inb[pos] as u16;
        let g2: u16 = inb[pos+1] as u16;
        let g3: u16 = inb[pos+2] as u16;
        pos += 3;

        out[width*row+col]   = (g1 << 4) | (g2 >> 4);
        out[width*row+col+1] = ((g2 & 0x0f) << 8) | g3;
      }
    }
  }))
}

pub fn decode_12le(buf: &[u8], width: usize, height: usize) -> Vec<u16> {
  decode_threaded(width, height, &(|out: &mut [u16], start, width, height| {
    let inb = &buf[((start*width*12/8) as usize)..buf.len()];
    let mut pos: usize = 0;

    for row in 0..height {
      for col in (0..width).step(2) {
        let g1: u16 = inb[pos] as u16;
        let g2: u16 = inb[pos+1] as u16;
        let g3: u16 = inb[pos+2] as u16;
        pos += 3;

        out[width*row+col]   = ((g2 & 0x0f) << 8) | g1;
        out[width*row+col+1] = (g3 << 4) | (g2 >> 4);
      }
    }
  }))
}

pub fn decode_12be_unpacked(buf: &[u8], width: usize, height: usize) -> Vec<u16> {
  decode_threaded(width, height, &(|out: &mut [u16], start, width, height| {
    let inb = &buf[((start*width*2) as usize)..buf.len()];
    let mut pos: usize = 0;

    for row in 0..height {
      for col in 0..width {
        let g1: u16 = inb[pos] as u16;
        let g2: u16 = inb[pos+1] as u16;
        pos += 2;

        out[width*row+col] = ((g1 & 0x0f) << 8) | g2;
      }
    }
  }))
}

pub fn decode_16le(buf: &[u8], width: usize, height: usize) -> Vec<u16> {
  decode_threaded(width, height, &(|out: &mut [u16], start, width, height| {
    let inb = &buf[((start*width*2) as usize)..buf.len()];
    let mut pos: usize = 0;

    for row in 0..height {
      for col in 0..width {
        let g1: u16 = inb[pos] as u16;
        let g2: u16 = inb[pos+1] as u16;
        pos += 2;

        out[width*row+col] = (g2 << 8) | g1;
      }
    }
  }))
}

pub fn decode_threaded<F>(width: usize, height: usize, closure: &F) -> Vec<u16>
  where F : Fn(&mut [u16], usize, usize, usize)+std::marker::Sync {
  let mut out: Vec<u16> = vec![0; (width*height) as usize];

  // Default to 4 threads as that should be close to the ideal speedup on most
  // machines. In the future we need a way to have this be a parameter or even
  // to set it dynamically based on how loaded the machine is.
  let threads = 4;

  // If we're only using one thread do it all sequentially and be done with it
  if threads < 2 || height < threads {
    closure(&mut out, 0, width, height);
    return out
  }

  let mut split = height/threads;
  if split*threads < height { // Make sure the last split is always the smallest
    split += 1;
  }

  crossbeam::scope(|scope| {
    let mut handles = Vec::new();
    for (i,out_part) in (&mut out[..]).chunks_mut(split*width).enumerate() {
      let start = split*(i);
      let tall = out_part.len()/width;
      let handle = scope.spawn(move || {
        closure(out_part, start, width, tall);
      });
      handles.push(handle);
    }

    for h in handles { h.join() };
  });

  out
}

#[derive(Debug, Clone)]
pub struct LookupTable {
  table: Vec<(u16, u16, u16)>,
}

impl LookupTable {
  pub fn new(table: &[u16]) -> LookupTable {
    let mut tbl = vec![(0,0,0); table.len()];
    for i in 0..table.len() {
      let center = table[i];
      let lower = if i > 0 {table[i-1]} else {center};
      let upper = if i < (table.len()-1) {table[i+1]} else {center};
      let base = center - ((upper - lower + 2) / 4);
      let delta = upper - lower;
      tbl[i] = (center, base, delta);
    }
    LookupTable {
      table: tbl,
    }
  }

//  pub fn lookup(&self, value: u16) -> u16 {
//    let (val, _, _) = self.table[value as usize];
//    val
//  }

  pub fn dither(&self, value: u16, rand: &mut u32) -> u16 {
    let (_, sbase, sdelta) = self.table[value as usize];
    let base = sbase as u32;
    let delta = sdelta as u32;
    let pixel = base + ((delta * (*rand & 2047) + 1024) >> 12);
    *rand = 15700 * (*rand & 65535) + (*rand >> 16);
    pixel as u16
  }
}

pub struct BitPump<'a> {
  buffer: &'a [u8],
  offset: usize,
}

impl<'a> BitPump<'a> {
  pub fn new(src: &'a [u8]) -> BitPump {
    BitPump {
      buffer: src,
      offset: 0,
    }
  }

  pub fn peek_bits(&self, nbits: u32) -> u32 {
    (LEu32(self.buffer, self.offset>>3) >> (self.offset&7)) & ((1<<nbits) - 1)
  }

  pub fn get_bits(&mut self, nbits: u32) -> u32 {
    let val = self.peek_bits(nbits);
    self.offset += nbits as usize;
    val
  }
}
<|endoftext|>


<commit_before><commit_msg>Conflicting test names<commit_after><|endoftext|>


<commit_before>extern crate num as libnum;

pub mod linalg {

	pub trait Metric<T> {
		fn norm(&self) -> T;
	}

	pub mod matrix;
	pub mod vector;
	pub mod utils;
}

pub mod learning {
	pub mod lin_reg;

	pub trait Model<T,U> {
		fn predict(&self, data:T) -> U;

		fn train(&mut self, data:T, value:U);
	}
}<commit_msg>Creating framework for distributions.<commit_after>extern crate num as libnum;

pub mod linalg {

	pub trait Metric<T> {
		fn norm(&self) -> T;
	}

	pub mod matrix;
	pub mod vector;
	pub mod utils;
}

pub mod learning {
	pub mod lin_reg;

	pub trait Model<T,U> {
		fn predict(&self, data:T) -> U;

		fn train(&mut self, data:T, value:U);
	}
}

pub mod stats {
	pub mod dist {
		pub trait Distribution<T> {
			fn pdf(&self, x:T) -> f64;

			fn logpdf(&self, x: T) -> f64;

			fn sample(&self) -> T;
		}
	}
}<|endoftext|>


<commit_before>#![stable(feature = "core_hint", since = "1.27.0")]

//! Hints to compiler that affects how code should be emitted or optimized.

use crate::intrinsics;

/// Informs the compiler that this point in the code is not reachable, enabling
/// further optimizations.
///
/// # Safety
///
/// Reaching this function is completely *undefined behavior* (UB). In
/// particular, the compiler assumes that all UB must never happen, and
/// therefore will eliminate all branches that reach to a call to
/// `unreachable_unchecked()`.
///
/// Like all instances of UB, if this assumption turns out to be wrong, i.e., the
/// `unreachable_unchecked()` call is actually reachable among all possible
/// control flow, the compiler will apply the wrong optimization strategy, and
/// may sometimes even corrupt seemingly unrelated code, causing
/// difficult-to-debug problems.
///
/// Use this function only when you can prove that the code will never call it.
/// Otherwise, consider using the [`unreachable!`] macro, which does not allow
/// optimizations but will panic when executed.
///
/// [`unreachable!`]: unreachable
///
/// # Example
///
/// ```
/// fn div_1(a: u32, b: u32) -> u32 {
///     use std::hint::unreachable_unchecked;
///
///     // `b.saturating_add(1)` is always positive (not zero),
///     // hence `checked_div` will never return `None`.
///     // Therefore, the else branch is unreachable.
///     a.checked_div(b.saturating_add(1))
///         .unwrap_or_else(|| unsafe { unreachable_unchecked() })
/// }
///
/// assert_eq!(div_1(7, 0), 7);
/// assert_eq!(div_1(9, 1), 4);
/// assert_eq!(div_1(11, u32::MAX), 0);
/// ```
#[inline]
#[stable(feature = "unreachable", since = "1.27.0")]
#[rustc_const_unstable(feature = "const_unreachable_unchecked", issue = "53188")]
pub const unsafe fn unreachable_unchecked() -> ! {
    // SAFETY: the safety contract for `intrinsics::unreachable` must
    // be upheld by the caller.
    unsafe { intrinsics::unreachable() }
}

/// Emits a machine instruction hinting to the processor that it is running in busy-wait
/// spin-loop ("spin lock").
///
/// For a discussion of different locking strategies and their trade-offs, see
/// [`core::sync::atomic::spin_loop_hint`].
///
/// **Note**: On platforms that do not support receiving spin-loop hints this function does not
/// do anything at all.
///
/// [`core::sync::atomic::spin_loop_hint`]: crate::sync::atomic::spin_loop_hint
#[inline]
#[unstable(feature = "renamed_spin_loop", issue = "55002")]
pub fn spin_loop() {
    #[cfg(all(any(target_arch = "x86", target_arch = "x86_64"), target_feature = "sse2"))]
    {
        #[cfg(target_arch = "x86")]
        {
            // SAFETY: the `cfg` attr ensures that we only execute this on x86 targets.
            unsafe { crate::arch::x86::_mm_pause() };
        }

        #[cfg(target_arch = "x86_64")]
        {
            // SAFETY: the `cfg` attr ensures that we only execute this on x86_64 targets.
            unsafe { crate::arch::x86_64::_mm_pause() };
        }
    }

    #[cfg(any(target_arch = "aarch64", all(target_arch = "arm", target_feature = "v6")))]
    {
        #[cfg(target_arch = "aarch64")]
        {
            // SAFETY: the `cfg` attr ensures that we only execute this on aarch64 targets.
            unsafe { crate::arch::aarch64::__yield() };
        }
        #[cfg(target_arch = "arm")]
        {
            // SAFETY: the `cfg` attr ensures that we only execute this on arm targets
            // with support for the v6 feature.
            unsafe { crate::arch::arm::__yield() };
        }
    }
}

/// An identity function that *__hints__* to the compiler to be maximally pessimistic about what
/// `black_box` could do.
///
/// [`std::convert::identity`]: crate::convert::identity
///
/// Unlike [`std::convert::identity`], a Rust compiler is encouraged to assume that `black_box` can
/// use `x` in any possible valid way that Rust code is allowed to without introducing undefined
/// behavior in the calling code. This property makes `black_box` useful for writing code in which
/// certain optimizations are not desired, such as benchmarks.
///
/// Note however, that `black_box` is only (and can only be) provided on a "best-effort" basis. The
/// extent to which it can block optimisations may vary depending upon the platform and code-gen
/// backend used. Programs cannot rely on `black_box` for *correctness* in any way.
#[inline]
#[unstable(feature = "test", issue = "50297")]
#[allow(unreachable_code)] // this makes #[cfg] a bit easier below.
pub fn black_box<T>(dummy: T) -> T {
    // We need to "use" the argument in some way LLVM can't introspect, and on
    // targets that support it we can typically leverage inline assembly to do
    // this. LLVM's interpretation of inline assembly is that it's, well, a black
    // box. This isn't the greatest implementation since it probably deoptimizes
    // more than we want, but it's so far good enough.

    #[cfg(not(miri))] // This is just a hint, so it is fine to skip in Miri.
    // SAFETY: the inline assembly is a no-op.
    unsafe {
        llvm_asm!("" : : "r"(&dummy));
    }

    dummy
}
<commit_msg>Revert broken link<commit_after>#![stable(feature = "core_hint", since = "1.27.0")]

//! Hints to compiler that affects how code should be emitted or optimized.

use crate::intrinsics;

/// Informs the compiler that this point in the code is not reachable, enabling
/// further optimizations.
///
/// # Safety
///
/// Reaching this function is completely *undefined behavior* (UB). In
/// particular, the compiler assumes that all UB must never happen, and
/// therefore will eliminate all branches that reach to a call to
/// `unreachable_unchecked()`.
///
/// Like all instances of UB, if this assumption turns out to be wrong, i.e., the
/// `unreachable_unchecked()` call is actually reachable among all possible
/// control flow, the compiler will apply the wrong optimization strategy, and
/// may sometimes even corrupt seemingly unrelated code, causing
/// difficult-to-debug problems.
///
/// Use this function only when you can prove that the code will never call it.
/// Otherwise, consider using the [`unreachable!`] macro, which does not allow
/// optimizations but will panic when executed.
///
/// [`unreachable!`]: unreachable
///
/// # Example
///
/// ```
/// fn div_1(a: u32, b: u32) -> u32 {
///     use std::hint::unreachable_unchecked;
///
///     // `b.saturating_add(1)` is always positive (not zero),
///     // hence `checked_div` will never return `None`.
///     // Therefore, the else branch is unreachable.
///     a.checked_div(b.saturating_add(1))
///         .unwrap_or_else(|| unsafe { unreachable_unchecked() })
/// }
///
/// assert_eq!(div_1(7, 0), 7);
/// assert_eq!(div_1(9, 1), 4);
/// assert_eq!(div_1(11, u32::MAX), 0);
/// ```
#[inline]
#[stable(feature = "unreachable", since = "1.27.0")]
#[rustc_const_unstable(feature = "const_unreachable_unchecked", issue = "53188")]
pub const unsafe fn unreachable_unchecked() -> ! {
    // SAFETY: the safety contract for `intrinsics::unreachable` must
    // be upheld by the caller.
    unsafe { intrinsics::unreachable() }
}

/// Emits a machine instruction hinting to the processor that it is running in busy-wait
/// spin-loop ("spin lock").
///
/// For a discussion of different locking strategies and their trade-offs, see
/// [`core::sync::atomic::spin_loop_hint`].
///
/// **Note**: On platforms that do not support receiving spin-loop hints this function does not
/// do anything at all.
///
/// [`core::sync::atomic::spin_loop_hint`]: crate::sync::atomic::spin_loop_hint
#[inline]
#[unstable(feature = "renamed_spin_loop", issue = "55002")]
pub fn spin_loop() {
    #[cfg(all(any(target_arch = "x86", target_arch = "x86_64"), target_feature = "sse2"))]
    {
        #[cfg(target_arch = "x86")]
        {
            // SAFETY: the `cfg` attr ensures that we only execute this on x86 targets.
            unsafe { crate::arch::x86::_mm_pause() };
        }

        #[cfg(target_arch = "x86_64")]
        {
            // SAFETY: the `cfg` attr ensures that we only execute this on x86_64 targets.
            unsafe { crate::arch::x86_64::_mm_pause() };
        }
    }

    #[cfg(any(target_arch = "aarch64", all(target_arch = "arm", target_feature = "v6")))]
    {
        #[cfg(target_arch = "aarch64")]
        {
            // SAFETY: the `cfg` attr ensures that we only execute this on aarch64 targets.
            unsafe { crate::arch::aarch64::__yield() };
        }
        #[cfg(target_arch = "arm")]
        {
            // SAFETY: the `cfg` attr ensures that we only execute this on arm targets
            // with support for the v6 feature.
            unsafe { crate::arch::arm::__yield() };
        }
    }
}

/// An identity function that *__hints__* to the compiler to be maximally pessimistic about what
/// `black_box` could do.
///
/// [`std::convert::identity`]: https://doc.rust-lang.org/core/convert/fn.identity.html
///
/// Unlike [`std::convert::identity`], a Rust compiler is encouraged to assume that `black_box` can
/// use `x` in any possible valid way that Rust code is allowed to without introducing undefined
/// behavior in the calling code. This property makes `black_box` useful for writing code in which
/// certain optimizations are not desired, such as benchmarks.
///
/// Note however, that `black_box` is only (and can only be) provided on a "best-effort" basis. The
/// extent to which it can block optimisations may vary depending upon the platform and code-gen
/// backend used. Programs cannot rely on `black_box` for *correctness* in any way.
#[inline]
#[unstable(feature = "test", issue = "50297")]
#[allow(unreachable_code)] // this makes #[cfg] a bit easier below.
pub fn black_box<T>(dummy: T) -> T {
    // We need to "use" the argument in some way LLVM can't introspect, and on
    // targets that support it we can typically leverage inline assembly to do
    // this. LLVM's interpretation of inline assembly is that it's, well, a black
    // box. This isn't the greatest implementation since it probably deoptimizes
    // more than we want, but it's so far good enough.

    #[cfg(not(miri))] // This is just a hint, so it is fine to skip in Miri.
    // SAFETY: the inline assembly is a no-op.
    unsafe {
        llvm_asm!("" : : "r"(&dummy));
    }

    dummy
}
<|endoftext|>


<commit_before>use slack_api as slack;

use std::env;

fn get_setup(
) -> Result<(String, impl slack::requests::SlackWebRequestSender), Box<dyn std::error::Error>> {
    // You can generate a legacy token to quickly test these apis
    // https://api.slack.com/custom-integrations/legacy-tokens
    let token = env::var("SLACK_API_TOKEN").map_err(|_| "SLACK_API_TOKEN env var must be set")?;
    let client = slack::default_client().map_err(|_| "Could not get default_client")?;
    Ok((token, client))
}

#[test]
#[ignore]
fn smoke_test() -> Result<(), Box<dyn std::error::Error>> {
    let (_, client) = get_setup()?;

    let resp = slack::api::test(
        &client,
        &slack::api::TestRequest {
            error: Some("my_error"),
            foo: Some("it's me, foo"),
        },
    )
    .err()
    .ok_or("Expected error")?;

    assert_eq!(format!("{:?}", resp), "Unknown(\"my_error\")");
    Ok(())
}

#[test]
#[ignore]
fn smoke_channels() -> Result<(), Box<dyn std::error::Error>> {
    let (token, client) = get_setup()?;

    let all_channels = slack::channels::list(
        &client,
        &token,
        &slack::channels::ListRequest {
            ..slack::channels::ListRequest::default()
        },
    )?
    .channels
    .ok_or("Expected some channels")?;

    assert!(all_channels.len() > 0);

    for channel in &all_channels[..10] {
        let channel_id = channel.id.as_ref().ok_or("expected channel id")?;

        let _channel_info = slack::channels::info(
            &client,
            &token,
            &slack::channels::InfoRequest {
                channel: &channel_id,
                ..Default::default()
            },
        )?
        .channel
        .ok_or("Expected some channel")?;

        let _channel_history = slack::channels::history(
            &client,
            &token,
            &slack::channels::HistoryRequest {
                channel: &channel_id,
                oldest: Some(1234567890.1234.into()),
                count: Some(1),
                ..Default::default()
            },
        )?
        .messages
        .ok_or("Expected some messages")?;
    }

    Ok(())
}
<commit_msg>log channel name being historied<commit_after>use slack_api as slack;

use std::env;

fn get_setup(
) -> Result<(String, impl slack::requests::SlackWebRequestSender), Box<dyn std::error::Error>> {
    // You can generate a legacy token to quickly test these apis
    // https://api.slack.com/custom-integrations/legacy-tokens
    let token = env::var("SLACK_API_TOKEN").map_err(|_| "SLACK_API_TOKEN env var must be set")?;
    let client = slack::default_client().map_err(|_| "Could not get default_client")?;
    Ok((token, client))
}

#[test]
#[ignore]
fn smoke_test() -> Result<(), Box<dyn std::error::Error>> {
    let (_, client) = get_setup()?;

    let resp = slack::api::test(
        &client,
        &slack::api::TestRequest {
            error: Some("my_error"),
            foo: Some("it's me, foo"),
        },
    )
    .err()
    .ok_or("Expected error")?;

    assert_eq!(format!("{:?}", resp), "Unknown(\"my_error\")");
    Ok(())
}

#[test]
#[ignore]
fn smoke_channels() -> Result<(), Box<dyn std::error::Error>> {
    let (token, client) = get_setup()?;

    let all_channels = slack::channels::list(
        &client,
        &token,
        &slack::channels::ListRequest {
            ..slack::channels::ListRequest::default()
        },
    )?
    .channels
    .ok_or("Expected some channels")?;

    assert!(all_channels.len() > 0);

    for channel in &all_channels[..10] {
        let channel_id = channel.id.as_ref().ok_or("expected channel id")?;

        let channel_info = slack::channels::info(
            &client,
            &token,
            &slack::channels::InfoRequest {
                channel: &channel_id,
                ..Default::default()
            },
        )?
        .channel
        .ok_or("Expected some channel")?;

        dbg!(channel_info.name);

        let _channel_history = slack::channels::history(
            &client,
            &token,
            &slack::channels::HistoryRequest {
                channel: &channel_id,
                oldest: Some(1234567890.1234.into()),
                count: Some(1),
                ..Default::default()
            },
        )?
        .messages
        .ok_or("Expected some messages")?;
    }

    Ok(())
}
<|endoftext|>


<commit_before>// Copyright 2016 Serde YAML Developers
//
// 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.

#![cfg_attr(feature = "cargo-clippy", allow(unreadable_literal))]

#[macro_use]
extern crate serde_derive;

extern crate serde;
extern crate serde_yaml;

extern crate unindent;
use unindent::unindent;

use std::f64;
use std::fmt::Debug;
use std::collections::BTreeMap;

fn test_serde<T>(thing: &T, yaml: &str)
    where T: serde::Serialize + serde::de::DeserializeOwned + PartialEq + Debug
{
    let serialized = serde_yaml::to_string(&thing).unwrap();
    assert_eq!(yaml, serialized);

    let deserialized: T = serde_yaml::from_str(yaml).unwrap();
    assert_eq!(*thing, deserialized);
}

#[test]
fn test_int() {
    let thing = 256;
    let yaml = unindent("
        ---
        256");
    test_serde(&thing, &yaml);
}

#[test]
fn test_int_max_u64() {
    let thing = ::std::u64::MAX;
    let yaml = unindent("
        ---
        18446744073709551615");
    test_serde(&thing, &yaml);
}

#[test]
fn test_int_min_i64() {
    let thing = ::std::i64::MIN;
    let yaml = unindent("
        ---
        -9223372036854775808");
    test_serde(&thing, &yaml);
}

#[test]
fn test_int_max_i64() {
    let thing = ::std::i64::MAX;
    let yaml = unindent("
        ---
        9223372036854775807");
    test_serde(&thing, &yaml);
}

#[test]
fn test_float() {
    let thing = 25.6;
    let yaml = unindent("
        ---
        25.6");
    test_serde(&thing, &yaml);

    let thing = 25.;
    let yaml = unindent("
        ---
        25.0");
    test_serde(&thing, &yaml);

    let thing = f64::INFINITY;
    let yaml = unindent("
        ---
        .inf");
    test_serde(&thing, &yaml);

    let thing = f64::NEG_INFINITY;
    let yaml = unindent("
        ---
        -.inf");
    test_serde(&thing, &yaml);

    let float: f64 = serde_yaml::from_str(&unindent("
        ---
        .nan")).unwrap();
    assert!(float.is_nan());
}

#[test]
fn test_vec() {
    let thing = vec![1, 2, 3];
    let yaml = unindent("
        ---
        - 1
        - 2
        - 3");
    test_serde(&thing, &yaml);
}

#[test]
fn test_map() {
    let mut thing = BTreeMap::new();
    thing.insert(String::from("x"), 1);
    thing.insert(String::from("y"), 2);
    let yaml = unindent(r#"
        ---
        x: 1
        "y": 2"#);
    test_serde(&thing, &yaml);
}

#[test]
fn test_basic_struct() {
    #[derive(Serialize, Deserialize, PartialEq, Debug)]
    struct Basic {
        x: isize,
        y: String,
        z: bool,
    }
    let thing = Basic {
        x: -4,
        y: String::from("hi\tquoted"),
        z: true,
    };
    let yaml = unindent(r#"
        ---
        x: -4
        "y": "hi\tquoted"
        z: true"#);
    test_serde(&thing, &yaml);
}

#[test]
fn test_nested_vec() {
    let thing = vec![vec![1, 2, 3], vec![4, 5, 6]];
    let yaml = unindent("
        ---
        - - 1
          - 2
          - 3
        - - 4
          - 5
          - 6");
    test_serde(&thing, &yaml);
}

#[test]
fn test_nested_struct() {
    #[derive(Serialize, Deserialize, PartialEq, Debug)]
    struct Outer {
        inner: Inner,
    }
    #[derive(Serialize, Deserialize, PartialEq, Debug)]
    struct Inner {
        v: u16,
    }
    let thing = Outer { inner: Inner { v: 512 } };
    let yaml = unindent(r#"
        ---
        inner:
          v: 512"#);
    test_serde(&thing, &yaml);
}

#[test]
fn test_option() {
    let thing = vec![Some(1), None, Some(3)];
    let yaml = unindent("
        ---
        - 1
        - ~
        - 3");
    test_serde(&thing, &yaml);
}

#[test]
fn test_unit() {
    let thing = vec![(), ()];
    let yaml = unindent("
        ---
        - ~
        - ~");
    test_serde(&thing, &yaml);
}

#[test]
fn test_unit_variant() {
    #[derive(Serialize, Deserialize, PartialEq, Debug)]
    enum Variant {
        First,
        Second,
    }
    let thing = Variant::First;
    let yaml = unindent(r#"
        ---
        First"#);
    test_serde(&thing, &yaml);
}

#[test]
fn test_newtype_struct() {
    #[derive(Serialize, Deserialize, PartialEq, Debug)]
    struct OriginalType {
        v: u16,
    }
    #[derive(Serialize, Deserialize, PartialEq, Debug)]
    struct NewType(OriginalType);
    let thing = NewType(OriginalType { v: 1 });
    let yaml = unindent(r#"
        ---
        v: 1"#);
    test_serde(&thing, &yaml);
}

#[test]
fn test_newtype_variant() {
    #[derive(Serialize, Deserialize, PartialEq, Debug)]
    enum Variant {
        Size(usize),
    }
    let thing = Variant::Size(127);
    let yaml = unindent(r#"
        ---
        Size: 127"#);
    test_serde(&thing, &yaml);
}

#[test]
fn test_tuple_variant() {
    #[derive(Serialize, Deserialize, PartialEq, Debug)]
    enum Variant {
        Rgb(u8, u8, u8),
    }
    let thing = Variant::Rgb(32, 64, 96);
    let yaml = unindent(r#"
        ---
        Rgb:
          - 32
          - 64
          - 96"#);
    test_serde(&thing, &yaml);
}

#[test]
fn test_struct_variant() {
    #[derive(Serialize, Deserialize, PartialEq, Debug)]
    enum Variant {
        Color { r: u8, g: u8, b: u8 },
    }
    let thing = Variant::Color {
        r: 32,
        g: 64,
        b: 96,
    };
    let yaml = unindent(r#"
        ---
        Color:
          r: 32
          g: 64
          b: 96"#);
    test_serde(&thing, &yaml);
}

#[test]
fn test_value() {
    use serde_yaml::{Mapping, Value, Number};

    #[derive(Serialize, Deserialize, PartialEq, Debug)]
    pub struct GenericInstructions {
        #[serde(rename="type")]
        pub typ: String,
        pub config: Value,
    }
    let thing = GenericInstructions {
        typ: "primary".to_string(),
        config: Value::Sequence(vec![Value::Null,
                                     Value::Bool(true),
                                     Value::Number(Number::from(65535)),
                                     Value::Number(Number::from(0.54321)),
                                     Value::String("s".into()),
                                     Value::Mapping(Mapping::new())]),
    };
    let yaml = unindent(r#"
        ---
        type: primary
        config:
          - ~
          - true
          - 65535
          - 0.54321
          - s
          - {}"#);
    test_serde(&thing, &yaml);
}

#[test]
fn test_mapping() {
    use serde_yaml::{Mapping, Value};
    #[derive(Serialize, Deserialize, PartialEq, Debug)]
    struct Data {
        pub substructure: Mapping,
    }

    let mut thing = Data { substructure: Mapping::new() };
    thing.substructure.insert(Value::String("a".to_owned()),
                              Value::String("foo".to_owned()));
    thing.substructure.insert(Value::String("b".to_owned()),
                              Value::String("bar".to_owned()));

    let yaml = unindent("
        ---
        substructure:
          a: foo
          b: bar");

    test_serde(&thing, &yaml);
}
<commit_msg>Ignore decimal_literal_representation lint<commit_after>// Copyright 2016 Serde YAML Developers
//
// 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.

#![cfg_attr(feature = "cargo-clippy", allow(unreadable_literal, decimal_literal_representation))]

#[macro_use]
extern crate serde_derive;

extern crate serde;
extern crate serde_yaml;

extern crate unindent;
use unindent::unindent;

use std::f64;
use std::fmt::Debug;
use std::collections::BTreeMap;

fn test_serde<T>(thing: &T, yaml: &str)
    where T: serde::Serialize + serde::de::DeserializeOwned + PartialEq + Debug
{
    let serialized = serde_yaml::to_string(&thing).unwrap();
    assert_eq!(yaml, serialized);

    let deserialized: T = serde_yaml::from_str(yaml).unwrap();
    assert_eq!(*thing, deserialized);
}

#[test]
fn test_int() {
    let thing = 256;
    let yaml = unindent("
        ---
        256");
    test_serde(&thing, &yaml);
}

#[test]
fn test_int_max_u64() {
    let thing = ::std::u64::MAX;
    let yaml = unindent("
        ---
        18446744073709551615");
    test_serde(&thing, &yaml);
}

#[test]
fn test_int_min_i64() {
    let thing = ::std::i64::MIN;
    let yaml = unindent("
        ---
        -9223372036854775808");
    test_serde(&thing, &yaml);
}

#[test]
fn test_int_max_i64() {
    let thing = ::std::i64::MAX;
    let yaml = unindent("
        ---
        9223372036854775807");
    test_serde(&thing, &yaml);
}

#[test]
fn test_float() {
    let thing = 25.6;
    let yaml = unindent("
        ---
        25.6");
    test_serde(&thing, &yaml);

    let thing = 25.;
    let yaml = unindent("
        ---
        25.0");
    test_serde(&thing, &yaml);

    let thing = f64::INFINITY;
    let yaml = unindent("
        ---
        .inf");
    test_serde(&thing, &yaml);

    let thing = f64::NEG_INFINITY;
    let yaml = unindent("
        ---
        -.inf");
    test_serde(&thing, &yaml);

    let float: f64 = serde_yaml::from_str(&unindent("
        ---
        .nan")).unwrap();
    assert!(float.is_nan());
}

#[test]
fn test_vec() {
    let thing = vec![1, 2, 3];
    let yaml = unindent("
        ---
        - 1
        - 2
        - 3");
    test_serde(&thing, &yaml);
}

#[test]
fn test_map() {
    let mut thing = BTreeMap::new();
    thing.insert(String::from("x"), 1);
    thing.insert(String::from("y"), 2);
    let yaml = unindent(r#"
        ---
        x: 1
        "y": 2"#);
    test_serde(&thing, &yaml);
}

#[test]
fn test_basic_struct() {
    #[derive(Serialize, Deserialize, PartialEq, Debug)]
    struct Basic {
        x: isize,
        y: String,
        z: bool,
    }
    let thing = Basic {
        x: -4,
        y: String::from("hi\tquoted"),
        z: true,
    };
    let yaml = unindent(r#"
        ---
        x: -4
        "y": "hi\tquoted"
        z: true"#);
    test_serde(&thing, &yaml);
}

#[test]
fn test_nested_vec() {
    let thing = vec![vec![1, 2, 3], vec![4, 5, 6]];
    let yaml = unindent("
        ---
        - - 1
          - 2
          - 3
        - - 4
          - 5
          - 6");
    test_serde(&thing, &yaml);
}

#[test]
fn test_nested_struct() {
    #[derive(Serialize, Deserialize, PartialEq, Debug)]
    struct Outer {
        inner: Inner,
    }
    #[derive(Serialize, Deserialize, PartialEq, Debug)]
    struct Inner {
        v: u16,
    }
    let thing = Outer { inner: Inner { v: 512 } };
    let yaml = unindent(r#"
        ---
        inner:
          v: 512"#);
    test_serde(&thing, &yaml);
}

#[test]
fn test_option() {
    let thing = vec![Some(1), None, Some(3)];
    let yaml = unindent("
        ---
        - 1
        - ~
        - 3");
    test_serde(&thing, &yaml);
}

#[test]
fn test_unit() {
    let thing = vec![(), ()];
    let yaml = unindent("
        ---
        - ~
        - ~");
    test_serde(&thing, &yaml);
}

#[test]
fn test_unit_variant() {
    #[derive(Serialize, Deserialize, PartialEq, Debug)]
    enum Variant {
        First,
        Second,
    }
    let thing = Variant::First;
    let yaml = unindent(r#"
        ---
        First"#);
    test_serde(&thing, &yaml);
}

#[test]
fn test_newtype_struct() {
    #[derive(Serialize, Deserialize, PartialEq, Debug)]
    struct OriginalType {
        v: u16,
    }
    #[derive(Serialize, Deserialize, PartialEq, Debug)]
    struct NewType(OriginalType);
    let thing = NewType(OriginalType { v: 1 });
    let yaml = unindent(r#"
        ---
        v: 1"#);
    test_serde(&thing, &yaml);
}

#[test]
fn test_newtype_variant() {
    #[derive(Serialize, Deserialize, PartialEq, Debug)]
    enum Variant {
        Size(usize),
    }
    let thing = Variant::Size(127);
    let yaml = unindent(r#"
        ---
        Size: 127"#);
    test_serde(&thing, &yaml);
}

#[test]
fn test_tuple_variant() {
    #[derive(Serialize, Deserialize, PartialEq, Debug)]
    enum Variant {
        Rgb(u8, u8, u8),
    }
    let thing = Variant::Rgb(32, 64, 96);
    let yaml = unindent(r#"
        ---
        Rgb:
          - 32
          - 64
          - 96"#);
    test_serde(&thing, &yaml);
}

#[test]
fn test_struct_variant() {
    #[derive(Serialize, Deserialize, PartialEq, Debug)]
    enum Variant {
        Color { r: u8, g: u8, b: u8 },
    }
    let thing = Variant::Color {
        r: 32,
        g: 64,
        b: 96,
    };
    let yaml = unindent(r#"
        ---
        Color:
          r: 32
          g: 64
          b: 96"#);
    test_serde(&thing, &yaml);
}

#[test]
fn test_value() {
    use serde_yaml::{Mapping, Value, Number};

    #[derive(Serialize, Deserialize, PartialEq, Debug)]
    pub struct GenericInstructions {
        #[serde(rename="type")]
        pub typ: String,
        pub config: Value,
    }
    let thing = GenericInstructions {
        typ: "primary".to_string(),
        config: Value::Sequence(vec![Value::Null,
                                     Value::Bool(true),
                                     Value::Number(Number::from(65535)),
                                     Value::Number(Number::from(0.54321)),
                                     Value::String("s".into()),
                                     Value::Mapping(Mapping::new())]),
    };
    let yaml = unindent(r#"
        ---
        type: primary
        config:
          - ~
          - true
          - 65535
          - 0.54321
          - s
          - {}"#);
    test_serde(&thing, &yaml);
}

#[test]
fn test_mapping() {
    use serde_yaml::{Mapping, Value};
    #[derive(Serialize, Deserialize, PartialEq, Debug)]
    struct Data {
        pub substructure: Mapping,
    }

    let mut thing = Data { substructure: Mapping::new() };
    thing.substructure.insert(Value::String("a".to_owned()),
                              Value::String("foo".to_owned()));
    thing.substructure.insert(Value::String("b".to_owned()),
                              Value::String("bar".to_owned()));

    let yaml = unindent("
        ---
        substructure:
          a: foo
          b: bar");

    test_serde(&thing, &yaml);
}
<|endoftext|>


<commit_before><commit_msg>Slab collision.<commit_after><|endoftext|>


<commit_before><commit_msg>concise filtering<commit_after><|endoftext|>


<commit_before><commit_msg>rm dead code in lsm storage<commit_after><|endoftext|>


<commit_before>#![feature(macro_rules)]
#![feature(phase)]

#![unstable]

#[phase(plugin)]
extern crate compile_msg;

extern crate libc;
extern crate native;

use std::sync::Mutex;

#[doc(hidden)]
pub mod ffi;

/// This static variable  will store the android_app* on creation, and set it back to 0 at
///  destruction.
/// Apart from this, the static is never written, so there is no risk of race condition.
static mut ANDROID_APP: *mut ffi::android_app = 0 as *mut ffi::android_app;

/// This is the structure that serves as user data in the android_app*
#[doc(hidden)]
struct Context {
    senders: Mutex<Vec<Sender<Event>>>,
}

/// An event triggered by the Android environment.
pub enum Event {

}

#[cfg(not(target_os = "android"))]
compile_note!("You are not compiling for Android")

#[macro_export]
macro_rules! android_start(
    ($main: ident) => (
        pub mod __android_start {
            extern crate android_glue;

            // this function is here because we are sure that it will be included by the linker
            // so we call app_dummy in it, in order to be sure that the native glue will be included
            #[start]
            pub fn start(_: int, _: *const *const u8) -> int {
                unsafe { android_glue::ffi::app_dummy() };
                1
            }

            #[no_mangle]
            #[inline(never)]
            #[allow(non_snake_case)]
            pub extern "C" fn android_main(app: *mut ()) {
                android_glue::android_main2(app, proc() super::$main());
            }
        }
    )
)

/// This is the function that must be called by `android_main`
#[doc(hidden)]
pub fn android_main2(app: *mut (), main_function: proc(): Send) {
    use native::NativeTaskBuilder;
    use std::task::TaskBuilder;
    use std::{mem, ptr};

    write_log("Entering android_main");

    unsafe { ANDROID_APP = std::mem::transmute(app) };
    let app: &mut ffi::android_app = unsafe { std::mem::transmute(app) };

    // starting the runtime
    native::start(1, &b"".as_ptr(), proc() {
        // creating the context that will be passed to the callback
        let context = Context { senders: Mutex::new(Vec::new()) };
        app.onAppCmd = commands_callback;
        app.onInputEvent = inputs_callback;

        // executing the main function in parallel
        TaskBuilder::new().native().spawn(main_function);

        // polling for events forever
        // note that this must be done in the same thread as android_main because ALooper are
        //  thread-local
        unsafe {
            loop {
                let mut events = mem::uninitialized();
                let mut source = mem::uninitialized();

                // passing -1 means that we are blocking
                let ident = ffi::ALooper_pollAll(-1, ptr::mut_null(), &mut events,
                    &mut source);

                // processing the event
                if !source.is_null() {
                    let source: *mut ffi::android_poll_source = mem::transmute(source);
                    ((*source).process)(ANDROID_APP, source);
                }
            }
        }
    });

    // terminating the application
    unsafe { ANDROID_APP = 0 as *mut ffi::android_app };
}

/// The callback for inputs.
pub extern fn inputs_callback(_: *mut ffi::android_app, event: *const ffi::AInputEvent)
    -> libc::int32_t
{
    let context = get_context();
    0
}

/// The callback for commands.
#[doc(hidden)]
pub extern fn commands_callback(_: *mut ffi::android_app, command: libc::int32_t) {
    let context = get_context();

    match command {
        ffi::APP_CMD_INIT_WINDOW => {

        },

        ffi::APP_CMD_SAVE_STATE => {

        },

        ffi::APP_CMD_TERM_WINDOW => {

        },

        ffi::APP_CMD_GAINED_FOCUS => {

        },

        ffi::APP_CMD_LOST_FOCUS => {

        },

        _ => ()
    }
}

/// Returns the current Context
#[doc(hidden)]
pub fn get_context() -> &'static Context {
    let context = unsafe { (*ANDROID_APP).userData };
    unsafe { std::mem::transmute(context) }
}

/// Adds a sender where events will be sent to.
pub fn add_sender(sender: Sender<Event>) {
    get_context().senders.lock().push(sender);
}

/// Returns a handle to the native window.
pub unsafe fn get_native_window() -> ffi::NativeWindowType {
    if ANDROID_APP.is_null() {
        fail!("The application was not initialized from android_main");
    }

    loop {
        let value = (*ANDROID_APP).window;
        if !value.is_null() {
            return value;
        }

        // spin-locking
        std::io::timer::sleep(std::time::Duration::milliseconds(10));
    }
}

///
pub fn write_log(message: &str) {
    message.with_c_str(|message| {
        b"RustAndroidGlue".with_c_str(|tag| {
            unsafe { ffi::__android_log_write(3, tag, message) };
        });
    });
}
<commit_msg>Fix get_context being public<commit_after>#![feature(macro_rules)]
#![feature(phase)]

#![unstable]

#[phase(plugin)]
extern crate compile_msg;

extern crate libc;
extern crate native;

use std::sync::Mutex;

#[doc(hidden)]
pub mod ffi;

/// This static variable  will store the android_app* on creation, and set it back to 0 at
///  destruction.
/// Apart from this, the static is never written, so there is no risk of race condition.
static mut ANDROID_APP: *mut ffi::android_app = 0 as *mut ffi::android_app;

/// This is the structure that serves as user data in the android_app*
#[doc(hidden)]
struct Context {
    senders: Mutex<Vec<Sender<Event>>>,
}

/// An event triggered by the Android environment.
pub enum Event {

}

#[cfg(not(target_os = "android"))]
compile_note!("You are not compiling for Android")

#[macro_export]
macro_rules! android_start(
    ($main: ident) => (
        pub mod __android_start {
            extern crate android_glue;

            // this function is here because we are sure that it will be included by the linker
            // so we call app_dummy in it, in order to be sure that the native glue will be included
            #[start]
            pub fn start(_: int, _: *const *const u8) -> int {
                unsafe { android_glue::ffi::app_dummy() };
                1
            }

            #[no_mangle]
            #[inline(never)]
            #[allow(non_snake_case)]
            pub extern "C" fn android_main(app: *mut ()) {
                android_glue::android_main2(app, proc() super::$main());
            }
        }
    )
)

/// This is the function that must be called by `android_main`
#[doc(hidden)]
pub fn android_main2(app: *mut (), main_function: proc(): Send) {
    use native::NativeTaskBuilder;
    use std::task::TaskBuilder;
    use std::{mem, ptr};

    write_log("Entering android_main");

    unsafe { ANDROID_APP = std::mem::transmute(app) };
    let app: &mut ffi::android_app = unsafe { std::mem::transmute(app) };

    // starting the runtime
    native::start(1, &b"".as_ptr(), proc() {
        // creating the context that will be passed to the callback
        let context = Context { senders: Mutex::new(Vec::new()) };
        app.onAppCmd = commands_callback;
        app.onInputEvent = inputs_callback;

        // executing the main function in parallel
        TaskBuilder::new().native().spawn(main_function);

        // polling for events forever
        // note that this must be done in the same thread as android_main because ALooper are
        //  thread-local
        unsafe {
            loop {
                let mut events = mem::uninitialized();
                let mut source = mem::uninitialized();

                // passing -1 means that we are blocking
                let ident = ffi::ALooper_pollAll(-1, ptr::mut_null(), &mut events,
                    &mut source);

                // processing the event
                if !source.is_null() {
                    let source: *mut ffi::android_poll_source = mem::transmute(source);
                    ((*source).process)(ANDROID_APP, source);
                }
            }
        }
    });

    // terminating the application
    unsafe { ANDROID_APP = 0 as *mut ffi::android_app };
}

/// The callback for inputs.
pub extern fn inputs_callback(_: *mut ffi::android_app, event: *const ffi::AInputEvent)
    -> libc::int32_t
{
    let context = get_context();
    0
}

/// The callback for commands.
#[doc(hidden)]
pub extern fn commands_callback(_: *mut ffi::android_app, command: libc::int32_t) {
    let context = get_context();

    match command {
        ffi::APP_CMD_INIT_WINDOW => {

        },

        ffi::APP_CMD_SAVE_STATE => {

        },

        ffi::APP_CMD_TERM_WINDOW => {

        },

        ffi::APP_CMD_GAINED_FOCUS => {

        },

        ffi::APP_CMD_LOST_FOCUS => {

        },

        _ => ()
    }
}

/// Returns the current Context.
fn get_context() -> &'static Context {
    let context = unsafe { (*ANDROID_APP).userData };
    unsafe { std::mem::transmute(context) }
}

/// Adds a sender where events will be sent to.
pub fn add_sender(sender: Sender<Event>) {
    get_context().senders.lock().push(sender);
}

/// Returns a handle to the native window.
pub unsafe fn get_native_window() -> ffi::NativeWindowType {
    if ANDROID_APP.is_null() {
        fail!("The application was not initialized from android_main");
    }

    loop {
        let value = (*ANDROID_APP).window;
        if !value.is_null() {
            return value;
        }

        // spin-locking
        std::io::timer::sleep(std::time::Duration::milliseconds(10));
    }
}

///
pub fn write_log(message: &str) {
    message.with_c_str(|message| {
        b"RustAndroidGlue".with_c_str(|tag| {
            unsafe { ffi::__android_log_write(3, tag, message) };
        });
    });
}
<|endoftext|>


<commit_before>use std::io;
use std::path::{PathBuf, Path};
use std::fs::{read_dir, PathExt};
use std::borrow::ToOwned;
use std::iter::IteratorExt;
use std::cmp::Ordering;
use std::mem;

use super::super::iter_reduce::{Reduce, IteratorReduce};

pub struct FilesystemWalker<'a, T> {
    cur: Vec<(PathBuf, T)>,
    file_map: &'a Fn(&Path) -> io::Result<T>,
    sort_map: &'a Fn(&(PathBuf, T), &(PathBuf, T)) -> Ordering
}

// The idea here is that we serve the most recently changed files first
// because those are likely to be the most relevant.
impl<'a, T> Iterator for FilesystemWalker<'a, T> {
    type Item = io::Result<(PathBuf, T)>;

    fn next(&mut self) -> Option<io::Result<(PathBuf, T)>> {
        loop {
            match self.cur.pop() {
                Some((path, extra)) => {
                    if path.is_dir() {
                        if let Err(e) = self.read_dir_sorted(&path) {
                            return Some(Err(e));
                        }
                    }
                    else {
                        return Some(Ok((path, extra)));
                    }
                },
                None => return None
            }
        }
    }
}

impl<'a, T> FilesystemWalker<'a, T> {
    pub fn new<F, S>(dir: &Path, file_map: &'a F, sort_map: &'a S) -> io::Result<FilesystemWalker<'a, T>>
           where F: Fn(&Path) -> io::Result<T>,
                 S: Fn(&(PathBuf, T), &(PathBuf, T)) -> Ordering {
        let mut walker = FilesystemWalker {
            cur: Vec::new(),
            file_map: file_map,
            sort_map: sort_map
        };

        try!(walker.read_dir_sorted(dir));

        Ok(walker)
    }

    fn read_dir_sorted(&mut self, dir: &Path) -> io::Result<()> {
        try!(
            read_dir(dir)
            .and_then(|list| list
                .map(|possible_entry| {
                    possible_entry.and_then(|entry| {
                        let path = entry.path();

                        (*self.file_map)(&path)
                            .map(|extra| {
                                let pair = (path.to_owned(), extra);
                                self.cur.push(pair);
                            })
                    })
                })
                .reduce()
            )
        );

        let other_self: &FilesystemWalker<'a, T>  = unsafe { mem::transmute(&mut *self) };

        self.cur.sort_by(|a, b| (*other_self.sort_map)(a, b) );

        Ok(())
    }
}

pub struct NewestFirst {
    walker: FilesystemWalker<'a, u64>,
    file_map: Box<Fn(&Path) -> io::Result<u64> + 'a>,
    sort_map: Box<Fn(&(PathBuf, u64), &(PathBuf, u64)) -> Ordering + 'a>
}

//impl<'a> NewestFirst<'a> {
    //pub fn new(dir: &Path) -> io::Result<NewestFirst<'a> {

    //}
//}

static MODIFIED_DATE: &'static Fn(&Path) -> io::Result<u64>                     = &modified_date;
static NEWEST_FIRST:  &'static Fn(&(PathBuf, u64), &(PathBuf, u64)) -> Ordering = &newest_first;

pub fn newest_first_walker(dir: &Path) -> io::Result<FilesystemWalker<'static, u64>> {
    FilesystemWalker::new(dir, MODIFIED_DATE, NEWEST_FIRST)
}

fn modified_date(path: &Path) -> io::Result<u64> {
    path.metadata()
        .map(|stats| {
            stats.modified()
        })
}

fn newest_first(a: &(PathBuf, u64), b: &(PathBuf, u64)) -> Ordering {
    let &(_, time_a) = a;
    let &(_, time_b) = b;

    a.cmp(b)
}

#[cfg(test)]
mod test {
    use std::old_io::Timer;
    use std::time::Duration;
    use std::fs::create_dir_all;

    use super::super::super::tempdir::TempDir;
    use super::super::super::write_to_disk;

    #[test]
    fn read_dir() {
        let temp_dir = TempDir::new("readdir-test").unwrap();
        let root_path = temp_dir.path();
        let sub_dir = root_path.join("sub");

        create_dir_all(&sub_dir).unwrap();

        {
            let file_path = root_path.join("filezero");
            write_to_disk(&file_path, b"test123").unwrap();
        }

        Timer::new().unwrap().sleep(Duration::milliseconds(50));

        {
            let file_path = sub_dir.join("firstfile");
            write_to_disk(&file_path, b"yolo").unwrap();
        }

        Timer::new().unwrap().sleep(Duration::milliseconds(50));

        {
            let file_path = root_path.join("second");
            write_to_disk(&file_path, b"hello").unwrap();
        }

        Timer::new().unwrap().sleep(Duration::milliseconds(50));

        {
            let file_path = root_path.join("third");
            write_to_disk(&file_path, b"waddaa").unwrap();
        }

        Timer::new().unwrap().sleep(Duration::milliseconds(50));

        {
            let file_path = sub_dir.join("deadlast");
            write_to_disk(&file_path, b"plswork").unwrap();
        }

        let list = super::newest_first_walker(temp_dir.path()).unwrap();

        let filenames: Vec<String> = list
            .map(|x| {
                let (path, _) = x.unwrap();

                path.file_name().unwrap().to_string_lossy().into_owned()
            })
            .collect();

        assert_eq!(&["deadlast", "third", "second", "firstfile", "filezero"], &filenames);
    }
}
<commit_msg>fix implementation of newest_first_walker<commit_after>use std::io;
use std::path::{PathBuf, Path};
use std::fs::{read_dir, PathExt};
use std::borrow::ToOwned;
use std::iter::IteratorExt;
use std::cmp::Ordering;
use std::mem;

use super::super::iter_reduce::{Reduce, IteratorReduce};

pub struct FilesystemWalker<'a, T: 'static> {
    cur: Vec<(PathBuf, T)>,
    file_map: &'a Fn(&Path) -> io::Result<T>,
    sort_map: &'a Fn(&(PathBuf, T), &(PathBuf, T)) -> Ordering
}

// The idea here is that we serve the most recently changed files first
// because those are likely to be the most relevant.
impl<'a, T> Iterator for FilesystemWalker<'a, T> {
    type Item = io::Result<(PathBuf, T)>;

    fn next(&mut self) -> Option<io::Result<(PathBuf, T)>> {
        loop {
            match self.cur.pop() {
                Some((path, extra)) => {
                    if path.is_dir() {
                        if let Err(e) = self.read_dir_sorted(&path) {
                            return Some(Err(e));
                        }
                    }
                    else {
                        return Some(Ok((path, extra)));
                    }
                },
                None => return None
            }
        }
    }
}

impl<'a, T> FilesystemWalker<'a, T> {
    pub fn new<F, S>(dir: &Path, file_map: &'a F, sort_map: &'a S) -> io::Result<FilesystemWalker<'a, T>>
           where F: Fn(&Path) -> io::Result<T>,
                 S: Fn(&(PathBuf, T), &(PathBuf, T)) -> Ordering {
        let mut walker = FilesystemWalker {
            cur: Vec::new(),
            file_map: file_map,
            sort_map: sort_map
        };

        try!(walker.read_dir_sorted(dir));

        Ok(walker)
    }

    fn read_dir_sorted(&mut self, dir: &Path) -> io::Result<()> {
        try!(
            read_dir(dir)
            .and_then(|list| list
                .map(|possible_entry| {
                    possible_entry.and_then(|entry| {
                        let path = entry.path();

                        (*self.file_map)(&path)
                            .map(|extra| {
                                let pair = (path.to_owned(), extra);
                                self.cur.push(pair);
                            })
                    })
                })
                .reduce()
            )
        );

        let other_self: &FilesystemWalker<'a, T>  = unsafe { mem::transmute(&mut *self) };

        self.cur.sort_by(|a, b| (*other_self.sort_map)(a, b) );

        Ok(())
    }
}

pub struct NewestFirst<'a> {
    walker: FilesystemWalker<'a, u64>,
    file_map: Box<Fn(&Path) -> io::Result<u64>>,
    sort_map: Box<Fn(&(PathBuf, u64), &(PathBuf, u64)) -> Ordering>
}

impl<'a> Iterator for NewestFirst<'a> {
    type Item = io::Result<(PathBuf, u64)>;

    fn next(&mut self) -> Option<io::Result<(PathBuf, u64)>> {
        self.walker.next()
    }
}

pub fn newest_first_walker(dir: &Path) -> io::Result<NewestFirst<'static>> {
    fn newest_first(a: &(PathBuf, u64), b: &(PathBuf, u64)) -> Ordering {
        let &(_, time_a) = a;
        let &(_, time_b) = b;

        time_a.cmp(&time_b)
    }

    fn modified_date(path: &Path) -> io::Result<u64> {
        path.metadata()
            .map(|stats| {
                stats.modified()
            })
    }

    let file_map = Box::new(modified_date);
    let sort_map = Box::new(newest_first);

    let walker: io::Result<FilesystemWalker<u64>> = FilesystemWalker::<u64>::new(
        dir,
        unsafe { mem::copy_lifetime("silly", &*file_map) },
        unsafe { mem::copy_lifetime("wadda", &*sort_map) }
    );

    Ok(NewestFirst {
        walker: try!(walker),
        file_map: file_map,
        sort_map: sort_map
    })
}

#[cfg(test)]
mod test {
    use std::old_io::Timer;
    use std::time::Duration;
    use std::fs::create_dir_all;

    use super::super::super::tempdir::TempDir;
    use super::super::super::write_to_disk;

    #[test]
    fn read_dir() {
        let temp_dir = TempDir::new("readdir-test").unwrap();
        let root_path = temp_dir.path();
        let sub_dir = root_path.join("sub");

        create_dir_all(&sub_dir).unwrap();

        {
            let file_path = root_path.join("filezero");
            write_to_disk(&file_path, b"test123").unwrap();
        }

        Timer::new().unwrap().sleep(Duration::milliseconds(50));

        {
            let file_path = sub_dir.join("firstfile");
            write_to_disk(&file_path, b"yolo").unwrap();
        }

        Timer::new().unwrap().sleep(Duration::milliseconds(50));

        {
            let file_path = root_path.join("second");
            write_to_disk(&file_path, b"hello").unwrap();
        }

        Timer::new().unwrap().sleep(Duration::milliseconds(50));

        {
            let file_path = root_path.join("third");
            write_to_disk(&file_path, b"waddaa").unwrap();
        }

        Timer::new().unwrap().sleep(Duration::milliseconds(50));

        {
            let file_path = sub_dir.join("deadlast");
            write_to_disk(&file_path, b"plswork").unwrap();
        }

        let list = super::newest_first_walker(temp_dir.path()).unwrap();

        let filenames: Vec<String> = list
            .map(|x| {
                let (path, _) = x.unwrap();

                path.file_name().unwrap().to_string_lossy().into_owned()
            })
            .collect();

        assert_eq!(&["deadlast", "third", "second", "firstfile", "filezero"], &filenames);
    }
}
<|endoftext|>


<commit_before>use crate::utils::{in_macro_or_desugar, snippet, span_lint_and_then};
use rustc::lint::{EarlyContext, EarlyLintPass, LintArray, LintPass};
use rustc::{declare_lint_pass, declare_tool_lint};
use rustc_errors::Applicability;
use syntax::ast::*;

declare_clippy_lint! {
    /// **What it does:** Checks for constants and statics with an explicit `'static` lifetime.
    ///
    /// **Why is this bad?** Adding `'static` to every reference can create very
    /// complicated types.
    ///
    /// **Known problems:** None.
    ///
    /// **Example:**
    /// ```ignore
    /// const FOO: &'static [(&'static str, &'static str, fn(&Bar) -> bool)] =
    /// &[...]
    /// static FOO: &'static [(&'static str, &'static str, fn(&Bar) -> bool)] =
    /// &[...]
    /// ```
    /// This code can be rewritten as
    /// ```ignore
    ///  const FOO: &[(&str, &str, fn(&Bar) -> bool)] = &[...]
    ///  static FOO: &[(&str, &str, fn(&Bar) -> bool)] = &[...]
    /// ```
    pub REDUNDANT_STATIC_LIFETIME,
    style,
    "Using explicit `'static` lifetime for constants or statics when elision rules would allow omitting them."
}

declare_lint_pass!(RedundantStaticLifetime => [REDUNDANT_STATIC_LIFETIME]);

impl RedundantStaticLifetime {
    // Recursively visit types
    pub fn visit_type(&mut self, ty: &Ty, cx: &EarlyContext<'_>, reason: &str) {
        match ty.node {
            // Be careful of nested structures (arrays and tuples)
            TyKind::Array(ref ty, _) => {
                self.visit_type(&*ty, cx, reason);
            },
            TyKind::Tup(ref tup) => {
                for tup_ty in tup {
                    self.visit_type(&*tup_ty, cx, reason);
                }
            },
            // This is what we are looking for !
            TyKind::Rptr(ref optional_lifetime, ref borrow_type) => {
                // Match the 'static lifetime
                if let Some(lifetime) = *optional_lifetime {
                    match borrow_type.ty.node {
                        TyKind::Path(..) | TyKind::Slice(..) | TyKind::Array(..) | TyKind::Tup(..) => {
                            if lifetime.ident.name == syntax::symbol::kw::StaticLifetime {
                                let snip = snippet(cx, borrow_type.ty.span, "<type>");
                                let sugg = format!("&{}", snip);
                                span_lint_and_then(cx, REDUNDANT_STATIC_LIFETIME, lifetime.ident.span, reason, |db| {
                                    db.span_suggestion(
                                        ty.span,
                                        "consider removing `'static`",
                                        sugg,
                                        Applicability::MachineApplicable, //snippet
                                    );
                                });
                            }
                        },
                        _ => {},
                    }
                }
                self.visit_type(&*borrow_type.ty, cx, reason);
            },
            TyKind::Slice(ref ty) => {
                self.visit_type(ty, cx, reason);
            },
            _ => {},
        }
    }
}

impl EarlyLintPass for RedundantStaticLifetime {
    fn check_item(&mut self, cx: &EarlyContext<'_>, item: &Item) {
        if !in_macro_or_desugar(item.span) {
            if let ItemKind::Const(ref var_type, _) = item.node {
                self.visit_type(var_type, cx, "Constants have by default a `'static` lifetime");
                // Don't check associated consts because `'static` cannot be elided on those (issue #2438)
            }

            if let ItemKind::Static(ref var_type, _, _) = item.node {
                self.visit_type(var_type, cx, "Statics have by default a `'static` lifetime");
            }
        }
    }
}
<commit_msg>Remove pub from RedundantStaticLifetime.visit_type function.<commit_after>use crate::utils::{in_macro_or_desugar, snippet, span_lint_and_then};
use rustc::lint::{EarlyContext, EarlyLintPass, LintArray, LintPass};
use rustc::{declare_lint_pass, declare_tool_lint};
use rustc_errors::Applicability;
use syntax::ast::*;

declare_clippy_lint! {
    /// **What it does:** Checks for constants and statics with an explicit `'static` lifetime.
    ///
    /// **Why is this bad?** Adding `'static` to every reference can create very
    /// complicated types.
    ///
    /// **Known problems:** None.
    ///
    /// **Example:**
    /// ```ignore
    /// const FOO: &'static [(&'static str, &'static str, fn(&Bar) -> bool)] =
    /// &[...]
    /// static FOO: &'static [(&'static str, &'static str, fn(&Bar) -> bool)] =
    /// &[...]
    /// ```
    /// This code can be rewritten as
    /// ```ignore
    ///  const FOO: &[(&str, &str, fn(&Bar) -> bool)] = &[...]
    ///  static FOO: &[(&str, &str, fn(&Bar) -> bool)] = &[...]
    /// ```
    pub REDUNDANT_STATIC_LIFETIME,
    style,
    "Using explicit `'static` lifetime for constants or statics when elision rules would allow omitting them."
}

declare_lint_pass!(RedundantStaticLifetime => [REDUNDANT_STATIC_LIFETIME]);

impl RedundantStaticLifetime {
    // Recursively visit types
    fn visit_type(&mut self, ty: &Ty, cx: &EarlyContext<'_>, reason: &str) {
        match ty.node {
            // Be careful of nested structures (arrays and tuples)
            TyKind::Array(ref ty, _) => {
                self.visit_type(&*ty, cx, reason);
            },
            TyKind::Tup(ref tup) => {
                for tup_ty in tup {
                    self.visit_type(&*tup_ty, cx, reason);
                }
            },
            // This is what we are looking for !
            TyKind::Rptr(ref optional_lifetime, ref borrow_type) => {
                // Match the 'static lifetime
                if let Some(lifetime) = *optional_lifetime {
                    match borrow_type.ty.node {
                        TyKind::Path(..) | TyKind::Slice(..) | TyKind::Array(..) | TyKind::Tup(..) => {
                            if lifetime.ident.name == syntax::symbol::kw::StaticLifetime {
                                let snip = snippet(cx, borrow_type.ty.span, "<type>");
                                let sugg = format!("&{}", snip);
                                span_lint_and_then(cx, REDUNDANT_STATIC_LIFETIME, lifetime.ident.span, reason, |db| {
                                    db.span_suggestion(
                                        ty.span,
                                        "consider removing `'static`",
                                        sugg,
                                        Applicability::MachineApplicable, //snippet
                                    );
                                });
                            }
                        },
                        _ => {},
                    }
                }
                self.visit_type(&*borrow_type.ty, cx, reason);
            },
            TyKind::Slice(ref ty) => {
                self.visit_type(ty, cx, reason);
            },
            _ => {},
        }
    }
}

impl EarlyLintPass for RedundantStaticLifetime {
    fn check_item(&mut self, cx: &EarlyContext<'_>, item: &Item) {
        if !in_macro_or_desugar(item.span) {
            if let ItemKind::Const(ref var_type, _) = item.node {
                self.visit_type(var_type, cx, "Constants have by default a `'static` lifetime");
                // Don't check associated consts because `'static` cannot be elided on those (issue #2438)
            }

            if let ItemKind::Static(ref var_type, _, _) = item.node {
                self.visit_type(var_type, cx, "Statics have by default a `'static` lifetime");
            }
        }
    }
}
<|endoftext|>


<commit_before>extern crate multipart;
extern crate iron;

use std::fs::File;
use std::io::{Read};
use multipart::server::{Multipart, Entries, SaveResult};
use iron::prelude::*;
use iron::status;

fn main(){
    Iron::new(process_request).http("localhost:80").expect("Could not bind localhost:80");
}

/// Processes a request and returns response or an occured error.
fn process_request(request: &mut Request) -> IronResult<Response> {
    // Getting a multipart reader wrapper
    match Multipart::from_request(request) {
        Ok(mut multipart) => {
            // Fetching all data and processing it.
            // save_all() reads the request fully, parsing all fields and saving all files
            // in a new temporary directory under the OS temporary directory.
            match multipart.save_all() {
                SaveResult::Full(entries) => process_entries(entries),
                SaveResult::Partial(entries, error) => {
                    try!(process_entries(entries));
                    Err(IronError::new(error, status::InternalServerError))
                }
                SaveResult::Error(error) => Err(IronError::new(error, status::InternalServerError)),
            }
        }
        Err(_) => Ok(Response::with((status::BadRequest, (status::BadRequest, "The request is not multipart")))),
    }
}

/// Processes saved entries from multipart request.
/// Returns an OK response or an error.
fn process_entries(entries: Entries) -> IronResult<Response> {
    for (name, field) in entries.fields {
        println!(r#"Field "{}": "{}""#, name, field);
    }

    for (name, savedfile) in entries.files {
        let filename = match savedfile.filename {
            Some(s) => s,
            None => "None".into(),
        };
        let mut file = match File::open(savedfile.path) {
            Ok(file) => file,
            Err(error) => return Err(IronError::new(error, (status::InternalServerError, "Server couldn't save file")))
        };
        let mut contents = String::new();
        if let Err(error) = file.read_to_string(&mut contents) {
            return Err(IronError::new(error, (status::BadRequest, "The file was not a text")))
        }

        println!(r#"Field "{}" is file "{}":"#, name, filename);
        println!("{}", contents);
    }
    Ok(Response::with((status::Ok, "Multipart data is processed")))
}
<commit_msg>Run cargo fmt<commit_after>extern crate multipart;
extern crate iron;

use std::fs::File;
use std::io::Read;
use multipart::server::{Multipart, Entries, SaveResult};
use iron::prelude::*;
use iron::status;

fn main() {
    Iron::new(process_request).http("localhost:80").expect("Could not bind localhost:80");
}

/// Processes a request and returns response or an occured error.
fn process_request(request: &mut Request) -> IronResult<Response> {
    // Getting a multipart reader wrapper
    match Multipart::from_request(request) {
        Ok(mut multipart) => {
            // Fetching all data and processing it.
            // save_all() reads the request fully, parsing all fields and saving all files
            // in a new temporary directory under the OS temporary directory.
            match multipart.save_all() {
                SaveResult::Full(entries) => process_entries(entries),
                SaveResult::Partial(entries, error) => {
                    try!(process_entries(entries));
                    Err(IronError::new(error, status::InternalServerError))
                }
                SaveResult::Error(error) => Err(IronError::new(error, status::InternalServerError)),
            }
        }
        Err(_) => {
            Ok(Response::with((status::BadRequest,
                               (status::BadRequest, "The request is not multipart"))))
        }
    }
}

/// Processes saved entries from multipart request.
/// Returns an OK response or an error.
fn process_entries(entries: Entries) -> IronResult<Response> {
    for (name, field) in entries.fields {
        println!(r#"Field "{}": "{}""#, name, field);
    }

    for (name, savedfile) in entries.files {
        let filename = match savedfile.filename {
            Some(s) => s,
            None => "None".into(),
        };
        let mut file = match File::open(savedfile.path) {
            Ok(file) => file,
            Err(error) => {
                return Err(IronError::new(error,
                                          (status::InternalServerError,
                                           "Server couldn't save file")))
            }
        };
        let mut contents = String::new();
        if let Err(error) = file.read_to_string(&mut contents) {
            return Err(IronError::new(error, (status::BadRequest, "The file was not a text")));
        }

        println!(r#"Field "{}" is file "{}":"#, name, filename);
        println!("{}", contents);
    }
    Ok(Response::with((status::Ok, "Multipart data is processed")))
}
<|endoftext|>