From 17ff8f4987e3a4c9e2aed170f0aa1b057832ef8d Mon Sep 17 00:00:00 2001 From: Jesse Luehrs Date: Sun, 27 Oct 2019 15:12:03 -0400 Subject: follow the signal api of a future returning a stream --- src/lib.rs | 53 +++++++++++++++++++++++++++++++++++++---------------- 1 file changed, 37 insertions(+), 16 deletions(-) (limited to 'src') diff --git a/src/lib.rs b/src/lib.rs index 74effe4..e43e0f7 100644 --- a/src/lib.rs +++ b/src/lib.rs @@ -3,6 +3,7 @@ use futures::future::Future as _; use futures::stream::Stream as _; +use snafu::futures01::FutureExt as _; use snafu::futures01::StreamExt as _; use snafu::ResultExt as _; use std::convert::TryInto as _; @@ -19,35 +20,55 @@ pub enum Error { SigWinchHandler { source: std::io::Error }, } -pub struct Resizer { - winches: - Box + Send>, - sent_initial_size: bool, +pub fn resizes() -> ResizeFuture { + ResizeFuture::default() } -impl Resizer { - pub fn new() -> Self { - Self::default() - } +pub struct ResizeFuture { + stream_fut: Box< + dyn futures::future::Future + + Send, + >, } -impl Default for Resizer { +impl Default for ResizeFuture { fn default() -> Self { - let winches = tokio_signal::unix::Signal::new( + let stream_fut = tokio_signal::unix::Signal::new( tokio_signal::unix::libc::SIGWINCH, ) - .flatten_stream() - .map(|_| ()) - .context(SigWinchHandler); + .context(SigWinchHandler) + .and_then(|stream| { + futures::future::ok(ResizeStream { + winches: Box::new( + stream.map(|_| ()).context(SigWinchHandler), + ), + sent_initial_size: false, + }) + }); Self { - winches: Box::new(winches), - sent_initial_size: false, + stream_fut: Box::new(stream_fut), } } } #[must_use = "streams do nothing unless polled"] -impl futures::stream::Stream for Resizer { +impl futures::future::Future for ResizeFuture { + type Item = ResizeStream; + type Error = Error; + + fn poll(&mut self) -> futures::Poll { + self.stream_fut.poll() + } +} + +pub struct ResizeStream { + winches: + Box + Send>, + sent_initial_size: bool, +} + +#[must_use = "streams do nothing unless polled"] +impl futures::stream::Stream for ResizeStream { type Item = (u16, u16); type Error = Error; -- cgit v1.2.3-54-g00ecf