<commit_before>//! A multi-producer, single-consumer, futures-aware, FIFO queue with back
//! pressure, for use communicating between tasks on the same thread.

use std::fmt;
use std::error::Error;
use std::any::Any;
use std::collections::VecDeque;
use std::rc::{Rc, Weak};
use std::cell::RefCell;

use task::{self, Task};
use {Async, AsyncSink, Poll, StartSend, Sink, Stream};

/// Creates a bounded in-memory channel with buffered storage.
///
/// This method creates concrete implementations of the `Stream` and `Sink`
/// traits which can be used to communicate a stream of values between tasks
/// with backpressure. The channel capacity is exactly `buffer`. On average,
/// sending a message through this channel performs no dynamic allocation.
pub fn channel<T>(buffer: usize) -> (Sender<T>, Receiver<T>) { channel_(Some(buffer)) }

fn channel_<T>(buffer: Option<usize>) -> (Sender<T>, Receiver<T>) {
    let shared = Rc::new(RefCell::new(Shared {
        buffer: VecDeque::new(),
        capacity: buffer,
        blocked_senders: VecDeque::new(),
        blocked_recv: None,
    }));
    let sender = Sender { shared: Rc::downgrade(&shared) };
    let receiver = Receiver { shared: shared };
    (sender, receiver)
}

struct Shared<T> {
    buffer: VecDeque<T>,
    capacity: Option<usize>,
    blocked_senders: VecDeque<Task>,
    blocked_recv: Option<Task>,
}

/// The transmission end of a channel.
///
/// This is created by the `channel` function.
pub struct Sender<T> {
    shared: Weak<RefCell<Shared<T>>>,
}

impl<T> Drop for Sender<T> {
    fn drop(&mut self) {
        if let Some(task) = self.shared.upgrade().and_then(|shared| shared.borrow_mut().blocked_recv.take()) {
            // Wake up receiver to avoid deadlock in case this was the last sender
            task.unpark();
        }
    }
}

impl<T> Clone for Sender<T> {
    fn clone(&self) -> Self { Sender { shared: self.shared.clone() } }
}

impl<T> Sink for Sender<T> {
    type SinkItem = T;
    type SinkError = SendError<T>;

    fn start_send(&mut self, msg: T) -> StartSend<T, SendError<T>> {
        if let Some(shared) = self.shared.upgrade() {
            let mut shared = shared.borrow_mut();

            match shared.capacity {
                Some(capacity) if shared.buffer.len() == capacity => {
                    shared.blocked_senders.push_back(task::park());
                    Ok(AsyncSink::NotReady(msg))
                }
                _ => {
                    shared.buffer.push_back(msg);
                    if let Some(task) = shared.blocked_recv.take() {
                        task.unpark();
                    }
                    Ok(AsyncSink::Ready)
                }
            }
        } else {
            Err(SendError(msg))
        }
    }

    fn poll_complete(&mut self) -> Poll<(), SendError<T>> {
        Ok(Async::Ready(()))
    }
}

/// The receiving end of a channel which implements the `Stream` trait.
///
/// This is created by the `channel` function.
pub struct Receiver<T> {
    shared: Rc<RefCell<Shared<T>>>,
}

impl<T> Drop for Receiver<T> {
    fn drop(&mut self) {
        let mut shared = self.shared.borrow_mut();
        for task in shared.blocked_senders.drain(..) {
            task.unpark();
        }
    }
}

impl<T> Stream for Receiver<T> {
    type Item = T;
    type Error = ();            // TODO: Replace with uninhabited type, as this can never fail

    fn poll(&mut self) -> Poll<Option<Self::Item>, Self::Error> {
        if let Some(shared) = Rc::get_mut(&mut self.shared) {
            return Ok(Async::Ready(shared.borrow_mut().buffer.pop_front()));
        }

        let mut shared = self.shared.borrow_mut();
        if let Some(msg) = shared.buffer.pop_front() {
            if let Some(task) = shared.blocked_senders.pop_front() {
                task.unpark();
            }
            Ok(Async::Ready(Some(msg)))
        } else {
            shared.blocked_recv = Some(task::park());
            Ok(Async::NotReady)
        }
    }
}

/// The transmission end of an unbounded channel.
///
/// This is created by the `unbounded` function.
pub struct UnboundedSender<T>(Sender<T>);

impl<T> Clone for UnboundedSender<T> {
    fn clone(&self) -> Self { UnboundedSender(self.0.clone()) }
}

impl<T> Sink for UnboundedSender<T> {
    type SinkItem = T;
    type SinkError = SendError<T>;

    fn start_send(&mut self, msg: T) -> StartSend<T, SendError<T>> { self.0.start_send(msg) }
    fn poll_complete(&mut self) -> Poll<(), SendError<T>> { Ok(Async::Ready(())) }
}

/// The receiving end of an unbounded channel.
///
/// This is created by the `unbounded` function.
pub struct UnboundedReceiver<T>(Receiver<T>);

impl<T> Stream for UnboundedReceiver<T> {
    type Item = T;
    type Error = ();            // TODO: Replace with uninhabited type, as this can never fail

    fn poll(&mut self) -> Poll<Option<Self::Item>, Self::Error> { self.0.poll() }
}

/// Creates an unbounded in-memory channel with buffered storage.
///
/// Identical semantics to `channel`, except with no limit to buffer size.
pub fn unbounded<T>() -> (UnboundedSender<T>, UnboundedReceiver<T>) {
    let (send, recv) = channel_(None);
    (UnboundedSender(send), UnboundedReceiver(recv))
}

/// Error type for sending, used when the receiving end of a channel is
/// dropped
pub struct SendError<T>(T);

impl<T> fmt::Debug for SendError<T> {
    fn fmt(&self, fmt: &mut fmt::Formatter) -> fmt::Result {
        fmt.debug_tuple("SendError")
            .field(&"...")
            .finish()
    }
}

impl<T> fmt::Display for SendError<T> {
    fn fmt(&self, fmt: &mut fmt::Formatter) -> fmt::Result {
        write!(fmt, "send failed because receiver is gone")
    }
}

impl<T: Any> Error for SendError<T>
{
    fn description(&self) -> &str {
        "send failed because receiver is gone"
    }
}

impl<T> SendError<T> {
    /// Returns the message that was attempted to be sent but failed.
    pub fn into_inner(self) -> T {
        self.0
    }
}
<commit_msg>Non-blocking UnboundedSender::send<commit_after>//! A multi-producer, single-consumer, futures-aware, FIFO queue with back
//! pressure, for use communicating between tasks on the same thread.

use std::fmt;
use std::error::Error;
use std::any::Any;
use std::collections::VecDeque;
use std::rc::{Rc, Weak};
use std::cell::RefCell;

use task::{self, Task};
use {Async, AsyncSink, Poll, StartSend, Sink, Stream};

/// Creates a bounded in-memory channel with buffered storage.
///
/// This method creates concrete implementations of the `Stream` and `Sink`
/// traits which can be used to communicate a stream of values between tasks
/// with backpressure. The channel capacity is exactly `buffer`. On average,
/// sending a message through this channel performs no dynamic allocation.
pub fn channel<T>(buffer: usize) -> (Sender<T>, Receiver<T>) { channel_(Some(buffer)) }

fn channel_<T>(buffer: Option<usize>) -> (Sender<T>, Receiver<T>) {
    let shared = Rc::new(RefCell::new(Shared {
        buffer: VecDeque::new(),
        capacity: buffer,
        blocked_senders: VecDeque::new(),
        blocked_recv: None,
    }));
    let sender = Sender { shared: Rc::downgrade(&shared) };
    let receiver = Receiver { shared: shared };
    (sender, receiver)
}

struct Shared<T> {
    buffer: VecDeque<T>,
    capacity: Option<usize>,
    blocked_senders: VecDeque<Task>,
    blocked_recv: Option<Task>,
}

/// The transmission end of a channel.
///
/// This is created by the `channel` function.
pub struct Sender<T> {
    shared: Weak<RefCell<Shared<T>>>,
}

impl<T> Drop for Sender<T> {
    fn drop(&mut self) {
        if let Some(task) = self.shared.upgrade().and_then(|shared| shared.borrow_mut().blocked_recv.take()) {
            // Wake up receiver to avoid deadlock in case this was the last sender
            task.unpark();
        }
    }
}

impl<T> Clone for Sender<T> {
    fn clone(&self) -> Self { Sender { shared: self.shared.clone() } }
}

impl<T> Sink for Sender<T> {
    type SinkItem = T;
    type SinkError = SendError<T>;

    fn start_send(&mut self, msg: T) -> StartSend<T, SendError<T>> {
        if let Some(shared) = self.shared.upgrade() {
            let mut shared = shared.borrow_mut();

            match shared.capacity {
                Some(capacity) if shared.buffer.len() == capacity => {
                    shared.blocked_senders.push_back(task::park());
                    Ok(AsyncSink::NotReady(msg))
                }
                _ => {
                    shared.buffer.push_back(msg);
                    if let Some(task) = shared.blocked_recv.take() {
                        task.unpark();
                    }
                    Ok(AsyncSink::Ready)
                }
            }
        } else {
            Err(SendError(msg))
        }
    }

    fn poll_complete(&mut self) -> Poll<(), SendError<T>> {
        Ok(Async::Ready(()))
    }
}

/// The receiving end of a channel which implements the `Stream` trait.
///
/// This is created by the `channel` function.
pub struct Receiver<T> {
    shared: Rc<RefCell<Shared<T>>>,
}

impl<T> Drop for Receiver<T> {
    fn drop(&mut self) {
        let mut shared = self.shared.borrow_mut();
        for task in shared.blocked_senders.drain(..) {
            task.unpark();
        }
    }
}

impl<T> Stream for Receiver<T> {
    type Item = T;
    type Error = ();            // TODO: Replace with uninhabited type, as this can never fail

    fn poll(&mut self) -> Poll<Option<Self::Item>, Self::Error> {
        if let Some(shared) = Rc::get_mut(&mut self.shared) {
            return Ok(Async::Ready(shared.borrow_mut().buffer.pop_front()));
        }

        let mut shared = self.shared.borrow_mut();
        if let Some(msg) = shared.buffer.pop_front() {
            if let Some(task) = shared.blocked_senders.pop_front() {
                task.unpark();
            }
            Ok(Async::Ready(Some(msg)))
        } else {
            shared.blocked_recv = Some(task::park());
            Ok(Async::NotReady)
        }
    }
}

/// The transmission end of an unbounded channel.
///
/// This is created by the `unbounded` function.
pub struct UnboundedSender<T>(Sender<T>);

impl<T> Clone for UnboundedSender<T> {
    fn clone(&self) -> Self { UnboundedSender(self.0.clone()) }
}

impl<T> Sink for UnboundedSender<T> {
    type SinkItem = T;
    type SinkError = SendError<T>;

    fn start_send(&mut self, msg: T) -> StartSend<T, SendError<T>> { self.0.start_send(msg) }
    fn poll_complete(&mut self) -> Poll<(), SendError<T>> { Ok(Async::Ready(())) }
}

impl<T> UnboundedSender<T> {
    /// Sends the provided message along this channel.
    ///
    /// This is an unbounded sender, so this function differs from `Sink::send`
    /// by ensuring the return type reflects that the channel is always ready to
    /// receive messages.
    pub fn send(&self, msg: T) -> Result<(), SendError<T>> {
        let shared = match self.0.shared.upgrade() {
            Some(shared) => shared,
            None => return Err(SendError(msg)),
        };
        let mut shared = shared.borrow_mut();
        shared.buffer.push_back(msg);
        if let Some(task) = shared.blocked_recv.take() {
            task.unpark();
        }
        Ok(())
    }
}

/// The receiving end of an unbounded channel.
///
/// This is created by the `unbounded` function.
pub struct UnboundedReceiver<T>(Receiver<T>);

