Lifetime for the function passed, which is then executed in the thread

I am trying to create a simple construction, similar to a pipeline, which performs each stage of the pipeline - these are separate threads and glue them all together with the transmission of the channel.

Pipe::source(buffer)
     .pipe(|input, output| {...})
     .pipe(|input, output| {...})
     .sink(writer)

For the rest of my life, I cannot understand the number of a function for a function pipe(). Here is my code:

use std::sync::mpsc::channel;
use std::io::{ChanReader,ChanWriter};
use std::thread::Thread;

struct Pipe {
    incoming: ChanReader
}

impl Pipe {
    fn source(source: &mut Buffer) -> Pipe {
        let (tx, rx) = channel();
        let reader = ChanReader::new(rx);
        let mut writer = ChanWriter::new(tx);

        loop {
            match source.read_char() {
                Ok(c) => writer.write_char(c),
                Err(_) => break
            };
        };

        Pipe { incoming: reader }
    }

    fn sink(&mut self, sink: &mut Writer) {
        loop {
            match self.incoming.read_char() {
                Ok(c) => sink.write_char(c),
                Err(_) => break
            };
        };
    }

    fn pipe(&self, transform: Box<Fn(&mut ChanReader, &mut ChanWriter)+Send>) -> Pipe {
        let (tx, rx) = channel();
        let reader = ChanReader::new(rx);
        let mut writer = ChanWriter::new(tx);

        Thread::spawn(move || {
            transform(&self.incoming, &writer);
        });

        Pipe { incoming: reader }
    }
}

And the compiler error:

src/lib.rs:39:28: 41:10 error: cannot infer an appropriate lifetime due to conflicting requirements
src/lib.rs:39         Thread::spawn(move || {
src/lib.rs:40             transform(&self.incoming, &writer);
src/lib.rs:41         });
src/lib.rs:39:9: 39:22 note: first, the lifetime cannot outlive the expression at 39:8...
src/lib.rs:39         Thread::spawn(move || {
                      ^~~~~~~~~~~~~
src/lib.rs:39:9: 39:22 note: ...so that the declared lifetime parameter bounds are satisfied
src/lib.rs:39         Thread::spawn(move || {
                      ^~~~~~~~~~~~~
src/lib.rs:39:9: 41:11 note: but, the lifetime must be valid for the call at 39:8...
src/lib.rs:39         Thread::spawn(move || {
src/lib.rs:40             transform(&self.incoming, &writer);
src/lib.rs:41         });
src/lib.rs:39:28: 41:10 note: ...so that argument is valid for the call
src/lib.rs:39         Thread::spawn(move || {
src/lib.rs:40             transform(&self.incoming, &writer);
src/lib.rs:41         });
src/lib.rs:39:9: 39:22 error: declared lifetime bound not satisfied
src/lib.rs:39         Thread::spawn(move || {
                      ^~~~~~~~~~~~~
src/lib.rs:34:87: 44:6 note: lifetime parameter instantiated with the anonymous lifetime #1 defined on the block at 34:86
src/lib.rs:34     fn pipe(&self, transform: Box<Fn(&mut ChanReader, &mut ChanWriter)+Send>) -> Pipe {
src/lib.rs:35         let (tx, rx) = channel();
src/lib.rs:36         let reader = ChanReader::new(rx);
src/lib.rs:37         let mut writer = ChanWriter::new(tx);
src/lib.rs:38
src/lib.rs:39         Thread::spawn(move || {
              ...
note: but lifetime parameter must outlive the static lifetime
error: aborting due to 2 previous errors
Could not compile `pipes`.

I use 1.0.0-dev.

+1
source share
1 answer

Thread::spawn , 'static; , . self.incoming - . ; . , , self pipe, :

fn pipe<F: FnOnce(&mut ChanReader, &mut ChanWriter) + Send>(mut self, transform: F) -> Pipe {
    let (tx, rx) = channel();
    let reader = ChanReader::new(rx);
    let mut writer = ChanWriter::new(tx);

    Thread::spawn(move || {
        transform(&mut self.incoming, &mut writer);
    });

    Pipe { incoming: reader }
}

self writer , dandy.

( , FnOnce .)

+2

All Articles