Browse Source

Move OptionFuture to its own file

refactor-section-runner
Alex Mikhalev 4 years ago
parent
commit
4c782addb7
  1. 4
      src/main.rs
  2. 37
      src/option_future.rs
  3. 43
      src/section_runner.rs

4
src/main.rs

@ -7,6 +7,7 @@ use tracing_subscriber::EnvFilter; @@ -7,6 +7,7 @@ use tracing_subscriber::EnvFilter;
mod db;
mod migrations;
mod model;
mod option_future;
mod section_interface;
mod section_runner;
#[cfg(test)]
@ -34,8 +35,7 @@ fn main() -> Result<()> { @@ -34,8 +35,7 @@ fn main() -> Result<()> {
tracing_subscriber::fmt()
.with_ansi(true)
.with_env_filter(
EnvFilter::try_from_default_env()
.unwrap_or_else(|_| EnvFilter::new("info")),
EnvFilter::try_from_default_env().unwrap_or_else(|_| EnvFilter::new("info")),
)
.init();
color_eyre::install()?;

37
src/option_future.rs

@ -0,0 +1,37 @@ @@ -0,0 +1,37 @@
use pin_project::pin_project;
use std::{
future::Future,
ops::Deref,
pin::Pin,
task::{Context, Poll},
};
#[pin_project]
#[derive(Debug, Clone)]
#[must_use = "futures do nothing unless you `.await` or poll them"]
pub struct OptionFuture<F>(#[pin] Option<F>);
impl<F: Future> Future for OptionFuture<F> {
type Output = Option<F::Output>;
fn poll(self: Pin<&mut Self>, cx: &mut Context<'_>) -> Poll<Self::Output> {
match self.project().0.as_pin_mut() {
Some(x) => x.poll(cx).map(Some),
None => Poll::Ready(None),
}
}
}
impl<F> Deref for OptionFuture<F> {
type Target = Option<F>;
fn deref(&self) -> &Self::Target {
&self.0
}
}
impl<T> From<Option<T>> for OptionFuture<T> {
fn from(option: Option<T>) -> Self {
OptionFuture(option)
}
}

43
src/section_runner.rs

@ -1,4 +1,5 @@ @@ -1,4 +1,5 @@
use crate::model::SectionRef;
use crate::option_future::OptionFuture;
use crate::section_interface::SectionInterface;
use mpsc::error::SendError;
use std::{
@ -166,48 +167,6 @@ impl SecRun { @@ -166,48 +167,6 @@ impl SecRun {
}
}
mod option_future {
use pin_project::pin_project;
use std::{
future::Future,
ops::Deref,
pin::Pin,
task::{Context, Poll},
};
#[pin_project]
#[derive(Debug, Clone)]
#[must_use = "futures do nothing unless you `.await` or poll them"]
pub struct OptionFuture<F>(#[pin] Option<F>);
impl<F: Future> Future for OptionFuture<F> {
type Output = Option<F::Output>;
fn poll(self: Pin<&mut Self>, cx: &mut Context<'_>) -> Poll<Self::Output> {
match self.project().0.as_pin_mut() {
Some(x) => x.poll(cx).map(Some),
None => Poll::Ready(None),
}
}
}
impl<F> Deref for OptionFuture<F> {
type Target = Option<F>;
fn deref(&self) -> &Self::Target {
&self.0
}
}
impl<T> From<Option<T>> for OptionFuture<T> {
fn from(option: Option<T>) -> Self {
OptionFuture(option)
}
}
}
use option_future::OptionFuture;
async fn runner_task(
interface: Arc<dyn SectionInterface + Sync>,
mut msg_recv: mpsc::Receiver<RunnerMsg>,

Loading…
Cancel
Save