impl<T> Stream for UnboundedReceiver<T> {
    type Item = T;
    type Error = ();            // TODO: Replace with uninhabited type, as this can never fail

    fn poll(&mut self) -> Poll<Option<Self::Item>, Self::Error> { self.0.poll() }
}

/// Creates an unbounded in-memory channel with buffered storage.
///
/// Identical semantics to `channel`, except with no limit to buffer size.
pub fn unbounded<T>() -> (UnboundedSender<T>, UnboundedReceiver<T>) {
    let (send, recv) = channel_(None);
    (UnboundedSender(send), UnboundedReceiver(recv))
}

/// Error type for sending, used when the receiving end of a channel is
/// dropped
pub struct SendError<T>(T);

impl<T> fmt::Debug for SendError<T> {
    fn fmt(&self, fmt: &mut fmt::Formatter) -> fmt::Result {
        fmt.debug_tuple("SendError")
            .field(&"...")
            .finish()
    }
}

impl<T> fmt::Display for SendError<T> {
    fn fmt(&self, fmt: &mut fmt::Formatter) -> fmt::Result {
        write!(fmt, "send failed because receiver is gone")
    }
}

impl<T: Any> Error for SendError<T>
{
    fn description(&self) -> &str {
        "send failed because receiver is gone"
    }
}

impl<T> SendError<T> {
    /// Returns the message that was attempted to be sent but failed.
    pub fn into_inner(self) -> T {
        self.0
    }
}
<|endoftext|>


<commit_before>extern crate proc_macro;
use self::proc_macro::TokenStream;

use quote::{quote, ToTokens};
use std::collections::HashSet as Set;
use syn::fold::{self, Fold};
use syn::parse::{Parse, ParseStream, Result};
use syn::punctuated::Punctuated;
use syn::{parse_macro_input, parse_quote, Expr, Ident, ItemFn, Local, Pat, Stmt, Token};

/// Parses a list of variable names separated by commas.
///
///     a, b, c
///
/// This is how the compiler passes in arguments to our attribute -- it is
/// everything inside the delimiters after the attribute name.
///
///     #[trace_var(a, b, c)]
///                 ^^^^^^^
struct Args {
    vars: Set<Ident>,
}

impl Parse for Args {
    fn parse(input: ParseStream) -> Result<Self> {
        let vars = Punctuated::<Ident, Token![,]>::parse_terminated(input)?;
        Ok(Args {
            vars: vars.into_iter().collect(),
        })
    }
}

impl Args {
    /// Determines whether the given `Expr` is a path referring to one of the
    /// variables we intend to print. Expressions are used as the left-hand side
    /// of the assignment operator.
    fn should_print_expr(&self, e: &Expr) -> bool {
        match *e {
            Expr::Path(ref e) => {
                if e.path.leading_colon.is_some() {
                    false
                } else if e.path.segments.len() != 1 {
                    false
                } else {
                    let first = e.path.segments.first().unwrap();
                    let segment = first.value();
                    self.vars.contains(&segment.ident) && segment.arguments.is_empty()
                }
            }
            _ => false,
        }
    }

    /// Determines whether the given `Pat` is an identifier equal to one of the
    /// variables we intend to print. Patterns are used as the left-hand side of
    /// a `let` binding.
    fn should_print_pat(&self, p: &Punctuated<Pat, Token![|]>) -> bool {
        if p.len() != 1 {
            return false;
        }
        match p[0] {
            Pat::Ident(ref p) => self.vars.contains(&p.ident),
            _ => false,
        }
    }

    /// Produces an expression that assigns the right-hand side to the left-hand
    /// side and then prints the value.
    ///
    ///     // Before
    ///     VAR = INIT
    ///
    ///     // After
    ///     { VAR = INIT; println!("VAR = {:?}", VAR); }
    fn assign_and_print(&mut self, left: Expr, op: &dyn ToTokens, right: Expr) -> Expr {
        let right = fold::fold_expr(self, right);
        parse_quote!({
            #left #op #right;
            println!(concat!(stringify!(#left), " = {:?}"), #left);
        })
    }

    /// Produces a let-binding that assigns the right-hand side to the left-hand
    /// side and then prints the value.
    ///
    ///     // Before
    ///     let VAR = INIT;
    ///
    ///     // After
    ///     let VAR = { let VAR = INIT; println!("VAR = {:?}", VAR); VAR };
    fn let_and_print(&mut self, local: Local) -> Stmt {
        let Local { pats, ty, init, .. } = local;
        let pat = &pats[0];
        let ty = ty.map(|(colon_token, ty)| quote!(#colon_token #ty));
        let init = self.fold_expr(*init.unwrap().1);
        let ident = match *pat {
            Pat::Ident(ref p) => &p.ident,
            _ => unreachable!(),
        };
        parse_quote! {
            let #pat #ty = {
                #[allow(unused_mut)]
                let #pat = #init;
                println!(concat!(stringify!(#ident), " = {:?}"), #ident);
                #ident
            };
        }
    }
}

/// The `Fold` trait is a way to traverse an owned syntax tree and replace some
/// of its nodes.
///
/// Syn provides two other syntax tree traversal traits: `Visit` which walks a
/// shared borrow of a syntax tree, and `VisitMut` which walks an exclusive
/// borrow of a syntax tree and can mutate it in place.
///
/// All three traits have a method corresponding to each type of node in Syn's
/// syntax tree. All of these methods have default no-op implementations that
/// simply recurse on any child nodes. We can override only those methods for
/// which we want non-default behavior. In this case the traversal needs to
/// transform `Expr` and `Stmt` nodes.
impl Fold for Args {
    fn fold_expr(&mut self, e: Expr) -> Expr {
        match e {
            Expr::Assign(e) => {
                if self.should_print_expr(&e.left) {
                    self.assign_and_print(*e.left, &e.eq_token, *e.right)
                } else {
                    Expr::Assign(fold::fold_expr_assign(self, e))
                }
            }
            Expr::AssignOp(e) => {
                if self.should_print_expr(&e.left) {
                    self.assign_and_print(*e.left, &e.op, *e.right)
                } else {
                    Expr::AssignOp(fold::fold_expr_assign_op(self, e))
                }
            }
            _ => fold::fold_expr(self, e),
        }
    }

    fn fold_stmt(&mut self, s: Stmt) -> Stmt {
        match s {
            Stmt::Local(s) => {
                if s.init.is_some() && self.should_print_pat(&s.pats) {
                    self.let_and_print(s)
                } else {
                    Stmt::Local(fold::fold_local(self, s))
                }
            }
            _ => fold::fold_stmt(self, s),
        }
    }
}

/// Attribute to print the value of the given variables each time they are
/// reassigned.
///
/// # Example
///
/// ```
/// #[trace_var(p, n)]
/// fn factorial(mut n: u64) -> u64 {
///     let mut p = 1;
///     while n > 1 {
///         p *= n;
///         n -= 1;
///     }
///     p
/// }
/// ```
#[proc_macro_attribute]
pub fn trace_var(args: TokenStream, input: TokenStream) -> TokenStream {
    let input = parse_macro_input!(input as ItemFn);

    // Parse the list of variables the user wanted to print.
    let mut args = parse_macro_input!(args as Args);

    // Use a syntax tree traversal to transform the function body.
    let output = args.fold_item_fn(input);

    // Hand the resulting function body back to the compiler.
    TokenStream::from(quote!(#output))
}
<commit_msg>Update use of first() in trace-var<commit_after>extern crate proc_macro;
use self::proc_macro::TokenStream;

use quote::{quote, ToTokens};
use std::collections::HashSet as Set;
use syn::fold::{self, Fold};
use syn::parse::{Parse, ParseStream, Result};
use syn::punctuated::Punctuated;
use syn::{parse_macro_input, parse_quote, Expr, Ident, ItemFn, Local, Pat, Stmt, Token};

/// Parses a list of variable names separated by commas.
///
///     a, b, c
///
/// This is how the compiler passes in arguments to our attribute -- it is
/// everything inside the delimiters after the attribute name.
///
///     #[trace_var(a, b, c)]
///                 ^^^^^^^
struct Args {
    vars: Set<Ident>,
}

impl Parse for Args {
    fn parse(input: ParseStream) -> Result<Self> {
        let vars = Punctuated::<Ident, Token![,]>::parse_terminated(input)?;
        Ok(Args {
            vars: vars.into_iter().collect(),
        })
    }
}

impl Args {
    /// Determines whether the given `Expr` is a path referring to one of the
    /// variables we intend to print. Expressions are used as the left-hand side
    /// of the assignment operator.
    fn should_print_expr(&self, e: &Expr) -> bool {
        match *e {
            Expr::Path(ref e) => {
                if e.path.leading_colon.is_some() {
                    false
                } else if e.path.segments.len() != 1 {
                    false
                } else {
                    let first = e.path.segments.first().unwrap();
                    self.vars.contains(&first.ident) && first.arguments.is_empty()
                }
            }
            _ => false,
        }
    }

    /// Determines whether the given `Pat` is an identifier equal to one of the
    /// variables we intend to print. Patterns are used as the left-hand side of
    /// a `let` binding.
    fn should_print_pat(&self, p: &Punctuated<Pat, Token![|]>) -> bool {
        if p.len() != 1 {
            return false;
        }
        match p[0] {
            Pat::Ident(ref p) => self.vars.contains(&p.ident),
            _ => false,
        }
    }

    /// Produces an expression that assigns the right-hand side to the left-hand
    /// side and then prints the value.
    ///
    ///     // Before
    ///     VAR = INIT
    ///
    ///     // After
    ///     { VAR = INIT; println!("VAR = {:?}", VAR); }
    fn assign_and_print(&mut self, left: Expr, op: &dyn ToTokens, right: Expr) -> Expr {
        let right = fold::fold_expr(self, right);
        parse_quote!({
            #left #op #right;
            println!(concat!(stringify!(#left), " = {:?}"), #left);
        })
    }

    /// Produces a let-binding that assigns the right-hand side to the left-hand
    /// side and then prints the value.
    ///
    ///     // Before
    ///     let VAR = INIT;
    ///
    ///     // After
    ///     let VAR = { let VAR = INIT; println!("VAR = {:?}", VAR); VAR };
    fn let_and_print(&mut self, local: Local) -> Stmt {
        let Local { pats, ty, init, .. } = local;
        let pat = &pats[0];
        let ty = ty.map(|(colon_token, ty)| quote!(#colon_token #ty));
        let init = self.fold_expr(*init.unwrap().1);
        let ident = match *pat {
            Pat::Ident(ref p) => &p.ident,
            _ => unreachable!(),
        };
        parse_quote! {
            let #pat #ty = {
                #[allow(unused_mut)]
                let #pat = #init;
                println!(concat!(stringify!(#ident), " = {:?}"), #ident);
                #ident
            };
        }
    }
}

/// The `Fold` trait is a way to traverse an owned syntax tree and replace some
/// of its nodes.
///
/// Syn provides two other syntax tree traversal traits: `Visit` which walks a
/// shared borrow of a syntax tree, and `VisitMut` which walks an exclusive
/// borrow of a syntax tree and can mutate it in place.
///
/// All three traits have a method corresponding to each type of node in Syn's
/// syntax tree. All of these methods have default no-op implementations that
/// simply recurse on any child nodes. We can override only those methods for
/// which we want non-default behavior. In this case the traversal needs to
/// transform `Expr` and `Stmt` nodes.
impl Fold for Args {
    fn fold_expr(&mut self, e: Expr) -> Expr {
        match e {
            Expr::Assign(e) => {
                if self.should_print_expr(&e.left) {
                    self.assign_and_print(*e.left, &e.eq_token, *e.right)
                } else {
                    Expr::Assign(fold::fold_expr_assign(self, e))
                }
            }
            Expr::AssignOp(e) => {
                if self.should_print_expr(&e.left) {
                    self.assign_and_print(*e.left, &e.op, *e.right)
                } else {
                    Expr::AssignOp(fold::fold_expr_assign_op(self, e))
                }
            }
            _ => fold::fold_expr(self, e),
        }
    }

    fn fold_stmt(&mut self, s: Stmt) -> Stmt {
        match s {
            Stmt::Local(s) => {
                if s.init.is_some() && self.should_print_pat(&s.pats) {
                    self.let_and_print(s)
                } else {
                    Stmt::Local(fold::fold_local(self, s))
                }
            }
            _ => fold::fold_stmt(self, s),
        }
    }
}

/// Attribute to print the value of the given variables each time they are
/// reassigned.
///
/// # Example
///
/// ```
/// #[trace_var(p, n)]
/// fn factorial(mut n: u64) -> u64 {
///     let mut p = 1;
///     while n > 1 {
///         p *= n;
///         n -= 1;
///     }
///     p
/// }
/// ```
#[proc_macro_attribute]
pub fn trace_var(args: TokenStream, input: TokenStream) -> TokenStream {
    let input = parse_macro_input!(input as ItemFn);

    // Parse the list of variables the user wanted to print.
    let mut args = parse_macro_input!(args as Args);

    // Use a syntax tree traversal to transform the function body.
    let output = args.fold_item_fn(input);

    // Hand the resulting function body back to the compiler.
    TokenStream::from(quote!(#output))
}
<|endoftext|>


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

use std::fmt;
use std::ops;

use super::Value;
use map::Map;

/// A type that can be used to index into a `serde_json::Value`. See the `get`
/// and `get_mut` methods of `Value`.
///
/// This trait is sealed and cannot be implemented for types outside of
/// `serde_json`.
pub trait Index: private::Sealed {
    /// Return None if the key is not already in the array or object.
    #[doc(hidden)]
    fn index_into<'v>(&self, v: &'v Value) -> Option<&'v Value>;

    /// Return None if the key is not already in the array or object.
    #[doc(hidden)]
    fn index_into_mut<'v>(&self, v: &'v mut Value) -> Option<&'v mut Value>;

    /// Panic if array index out of bounds. If key is not already in the object,
    /// insert it with a value of null. Panic if Value is a type that cannot be
    /// indexed into, except if Value is null then it can be treated as an empty
    /// object.
    #[doc(hidden)]
    fn index_or_insert<'v>(&self, v: &'v mut Value) -> &'v mut Value;
}

impl Index for usize {
    fn index_into<'v>(&self, v: &'v Value) -> Option<&'v Value> {
        match *v {
            Value::Array(ref vec) => vec.get(*self),
            _ => None,
        }
    }
    fn index_into_mut<'v>(&self, v: &'v mut Value) -> Option<&'v mut Value> {
        match *v {
            Value::Array(ref mut vec) => vec.get_mut(*self),
            _ => None,
        }
    }
    fn index_or_insert<'v>(&self, v: &'v mut Value) -> &'v mut Value {
        match *v {
            Value::Array(ref mut vec) => {
                let len = vec.len();
                vec.get_mut(*self)
                    .unwrap_or_else(
                        || {
                            panic!(
                                "cannot access index {} of JSON array of length {}",
                                self,
                                len
                            )
                        },
                    )
            }
            _ => panic!("cannot access index {} of JSON {}", self, Type(v)),
        }
    }
}

impl Index for str {
    fn index_into<'v>(&self, v: &'v Value) -> Option<&'v Value> {
        match *v {
            Value::Object(ref map) => map.get(self),
            _ => None,
        }
    }
    fn index_into_mut<'v>(&self, v: &'v mut Value) -> Option<&'v mut Value> {
        match *v {
            Value::Object(ref mut map) => map.get_mut(self),
            _ => None,
        }
    }
    fn index_or_insert<'v>(&self, v: &'v mut Value) -> &'v mut Value {
        if let Value::Null = *v {
            let mut map = Map::new();
            map.insert(self.to_owned(), Value::Null);
            *v = Value::Object(map);
        }
        match *v {
            Value::Object(ref mut map) => {
                map.entry(self.to_owned()).or_insert(Value::Null)
            }
            _ => panic!("cannot access key {:?} in JSON {}", self, Type(v)),
        }
    }
}

impl Index for String {
    fn index_into<'v>(&self, v: &'v Value) -> Option<&'v Value> {
        self[..].index_into(v)
    }
    fn index_into_mut<'v>(&self, v: &'v mut Value) -> Option<&'v mut Value> {
        self[..].index_into_mut(v)
    }
    fn index_or_insert<'v>(&self, v: &'v mut Value) -> &'v mut Value {
        self[..].index_or_insert(v)
    }
}

