Implementing rlua funcation that returns a new entry when it calling by using futures:Stream

#1

Hello,
I would like implement a rlua funcation that returns a new entry when it calling by using futures:Stream.
Something like this :
for packet in Tcp_Read() do print(packet) end

For example please consider the following code.

struct SocketHandler<R> {
	reader: Option<R>,
	buf: Box<[u8]>,
}

fn socket_handler<R>(reader: R) -> SocketHandler<R>
where
	R: AsyncRead,
{
	SocketHandler {
		reader: Some(reader),
		buf: Box::new([0; 40]),
	}
}

impl<R> futures::Stream for SocketHandler<R>
where
	R: AsyncRead,
{
	type Item = usize;
	type Error = ();
	fn poll(&mut self) -> Result<Async<Option<Self::Item>>, Self::Error> {
		let reader = self.reader.as_mut().unwrap();
		let n = futures::try_ready!(reader.poll_read(&mut self.buf).map_err(|_| ()));

		if n == 0 {
			Ok(Async::Ready(None))
		} else {
			Ok(Async::Ready(Some(n)))
		}
	}
}

I did try to use wait().next() for passing into rlua as an mut_function but unfortunately, wait() behavior is always blocking
Could please give me some hint to implementing this in rlua ?
Is possible implement as Async too?

Thanks.

0 Likes

[Solved] Implementing Function Iterator in rlua