fuchsia_fuzzctl_test/
util.rs

1// Copyright 2022 The Fuchsia Authors. All rights reserved.
2// Use of this source code is governed by a BSD-style license that can be
3// found in the LICENSE file.
4
5use anyhow::Result;
6use fuchsia_async as fasync;
7use fuchsia_fuzzctl::{OutputSink, Writer};
8use futures::Future;
9
10/// Wraps a given `future` to display any returned errors using the given `writer`.
11pub fn create_task<F, O>(future: F, writer: &Writer<O>) -> fasync::Task<()>
12where
13    F: Future<Output = Result<()>> + 'static,
14    O: OutputSink,
15{
16    let writer = writer.clone();
17    let wrapped = || async move {
18        if let Err(e) = future.await {
19            writer.error(format!("task failed: {:?}", e));
20        }
21    };
22    fasync::Task::local(wrapped())
23}