1
  2
  3
  4
  5
  6
  7
  8
  9
 10
 11
 12
 13
 14
 15
 16
 17
 18
 19
 20
 21
 22
 23
 24
 25
 26
 27
 28
 29
 30
 31
 32
 33
 34
 35
 36
 37
 38
 39
 40
 41
 42
 43
 44
 45
 46
 47
 48
 49
 50
 51
 52
 53
 54
 55
 56
 57
 58
 59
 60
 61
 62
 63
 64
 65
 66
 67
 68
 69
 70
 71
 72
 73
 74
 75
 76
 77
 78
 79
 80
 81
 82
 83
 84
 85
 86
 87
 88
 89
 90
 91
 92
 93
 94
 95
 96
 97
 98
 99
100
101
102
103
104
105
106
107
108
109
110
111
112
113
114
115
use {CANCELLED_TWICE, POLLED_TWICE, Spawn};
use conch_parser::ast;
use env::{FileDescEnvironment, SubEnvironment};
use future::{EnvFuture, Poll};
use io::FileDesc;
use spawn::{ExitResult, Pipeline, pipeline, SpawnedPipeline};
use std::fmt;
use std::io;
use std::iter;

/// A future representing the spawning of a `ListableCommand`.
#[must_use = "futures do nothing unless polled"]
pub struct ListableCommand<S, E>
    where S: Spawn<E>,
{
    state: State<Pipeline<S, E>>,
}

impl<S, E> fmt::Debug for ListableCommand<S, E>
    where S: Spawn<E> + fmt::Debug,
          S::EnvFuture: fmt::Debug,
          S::Future: fmt::Debug,
          S::Error: fmt::Debug,
          E: fmt::Debug,
{
    fn fmt(&self, fmt: &mut fmt::Formatter) -> fmt::Result {
        fmt.debug_struct("ListableCommand")
            .field("state", &self.state)
            .finish()
    }
}

#[derive(Debug)]
enum State<F> {
    Init(Option<io::Result<F>>),
    Spawned(F),
}

impl<S, E> Spawn<E> for ast::ListableCommand<S>
    where S: Spawn<E>,
          S::Error: From<io::Error>,
          E: FileDescEnvironment + SubEnvironment,
          E::FileHandle: From<FileDesc> + Clone,
{
    type EnvFuture = ListableCommand<S, E>;
    type Future = ExitResult<SpawnedPipeline<S, E>>;
    type Error = S::Error;

    fn spawn(self, env: &E) -> Self::EnvFuture {
        let pipeline = match self {
            ast::ListableCommand::Single(cmd) => pipeline(false, iter::once(cmd), env),
            ast::ListableCommand::Pipe(invert, cmds) => pipeline(invert, cmds, env),
        };

        ListableCommand {
            state: State::Init(Some(pipeline)),
        }
    }
}

impl<'a, S: 'a, E> Spawn<E> for &'a ast::ListableCommand<S>
    where &'a S: Spawn<E>,
          <&'a S as Spawn<E>>::Error: From<io::Error>,
          E: FileDescEnvironment + SubEnvironment,
          E::FileHandle: From<FileDesc> + Clone,
{
    type EnvFuture = ListableCommand<&'a S, E>;
    type Future = ExitResult<SpawnedPipeline<&'a S, E>>;
    type Error = <&'a S as Spawn<E>>::Error;

    fn spawn(self, env: &E) -> Self::EnvFuture {
        let pipeline = match *self {
            ast::ListableCommand::Single(ref cmd) => pipeline(false, iter::once(cmd), env),
            ast::ListableCommand::Pipe(invert, ref cmds) => pipeline(invert, cmds, env),
        };

        ListableCommand {
            state: State::Init(Some(pipeline)),
        }
    }
}

impl<S, E> EnvFuture<E> for ListableCommand<S, E>
    where S: Spawn<E>,
          S::Error: From<io::Error>,
          E: FileDescEnvironment + SubEnvironment,
          E::FileHandle: From<FileDesc> + Clone,
{
    type Item = ExitResult<SpawnedPipeline<S, E>>;
    type Error = S::Error;

    fn poll(&mut self, env: &mut E) -> Poll<Self::Item, Self::Error> {
        loop {
            let next_state = match self.state {
                State::Init(ref mut pipeline) => {
                    let pipeline = pipeline.take().expect(POLLED_TWICE);
                    State::Spawned(try!(pipeline))
                },

                State::Spawned(ref mut f) => return f.poll(env),
            };

            self.state = next_state;
        };
    }

    fn cancel(&mut self, env: &mut E) {
        match self.state {
            State::Init(ref mut pipeline) => {
                drop(pipeline.take().expect(CANCELLED_TWICE));
            },
            State::Spawned(ref mut f) => f.cancel(env),
        }
    }
}