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
use std::sync::mpsc;

use crate::{Sender, Stream, stream};

pub struct SenderFromReceiver<'a,T>{
    sender: Sender<'a,T>,
    receiver: mpsc::Receiver<T>,
}

impl<'a, T: 'a> SenderFromReceiver<'a,T>{
    pub fn new(receiver: mpsc::Receiver<T>) -> (SenderFromReceiver<'a,T>, Stream<'a,T>){
        let (se, st) = stream();
        let se = SenderFromReceiver{
            sender: se,
            receiver: receiver,
        };
        (se,st)
    }

    pub fn poll(&mut self){
		loop {
		    match self.receiver.try_recv(){
			    Ok(v) => self.sender.send(v),
			    Err(mpsc::TryRecvError::Disconnected) => return,
			    Err(mpsc::TryRecvError::Empty) => return,
			}
		}
    }
}