impl<'a, T: ?Sized> Index for &'a T
where
    T: Index,
{
    fn index_into<'v>(&self, v: &'v Value) -> Option<&'v Value> {
        (**self).index_into(v)
    }
    fn index_into_mut<'v>(&self, v: &'v mut Value) -> Option<&'v mut Value> {
        (**self).index_into_mut(v)
    }
    fn index_or_insert<'v>(&self, v: &'v mut Value) -> &'v mut Value {
        (**self).index_or_insert(v)
    }
}

// Prevent users from implementing the Index trait.
mod private {
    pub trait Sealed {}
    impl Sealed for usize {}
    impl Sealed for str {}
    impl Sealed for String {}
    impl<'a, T: ?Sized> Sealed for &'a T
    where
        T: Sealed,
    {
    }
}

/// Used in panic messages.
struct Type<'a>(&'a Value);

impl<'a> fmt::Display for Type<'a> {
    fn fmt(&self, formatter: &mut fmt::Formatter) -> fmt::Result {
        match *self.0 {
            Value::Null => formatter.write_str("null"),
            Value::Bool(_) => formatter.write_str("boolean"),
            Value::Number(_) => formatter.write_str("number"),
            Value::String(_) => formatter.write_str("string"),
            Value::Array(_) => formatter.write_str("array"),
            Value::Object(_) => formatter.write_str("object"),
        }
    }
}

// The usual semantics of Index is to panic on invalid indexing.
//
// That said, the usual semantics are for things like Vec and BTreeMap which
// have different use cases than Value. If you are working with a Vec, you know
// that you are working with a Vec and you can get the len of the Vec and make
// sure your indices are within bounds. The Value use cases are more
// loosey-goosey. You got some JSON from an endpoint and you want to pull values
// out of it. Outside of this Index impl, you already have the option of using
// value.as_array() and working with the Vec directly, or matching on
// Value::Array and getting the Vec directly. The Index impl means you can skip
// that and index directly into the thing using a concise syntax. You don't have
// to check the type, you don't have to check the len, it is all about what you
// expect the Value to look like.
//
// Basically the use cases that would be well served by panicking here are
// better served by using one of the other approaches: get and get_mut,
// as_array, or match. The value of this impl is that it adds a way of working
// with Value that is not well served by the existing approaches: concise and
// careless and sometimes that is exactly what you want.
impl<I> ops::Index<I> for Value
where
    I: Index,
{
    type Output = Value;

    /// Index into a `serde_json::Value` using the syntax `value[0]` or
    /// `value["k"]`.
    ///
    /// Returns `Value::Null` if the type of `self` does not match the type of
    /// the index, for example if the index is a string and `self` is an array
    /// or a number. Also returns `Value::Null` if the given key does not exist
    /// in the map or the given index is not within the bounds of the array.
    ///
    /// For retrieving deeply nested values, you should have a look at the
    /// `Value::pointer` method.
    ///
    /// # Examples
    ///
    /// ```rust
    /// # #[macro_use]
    /// # extern crate serde_json;
    /// #
    /// # fn main() {
    /// let data = json!({
    ///     "x": {
    ///         "y": ["z", "zz"]
    ///     }
    /// });
    ///
    /// assert_eq!(data["x"]["y"], json!(["z", "zz"]));
    /// assert_eq!(data["x"]["y"][0], json!("z"));
    ///
    /// assert_eq!(data["a"], json!(null)); // returns null for undefined values
    /// assert_eq!(data["a"]["b"], json!(null)); // does not panic
    /// # }
    /// ```
    fn index(&self, index: I) -> &Value {
        static NULL: Value = Value::Null;
        index.index_into(self).unwrap_or(&NULL)
    }
}

impl<I> ops::IndexMut<I> for Value
where
    I: Index,
{
    /// Write into a `serde_json::Value` using the syntax `value[0] = ...` or
    /// `value["k"] = ...`.
    ///
    /// If the index is a number, the value must be an array of length bigger
    /// than the index. Indexing into a value that is not an array or an array
    /// that is too small will panic.
    ///
    /// If the index is a string, the value must be an object or null which is
    /// treated like an empty object. If the key is not already present in the
    /// object, it will be inserted with a value of null. Indexing into a value
    /// that is neither an object nor null will panic.
    ///
    /// # Examples
    ///
    /// ```rust
    /// # #[macro_use]
    /// # extern crate serde_json;
    /// #
    /// # fn main() {
    /// let mut data = json!({ "x": 0 });
    ///
    /// // replace an existing key
    /// data["x"] = json!(1);
    ///
    /// // insert a new key
    /// data["y"] = json!([false, false, false]);
    ///
    /// // replace an array value
    /// data["y"][0] = json!(true);
    ///
    /// // inserted a deeply nested key
    /// data["a"]["b"]["c"]["d"] = json!(true);
    ///
    /// println!("{}", data);
    /// # }
    /// ```
    fn index_mut(&mut self, index: I) -> &mut Value {
        index.index_or_insert(self)
    }
}
<commit_msg>More detailed documentation of Index trait<commit_after>// Copyright 2017 Serde 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.

use std::fmt;
use std::ops;

use super::Value;
use map::Map;

/// A type that can be used to index into a `serde_json::Value`.
///
/// The [`get`] and [`get_mut`] methods of `Value` accept any type that
/// implements `Index`, as does the [square-bracket indexing operator]. This
/// trait is implemented for strings which are used as the index into a JSON
/// map, and for `usize` which is used as the index into a JSON array.
///
/// [`get`]: ../enum.Value.html#method.get
/// [`get_mut`]: ../enum.Value.html#method.get_mut
/// [square-bracket indexing operator]: ../enum.Value.html#impl-Index%3CI%3E
///
/// This trait is sealed and cannot be implemented for types outside of
/// `serde_json`.
///
/// # Examples
///
/// ```rust
/// # #[macro_use]
/// # extern crate serde_json;
/// #
/// # fn main() {
/// let data = json!({ "inner": [1, 2, 3] });
///
/// // Data is a JSON map so it can be indexed with a string.
/// let inner = &data["inner"];
///
/// // Inner is a JSON array so it can be indexed with an integer.
/// let first = &inner[0];
///
/// assert_eq!(first, 1);
/// # }
/// ```
pub trait Index: private::Sealed {
    /// Return None if the key is not already in the array or object.
    #[doc(hidden)]
    fn index_into<'v>(&self, v: &'v Value) -> Option<&'v Value>;

    /// Return None if the key is not already in the array or object.
    #[doc(hidden)]
    fn index_into_mut<'v>(&self, v: &'v mut Value) -> Option<&'v mut Value>;

    /// Panic if array index out of bounds. If key is not already in the object,
    /// insert it with a value of null. Panic if Value is a type that cannot be
    /// indexed into, except if Value is null then it can be treated as an empty
    /// object.
    #[doc(hidden)]
    fn index_or_insert<'v>(&self, v: &'v mut Value) -> &'v mut Value;
}

impl Index for usize {
    fn index_into<'v>(&self, v: &'v Value) -> Option<&'v Value> {
        match *v {
            Value::Array(ref vec) => vec.get(*self),
            _ => None,
        }
    }
    fn index_into_mut<'v>(&self, v: &'v mut Value) -> Option<&'v mut Value> {
        match *v {
            Value::Array(ref mut vec) => vec.get_mut(*self),
            _ => None,
        }
    }
    fn index_or_insert<'v>(&self, v: &'v mut Value) -> &'v mut Value {
        match *v {
            Value::Array(ref mut vec) => {
                let len = vec.len();
                vec.get_mut(*self)
                    .unwrap_or_else(
                        || {
                            panic!(
                                "cannot access index {} of JSON array of length {}",
                                self,
                                len
                            )
                        },
                    )
            }
            _ => panic!("cannot access index {} of JSON {}", self, Type(v)),
        }
    }
}

impl Index for str {
    fn index_into<'v>(&self, v: &'v Value) -> Option<&'v Value> {
        match *v {
            Value::Object(ref map) => map.get(self),
            _ => None,
        }
    }
    fn index_into_mut<'v>(&self, v: &'v mut Value) -> Option<&'v mut Value> {
        match *v {
            Value::Object(ref mut map) => map.get_mut(self),
            _ => None,
        }
    }
    fn index_or_insert<'v>(&self, v: &'v mut Value) -> &'v mut Value {
        if let Value::Null = *v {
            let mut map = Map::new();
            map.insert(self.to_owned(), Value::Null);
            *v = Value::Object(map);
        }
        match *v {
            Value::Object(ref mut map) => {
                map.entry(self.to_owned()).or_insert(Value::Null)
            }
            _ => panic!("cannot access key {:?} in JSON {}", self, Type(v)),
        }
    }
}

impl Index for String {
    fn index_into<'v>(&self, v: &'v Value) -> Option<&'v Value> {
        self[..].index_into(v)
    }
    fn index_into_mut<'v>(&self, v: &'v mut Value) -> Option<&'v mut Value> {
        self[..].index_into_mut(v)
    }
    fn index_or_insert<'v>(&self, v: &'v mut Value) -> &'v mut Value {
        self[..].index_or_insert(v)
    }
}

impl<'a, T: ?Sized> Index for &'a T
where
    T: Index,
{
    fn index_into<'v>(&self, v: &'v Value) -> Option<&'v Value> {
        (**self).index_into(v)
    }
    fn index_into_mut<'v>(&self, v: &'v mut Value) -> Option<&'v mut Value> {
        (**self).index_into_mut(v)
    }
    fn index_or_insert<'v>(&self, v: &'v mut Value) -> &'v mut Value {
        (**self).index_or_insert(v)
    }
}

// Prevent users from implementing the Index trait.
mod private {
    pub trait Sealed {}
    impl Sealed for usize {}
    impl Sealed for str {}
    impl Sealed for String {}
    impl<'a, T: ?Sized> Sealed for &'a T
    where
        T: Sealed,
    {
    }
}

/// Used in panic messages.
struct Type<'a>(&'a Value);

impl<'a> fmt::Display for Type<'a> {
    fn fmt(&self, formatter: &mut fmt::Formatter) -> fmt::Result {
        match *self.0 {
            Value::Null => formatter.write_str("null"),
            Value::Bool(_) => formatter.write_str("boolean"),
            Value::Number(_) => formatter.write_str("number"),
            Value::String(_) => formatter.write_str("string"),
            Value::Array(_) => formatter.write_str("array"),
            Value::Object(_) => formatter.write_str("object"),
        }
    }
}

// The usual semantics of Index is to panic on invalid indexing.
//
// That said, the usual semantics are for things like Vec and BTreeMap which
// have different use cases than Value. If you are working with a Vec, you know
// that you are working with a Vec and you can get the len of the Vec and make
// sure your indices are within bounds. The Value use cases are more
// loosey-goosey. You got some JSON from an endpoint and you want to pull values
// out of it. Outside of this Index impl, you already have the option of using
// value.as_array() and working with the Vec directly, or matching on
// Value::Array and getting the Vec directly. The Index impl means you can skip
// that and index directly into the thing using a concise syntax. You don't have
// to check the type, you don't have to check the len, it is all about what you
// expect the Value to look like.
//
// Basically the use cases that would be well served by panicking here are
// better served by using one of the other approaches: get and get_mut,
// as_array, or match. The value of this impl is that it adds a way of working
// with Value that is not well served by the existing approaches: concise and
// careless and sometimes that is exactly what you want.
impl<I> ops::Index<I> for Value
where
    I: Index,
{
    type Output = Value;

    /// Index into a `serde_json::Value` using the syntax `value[0]` or
    /// `value["k"]`.
    ///
    /// Returns `Value::Null` if the type of `self` does not match the type of
    /// the index, for example if the index is a string and `self` is an array
    /// or a number. Also returns `Value::Null` if the given key does not exist
    /// in the map or the given index is not within the bounds of the array.
    ///
    /// For retrieving deeply nested values, you should have a look at the
    /// `Value::pointer` method.
    ///
    /// # Examples
    ///
    /// ```rust
    /// # #[macro_use]
    /// # extern crate serde_json;
    /// #
    /// # fn main() {
    /// let data = json!({
    ///     "x": {
    ///         "y": ["z", "zz"]
    ///     }
    /// });
    ///
    /// assert_eq!(data["x"]["y"], json!(["z", "zz"]));
    /// assert_eq!(data["x"]["y"][0], json!("z"));
    ///
    /// assert_eq!(data["a"], json!(null)); // returns null for undefined values
    /// assert_eq!(data["a"]["b"], json!(null)); // does not panic
    /// # }
    /// ```
    fn index(&self, index: I) -> &Value {
        static NULL: Value = Value::Null;
        index.index_into(self).unwrap_or(&NULL)
    }
}

impl<I> ops::IndexMut<I> for Value
where
    I: Index,
{
    /// Write into a `serde_json::Value` using the syntax `value[0] = ...` or
    /// `value["k"] = ...`.
    ///
    /// If the index is a number, the value must be an array of length bigger
    /// than the index. Indexing into a value that is not an array or an array
    /// that is too small will panic.
    ///
    /// If the index is a string, the value must be an object or null which is
    /// treated like an empty object. If the key is not already present in the
    /// object, it will be inserted with a value of null. Indexing into a value
    /// that is neither an object nor null will panic.
    ///
    /// # Examples
    ///
    /// ```rust
    /// # #[macro_use]
    /// # extern crate serde_json;
    /// #
    /// # fn main() {
    /// let mut data = json!({ "x": 0 });
    ///
    /// // replace an existing key
    /// data["x"] = json!(1);
    ///
    /// // insert a new key
    /// data["y"] = json!([false, false, false]);
    ///
    /// // replace an array value
    /// data["y"][0] = json!(true);
    ///
    /// // inserted a deeply nested key
    /// data["a"]["b"]["c"]["d"] = json!(true);
    ///
    /// println!("{}", data);
    /// # }
    /// ```
    fn index_mut(&mut self, index: I) -> &mut Value {
        index.index_or_insert(self)
    }
}
<|endoftext|>


<commit_before>use std::collections::HashMap;
use std::net::IpAddr;
use std::net::SocketAddr;
use std::io::{Read, Write};
use std::io;
use std::error::Error;
use std::fs::OpenOptions;
use std::path::Path;
use std::fs::File;

use mio::*;
use mio::tcp::TcpStream;
use mio::channel::channel;
use mio::channel::{Sender, Receiver};

use metainfo::MetaInfo;
use metainfo::SHA1Hash20b;

use wire::handler::{ServerHandler};
use wire::action::{PeerAction, PeerStreamAction};
use wire::msg::{PeerMsg, parse_peermsg, parse_handshake};
use wire::action::PeerId;
use wire::peer::PeerServer;

const OUTSIDE_MSG: Token = Token(0);
pub type StreamId = u32;

pub struct Protocol {
    streams: HashMap<StreamId, (TcpStream, PeerStream)>,
    handler: PeerServer,
    poll: Poll,
    sender: Sender<ChanMsg>,
    receiver: Receiver<ChanMsg>,
    info: MetaInfo,
    info_hash: SHA1Hash20b,
    pending_actions: Vec<PeerAction>,
    stats: Stats,
    next_peer_id: usize,
}

const LOG_IN: bool = false;

#[derive(Debug, Copy, Clone)]
pub struct Stats {
    pub uploaded: usize,
    pub downloaded: usize,
    pub peer_count: usize,
}

impl Stats {
    pub fn new() -> Stats {
        Stats {
            uploaded: 0,
            downloaded: 0,
            peer_count: 0,
        }
    }
}

pub struct PeerStream {
    id: StreamId,
    bytes_in: Vec<u8>,
    bytes_out: Vec<u8>,
    handshake_sent: bool,
    handshake_received: bool,
    disconnected: bool,
}

impl PeerStream {
    pub fn new(id: StreamId) -> PeerStream {
        PeerStream {
            id: id,
            bytes_in: Vec::new(),
            bytes_out: Vec::new(),
            handshake_sent: true,
            handshake_received: false,
            disconnected: false,
        }
    }

    pub fn write_in(&mut self, mut bytes: Vec<u8>) {
        let path = &format!("{}.dat", self.id);
        if LOG_IN {
            if !Path::new(path).exists() {
                File::create(path);
            }
            let mut file =
                OpenOptions::new()
                .write(true)
                .append(true)
                .open(path)
                .unwrap();
        }
        self.bytes_in.append(&mut bytes);
    }

    pub fn write_out(&mut self, mut bytes: Vec<u8>) {
        info!("This goes out {:?}", bytes);
        self.bytes_out.append(&mut bytes);
    }

    pub fn message(&mut self) -> Option<PeerMsg> {
        info!("Bytes in: {}", self.bytes_in.len());
        if self.bytes_in.len() == 0 {
            return None;
        }

        match parse_peermsg(&self.bytes_in) {
            Ok((msg, offset)) => {
                info!("Parsed a message of len {}", offset);
                if offset < self.bytes_in.len() {
                    self.bytes_in = self.bytes_in.split_off(offset);
                } else {
                    self.bytes_in = Vec::new();
                }
                info!("Message: {:?}", msg);
                Some(msg)
            }
            Err(err) => {
                info!("Message not ready {:?}", err);
                // TODO It needs to distinguish between recoverable and non-recoverable
                None
            }
        }
    }

    pub fn take(&mut self, out: &mut Write) -> io::Result<usize> {
        const MAX_BYTES_WRITE: usize = 1024 * 128;
        info!("Attempting to write {} bytes", self.bytes_out.len());
        let result = out.write(&self.bytes_out);
        match result {
            Ok(offset) => {
                info!("Wrote {} bytes", offset);
                self.bytes_out = self.bytes_out.split_off(offset);
            }
            Err(ref err) => {
                info!("Writing failed: {}", err);
            }
        };
        result
    }

    pub fn len_in(&self) -> usize {
        self.bytes_in.len()
    }
}

#[derive(Debug)]
pub enum ChanMsg {
    NewPeer(IpAddr, u16),
    StatsRequest,
    StatsResponse(Stats),
    ThrottleOn(usize),
    ThrottleOff
}

impl Protocol {
    pub fn new(info: &MetaInfo,
               hash: SHA1Hash20b,
               our_peer_id: &str)
               -> (Protocol, Sender<ChanMsg>, Receiver<ChanMsg>) {
        let poll = Poll::new().unwrap();

        match (channel(), channel()) {
            ((to_inside, from_outside), (to_outside, from_inside)) => {

                poll.register(&from_outside,
                              OUTSIDE_MSG,
                              Ready::readable(),
                              PollOpt::level())
                    .unwrap();

                let proto = Protocol {
                    streams: HashMap::new(),
                    poll: poll,
                    sender: to_outside,
                    receiver: from_outside,
                    info: info.clone(),
                    info_hash: hash.clone(),
                    pending_actions: Vec::new(),
                    stats: Stats::new(),
                    handler: ServerHandler::new(info.clone(), hash.clone(), our_peer_id),
                    next_peer_id: 1,
                };

                (proto, to_inside, from_inside)
            }
        }
    }

    pub fn run(&mut self) {
        const EVENT_CAPACITY: usize = 32;
        let mut events = Events::with_capacity(EVENT_CAPACITY);
        loop {
            self.poll.poll(&mut events, None).unwrap();
            for event in events.iter() {
                self._handle_event(event);
            }
            info!("All events handled");
            let actions = self.handler.on_loop();
            for action in actions {
                match self.streams.get_mut(&action.0) {
                    Some(&mut (_, ref mut peer)) => {
                        Protocol::_perform_action(action, peer);
                    }
                    None => ()
                }
            }
        }
    }

    fn _handle_event(&mut self, event: Event) {
        match event.token() {
            OUTSIDE_MSG => {
                loop {
                    match self.receiver.try_recv() {
                        Ok(msg) => self._handle_outside_msg(msg),
                        _ => break,
                    }
                }
            }
            _ => self._handle_socket_event(event),
        }
    }

    fn _get_stream_id(&self, token: Token) -> Option<StreamId> {
        Some(match token {
            Token(id) => id as StreamId,
        })
    }

    fn _perform_action(action: PeerAction, peer: &mut PeerStream) {
        let id = action.0;
        match action.1 {
            PeerStreamAction::Nothing => (),
            PeerStreamAction::SendMessages(msgs) => {
                for msg in msgs {
                    info!("Writing message {:?}", msg);
                    let bytes = msg.into();
                    peer.write_out(bytes);
                }
            }
            PeerStreamAction::Disconnect => {
                peer.disconnected = true;
            }
        }
    }

    fn _handle_socket_event(&mut self, event: Event) {
        let kind = event.kind();
        let peer_id: StreamId = match self._get_stream_id(event.token()) {
            Some(p_id) => p_id,
            None => return,
        };

        if let Some((mut tcp_stream, mut peer_stream)) = self.streams.remove(&peer_id) {
            info!("Got event {:?} from peer id {}", event, peer_id);
            if (kind.is_readable()) {
                let read_result =
                    Protocol::_handle_read(&mut tcp_stream, &mut peer_stream, &mut self.handler);

                for action in read_result.0 {
                    Protocol::_perform_action(action, &mut peer_stream);
                }

                self.stats.uploaded += read_result.1;
            }

            let mut fatal_error_happened = false;
            if kind.is_writable() {
                // write pending messages
                let write_result =
                    Protocol::_handle_write(&mut tcp_stream, &mut peer_stream, &mut self.handler);

                if write_result.is_err() {
                    fatal_error_happened = true;
                }
            }

            if kind.is_hup() || kind.is_error() || fatal_error_happened {
                info!("Disconnected from peer id {}", peer_stream.id);
                self.poll.deregister(&tcp_stream);
                Protocol::_handle_hup(&mut tcp_stream, &mut peer_stream, &mut self.handler);

                // do not return socket to collection
                return;
            }

            self.streams.insert(peer_stream.id, (tcp_stream, peer_stream));
        }

    }

    fn _handle_read(socket: &mut TcpStream,
                    peer: &mut PeerStream,
                    handler: &mut PeerServer)
                    -> (Vec<PeerAction>, usize) {

        const READ_BUF_SIZE: usize = 1024;
        let mut buf = Vec::with_capacity(READ_BUF_SIZE);
        buf.resize(READ_BUF_SIZE, 0);
        let bytes_read = match socket.read(&mut buf) {
            Ok(bytes_read) => {
                peer.write_in(buf);
                info!("Read {} bytes from {}", bytes_read, peer.id);//, socket.peer_addr());
                bytes_read
            }
            Err(err) => 0,
        };

        // Attempt to parse a handshake first

        let mut actions = Vec::new();
        loop {
            let msg = if !peer.handshake_received {
                match parse_handshake(&peer.bytes_in) {
                    Ok((handshake, offset)) => {
                        peer.bytes_in = peer.bytes_in.split_off(offset);
                        peer.handshake_received = true;
                        info!("Handshake received: {:?}", handshake);
                        Some(handshake)
                    }
                    Err(e) => {
                        info!("Error parsing handshake: {:?}", e);
                        None
                    }
                }
            } else {
                peer.message()
            };

            let actions = match msg {
                Some(msg) => {
                    info!("Received from peer {:?}", msg);
                    actions.push(handler.on_message_receive(peer.id, msg));
                }
                None => {
                    info!("Error parsing. {} bytes left", peer.bytes_in.len());
                    return (Vec::new(), bytes_read);
                }
            };
        }

        info!("Read {} bytes", bytes_read);
        (actions, bytes_read)
    }

    fn _handle_write(socket: &mut TcpStream,
                     peer: &mut PeerStream,
                     handler: &mut PeerServer)
                     -> io::Result<usize> {
        peer.take(socket)
    }

    fn _handle_hup(socket: &mut TcpStream, peer: &mut PeerStream, handler: &mut PeerServer) {
        handler.on_peer_disconnect(peer.id);
    }

    fn _handle_outside_msg(&mut self, msg: ChanMsg) {
        match msg {
            ChanMsg::NewPeer(ip, port) => self._handle_new_peer(ip, port),
            ChanMsg::StatsRequest => {
                self.sender.send(ChanMsg::StatsResponse(self.stats));
            }
            _ => (),
        }
    }

    fn _handle_new_peer(&mut self, addr: IpAddr, port: u16) {
        for (id, &(ref socket, _)) in &self.streams {
            match (socket.peer_addr(), addr) {
                (Ok(SocketAddr::V4(peer)), IpAddr::V4(p)) => {
                    if peer.port() == port && peer.ip().octets() == p.octets() {
                        return;
                    }
                }
                (Ok(SocketAddr::V6(peer)), IpAddr::V6(p)) => unimplemented!(),
                (Err(e), _) => {
                    continue;
                }
                _ => continue,
            }
        }

        match self._connect_to_peer(addr, port) {
            Some((sock, Token(id_usize))) => {
                let id = id_usize as u32;
                let mut stream = PeerStream::new(id);
                let action = self.handler.on_peer_connect(id);

                Protocol::_perform_action(action, &mut stream);

                self.streams.insert(id, (sock, stream));
            }
            None => (),
        }
    }

    fn _connect_to_peer(&mut self, addr: IpAddr, port: u16) -> Option<(TcpStream, Token)> {
        let sock_addr = SocketAddr::new(addr, port);
        let token = Token(self.next_peer_id);
        info!("Trying to connect to {} on port {}", addr, port);
        match TcpStream::connect(&sock_addr) {
            Ok(sock) => {
                self.poll
                    .register(&sock, token, Ready::all(), PollOpt::edge());

                self.next_peer_id += 1;
                return Some((sock, token));
            }
            Err(e) => {
                info!("Could not connect: {}", e);
                return None;
            }
        }
    }
}
<commit_msg>Turn logging on<commit_after>use std::collections::HashMap;
use std::net::IpAddr;
use std::net::SocketAddr;
use std::io::{Read, Write};
use std::io;
use std::error::Error;
use std::fs::OpenOptions;
use std::path::Path;
use std::fs::File;

use mio::*;
use mio::tcp::TcpStream;
use mio::channel::channel;
use mio::channel::{Sender, Receiver};

use metainfo::MetaInfo;
use metainfo::SHA1Hash20b;

use wire::handler::{ServerHandler};
use wire::action::{PeerAction, PeerStreamAction};
use wire::msg::{PeerMsg, parse_peermsg, parse_handshake};
use wire::action::PeerId;
use wire::peer::PeerServer;

const OUTSIDE_MSG: Token = Token(0);
pub type StreamId = u32;

pub struct Protocol {
    streams: HashMap<StreamId, (TcpStream, PeerStream)>,
    handler: PeerServer,
    poll: Poll,
    sender: Sender<ChanMsg>,
    receiver: Receiver<ChanMsg>,
    info: MetaInfo,
    info_hash: SHA1Hash20b,
    pending_actions: Vec<PeerAction>,
    stats: Stats,
    next_peer_id: usize,
}

const LOG_IN: bool = true;

#[derive(Debug, Copy, Clone)]
pub struct Stats {
    pub uploaded: usize,
    pub downloaded: usize,
    pub peer_count: usize,
}

impl Stats {
    pub fn new() -> Stats {
        Stats {
            uploaded: 0,
            downloaded: 0,
            peer_count: 0,
        }
    }
}

pub struct PeerStream {
    id: StreamId,
    bytes_in: Vec<u8>,
    bytes_out: Vec<u8>,
    handshake_sent: bool,
    handshake_received: bool,
    disconnected: bool,
}

impl PeerStream {
    pub fn new(id: StreamId) -> PeerStream {
        PeerStream {
            id: id,
            bytes_in: Vec::new(),
            bytes_out: Vec::new(),
            handshake_sent: true,
            handshake_received: false,
            disconnected: false,
        }
    }

    pub fn write_in(&mut self, mut bytes: Vec<u8>) {
        let path = &format!("{}.dat", self.id);
        if LOG_IN {
            if !Path::new(path).exists() {
                File::create(path);
            }
            let mut file =
                OpenOptions::new()
                .write(true)
                .append(true)
                .open(path)
                .unwrap();
        }
        self.bytes_in.append(&mut bytes);
    }

    pub fn write_out(&mut self, mut bytes: Vec<u8>) {
        info!("This goes out {:?}", bytes);
        self.bytes_out.append(&mut bytes);
    }

    pub fn message(&mut self) -> Option<PeerMsg> {
        info!("Bytes in: {}", self.bytes_in.len());
        if self.bytes_in.len() == 0 {
            return None;
        }

        match parse_peermsg(&self.bytes_in) {
            Ok((msg, offset)) => {
                info!("Parsed a message of len {}", offset);
                if offset < self.bytes_in.len() {
                    self.bytes_in = self.bytes_in.split_off(offset);
                } else {
                    self.bytes_in = Vec::new();
                }
                info!("Message: {:?}", msg);
                Some(msg)
            }
            Err(err) => {
                info!("Message not ready {:?}", err);
                // TODO It needs to distinguish between recoverable and non-recoverable
                None
            }
        }
    }

    pub fn take(&mut self, out: &mut Write) -> io::Result<usize> {
        const MAX_BYTES_WRITE: usize = 1024 * 128;
        info!("Attempting to write {} bytes", self.bytes_out.len());
        let result = out.write(&self.bytes_out);
        match result {
            Ok(offset) => {
                info!("Wrote {} bytes", offset);
                self.bytes_out = self.bytes_out.split_off(offset);
            }
            Err(ref err) => {
                info!("Writing failed: {}", err);
            }
        };
        result
    }

    pub fn len_in(&self) -> usize {
        self.bytes_in.len()
    }
}

#[derive(Debug)]
pub enum ChanMsg {
    NewPeer(IpAddr, u16),
    StatsRequest,
    StatsResponse(Stats),
    ThrottleOn(usize),
    ThrottleOff
}

impl Protocol {
    pub fn new(info: &MetaInfo,
               hash: SHA1Hash20b,
               our_peer_id: &str)
               -> (Protocol, Sender<ChanMsg>, Receiver<ChanMsg>) {
        let poll = Poll::new().unwrap();

        match (channel(), channel()) {
            ((to_inside, from_outside), (to_outside, from_inside)) => {

                poll.register(&from_outside,
                              OUTSIDE_MSG,
                              Ready::readable(),
                              PollOpt::level())
                    .unwrap();

                let proto = Protocol {
                    streams: HashMap::new(),
                    poll: poll,
                    sender: to_outside,
                    receiver: from_outside,
                    info: info.clone(),
                    info_hash: hash.clone(),
                    pending_actions: Vec::new(),
                    stats: Stats::new(),
                    handler: ServerHandler::new(info.clone(), hash.clone(), our_peer_id),
                    next_peer_id: 1,
                };

                (proto, to_inside, from_inside)
            }
        }
    }

    pub fn run(&mut self) {
        const EVENT_CAPACITY: usize = 32;
        let mut events = Events::with_capacity(EVENT_CAPACITY);
        loop {
            self.poll.poll(&mut events, None).unwrap();
            for event in events.iter() {
                self._handle_event(event);
            }
            info!("All events handled");
            let actions = self.handler.on_loop();
            for action in actions {
                match self.streams.get_mut(&action.0) {
                    Some(&mut (_, ref mut peer)) => {
                        Protocol::_perform_action(action, peer);
                    }
                    None => ()
                }
            }
        }
    }

    fn _handle_event(&mut self, event: Event) {
        match event.token() {
            OUTSIDE_MSG => {
                loop {
                    match self.receiver.try_recv() {
                        Ok(msg) => self._handle_outside_msg(msg),
                        _ => break,
                    }
                }
            }
            _ => self._handle_socket_event(event),
        }
    }

    fn _get_stream_id(&self, token: Token) -> Option<StreamId> {
        Some(match token {
            Token(id) => id as StreamId,
        })
    }

    fn _perform_action(action: PeerAction, peer: &mut PeerStream) {
        let id = action.0;
        match action.1 {
            PeerStreamAction::Nothing => (),
            PeerStreamAction::SendMessages(msgs) => {
                for msg in msgs {
                    info!("Writing message {:?}", msg);
                    let bytes = msg.into();
                    peer.write_out(bytes);
                }
            }
            PeerStreamAction::Disconnect => {
                peer.disconnected = true;
            }
        }
    }

    fn _handle_socket_event(&mut self, event: Event) {
        let kind = event.kind();
        let peer_id: StreamId = match self._get_stream_id(event.token()) {
            Some(p_id) => p_id,
            None => return,
        };

        if let Some((mut tcp_stream, mut peer_stream)) = self.streams.remove(&peer_id) {
            info!("Got event {:?} from peer id {}", event, peer_id);
            if (kind.is_readable()) {
                let read_result =
                    Protocol::_handle_read(&mut tcp_stream, &mut peer_stream, &mut self.handler);

                for action in read_result.0 {
                    Protocol::_perform_action(action, &mut peer_stream);
                }

                self.stats.uploaded += read_result.1;
            }

            let mut fatal_error_happened = false;
            if kind.is_writable() {
                // write pending messages
                let write_result =
                    Protocol::_handle_write(&mut tcp_stream, &mut peer_stream, &mut self.handler);

                if write_result.is_err() {
                    fatal_error_happened = true;
                }
            }

            if kind.is_hup() || kind.is_error() || fatal_error_happened {
                info!("Disconnected from peer id {}", peer_stream.id);
                self.poll.deregister(&tcp_stream);
                Protocol::_handle_hup(&mut tcp_stream, &mut peer_stream, &mut self.handler);

                // do not return socket to collection
                return;
            }

            self.streams.insert(peer_stream.id, (tcp_stream, peer_stream));
        }

    }

    fn _handle_read(socket: &mut TcpStream,
                    peer: &mut PeerStream,
                    handler: &mut PeerServer)
                    -> (Vec<PeerAction>, usize) {

        const READ_BUF_SIZE: usize = 1024;
        let mut buf = Vec::with_capacity(READ_BUF_SIZE);
        buf.resize(READ_BUF_SIZE, 0);
        let bytes_read = match socket.read(&mut buf) {
            Ok(bytes_read) => {
                peer.write_in(buf);
                info!("Read {} bytes from {}", bytes_read, peer.id);//, socket.peer_addr());
                bytes_read
            }
            Err(err) => 0,
        };

        // Attempt to parse a handshake first

        let mut actions = Vec::new();
        loop {
            let msg = if !peer.handshake_received {
                match parse_handshake(&peer.bytes_in) {
                    Ok((handshake, offset)) => {
                        peer.bytes_in = peer.bytes_in.split_off(offset);
                        peer.handshake_received = true;
                        info!("Handshake received: {:?}", handshake);
                        Some(handshake)
                    }
                    Err(e) => {
                        info!("Error parsing handshake: {:?}", e);
                        None
                    }
                }
            } else {
                peer.message()
            };

            let actions = match msg {
                Some(msg) => {
                    info!("Received from peer {:?}", msg);
                    actions.push(handler.on_message_receive(peer.id, msg));
                }
                None => {
                    info!("Error parsing. {} bytes left", peer.bytes_in.len());
                    return (Vec::new(), bytes_read);
                }
            };
        }

        info!("Read {} bytes", bytes_read);
        (actions, bytes_read)
    }

    fn _handle_write(socket: &mut TcpStream,
                     peer: &mut PeerStream,
                     handler: &mut PeerServer)
                     -> io::Result<usize> {
        peer.take(socket)
    }

    fn _handle_hup(socket: &mut TcpStream, peer: &mut PeerStream, handler: &mut PeerServer) {
        handler.on_peer_disconnect(peer.id);
    }

    fn _handle_outside_msg(&mut self, msg: ChanMsg) {
        match msg {
            ChanMsg::NewPeer(ip, port) => self._handle_new_peer(ip, port),
            ChanMsg::StatsRequest => {
                self.sender.send(ChanMsg::StatsResponse(self.stats));
            }
            _ => (),
        }
    }

    fn _handle_new_peer(&mut self, addr: IpAddr, port: u16) {
        for (id, &(ref socket, _)) in &self.streams {
            match (socket.peer_addr(), addr) {
                (Ok(SocketAddr::V4(peer)), IpAddr::V4(p)) => {
                    if peer.port() == port && peer.ip().octets() == p.octets() {
                        return;
                    }
                }
                (Ok(SocketAddr::V6(peer)), IpAddr::V6(p)) => unimplemented!(),
                (Err(e), _) => {
                    continue;
                }
                _ => continue,
            }
        }

        match self._connect_to_peer(addr, port) {
            Some((sock, Token(id_usize))) => {
                let id = id_usize as u32;
                let mut stream = PeerStream::new(id);
                let action = self.handler.on_peer_connect(id);

                Protocol::_perform_action(action, &mut stream);

                self.streams.insert(id, (sock, stream));
            }
            None => (),
        }
    }

    fn _connect_to_peer(&mut self, addr: IpAddr, port: u16) -> Option<(TcpStream, Token)> {
        let sock_addr = SocketAddr::new(addr, port);
        let token = Token(self.next_peer_id);
        info!("Trying to connect to {} on port {}", addr, port);
        match TcpStream::connect(&sock_addr) {
            Ok(sock) => {
                self.poll
                    .register(&sock, token, Ready::all(), PollOpt::edge());

                self.next_peer_id += 1;
                return Some((sock, token));
            }
            Err(e) => {
                info!("Could not connect: {}", e);
                return None;
            }
        }
    }
}
<|endoftext|>


<commit_before>//! This module defines a cheaply-copyable cursor into a TokenStream's data.
//!
//! It does this by copying the data into a stably-addressed structured buffer,
//! and holding raw pointers into that buffer to allow walking through delimited
//! sequences cheaply.
//!
//! This module is heavily commented as it contains the only unsafe code in
//! `syn`, and caution should be made when editing it. It provides a safe
//! interface, but is fragile internally.

use proc_macro2::*;

use std::ptr;
use std::fmt;
use std::marker::PhantomData;

/// Internal type which is used instead of `TokenTree` to represent a single
/// `TokenTree` within a `SynomBuffer`.
#[derive(Debug)]
enum Entry {
    /// Mimicing types from proc-macro.
    Sequence(Span, Delimiter, SynomBuffer),
    Word(Span, Symbol),
    Op(Span, char, OpKind),
    Literal(Span, Literal),
    /// End entries contain a raw pointer to the entry from the containing
    /// TokenTree.
    End(*const Entry),
}

// A `Sync` impl is necessary here because we need a global `Entry` object for
// the empty sequence case. This impl is requied as `*const Entry` is neither
// `Send` nor `Sync` by default.
unsafe impl Sync for Entry {}
// We also implement `Send` for completeness, but we never use it.
unsafe impl Send for Entry {}

/// A buffer of data which contains a structured representation of the input
/// `TokenStream` object.
#[derive(Debug)]
pub struct SynomBuffer {
    // NOTE: Do not derive clone on this - there are raw pointers inside which
    // will be messed up. Moving the `SynomBuffer` itself is safe as the actual
    // backing slices won't be moved.
    data: Box<[Entry]>,
}

impl SynomBuffer {
    // NOTE: DO NOT MUTATE THE `Vec` RETURNED FROM THIS FUNCTION ONCE IT
    // RETURNS, THE ADDRESS OF ITS BACKING MEMORY MUST REMAIN STABLE.
    fn inner_new(stream: TokenStream, up: *const Entry) -> SynomBuffer {
        // Build up the entries list, recording the locations of any Sequences
        // in the list to be processed later.
        let mut entries = Vec::new();
        let mut seqs = Vec::new();
        for tt in stream.into_iter() {
            match tt.kind {
                TokenKind::Word(sym) => {
                    entries.push(Entry::Word(tt.span, sym));
                }
                TokenKind::Op(chr, ok) => {
                    entries.push(Entry::Op(tt.span, chr, ok));
                }
                TokenKind::Literal(lit) => {
                    entries.push(Entry::Literal(tt.span, lit));
                }
                TokenKind::Sequence(delim, seq_stream) => {
                    // Record the index of the interesting entry, and store an
                    // `End(null)` there temporarially.
                    seqs.push((entries.len(), tt.span, delim, seq_stream));
                    entries.push(Entry::End(ptr::null()));
                }
            }
        }
        // Add an `End` entry to the end with a reference to the enclosing token
        // stream which was passed in.
        entries.push(Entry::End(up));

        // NOTE: This is done to ensure that we don't accidentally modify the
        // length of the backing buffer. The backing buffer must remain at a
        // constant address after this point, as we are going to store a raw
        // pointer into it.
        let mut entries = entries.into_boxed_slice();
        for (idx, span, delim, seq_stream) in seqs {
            // We know that this index refers to one of the temporary
            // `End(null)` entries, and we know that the last entry is
            // `End(up)`, so the next index is also valid.
            let seq_up = &entries[idx + 1] as *const Entry;

            // The end entry stored at the end of this Entry::Sequence should
            // point to the Entry which follows the Sequence in the list.
            let inner = Self::inner_new(seq_stream, seq_up);
            entries[idx] = Entry::Sequence(span, delim, inner);
        }

        SynomBuffer {
            data: entries
        }
    }

    /// Create a new SynomBuffer, which contains the data from the given
    /// TokenStream.
    pub fn new(stream: TokenStream) -> SynomBuffer {
        Self::inner_new(stream, ptr::null())
    }

    /// Create a cursor referencing the first token in the input.
    pub fn begin(&self) -> Cursor {
        unsafe {
            Cursor::create(&self.data[0], &self.data[self.data.len() - 1])
        }
    }
}

#[derive(Copy, Clone, Debug)]
pub struct SeqInfo<'a> {
    pub span: Span,
    pub inside: Cursor<'a>,
    pub outside: Cursor<'a>,
}

/// A cursor into an input `TokenStream`'s data. This cursor holds a reference
/// into the immutable data which is used internally to represent a
/// `TokenStream`, and can be efficiently manipulated and copied around.
///
/// An empty `Cursor` can be created directly, or one may create a `SynomBuffer`
/// object and get a cursor to its first token with `begin()`.
///
/// Two cursors are equal if they have the same location in the same input
/// stream, and have the same scope.
#[derive(Copy, Clone, Eq, PartialEq)]
pub struct Cursor<'a> {
    /// The current entry which the `Cursor` is pointing at.
    ptr: *const Entry,
    /// This is the only `Entry::End(..)` object which this cursor is allowed to
    /// point at. All other `End` objects are skipped over in `Cursor::create`.
    scope: *const Entry,
    /// This uses the &'a reference which guarantees that these pointers are
    /// still valid.
    marker: PhantomData<&'a Entry>,
}

// We can safely implement `Send` and `Sync` for `Cursor` because it is
// basically an `&Entry`.
unsafe impl<'a> Send for Cursor<'a> {}
unsafe impl<'a> Sync for Cursor<'a> {}

impl<'a> Cursor<'a> {
    /// Create a cursor referencing a static empty TokenStream.
    pub fn empty() -> Self {
        static EMPTY_ENTRY: Entry = Entry::End(0 as *const Entry);
        Cursor {
            ptr: &EMPTY_ENTRY,
            scope: &EMPTY_ENTRY,
            marker: PhantomData,
        }
    }

    /// This create method intelligently exits non-explicitly-entered
    /// `None`-delimited scopes when the cursor reaches the end of them,
    /// allowing for them to be treated transparently.
    unsafe fn create(mut ptr: *const Entry, scope: *const Entry) -> Self {
        // NOTE: If we're looking at a `End(..)`, we want to advance the cursor
        // past it, unless `ptr == scope`, which means that we're at the edge of
        // our cursor's scope. We should only have `ptr != scope` at the exit
        // from None-delimited sequences entered with `ignore_none`.
        while let Entry::End(exit) = *ptr {
            if ptr == scope {
                break;
            }
            ptr = exit;
        }

        Cursor {
            ptr: ptr,
            scope: scope,
            marker: PhantomData,
        }
    }

    /// Get the current entry.
    fn entry(self) -> &'a Entry {
        unsafe { &*self.ptr }
    }

    /// Bump the cursor to point at the next token after the current one. This
    /// is undefined behavior if the cursor is currently looking at an
    /// `Entry::End`.
    unsafe fn bump(self) -> Cursor<'a> {
        Cursor::create(self.ptr.offset(1), self.scope)
    }

    /// If the cursor is looking at a `None`-delimited sequence, move it to look
    /// at the first token inside instead. If the sequence is empty, this will
    /// move the cursor past the `None`-delimited sequence.
    ///
    /// WARNING: This mutates its argument.
    fn ignore_none(&mut self) {
        if let Entry::Sequence(_, Delimiter::None, ref buf) = *self.entry() {
            // NOTE: We call `Cursor::create` here to make sure that situations
            // where we should immediately exit the span after entering it are
            // handled correctly.
            unsafe {
                *self = Cursor::create(&buf.data[0], self.scope);
            }
        }
    }

    /// Check if the cursor is currently pointing at the end of its valid scope.
    #[inline]
    pub fn eof(self) -> bool {
        // We're at eof if we're at the end of our scope.
        self.ptr == self.scope
    }

    /// If the cursor is pointing at a Seq with the given `Delimiter`, return a
    /// cursor into that sequence, and one pointing to the next `TokenTree`.
    pub fn seq(mut self, seq_delim: Delimiter) -> Option<SeqInfo<'a>> {
        // If we're not trying to enter a none-delimited sequence, we want to
        // ignore them. We have to make sure to _not_ ignore them when we want
        // to enter them, of course. For obvious reasons.
        if seq_delim != Delimiter::None {
            self.ignore_none();
        }

        match *self.entry() {
            Entry::Sequence(span, delim, ref buf) => {
                if delim != seq_delim {
                    return None;
                }

                Some(SeqInfo {
                    span: span,
                    inside: buf.begin(),
                    outside: unsafe { self.bump() },
                })
            }
            _ => None
        }
    }

    /// If the cursor is pointing at a Word, return it and a cursor pointing at
    /// the next `TokenTree`.
    pub fn word(mut self) -> Option<(Cursor<'a>, Span, Symbol)> {
        self.ignore_none();
        match *self.entry() {
            Entry::Word(span, sym) => {
                Some((
                    unsafe { self.bump() },
                    span,
                    sym,
                ))
            }
            _ => None
        }
    }

    /// If the cursor is pointing at an Op, return it and a cursor pointing
    /// at the next `TokenTree`.
    pub fn op(mut self) -> Option<(Cursor<'a>, Span, char, OpKind)> {
        self.ignore_none();
        match *self.entry() {
            Entry::Op(span, chr, kind) => {
                Some((
                    unsafe { self.bump() },
                    span,
                    chr,
                    kind,
                ))
            }
            _ => None
        }
    }

    /// If the cursor is pointing at a Literal, return it and a cursor pointing
    /// at the next `TokenTree`.
    pub fn literal(mut self) -> Option<(Cursor<'a>, Span, Literal)> {
        self.ignore_none();
        match *self.entry() {
            Entry::Literal(span, ref lit) => {
                Some((
                    unsafe { self.bump() },
                    span,
                    lit.clone(),
                ))
            }
            _ => None
        }
    }

    /// Copy all remaining tokens visible from this cursor into a `TokenStream`.
    pub fn token_stream(self) -> TokenStream {
        let mut tts = Vec::new();
        let mut cursor = self;
        while let Some((next, tt)) = cursor.token_tree() {
            tts.push(tt);
            cursor = next;
        }
        tts.into_iter().collect()
    }

    /// If the cursor is looking at a `TokenTree`, returns it along with a
    /// cursor pointing to the next token in the sequence, otherwise returns
    /// `None`.
    ///
    /// This method does not treat `None`-delimited sequences as invisible, and
    /// will return a `Sequence(None, ..)` if the cursor is looking at one.
    pub fn token_tree(self) -> Option<(Cursor<'a>, TokenTree)> {
        let tree = match *self.entry() {
            Entry::Sequence(span, delim, ref buf) => {
                let stream = buf.begin().token_stream();
                TokenTree {
                    span: span,
                    kind: TokenKind::Sequence(delim, stream),
                }
            }
            Entry::Literal(span, ref lit) => {
                TokenTree {
                    span: span,
                    kind: TokenKind::Literal(lit.clone()),
                }
            }
            Entry::Word(span, sym) => {
                TokenTree {
                    span: span,
                    kind: TokenKind::Word(sym),
                }
            }
            Entry::Op(span, chr, kind) => {
                TokenTree {
                    span: span,
                    kind: TokenKind::Op(chr, kind),
                }
            }
            Entry::End(..) => {
                return None;
            }
        };

        Some((
            unsafe { self.bump() },
            tree
        ))
    }
}

// We do a custom implementation for `Debug` as the default implementation is
// pretty useless.
impl<'a> fmt::Debug for Cursor<'a> {
    fn fmt(&self, f: &mut fmt::Formatter) -> fmt::Result {
        f.debug_struct("Cursor")
            .field("ptr", &self.ptr)
            .field("scope", &self.scope)
            // Dummy `entry` field to show data behind the `ptr` ptr.
            .field("entry", self.entry())
            .finish()
    }
}
<commit_msg>Ensure that `Entry` is neither `Send` nor `Sync`<commit_after>//! This module defines a cheaply-copyable cursor into a TokenStream's data.
//!
//! It does this by copying the data into a stably-addressed structured buffer,
//! and holding raw pointers into that buffer to allow walking through delimited
//! sequences cheaply.
//!
//! This module is heavily commented as it contains the only unsafe code in
//! `syn`, and caution should be made when editing it. It provides a safe
//! interface, but is fragile internally.

use proc_macro2::*;

use std::ptr;
use std::fmt;
use std::marker::PhantomData;

/// Internal type which is used instead of `TokenTree` to represent a single
/// `TokenTree` within a `SynomBuffer`.
#[derive(Debug)]
enum Entry {
    /// Mimicing types from proc-macro.
    Sequence(Span, Delimiter, SynomBuffer),
    Word(Span, Symbol),
    Op(Span, char, OpKind),
    Literal(Span, Literal),
    /// End entries contain a raw pointer to the entry from the containing
    /// TokenTree.
    End(*const Entry),
}

/// A buffer of data which contains a structured representation of the input
/// `TokenStream` object.
#[derive(Debug)]
pub struct SynomBuffer {
    // NOTE: Do not derive clone on this - there are raw pointers inside which
    // will be messed up. Moving the `SynomBuffer` itself is safe as the actual
    // backing slices won't be moved.
    data: Box<[Entry]>,
}

impl SynomBuffer {
    // NOTE: DO NOT MUTATE THE `Vec` RETURNED FROM THIS FUNCTION ONCE IT
    // RETURNS, THE ADDRESS OF ITS BACKING MEMORY MUST REMAIN STABLE.
    fn inner_new(stream: TokenStream, up: *const Entry) -> SynomBuffer {
        // Build up the entries list, recording the locations of any Sequences
        // in the list to be processed later.
        let mut entries = Vec::new();
        let mut seqs = Vec::new();
        for tt in stream.into_iter() {
            match tt.kind {
                TokenKind::Word(sym) => {
                    entries.push(Entry::Word(tt.span, sym));
                }
                TokenKind::Op(chr, ok) => {
                    entries.push(Entry::Op(tt.span, chr, ok));
                }
                TokenKind::Literal(lit) => {
                    entries.push(Entry::Literal(tt.span, lit));
                }
                TokenKind::Sequence(delim, seq_stream) => {
                    // Record the index of the interesting entry, and store an
                    // `End(null)` there temporarially.
                    seqs.push((entries.len(), tt.span, delim, seq_stream));
                    entries.push(Entry::End(ptr::null()));
                }
            }
        }
        // Add an `End` entry to the end with a reference to the enclosing token
        // stream which was passed in.
        entries.push(Entry::End(up));

        // NOTE: This is done to ensure that we don't accidentally modify the
        // length of the backing buffer. The backing buffer must remain at a
        // constant address after this point, as we are going to store a raw
        // pointer into it.
        let mut entries = entries.into_boxed_slice();
        for (idx, span, delim, seq_stream) in seqs {
            // We know that this index refers to one of the temporary
            // `End(null)` entries, and we know that the last entry is
            // `End(up)`, so the next index is also valid.
            let seq_up = &entries[idx + 1] as *const Entry;

            // The end entry stored at the end of this Entry::Sequence should
            // point to the Entry which follows the Sequence in the list.
            let inner = Self::inner_new(seq_stream, seq_up);
            entries[idx] = Entry::Sequence(span, delim, inner);
        }

        SynomBuffer {
            data: entries
        }
    }

    /// Create a new SynomBuffer, which contains the data from the given
    /// TokenStream.
    pub fn new(stream: TokenStream) -> SynomBuffer {
        Self::inner_new(stream, ptr::null())
    }

    /// Create a cursor referencing the first token in the input.
    pub fn begin(&self) -> Cursor {
        unsafe {
            Cursor::create(&self.data[0], &self.data[self.data.len() - 1])
        }
    }
}

#[derive(Copy, Clone, Debug)]
pub struct SeqInfo<'a> {
    pub span: Span,
    pub inside: Cursor<'a>,
    pub outside: Cursor<'a>,
}

/// A cursor into an input `TokenStream`'s data. This cursor holds a reference
/// into the immutable data which is used internally to represent a
/// `TokenStream`, and can be efficiently manipulated and copied around.
///
/// An empty `Cursor` can be created directly, or one may create a `SynomBuffer`
/// object and get a cursor to its first token with `begin()`.
///
/// Two cursors are equal if they have the same location in the same input
/// stream, and have the same scope.
#[derive(Copy, Clone, Eq, PartialEq)]
pub struct Cursor<'a> {
    /// The current entry which the `Cursor` is pointing at.
    ptr: *const Entry,
    /// This is the only `Entry::End(..)` object which this cursor is allowed to
    /// point at. All other `End` objects are skipped over in `Cursor::create`.
    scope: *const Entry,
    /// This uses the &'a reference which guarantees that these pointers are
    /// still valid.
    marker: PhantomData<&'a Entry>,
}

impl<'a> Cursor<'a> {
    /// Create a cursor referencing a static empty TokenStream.
    pub fn empty() -> Self {
        // It's safe in this situation for us to put an `Entry` object in global
        // storage, despite it not actually being safe to send across threads
        // (`Symbol` is a reference into a thread-local table). This is because
        // this entry never includes a `Symbol` object.
        //
        // This wrapper struct allows us to break the rules and put a `Sync`
        // object in global storage.
        struct UnsafeSyncEntry(Entry);
        unsafe impl Sync for UnsafeSyncEntry {}
        static EMPTY_ENTRY: UnsafeSyncEntry =
            UnsafeSyncEntry(Entry::End(0 as *const Entry));

        Cursor {
            ptr: &EMPTY_ENTRY.0,
            scope: &EMPTY_ENTRY.0,
            marker: PhantomData,
        }
    }

    /// This create method intelligently exits non-explicitly-entered
    /// `None`-delimited scopes when the cursor reaches the end of them,
    /// allowing for them to be treated transparently.
    unsafe fn create(mut ptr: *const Entry, scope: *const Entry) -> Self {
        // NOTE: If we're looking at a `End(..)`, we want to advance the cursor
        // past it, unless `ptr == scope`, which means that we're at the edge of
        // our cursor's scope. We should only have `ptr != scope` at the exit
        // from None-delimited sequences entered with `ignore_none`.
        while let Entry::End(exit) = *ptr {
            if ptr == scope {
                break;
            }
            ptr = exit;
        }

        Cursor {
            ptr: ptr,
            scope: scope,
            marker: PhantomData,
        }
    }

    /// Get the current entry.
    fn entry(self) -> &'a Entry {
        unsafe { &*self.ptr }
    }

    /// Bump the cursor to point at the next token after the current one. This
    /// is undefined behavior if the cursor is currently looking at an
    /// `Entry::End`.
    unsafe fn bump(self) -> Cursor<'a> {
        Cursor::create(self.ptr.offset(1), self.scope)
    }

    /// If the cursor is looking at a `None`-delimited sequence, move it to look
    /// at the first token inside instead. If the sequence is empty, this will
    /// move the cursor past the `None`-delimited sequence.
    ///
    /// WARNING: This mutates its argument.
    fn ignore_none(&mut self) {
        if let Entry::Sequence(_, Delimiter::None, ref buf) = *self.entry() {
            // NOTE: We call `Cursor::create` here to make sure that situations
            // where we should immediately exit the span after entering it are
            // handled correctly.
            unsafe {
                *self = Cursor::create(&buf.data[0], self.scope);
            }
        }
    }

    /// Check if the cursor is currently pointing at the end of its valid scope.
    #[inline]
    pub fn eof(self) -> bool {
        // We're at eof if we're at the end of our scope.
        self.ptr == self.scope
    }

    /// If the cursor is pointing at a Seq with the given `Delimiter`, return a
    /// cursor into that sequence, and one pointing to the next `TokenTree`.
    pub fn seq(mut self, seq_delim: Delimiter) -> Option<SeqInfo<'a>> {
        // If we're not trying to enter a none-delimited sequence, we want to
        // ignore them. We have to make sure to _not_ ignore them when we want
        // to enter them, of course. For obvious reasons.
        if seq_delim != Delimiter::None {
            self.ignore_none();
        }

        match *self.entry() {
            Entry::Sequence(span, delim, ref buf) => {
                if delim != seq_delim {
                    return None;
                }

                Some(SeqInfo {
                    span: span,
                    inside: buf.begin(),
                    outside: unsafe { self.bump() },
                })
            }
            _ => None
        }
    }

    /// If the cursor is pointing at a Word, return it and a cursor pointing at
    /// the next `TokenTree`.
    pub fn word(mut self) -> Option<(Cursor<'a>, Span, Symbol)> {
        self.ignore_none();
        match *self.entry() {
            Entry::Word(span, sym) => {
                Some((
                    unsafe { self.bump() },
                    span,
                    sym,
                ))
            }
            _ => None
        }
    }

    /// If the cursor is pointing at an Op, return it and a cursor pointing
    /// at the next `TokenTree`.
    pub fn op(mut self) -> Option<(Cursor<'a>, Span, char, OpKind)> {
        self.ignore_none();
        match *self.entry() {
            Entry::Op(span, chr, kind) => {
                Some((
                    unsafe { self.bump() },
                    span,
                    chr,
                    kind,
                ))
            }
            _ => None
        }
    }

    /// If the cursor is pointing at a Literal, return it and a cursor pointing
    /// at the next `TokenTree`.
    pub fn literal(mut self) -> Option<(Cursor<'a>, Span, Literal)> {
        self.ignore_none();
        match *self.entry() {
            Entry::Literal(span, ref lit) => {
                Some((
                    unsafe { self.bump() },
                    span,
                    lit.clone(),
                ))
            }
            _ => None
        }
    }

    /// Copy all remaining tokens visible from this cursor into a `TokenStream`.
    pub fn token_stream(self) -> TokenStream {
        let mut tts = Vec::new();
        let mut cursor = self;
        while let Some((next, tt)) = cursor.token_tree() {
            tts.push(tt);
            cursor = next;
        }
        tts.into_iter().collect()
    }

    /// If the cursor is looking at a `TokenTree`, returns it along with a
    /// cursor pointing to the next token in the sequence, otherwise returns
    /// `None`.
    ///
    /// This method does not treat `None`-delimited sequences as invisible, and
    /// will return a `Sequence(None, ..)` if the cursor is looking at one.
    pub fn token_tree(self) -> Option<(Cursor<'a>, TokenTree)> {
        let tree = match *self.entry() {
            Entry::Sequence(span, delim, ref buf) => {
                let stream = buf.begin().token_stream();
                TokenTree {
                    span: span,
                    kind: TokenKind::Sequence(delim, stream),
                }
            }
            Entry::Literal(span, ref lit) => {
                TokenTree {
                    span: span,
                    kind: TokenKind::Literal(lit.clone()),
                }
            }
            Entry::Word(span, sym) => {
                TokenTree {
                    span: span,
                    kind: TokenKind::Word(sym),
                }
            }
            Entry::Op(span, chr, kind) => {
                TokenTree {
                    span: span,
                    kind: TokenKind::Op(chr, kind),
                }
            }
            Entry::End(..) => {
                return None;
            }
        };

        Some((
            unsafe { self.bump() },
            tree
        ))
    }
}

// We do a custom implementation for `Debug` as the default implementation is
// pretty useless.
impl<'a> fmt::Debug for Cursor<'a> {
    fn fmt(&self, f: &mut fmt::Formatter) -> fmt::Result {
        f.debug_struct("Cursor")
            .field("ptr", &self.ptr)
            .field("scope", &self.scope)
            // Dummy `entry` field to show data behind the `ptr` ptr.
            .field("entry", self.entry())
            .finish()
    }
}
<|endoftext|>


<commit_before>// Copyright 2018 The Chromium OS Authors. All rights reserved.
// Use of this source code is governed by a BSD-style license that can be
// found in the LICENSE file.

// Exported interface to basic qcow functionality to be used from C.

extern crate libc;
extern crate qcow;

use libc::{EINVAL, EIO};
use std::ffi::CStr;
use std::fs::{File, OpenOptions};
use std::os::raw::{c_char, c_int};
use std::os::unix::io::FromRawFd;

use qcow::{ImageType, QcowFile};

#[no_mangle]
pub unsafe extern "C" fn create_qcow_with_size(path: *const c_char, virtual_size: u64) -> c_int {
    // NULL pointers are checked, but this will access any other invalid pointer passed from C
    // code. It's the caller's responsibility to pass a valid pointer.
    if path.is_null() {
        return -EINVAL;
    }
    let c_str = CStr::from_ptr(path);
    let file_path = match c_str.to_str() {
        Ok(s) => s,
        Err(_) => return -EINVAL,
    };

    let file = match OpenOptions::new()
        .create(true)
        .read(true)
        .write(true)
        .open(file_path)
    {
        Ok(f) => f,
        Err(_) => return -1,
    };

    match QcowFile::new(file, virtual_size) {
        Ok(_) => 0,
        Err(_) => -1,
    }
}

#[no_mangle]
pub unsafe extern "C" fn convert_to_qcow2(src_fd: c_int, dst_fd: c_int) -> c_int {
    // The caller is responsible for passing valid file descriptors.
    let src_file = File::from_raw_fd(src_fd);
    let dst_file = File::from_raw_fd(dst_fd);

    match qcow::convert(src_file, dst_file, ImageType::Qcow2) {
        Ok(_) => 0,
        Err(_) => -EIO,
    }
}

#[no_mangle]
pub unsafe extern "C" fn convert_to_raw(src_fd: c_int, dst_fd: c_int) -> c_int {
    // The caller is responsible for passing valid file descriptors.
    let src_file = File::from_raw_fd(src_fd);
    let dst_file = File::from_raw_fd(dst_fd);

    match qcow::convert(src_file, dst_file, ImageType::Raw) {
        Ok(_) => 0,
        Err(_) => -EIO,
    }
}
<commit_msg>qcow_utils: do not close given fds in `convert_to_*` functions<commit_after>// Copyright 2018 The Chromium OS Authors. All rights reserved.
// Use of this source code is governed by a BSD-style license that can be
// found in the LICENSE file.

// Exported interface to basic qcow functionality to be used from C.

extern crate libc;
extern crate qcow;

use libc::{EBADFD, EINVAL, EIO};
use std::ffi::CStr;
use std::fs::{File, OpenOptions};
use std::mem::forget;
use std::os::raw::{c_char, c_int};
use std::os::unix::io::FromRawFd;
use std::panic::catch_unwind;

use qcow::{ImageType, QcowFile};

#[no_mangle]
pub unsafe extern "C" fn create_qcow_with_size(path: *const c_char, virtual_size: u64) -> c_int {
    // NULL pointers are checked, but this will access any other invalid pointer passed from C
    // code. It's the caller's responsibility to pass a valid pointer.
    if path.is_null() {
        return -EINVAL;
    }
    let c_str = CStr::from_ptr(path);
    let file_path = match c_str.to_str() {
        Ok(s) => s,
        Err(_) => return -EINVAL,
    };

    let file = match OpenOptions::new()
        .create(true)
        .read(true)
        .write(true)
        .open(file_path)
    {
        Ok(f) => f,
        Err(_) => return -1,
    };

    match QcowFile::new(file, virtual_size) {
        Ok(_) => 0,
        Err(_) => -1,
    }
}

#[no_mangle]
pub unsafe extern "C" fn convert_to_qcow2(src_fd: c_int, dst_fd: c_int) -> c_int {
    // The caller is responsible for passing valid file descriptors.
    // The caller retains ownership of the file descriptors.
    let src_file = File::from_raw_fd(src_fd);
    let src_file_owned = src_file.try_clone();
    forget(src_file);

    let dst_file = File::from_raw_fd(dst_fd);
    let dst_file_owned = dst_file.try_clone();
    forget(dst_file);

    match (src_file_owned, dst_file_owned) {
        (Ok(src_file), Ok(dst_file)) => {
            catch_unwind(
                || match qcow::convert(src_file, dst_file, ImageType::Qcow2) {
                    Ok(_) => 0,
                    Err(_) => -EIO,
                },
            ).unwrap_or(-EIO)
        }
        _ => -EBADFD,
    }
}

#[no_mangle]
pub unsafe extern "C" fn convert_to_raw(src_fd: c_int, dst_fd: c_int) -> c_int {
    // The caller is responsible for passing valid file descriptors.
    // The caller retains ownership of the file descriptors.
    let src_file = File::from_raw_fd(src_fd);
    let src_file_owned = src_file.try_clone();
    forget(src_file);

    let dst_file = File::from_raw_fd(dst_fd);
    let dst_file_owned = dst_file.try_clone();
    forget(dst_file);

    match (src_file_owned, dst_file_owned) {
        (Ok(src_file), Ok(dst_file)) => {
            catch_unwind(|| match qcow::convert(src_file, dst_file, ImageType::Raw) {
                Ok(_) => 0,
                Err(_) => -EIO,
            }).unwrap_or(-EIO)
        }
        _ => -EBADFD,
    }
}
<|endoftext|>